mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 09:43:23 +00:00
[PM-5562] Implement Domain Settings state provider (#8226)
* create domain settings state provider * replace callsites for defaultUriMatch and neverDomains with DomainSettingsService equivalents * replace callsites for equivalentDomains with DomainSettingsService equivalents and clean up unused AccountSettingsSettings * add migrations for domain settings state * do not use enum for URI match strategy constants and types * add getUrlEquivalentDomains test * PR suggestions/cleanup * refactor getUrlEquivalentDomains to return an observable Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com> * update tests * add UriMatchStrategy docs notes * service class renames * use service abstraction at callsites previously using service class directly --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com>
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import * as lunr from "lunr";
|
||||
|
||||
import { SearchService as SearchServiceAbstraction } from "../abstractions/search.service";
|
||||
import { UriMatchStrategy } from "../models/domain/domain-service";
|
||||
import { I18nService } from "../platform/abstractions/i18n.service";
|
||||
import { LogService } from "../platform/abstractions/log.service";
|
||||
import { SendView } from "../tools/send/models/view/send.view";
|
||||
import { FieldType, UriMatchType } from "../vault/enums";
|
||||
import { FieldType } from "../vault/enums";
|
||||
import { CipherType } from "../vault/enums/cipher-type";
|
||||
import { CipherView } from "../vault/models/view/cipher.view";
|
||||
|
||||
@@ -288,7 +289,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
return;
|
||||
}
|
||||
let uri = u.uri;
|
||||
if (u.match !== UriMatchType.RegularExpression) {
|
||||
if (u.match !== UriMatchStrategy.RegularExpression) {
|
||||
const protocolIndex = uri.indexOf("://");
|
||||
if (protocolIndex > -1) {
|
||||
uri = uri.substr(protocolIndex + 3);
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
import { mock, MockProxy } from "jest-mock-extended";
|
||||
import { BehaviorSubject, firstValueFrom } from "rxjs";
|
||||
|
||||
import { CryptoService } from "../platform/abstractions/crypto.service";
|
||||
import { EncryptService } from "../platform/abstractions/encrypt.service";
|
||||
import { StateService } from "../platform/abstractions/state.service";
|
||||
import { ContainerService } from "../platform/services/container.service";
|
||||
|
||||
import { SettingsService } from "./settings.service";
|
||||
|
||||
describe("SettingsService", () => {
|
||||
let settingsService: SettingsService;
|
||||
|
||||
let cryptoService: MockProxy<CryptoService>;
|
||||
let encryptService: MockProxy<EncryptService>;
|
||||
let stateService: MockProxy<StateService>;
|
||||
let activeAccount: BehaviorSubject<string>;
|
||||
let activeAccountUnlocked: BehaviorSubject<boolean>;
|
||||
|
||||
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"],
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
cryptoService = mock<CryptoService>();
|
||||
encryptService = mock<EncryptService>();
|
||||
stateService = mock<StateService>();
|
||||
activeAccount = new BehaviorSubject("123");
|
||||
activeAccountUnlocked = new BehaviorSubject(true);
|
||||
|
||||
stateService.getSettings.mockResolvedValue({ equivalentDomains: mockEquivalentDomains });
|
||||
stateService.activeAccount$ = activeAccount;
|
||||
stateService.activeAccountUnlocked$ = activeAccountUnlocked;
|
||||
(window as any).bitwardenContainerService = new ContainerService(cryptoService, encryptService);
|
||||
|
||||
settingsService = new SettingsService(stateService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
activeAccount.complete();
|
||||
activeAccountUnlocked.complete();
|
||||
});
|
||||
|
||||
describe("getEquivalentDomains", () => {
|
||||
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);
|
||||
});
|
||||
|
||||
it("returns an empty set if there are no equivalent domains", () => {
|
||||
const actual = settingsService.getEquivalentDomains("asdf");
|
||||
expect(actual).toEqual(new Set());
|
||||
});
|
||||
});
|
||||
|
||||
it("setEquivalentDomains", async () => {
|
||||
await settingsService.setEquivalentDomains([["test2"], ["domains2"]]);
|
||||
|
||||
expect(stateService.setSettings).toBeCalledTimes(1);
|
||||
|
||||
expect((await firstValueFrom(settingsService.settings$)).equivalentDomains).toEqual([
|
||||
["test2"],
|
||||
["domains2"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("clear", async () => {
|
||||
await settingsService.clear();
|
||||
|
||||
expect(stateService.setSettings).toBeCalledTimes(1);
|
||||
|
||||
expect(await firstValueFrom(settingsService.settings$)).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -3,13 +3,10 @@ import { BehaviorSubject, concatMap } from "rxjs";
|
||||
import { SettingsService as SettingsServiceAbstraction } from "../abstractions/settings.service";
|
||||
import { StateService } from "../platform/abstractions/state.service";
|
||||
import { Utils } from "../platform/misc/utils";
|
||||
import { AccountSettingsSettings } from "../platform/models/domain/account";
|
||||
|
||||
export class SettingsService implements SettingsServiceAbstraction {
|
||||
protected _settings: BehaviorSubject<AccountSettingsSettings> = new BehaviorSubject({});
|
||||
protected _disableFavicon = new BehaviorSubject<boolean>(null);
|
||||
|
||||
settings$ = this._settings.asObservable();
|
||||
disableFavicon$ = this._disableFavicon.asObservable();
|
||||
|
||||
constructor(private stateService: StateService) {
|
||||
@@ -21,50 +18,17 @@ export class SettingsService implements SettingsServiceAbstraction {
|
||||
}
|
||||
|
||||
if (!unlocked) {
|
||||
this._settings.next({});
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await this.stateService.getSettings();
|
||||
const disableFavicon = await this.stateService.getDisableFavicon();
|
||||
|
||||
this._settings.next(data);
|
||||
this._disableFavicon.next(disableFavicon);
|
||||
}),
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
async setEquivalentDomains(equivalentDomains: string[][]): Promise<void> {
|
||||
const settings = this._settings.getValue() ?? {};
|
||||
|
||||
settings.equivalentDomains = equivalentDomains;
|
||||
|
||||
this._settings.next(settings);
|
||||
await this.stateService.setSettings(settings);
|
||||
}
|
||||
|
||||
getEquivalentDomains(url: string): Set<string> {
|
||||
const domain = Utils.getDomain(url);
|
||||
if (domain == null) {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
const settings = this._settings.getValue();
|
||||
|
||||
let result: string[] = [];
|
||||
|
||||
if (settings?.equivalentDomains != null) {
|
||||
settings.equivalentDomains
|
||||
.filter((ed) => ed.length > 0 && ed.includes(domain))
|
||||
.forEach((ed) => {
|
||||
result = result.concat(ed);
|
||||
});
|
||||
}
|
||||
|
||||
return new Set(result);
|
||||
}
|
||||
|
||||
async setDisableFavicon(value: boolean) {
|
||||
this._disableFavicon.next(value);
|
||||
await this.stateService.setDisableFavicon(value);
|
||||
@@ -73,12 +37,4 @@ export class SettingsService implements SettingsServiceAbstraction {
|
||||
getDisableFavicon(): boolean {
|
||||
return this._disableFavicon.getValue();
|
||||
}
|
||||
|
||||
async clear(userId?: string): Promise<void> {
|
||||
if (userId == null || userId == (await this.stateService.getUserId())) {
|
||||
this._settings.next({});
|
||||
}
|
||||
|
||||
await this.stateService.setSettings(null, { userId: userId });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user