mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
[PM-19172] Passes UserId through change email component and adds tests (#13686)
* add tests and pass userid * add tests for getOrDeriveMasterKey * remove extra coalesce for token
This commit is contained in:
@@ -736,6 +736,52 @@ describe("keyService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOrDeriveMasterKey", () => {
|
||||
it("returns the master key if it is already available", async () => {
|
||||
const getMasterKey = jest
|
||||
.spyOn(masterPasswordService, "masterKey$")
|
||||
.mockReturnValue(of("masterKey" as any));
|
||||
|
||||
const result = await keyService.getOrDeriveMasterKey("password", mockUserId);
|
||||
|
||||
expect(getMasterKey).toHaveBeenCalledWith(mockUserId);
|
||||
expect(result).toEqual("masterKey");
|
||||
});
|
||||
|
||||
it("derives the master key if it is not available", async () => {
|
||||
const getMasterKey = jest
|
||||
.spyOn(masterPasswordService, "masterKey$")
|
||||
.mockReturnValue(of(null as any));
|
||||
|
||||
const deriveKeyFromPassword = jest
|
||||
.spyOn(keyGenerationService, "deriveKeyFromPassword")
|
||||
.mockResolvedValue("mockMasterKey" as any);
|
||||
|
||||
kdfConfigService.getKdfConfig$.mockReturnValue(of("mockKdfConfig" as any));
|
||||
|
||||
const result = await keyService.getOrDeriveMasterKey("password", mockUserId);
|
||||
|
||||
expect(getMasterKey).toHaveBeenCalledWith(mockUserId);
|
||||
expect(deriveKeyFromPassword).toHaveBeenCalledWith("password", "email", "mockKdfConfig");
|
||||
expect(result).toEqual("mockMasterKey");
|
||||
});
|
||||
|
||||
it("throws an error if no user is found", async () => {
|
||||
accountService.activeAccountSubject.next(null);
|
||||
|
||||
await expect(keyService.getOrDeriveMasterKey("password")).rejects.toThrow("No user found");
|
||||
});
|
||||
|
||||
it("throws an error if no kdf config is found", async () => {
|
||||
jest.spyOn(masterPasswordService, "masterKey$").mockReturnValue(of(null as any));
|
||||
kdfConfigService.getKdfConfig$.mockReturnValue(of(null));
|
||||
|
||||
await expect(keyService.getOrDeriveMasterKey("password", mockUserId)).rejects.toThrow(
|
||||
"No kdf found for user",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareKeyHash", () => {
|
||||
type TestCase = {
|
||||
masterKey: MasterKey;
|
||||
|
||||
@@ -287,10 +287,15 @@ export class DefaultKeyService implements KeyServiceAbstraction {
|
||||
),
|
||||
);
|
||||
const masterKey = await firstValueFrom(this.masterPasswordService.masterKey$(resolvedUserId));
|
||||
return (
|
||||
masterKey ||
|
||||
(await this.makeMasterKey(password, email, await this.kdfConfigService.getKdfConfig()))
|
||||
);
|
||||
if (masterKey != null) {
|
||||
return masterKey;
|
||||
}
|
||||
|
||||
const kdf = await firstValueFrom(this.kdfConfigService.getKdfConfig$(resolvedUserId));
|
||||
if (kdf == null) {
|
||||
throw new Error("No kdf found for user");
|
||||
}
|
||||
return await this.makeMasterKey(password, email, kdf);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user