1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-17 20:31:50 +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.
*/
abstract getEnvironment$(userId?: string): Observable<Environment | undefined>;
/**
* @deprecated Use {@link getEnvironment$} instead.
*/
abstract getEnvironment(userId?: string): Promise<Environment | undefined>;
}

View File

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

View File

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

View File

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

View File

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