1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00

refactor feature flag state fetching and update tests

This commit is contained in:
Jonathan Prusik
2025-01-03 09:49:58 -05:00
parent b962373f71
commit 2b1a0046ee
5 changed files with 23 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
import { mock, MockProxy, mockReset } from "jest-mock-extended"; import { mock, MockProxy, mockReset } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs"; import { BehaviorSubject, of } from "rxjs";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
@@ -151,6 +151,8 @@ describe("OverlayBackground", () => {
} }
beforeEach(() => { beforeEach(() => {
configService = mock<ConfigService>();
configService.getFeatureFlag$.mockImplementation(() => of(true));
accountService = mockAccountServiceWith(mockUserId); accountService = mockAccountServiceWith(mockUserId);
fakeStateProvider = new FakeStateProvider(accountService); fakeStateProvider = new FakeStateProvider(accountService);
showFaviconsMock$ = new BehaviorSubject(true); showFaviconsMock$ = new BehaviorSubject(true);

View File

@@ -94,6 +94,8 @@ describe("OverlayBackground", () => {
}; };
beforeEach(() => { beforeEach(() => {
configService = mock<ConfigService>();
configService.getFeatureFlag$.mockImplementation(() => of(true));
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, configService); domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, configService);
activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Unlocked); activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Unlocked);
authService = mock<AuthService>(); authService = mock<AuthService>();

View File

@@ -1,5 +1,5 @@
import { mock } from "jest-mock-extended"; import { mock } from "jest-mock-extended";
import { firstValueFrom, of } from "rxjs"; import { of } from "rxjs";
import { PolicyService } from "@bitwarden/common/admin-console/services/policy/policy.service"; import { PolicyService } from "@bitwarden/common/admin-console/services/policy/policy.service";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
@@ -24,15 +24,6 @@ import { FilelessImportPort, FilelessImportType } from "../enums/fileless-import
import FilelessImporterBackground from "./fileless-importer.background"; import FilelessImporterBackground from "./fileless-importer.background";
jest.mock("rxjs", () => {
const rxjs = jest.requireActual("rxjs");
const { firstValueFrom } = rxjs;
return {
...rxjs,
firstValueFrom: jest.fn(firstValueFrom),
};
});
describe("FilelessImporterBackground ", () => { describe("FilelessImporterBackground ", () => {
let filelessImporterBackground: FilelessImporterBackground; let filelessImporterBackground: FilelessImporterBackground;
const configService = mock<ConfigService>(); const configService = mock<ConfigService>();
@@ -49,6 +40,7 @@ describe("FilelessImporterBackground ", () => {
beforeEach(() => { beforeEach(() => {
domainSettingsService.blockedInteractionsUris$ = of(null); domainSettingsService.blockedInteractionsUris$ = of(null);
policyService.policyAppliesToActiveUser$.mockImplementation(() => of(true));
scriptInjectorService = new BrowserScriptInjectorService( scriptInjectorService = new BrowserScriptInjectorService(
domainSettingsService, domainSettingsService,
platformUtilsService, platformUtilsService,
@@ -102,7 +94,6 @@ describe("FilelessImporterBackground ", () => {
}); });
it("posts a message to the port indicating that the fileless import feature is disabled if the user's auth status is not unlocked", async () => { it("posts a message to the port indicating that the fileless import feature is disabled if the user's auth status is not unlocked", async () => {
(firstValueFrom as jest.Mock).mockResolvedValue(false);
jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Locked); jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Locked);
triggerRuntimeOnConnectEvent(lpImporterPort); triggerRuntimeOnConnectEvent(lpImporterPort);
@@ -115,8 +106,6 @@ describe("FilelessImporterBackground ", () => {
}); });
it("posts a message to the port indicating that the fileless import feature is disabled if the user's policy removes individual vaults", async () => { it("posts a message to the port indicating that the fileless import feature is disabled if the user's policy removes individual vaults", async () => {
(firstValueFrom as jest.Mock).mockResolvedValue(true);
triggerRuntimeOnConnectEvent(lpImporterPort); triggerRuntimeOnConnectEvent(lpImporterPort);
await flushPromises(); await flushPromises();
@@ -139,7 +128,8 @@ describe("FilelessImporterBackground ", () => {
}); });
it("posts a message to the port indicating that the fileless import feature is enabled", async () => { it("posts a message to the port indicating that the fileless import feature is enabled", async () => {
(firstValueFrom as jest.Mock).mockResolvedValue(false); policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
triggerRuntimeOnConnectEvent(lpImporterPort); triggerRuntimeOnConnectEvent(lpImporterPort);
await flushPromises(); await flushPromises();
@@ -150,6 +140,7 @@ describe("FilelessImporterBackground ", () => {
}); });
it("triggers an injection of the `lp-suppress-import-download.js` script in manifest v3", async () => { it("triggers an injection of the `lp-suppress-import-download.js` script in manifest v3", async () => {
policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
manifestVersionSpy.mockReturnValue(3); manifestVersionSpy.mockReturnValue(3);
triggerRuntimeOnConnectEvent(lpImporterPort); triggerRuntimeOnConnectEvent(lpImporterPort);
@@ -163,7 +154,7 @@ describe("FilelessImporterBackground ", () => {
}); });
it("triggers an injection of the `lp-suppress-import-download-script-append-mv2.js` script in manifest v2", async () => { it("triggers an injection of the `lp-suppress-import-download-script-append-mv2.js` script in manifest v2", async () => {
(firstValueFrom as jest.Mock).mockResolvedValueOnce(false); policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
manifestVersionSpy.mockReturnValue(2); manifestVersionSpy.mockReturnValue(2);
triggerRuntimeOnConnectEvent(lpImporterPort); triggerRuntimeOnConnectEvent(lpImporterPort);
@@ -182,9 +173,10 @@ describe("FilelessImporterBackground ", () => {
let lpImporterPort: chrome.runtime.Port; let lpImporterPort: chrome.runtime.Port;
beforeEach(async () => { beforeEach(async () => {
policyService.policyAppliesToActiveUser$.mockImplementation(() => of(false));
jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Unlocked); jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Unlocked);
jest.spyOn(configService, "getFeatureFlag").mockResolvedValue(true); jest.spyOn(configService, "getFeatureFlag").mockResolvedValue(true);
(firstValueFrom as jest.Mock).mockResolvedValue(false);
triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.NotificationBar)); triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.NotificationBar));
triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.LpImporter)); triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.LpImporter));
await flushPromises(); await flushPromises();

View File

@@ -11,10 +11,9 @@ import { DefaultDomainSettingsService, DomainSettingsService } from "./domain-se
describe("DefaultDomainSettingsService", () => { describe("DefaultDomainSettingsService", () => {
let domainSettingsService: DomainSettingsService; let domainSettingsService: DomainSettingsService;
const configServiceMock = mock<ConfigService>(); let configService: MockProxy<ConfigService>;
const mockUserId = Utils.newGuid() as UserId; const mockUserId = Utils.newGuid() as UserId;
const accountService: FakeAccountService = mockAccountServiceWith(mockUserId); const accountService: FakeAccountService = mockAccountServiceWith(mockUserId);
let mockConfigService: MockProxy<ConfigService>;
const fakeStateProvider: FakeStateProvider = new FakeStateProvider(accountService); const fakeStateProvider: FakeStateProvider = new FakeStateProvider(accountService);
const mockEquivalentDomains = [ const mockEquivalentDomains = [
@@ -24,8 +23,9 @@ describe("DefaultDomainSettingsService", () => {
]; ];
beforeEach(() => { beforeEach(() => {
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, mockConfigService); configService = mock<ConfigService>();
jest.spyOn(configServiceMock, "getFeatureFlag$").mockReturnValue(of(false)); configService.getFeatureFlag$.mockImplementation(() => of(false));
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, configService);
jest.spyOn(domainSettingsService, "getUrlEquivalentDomains"); jest.spyOn(domainSettingsService, "getUrlEquivalentDomains");
domainSettingsService.equivalentDomains$ = of(mockEquivalentDomains); domainSettingsService.equivalentDomains$ = of(mockEquivalentDomains);

View File

@@ -99,7 +99,12 @@ export class DefaultDomainSettingsService implements DomainSettingsService {
this.blockedInteractionsUris$ = this.configService this.blockedInteractionsUris$ = this.configService
.getFeatureFlag$(FeatureFlag.BlockBrowserInjectionsByDomain) .getFeatureFlag$(FeatureFlag.BlockBrowserInjectionsByDomain)
.pipe(switchMap((enabled) => (enabled ? this.blockedInteractionsUrisState.state$ : of({})))); .pipe(
switchMap((featureIsEnabled) =>
featureIsEnabled ? this.blockedInteractionsUrisState.state$ : of({} as NeverDomains),
),
map((disabledUris) => (Object.keys(disabledUris).length ? disabledUris : null)),
);
this.equivalentDomainsState = this.stateProvider.getActive(EQUIVALENT_DOMAINS); this.equivalentDomainsState = this.stateProvider.getActive(EQUIVALENT_DOMAINS);
this.equivalentDomains$ = this.equivalentDomainsState.state$.pipe(map((x) => x ?? null)); this.equivalentDomains$ = this.equivalentDomainsState.state$.pipe(map((x) => x ?? null));