Skip to content

Commit

Permalink
Auth/PM-13114 - WebEnvService Refactor + Unit Tests to support QA Env…
Browse files Browse the repository at this point in the history
… Selector (#11397)

* PM-13114 - WebEnvSvc - use hostname vs domain check for init and setEnv (tests TODO)

* PM-13114 - WebEnvSvc + URLs webpack config - use expected string variable on process.env.URLS to ensure tests can properly mock the WebEnvSvc

* PM-13114 - WebEnvSvc - setEnvironment - fix issue with returning currentRegion urls instead of currentEnv urls.

* PM-13114 - WebEnvSvc - setEnv - refactor names to improve clarity.

* PM-13114 - WebEnvSvc spec file - Test all prod scenarios

* PM-13144 - Work with Justin to move process.env.Urls access into injection token and remove webpack string type conversion.

* PM-13114 - WIP on getting additionalRegionConfigs injected via injection token to default env service.

* PM-13114 - Update all background inits to pass process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[] to env service.

* PM-13114 - WebEnvSvc - adjust order of constructor deps

* PM-13114 - WebEnvSvc - add WebRegionConfig to extend RegionConfig type and be accurate for what the WebEnvSvc uses.

* PM-13114 - WebEnvSvc Tests - US QA tested

* PM-13114 - WebEnvSvc tests - refactor QA naming to make it more clear.

* PM-13114 - WebEnvSvc - test QA EU

* PM-13114 - WebEnvSvc - remove promise resolve per PR feedback.
  • Loading branch information
JaredSnider-Bitwarden authored Oct 4, 2024
1 parent e6ff647 commit 87cb45c
Show file tree
Hide file tree
Showing 12 changed files with 560 additions and 33 deletions.
2 changes: 2 additions & 0 deletions apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { ConfigService } from "@bitwarden/common/platform/abstractions/config/co
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { CryptoService as CryptoServiceAbstraction } from "@bitwarden/common/platform/abstractions/crypto.service";
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
import { RegionConfig } from "@bitwarden/common/platform/abstractions/environment.service";
import { Fido2ActiveRequestManager as Fido2ActiveRequestManagerAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-active-request-manager.abstraction";
import { Fido2AuthenticatorService as Fido2AuthenticatorServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-authenticator.service.abstraction";
import { Fido2ClientService as Fido2ClientServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-client.service.abstraction";
Expand Down Expand Up @@ -570,6 +571,7 @@ export default class MainBackground {
this.logService,
this.stateProvider,
this.accountService,
process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[],
);
this.biometricStateService = new DefaultBiometricStateService(this.stateProvider);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { firstValueFrom } from "rxjs";

import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { Region } from "@bitwarden/common/platform/abstractions/environment.service";
import { Region, RegionConfig } from "@bitwarden/common/platform/abstractions/environment.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { DefaultEnvironmentService } from "@bitwarden/common/platform/services/default-environment.service";
import { StateProvider } from "@bitwarden/common/platform/state";
Expand All @@ -14,8 +14,9 @@ export class BrowserEnvironmentService extends DefaultEnvironmentService {
private logService: LogService,
stateProvider: StateProvider,
accountService: AccountService,
additionalRegionConfigs: RegionConfig[] = [],
) {
super(stateProvider, accountService);
super(stateProvider, accountService, additionalRegionConfigs);
}

async hasManagedEnvironment(): Promise<boolean> {
Expand Down
3 changes: 2 additions & 1 deletion apps/browser/src/popup/services/services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DEFAULT_VAULT_TIMEOUT,
INTRAPROCESS_MESSAGING_SUBJECT,
CLIENT_TYPE,
ENV_ADDITIONAL_REGIONS,
} from "@bitwarden/angular/services/injection-tokens";
import { JslibServicesModule } from "@bitwarden/angular/services/jslib-services.module";
import { AnonLayoutWrapperDataService, LockComponentService } from "@bitwarden/auth/angular";
Expand Down Expand Up @@ -197,7 +198,7 @@ const safeProviders: SafeProvider[] = [
safeProvider({
provide: BrowserEnvironmentService,
useClass: BrowserEnvironmentService,
deps: [LogService, StateProvider, AccountServiceAbstraction],
deps: [LogService, StateProvider, AccountServiceAbstraction, ENV_ADDITIONAL_REGIONS],
}),
safeProvider({
provide: I18nServiceAbstraction,
Expand Down
6 changes: 5 additions & 1 deletion apps/cli/src/service-container/service-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/bill
import { ClientType } from "@bitwarden/common/enums";
import { ConfigApiServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config-api.service.abstraction";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import {
EnvironmentService,
RegionConfig,
} from "@bitwarden/common/platform/abstractions/environment.service";
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
import { KeySuffixOptions, LogLevelType } from "@bitwarden/common/platform/enums";
import { StateFactory } from "@bitwarden/common/platform/factories/state-factory";
Expand Down Expand Up @@ -346,6 +349,7 @@ export class ServiceContainer {
this.environmentService = new DefaultEnvironmentService(
this.stateProvider,
this.accountService,
process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[],
);

this.keyGenerationService = new KeyGenerationService(this.cryptoFunctionService);
Expand Down
7 changes: 6 additions & 1 deletion apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Subject, firstValueFrom } from "rxjs";

import { AccountServiceImplementation } from "@bitwarden/common/auth/services/account.service";
import { ClientType } from "@bitwarden/common/enums";
import { RegionConfig } from "@bitwarden/common/platform/abstractions/environment.service";
import { Message, MessageSender } from "@bitwarden/common/platform/messaging";
// eslint-disable-next-line no-restricted-imports -- For dependency creation
import { SubjectMessageSender } from "@bitwarden/common/platform/messaging/internal";
Expand Down Expand Up @@ -152,7 +153,11 @@ export class Main {
new DefaultDerivedStateProvider(),
);

this.environmentService = new DefaultEnvironmentService(stateProvider, accountService);
this.environmentService = new DefaultEnvironmentService(
stateProvider,
accountService,
process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[],
);

this.migrationRunner = new MigrationRunner(
this.storageService,
Expand Down
13 changes: 11 additions & 2 deletions apps/web/src/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SafeProvider, safeProvider } from "@bitwarden/angular/platform/utils/sa
import {
CLIENT_TYPE,
DEFAULT_VAULT_TIMEOUT,
ENV_ADDITIONAL_REGIONS,
LOCALES_DIRECTORY,
MEMORY_STORAGE,
OBSERVABLE_DISK_LOCAL_STORAGE,
Expand Down Expand Up @@ -42,7 +43,10 @@ import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.ser
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { CryptoService as CryptoServiceAbstraction } from "@bitwarden/common/platform/abstractions/crypto.service";
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import {
EnvironmentService,
Urls,
} from "@bitwarden/common/platform/abstractions/environment.service";
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
Expand Down Expand Up @@ -84,6 +88,7 @@ import { WebStorageServiceProvider } from "../platform/web-storage-service.provi

import { EventService } from "./event.service";
import { InitService } from "./init.service";
import { ENV_URLS } from "./injection-tokens";
import { ModalService } from "./modal.service";
import { RouterService } from "./router.service";
import { WebFileDownloadService } from "./web-file-download.service";
Expand Down Expand Up @@ -173,10 +178,14 @@ const safeProviders: SafeProvider[] = [
useClass: WebMigrationRunner,
deps: [AbstractStorageService, LogService, MigrationBuilderService, WindowStorageService],
}),
safeProvider({
provide: ENV_URLS,
useValue: process.env.URLS as Urls,
}),
safeProvider({
provide: EnvironmentService,
useClass: WebEnvironmentService,
deps: [WINDOW, StateProvider, AccountService, Router],
deps: [WINDOW, StateProvider, AccountService, ENV_ADDITIONAL_REGIONS, Router, ENV_URLS],
}),
safeProvider({
provide: BiometricsService,
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/app/core/injection-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Put web specific injection tokens here
import { SafeInjectionToken } from "@bitwarden/angular/services/injection-tokens";
import { Urls } from "@bitwarden/common/platform/abstractions/environment.service";

/**
* Injection token for injecting the NodeJS process.env urls into services.
* Using an injection token allows services to be tested without needing to
* mock the process.env.
*/
export const ENV_URLS = new SafeInjectionToken<Urls>("ENV_URLS");
Loading

0 comments on commit 87cb45c

Please sign in to comment.