1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-30 22:41:33 +01:00

[PM-16262] Make getEnvironment observable and use it in SdkService (#12501)

* feat: re-implement getEnvironment as an observable

* feat: deprecate `getEnvironment`

* fix: use correct environment function in SdkService

* fix: test
This commit is contained in:
Andreas Coroiu 2024-12-20 15:20:23 +01:00 committed by GitHub
parent 2041799174
commit 1d335bb164
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 20 deletions

View File

@ -128,5 +128,10 @@ export abstract class EnvironmentService {
/** /**
* Get the environment from state. Useful if you need to get the environment for another user. * Get the environment from state. Useful if you need to get the environment for another user.
*/ */
abstract getEnvironment$(userId?: string): Observable<Environment | undefined>;
/**
* @deprecated Use {@link getEnvironment$} instead.
*/
abstract getEnvironment(userId?: string): Promise<Environment | undefined>; abstract getEnvironment(userId?: string): Promise<Environment | undefined>;
} }

View File

@ -314,7 +314,7 @@ describe("EnvironmentService", () => {
await switchUser(testUser); await switchUser(testUser);
const env = await sut.getEnvironment(); const env = await firstValueFrom(sut.getEnvironment$());
expect(env.getHostname()).toBe(expectedHost); expect(env.getHostname()).toBe(expectedHost);
}); });
@ -325,7 +325,7 @@ describe("EnvironmentService", () => {
setGlobalData(region, new EnvironmentUrls()); setGlobalData(region, new EnvironmentUrls());
setUserData(Region.US, new EnvironmentUrls()); setUserData(Region.US, new EnvironmentUrls());
const env = await sut.getEnvironment(); const env = await firstValueFrom(sut.getEnvironment$());
expect(env.getHostname()).toBe(expectedHost); expect(env.getHostname()).toBe(expectedHost);
}); });
@ -338,7 +338,7 @@ describe("EnvironmentService", () => {
setGlobalData(region, new EnvironmentUrls()); setGlobalData(region, new EnvironmentUrls());
setUserData(Region.US, new EnvironmentUrls()); setUserData(Region.US, new EnvironmentUrls());
const env = await sut.getEnvironment(testUser); const env = await firstValueFrom(sut.getEnvironment$(testUser));
expect(env.getHostname()).toBe(expectedHost); expect(env.getHostname()).toBe(expectedHost);
}, },
); );
@ -355,7 +355,7 @@ describe("EnvironmentService", () => {
await switchUser(testUser); await switchUser(testUser);
const env = await sut.getEnvironment(alternateTestUser); const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser));
expect(env.getHostname()).toBe(expectedHost); expect(env.getHostname()).toBe(expectedHost);
}, },
); );
@ -366,7 +366,7 @@ describe("EnvironmentService", () => {
setGlobalData(Region.SelfHosted, globalSelfHostUrls); setGlobalData(Region.SelfHosted, globalSelfHostUrls);
setUserData(Region.EU, new EnvironmentUrls()); setUserData(Region.EU, new EnvironmentUrls());
const env = await sut.getEnvironment(); const env = await firstValueFrom(sut.getEnvironment$());
expect(env.getHostname()).toBe("base.example.com"); expect(env.getHostname()).toBe("base.example.com");
}); });
@ -377,7 +377,7 @@ describe("EnvironmentService", () => {
setGlobalData(Region.SelfHosted, globalSelfHostUrls); setGlobalData(Region.SelfHosted, globalSelfHostUrls);
setUserData(Region.EU, new EnvironmentUrls()); setUserData(Region.EU, new EnvironmentUrls());
const env = await sut.getEnvironment(); const env = await firstValueFrom(sut.getEnvironment$());
expect(env.getHostname()).toBe("vault.example.com"); expect(env.getHostname()).toBe("vault.example.com");
}); });
@ -391,7 +391,7 @@ describe("EnvironmentService", () => {
await switchUser(testUser); await switchUser(testUser);
const env = await sut.getEnvironment(alternateTestUser); const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser));
expect(env.getHostname()).toBe("base.example.com"); expect(env.getHostname()).toBe("base.example.com");
}); });
}); });

View File

@ -271,25 +271,32 @@ export class DefaultEnvironmentService implements EnvironmentService {
} }
} }
async getEnvironment(userId?: UserId): Promise<Environment | undefined> { getEnvironment$(userId?: UserId): Observable<Environment | undefined> {
if (userId == null) { if (userId == null) {
return await firstValueFrom(this.environment$); return this.environment$;
} }
const state = await this.getEnvironmentState(userId); return this.activeAccountId$.pipe(
return this.buildEnvironment(state.region, state.urls); switchMap((activeUserId) => {
}
private async getEnvironmentState(userId: UserId | null) {
// Previous rules dictated that we only get from user scoped state if there is an active user. // Previous rules dictated that we only get from user scoped state if there is an active user.
const activeUserId = await firstValueFrom(this.activeAccountId$); if (activeUserId == null) {
return activeUserId == null return this.globalState.state$;
? await firstValueFrom(this.globalState.state$) }
: await firstValueFrom( return this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$;
this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$, }),
map((state) => {
return this.buildEnvironment(state?.region, state?.urls);
}),
); );
} }
/**
* @deprecated Use getEnvironment$ instead.
*/
async getEnvironment(userId?: UserId): Promise<Environment | undefined> {
return firstValueFrom(this.getEnvironment$(userId));
}
async seedUserEnvironment(userId: UserId) { async seedUserEnvironment(userId: UserId) {
const global = await firstValueFrom(this.globalState.state$); const global = await firstValueFrom(this.globalState.state$);
await this.stateProvider.getUser(userId, USER_ENVIRONMENT_KEY).update(() => global); await this.stateProvider.getUser(userId, USER_ENVIRONMENT_KEY).update(() => global);

View File

@ -56,6 +56,9 @@ describe("DefaultSdkService", () => {
const userId = "user-id" as UserId; const userId = "user-id" as UserId;
beforeEach(() => { beforeEach(() => {
environmentService.getEnvironment$
.calledWith(userId)
.mockReturnValue(new BehaviorSubject(mock<Environment>()));
accountService.accounts$ = of({ accountService.accounts$ = of({
[userId]: { email: "email", emailVerified: true, name: "name" } as AccountInfo, [userId]: { email: "email", emailVerified: true, name: "name" } as AccountInfo,
}); });

View File

@ -78,7 +78,7 @@ export class DefaultSdkService implements SdkService {
); );
const client$ = combineLatest([ const client$ = combineLatest([
this.environmentService.environment$, this.environmentService.getEnvironment$(userId),
account$, account$,
kdfParams$, kdfParams$,
privateKey$, privateKey$,