2022-10-10 17:19:01 +02:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
2022-08-30 16:19:09 +02:00
|
|
|
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
|
|
|
import { BehaviorSubject, firstValueFrom } from "rxjs";
|
|
|
|
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
2022-10-27 23:38:54 +02:00
|
|
|
import { EncryptService } from "@bitwarden/common/abstractions/encrypt.service";
|
2022-08-30 16:19:09 +02:00
|
|
|
import { ContainerService } from "@bitwarden/common/services/container.service";
|
|
|
|
import { SettingsService } from "@bitwarden/common/services/settings.service";
|
|
|
|
import { StateService } from "@bitwarden/common/services/state.service";
|
|
|
|
|
|
|
|
describe("SettingsService", () => {
|
|
|
|
let settingsService: SettingsService;
|
|
|
|
|
|
|
|
let cryptoService: SubstituteOf<CryptoService>;
|
2022-10-27 23:38:54 +02:00
|
|
|
let encryptService: SubstituteOf<EncryptService>;
|
2022-08-30 16:19:09 +02:00
|
|
|
let stateService: SubstituteOf<StateService>;
|
|
|
|
let activeAccount: BehaviorSubject<string>;
|
|
|
|
let activeAccountUnlocked: BehaviorSubject<boolean>;
|
|
|
|
|
2023-04-06 05:30:26 +02:00
|
|
|
const mockEquivalentDomains = [
|
|
|
|
["example.com", "exampleapp.com", "example.co.uk", "ejemplo.es"],
|
|
|
|
["bitwarden.com", "bitwarden.co.uk", "sm-bitwarden.com"],
|
|
|
|
["example.co.uk", "exampleapp.co.uk"],
|
|
|
|
];
|
|
|
|
|
2022-08-30 16:19:09 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
cryptoService = Substitute.for();
|
2022-09-02 03:15:19 +02:00
|
|
|
encryptService = Substitute.for();
|
2022-08-30 16:19:09 +02:00
|
|
|
stateService = Substitute.for();
|
|
|
|
activeAccount = new BehaviorSubject("123");
|
|
|
|
activeAccountUnlocked = new BehaviorSubject(true);
|
|
|
|
|
2023-04-06 05:30:26 +02:00
|
|
|
stateService.getSettings().resolves({ equivalentDomains: mockEquivalentDomains });
|
2022-08-30 16:19:09 +02:00
|
|
|
stateService.activeAccount$.returns(activeAccount);
|
|
|
|
stateService.activeAccountUnlocked$.returns(activeAccountUnlocked);
|
2022-09-02 03:15:19 +02:00
|
|
|
(window as any).bitwardenContainerService = new ContainerService(cryptoService, encryptService);
|
2022-08-30 16:19:09 +02:00
|
|
|
|
|
|
|
settingsService = new SettingsService(stateService);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
activeAccount.complete();
|
|
|
|
activeAccountUnlocked.complete();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getEquivalentDomains", () => {
|
2023-04-06 05:30:26 +02:00
|
|
|
it("returns all equivalent domains for a URL", async () => {
|
|
|
|
const actual = settingsService.getEquivalentDomains("example.co.uk");
|
|
|
|
const expected = new Set([
|
|
|
|
"example.com",
|
|
|
|
"exampleapp.com",
|
|
|
|
"example.co.uk",
|
|
|
|
"ejemplo.es",
|
|
|
|
"exampleapp.co.uk",
|
|
|
|
]);
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
2022-08-30 16:19:09 +02:00
|
|
|
|
2023-04-06 05:30:26 +02:00
|
|
|
it("returns an empty set if there are no equivalent domains", () => {
|
|
|
|
const actual = settingsService.getEquivalentDomains("asdf");
|
|
|
|
expect(actual).toEqual(new Set());
|
2022-08-30 16:19:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("setEquivalentDomains", async () => {
|
|
|
|
await settingsService.setEquivalentDomains([["test2"], ["domains2"]]);
|
|
|
|
|
|
|
|
stateService.received(1).setSettings(Arg.any());
|
|
|
|
|
|
|
|
expect((await firstValueFrom(settingsService.settings$)).equivalentDomains).toEqual([
|
|
|
|
["test2"],
|
|
|
|
["domains2"],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("clear", async () => {
|
|
|
|
await settingsService.clear();
|
|
|
|
|
|
|
|
stateService.received(1).setSettings(Arg.any(), Arg.any());
|
|
|
|
|
|
|
|
expect(await firstValueFrom(settingsService.settings$)).toEqual({});
|
|
|
|
});
|
|
|
|
});
|