mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 07:43:35 +00:00
Auth/PM-5712 - Extension & Desktop Account Switcher - Fix incorrect env showing when adding new accounts (#13362)
* PM-5712 - Refactor env service to require user id instead of having global and active user state fallbacks per working session with Justin. * PM-5712 - AccountSwitcherService tests - fix tests and add env assertions.
This commit is contained in:
@@ -9,7 +9,10 @@ import {
|
||||
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
||||
import { AvatarService } from "@bitwarden/common/auth/abstractions/avatar.service";
|
||||
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import {
|
||||
Environment,
|
||||
EnvironmentService,
|
||||
} from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||
import { UserId } from "@bitwarden/common/types/guid";
|
||||
@@ -21,6 +24,12 @@ describe("AccountSwitcherService", () => {
|
||||
let activeAccountSubject: BehaviorSubject<Account | null>;
|
||||
let authStatusSubject: ReplaySubject<Record<UserId, AuthenticationStatus>>;
|
||||
|
||||
let envBSubject: BehaviorSubject<Environment | undefined>;
|
||||
const mockHostName = "mockHostName";
|
||||
const mockEnv: Partial<Environment> = {
|
||||
getHostname: () => mockHostName,
|
||||
};
|
||||
|
||||
const accountService = mock<AccountService>();
|
||||
const avatarService = mock<AvatarService>();
|
||||
const messagingService = mock<MessagingService>();
|
||||
@@ -41,6 +50,9 @@ describe("AccountSwitcherService", () => {
|
||||
accountService.activeAccount$ = activeAccountSubject;
|
||||
authService.authStatuses$ = authStatusSubject;
|
||||
|
||||
envBSubject = new BehaviorSubject<Environment | undefined>(mockEnv as Environment);
|
||||
environmentService.getEnvironment$.mockReturnValue(envBSubject);
|
||||
|
||||
accountSwitcherService = new AccountSwitcherService(
|
||||
accountService,
|
||||
avatarService,
|
||||
@@ -79,11 +91,16 @@ describe("AccountSwitcherService", () => {
|
||||
expect(accounts).toHaveLength(3);
|
||||
expect(accounts[0].id).toBe("1");
|
||||
expect(accounts[0].isActive).toBeTruthy();
|
||||
|
||||
expect(accounts[0].server).toBe(mockHostName);
|
||||
|
||||
expect(accounts[1].id).toBe("2");
|
||||
expect(accounts[1].isActive).toBeFalsy();
|
||||
expect(accounts[1].server).toBe(mockHostName);
|
||||
|
||||
expect(accounts[2].id).toBe("addAccount");
|
||||
expect(accounts[2].isActive).toBeFalsy();
|
||||
expect(accounts[2].server).toBe(undefined);
|
||||
});
|
||||
|
||||
it.each([5, 6])(
|
||||
|
||||
@@ -66,11 +66,12 @@ export class AccountSwitcherService {
|
||||
const hasMaxAccounts = loggedInIds.length >= this.ACCOUNT_LIMIT;
|
||||
const options: AvailableAccount[] = await Promise.all(
|
||||
loggedInIds.map(async (id: UserId) => {
|
||||
const userEnv = await firstValueFrom(this.environmentService.getEnvironment$(id));
|
||||
return {
|
||||
name: accounts[id].name ?? accounts[id].email,
|
||||
email: accounts[id].email,
|
||||
id: id,
|
||||
server: (await this.environmentService.getEnvironment(id))?.getHostname(),
|
||||
server: userEnv?.getHostname(),
|
||||
status: accountStatuses[id],
|
||||
isActive: id === activeAccount?.id,
|
||||
avatarColor: await firstValueFrom(
|
||||
|
||||
@@ -110,7 +110,9 @@ export class AccountSwitcherComponent implements OnInit {
|
||||
name: active.name,
|
||||
email: active.email,
|
||||
avatarColor: await firstValueFrom(this.avatarService.avatarColor$),
|
||||
server: (await this.environmentService.getEnvironment())?.getHostname(),
|
||||
server: (
|
||||
await firstValueFrom(this.environmentService.getEnvironment$(active.id))
|
||||
)?.getHostname(),
|
||||
};
|
||||
}),
|
||||
);
|
||||
@@ -221,7 +223,9 @@ export class AccountSwitcherComponent implements OnInit {
|
||||
email: baseAccounts[userId].email,
|
||||
authenticationStatus: await this.authService.getAuthStatus(userId),
|
||||
avatarColor: await firstValueFrom(this.avatarService.getUserAvatarColor$(userId as UserId)),
|
||||
server: (await this.environmentService.getEnvironment(userId))?.getHostname(),
|
||||
server: (
|
||||
await firstValueFrom(this.environmentService.getEnvironment$(userId as UserId))
|
||||
)?.getHostname(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user