1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 06:43:35 +00:00

[PM-5270] Update PasswordResetEnrollmentService to take dependency on AccountService (#8080)

* take dependency on AccountService

* update test
This commit is contained in:
rr-bw
2024-03-12 10:11:42 -07:00
committed by GitHub
parent 6b74daacd6
commit 790b310d22
3 changed files with 25 additions and 9 deletions

View File

@@ -736,7 +736,7 @@ import { ModalService } from "./modal.service";
useClass: PasswordResetEnrollmentServiceImplementation, useClass: PasswordResetEnrollmentServiceImplementation,
deps: [ deps: [
OrganizationApiServiceAbstraction, OrganizationApiServiceAbstraction,
StateServiceAbstraction, AccountServiceAbstraction,
CryptoServiceAbstraction, CryptoServiceAbstraction,
OrganizationUserService, OrganizationUserService,
I18nServiceAbstraction, I18nServiceAbstraction,

View File

@@ -1,17 +1,22 @@
import { mock, MockProxy } from "jest-mock-extended"; import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs";
import { UserId } from "../../../../common/src/types/guid";
import { OrganizationApiServiceAbstraction } from "../../admin-console/abstractions/organization/organization-api.service.abstraction"; import { OrganizationApiServiceAbstraction } from "../../admin-console/abstractions/organization/organization-api.service.abstraction";
import { OrganizationUserService } from "../../admin-console/abstractions/organization-user/organization-user.service"; import { OrganizationUserService } from "../../admin-console/abstractions/organization-user/organization-user.service";
import { OrganizationAutoEnrollStatusResponse } from "../../admin-console/models/response/organization-auto-enroll-status.response"; import { OrganizationAutoEnrollStatusResponse } from "../../admin-console/models/response/organization-auto-enroll-status.response";
import { CryptoService } from "../../platform/abstractions/crypto.service"; import { CryptoService } from "../../platform/abstractions/crypto.service";
import { I18nService } from "../../platform/abstractions/i18n.service"; import { I18nService } from "../../platform/abstractions/i18n.service";
import { StateService } from "../../platform/abstractions/state.service"; import { AccountInfo, AccountService } from "../abstractions/account.service";
import { AuthenticationStatus } from "../enums/authentication-status";
import { PasswordResetEnrollmentServiceImplementation } from "./password-reset-enrollment.service.implementation"; import { PasswordResetEnrollmentServiceImplementation } from "./password-reset-enrollment.service.implementation";
describe("PasswordResetEnrollmentServiceImplementation", () => { describe("PasswordResetEnrollmentServiceImplementation", () => {
const activeAccountSubject = new BehaviorSubject<{ id: UserId } & AccountInfo>(null);
let organizationApiService: MockProxy<OrganizationApiServiceAbstraction>; let organizationApiService: MockProxy<OrganizationApiServiceAbstraction>;
let stateService: MockProxy<StateService>; let accountService: MockProxy<AccountService>;
let cryptoService: MockProxy<CryptoService>; let cryptoService: MockProxy<CryptoService>;
let organizationUserService: MockProxy<OrganizationUserService>; let organizationUserService: MockProxy<OrganizationUserService>;
let i18nService: MockProxy<I18nService>; let i18nService: MockProxy<I18nService>;
@@ -19,13 +24,14 @@ describe("PasswordResetEnrollmentServiceImplementation", () => {
beforeEach(() => { beforeEach(() => {
organizationApiService = mock<OrganizationApiServiceAbstraction>(); organizationApiService = mock<OrganizationApiServiceAbstraction>();
stateService = mock<StateService>(); accountService = mock<AccountService>();
accountService.activeAccount$ = activeAccountSubject;
cryptoService = mock<CryptoService>(); cryptoService = mock<CryptoService>();
organizationUserService = mock<OrganizationUserService>(); organizationUserService = mock<OrganizationUserService>();
i18nService = mock<I18nService>(); i18nService = mock<I18nService>();
service = new PasswordResetEnrollmentServiceImplementation( service = new PasswordResetEnrollmentServiceImplementation(
organizationApiService, organizationApiService,
stateService, accountService,
cryptoService, cryptoService,
organizationUserService, organizationUserService,
i18nService, i18nService,
@@ -81,7 +87,14 @@ describe("PasswordResetEnrollmentServiceImplementation", () => {
}; };
const encryptedKey = { encryptedString: "encryptedString" }; const encryptedKey = { encryptedString: "encryptedString" };
organizationApiService.getKeys.mockResolvedValue(orgKeyResponse as any); organizationApiService.getKeys.mockResolvedValue(orgKeyResponse as any);
stateService.getUserId.mockResolvedValue("userId");
const user1AccountInfo: AccountInfo = {
name: "Test User 1",
email: "test1@email.com",
status: AuthenticationStatus.Unlocked,
};
activeAccountSubject.next(Object.assign(user1AccountInfo, { id: "userId" as UserId }));
cryptoService.getUserKey.mockResolvedValue({ key: "key" } as any); cryptoService.getUserKey.mockResolvedValue({ key: "key" } as any);
cryptoService.rsaEncrypt.mockResolvedValue(encryptedKey as any); cryptoService.rsaEncrypt.mockResolvedValue(encryptedKey as any);

View File

@@ -1,11 +1,13 @@
import { firstValueFrom, map } from "rxjs";
import { OrganizationApiServiceAbstraction } from "../../admin-console/abstractions/organization/organization-api.service.abstraction"; import { OrganizationApiServiceAbstraction } from "../../admin-console/abstractions/organization/organization-api.service.abstraction";
import { OrganizationUserService } from "../../admin-console/abstractions/organization-user/organization-user.service"; import { OrganizationUserService } from "../../admin-console/abstractions/organization-user/organization-user.service";
import { OrganizationUserResetPasswordEnrollmentRequest } from "../../admin-console/abstractions/organization-user/requests"; import { OrganizationUserResetPasswordEnrollmentRequest } from "../../admin-console/abstractions/organization-user/requests";
import { CryptoService } from "../../platform/abstractions/crypto.service"; import { CryptoService } from "../../platform/abstractions/crypto.service";
import { I18nService } from "../../platform/abstractions/i18n.service"; import { I18nService } from "../../platform/abstractions/i18n.service";
import { StateService } from "../../platform/abstractions/state.service";
import { Utils } from "../../platform/misc/utils"; import { Utils } from "../../platform/misc/utils";
import { UserKey } from "../../types/key"; import { UserKey } from "../../types/key";
import { AccountService } from "../abstractions/account.service";
import { PasswordResetEnrollmentServiceAbstraction } from "../abstractions/password-reset-enrollment.service.abstraction"; import { PasswordResetEnrollmentServiceAbstraction } from "../abstractions/password-reset-enrollment.service.abstraction";
export class PasswordResetEnrollmentServiceImplementation export class PasswordResetEnrollmentServiceImplementation
@@ -13,7 +15,7 @@ export class PasswordResetEnrollmentServiceImplementation
{ {
constructor( constructor(
protected organizationApiService: OrganizationApiServiceAbstraction, protected organizationApiService: OrganizationApiServiceAbstraction,
protected stateService: StateService, protected accountService: AccountService,
protected cryptoService: CryptoService, protected cryptoService: CryptoService,
protected organizationUserService: OrganizationUserService, protected organizationUserService: OrganizationUserService,
protected i18nService: I18nService, protected i18nService: I18nService,
@@ -38,7 +40,8 @@ export class PasswordResetEnrollmentServiceImplementation
const orgPublicKey = Utils.fromB64ToArray(orgKeyResponse.publicKey); const orgPublicKey = Utils.fromB64ToArray(orgKeyResponse.publicKey);
userId = userId ?? (await this.stateService.getUserId()); userId =
userId ?? (await firstValueFrom(this.accountService.activeAccount$.pipe(map((a) => a?.id))));
userKey = userKey ?? (await this.cryptoService.getUserKey(userId)); userKey = userKey ?? (await this.cryptoService.getUserKey(userId));
// RSA Encrypt user's userKey.key with organization public key // RSA Encrypt user's userKey.key with organization public key
const encryptedKey = await this.cryptoService.rsaEncrypt(userKey.key, orgPublicKey); const encryptedKey = await this.cryptoService.rsaEncrypt(userKey.key, orgPublicKey);