1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-29 12:55:21 +01:00
bitwarden-browser/libs/common/spec/fake-account-service.ts
Matt Gibson c02723d6a6
Specify clearOn options for platform services (#8584)
* Use UserKeys in biometric state

* Remove global clear todo. Answer is never

* User UserKeys in crypto state

* Clear userkey on both lock and logout via User Key Definitions

* Use UserKeyDefinitions in environment service

* Rely on userKeyDefinition to clear org keys

* Rely on userKeyDefinition to clear provider keys

* Rely on userKeyDefinition to clear user keys

* Rely on userKeyDefinitions to clear user asym key pair
2024-04-09 11:17:00 -04:00

75 lines
2.6 KiB
TypeScript

import { mock } from "jest-mock-extended";
import { Observable, ReplaySubject } from "rxjs";
import { AccountInfo, AccountService } from "../src/auth/abstractions/account.service";
import { AuthenticationStatus } from "../src/auth/enums/authentication-status";
import { UserId } from "../src/types/guid";
export function mockAccountServiceWith(
userId: UserId,
info: Partial<AccountInfo> = {},
): FakeAccountService {
const fullInfo: AccountInfo = {
...info,
...{
name: "name",
email: "email",
status: AuthenticationStatus.Locked,
},
};
const service = new FakeAccountService({ [userId]: fullInfo });
service.activeAccountSubject.next({ id: userId, ...fullInfo });
return service;
}
export class FakeAccountService implements AccountService {
mock = mock<AccountService>();
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
accountsSubject = new ReplaySubject<Record<UserId, AccountInfo>>(1);
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
activeAccountSubject = new ReplaySubject<{ id: UserId } & AccountInfo>(1);
private _activeUserId: UserId;
get activeUserId() {
return this._activeUserId;
}
accounts$ = this.accountsSubject.asObservable();
activeAccount$ = this.activeAccountSubject.asObservable();
accountLock$: Observable<UserId>;
accountLogout$: Observable<UserId>;
constructor(initialData: Record<UserId, AccountInfo>) {
this.accountsSubject.next(initialData);
this.activeAccountSubject.subscribe((data) => (this._activeUserId = data?.id));
this.activeAccountSubject.next(null);
}
async addAccount(userId: UserId, accountData: AccountInfo): Promise<void> {
const current = this.accountsSubject["_buffer"][0] ?? {};
this.accountsSubject.next({ ...current, [userId]: accountData });
await this.mock.addAccount(userId, accountData);
}
async setAccountName(userId: UserId, name: string): Promise<void> {
await this.mock.setAccountName(userId, name);
}
async setAccountEmail(userId: UserId, email: string): Promise<void> {
await this.mock.setAccountEmail(userId, email);
}
async setAccountStatus(userId: UserId, status: AuthenticationStatus): Promise<void> {
await this.mock.setAccountStatus(userId, status);
}
async setMaxAccountStatus(userId: UserId, maxStatus: AuthenticationStatus): Promise<void> {
await this.mock.setMaxAccountStatus(userId, maxStatus);
}
async switchAccount(userId: UserId): Promise<void> {
const next =
userId == null ? null : { id: userId, ...this.accountsSubject["_buffer"]?.[0]?.[userId] };
this.activeAccountSubject.next(next);
await this.mock.switchAccount(userId);
}
}