From dec5a561e28637c7f6df61934dd494871a2e2805 Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Wed, 1 May 2024 17:08:55 -0700 Subject: [PATCH] add tests for storePinKeyEncryptedUserKey() --- .../common/services/pin/pin.service.spec.ts | 54 +++++++++++++++---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/libs/auth/src/common/services/pin/pin.service.spec.ts b/libs/auth/src/common/services/pin/pin.service.spec.ts index 11aa005bc9..2c8bb6adf0 100644 --- a/libs/auth/src/common/services/pin/pin.service.spec.ts +++ b/libs/auth/src/common/services/pin/pin.service.spec.ts @@ -123,7 +123,7 @@ describe("PinService", () => { }); }); - describe("get/clear/create pinKeyEncryptedUserKey methods", () => { + describe("get/clear/create/store pinKeyEncryptedUserKey methods", () => { describe("getPinKeyEncryptedUserKey()", () => { it("should get the pinKeyEncryptedUserKey of the specified userId", async () => { await sut.getPinKeyEncryptedUserKey(mockUserId); @@ -176,6 +176,48 @@ describe("PinService", () => { sut.createPinKeyEncryptedUserKey(mockPin, undefined, mockUserId), ).rejects.toThrow("No UserKey provided. Cannot create pinKeyEncryptedUserKey."); }); + + it("should create a pinKeyEncryptedUserKey", async () => { + sut.makePinKey = jest.fn().mockResolvedValue(mockPinKey); + + await sut.createPinKeyEncryptedUserKey(mockPin, mockUserKey, mockUserId); + + expect(encryptService.encrypt).toHaveBeenCalledWith(mockUserKey.key, mockPinKey); + }); + }); + + describe("storePinKeyEncryptedUserKey", () => { + it("should store a pinKeyEncryptedUserKey (persistent version) when 'storeAsEphemeral' is false", async () => { + const storeAsEphemeral = false; + + await sut.storePinKeyEncryptedUserKey( + pinKeyEncryptedUserKeyPersistant, + storeAsEphemeral, + mockUserId, + ); + + expect(stateProvider.mock.setUserState).toHaveBeenCalledWith( + PIN_KEY_ENCRYPTED_USER_KEY, + pinKeyEncryptedUserKeyPersistant.encryptedString, + mockUserId, + ); + }); + + it("should store a pinKeyEncryptedUserKeyEphemeral when 'storeAsEphemeral' is true", async () => { + const storeAsEphemeral = true; + + await sut.storePinKeyEncryptedUserKey( + pinKeyEncryptedUserKeyEphemeral, + storeAsEphemeral, + mockUserId, + ); + + expect(stateProvider.mock.setUserState).toHaveBeenCalledWith( + PIN_KEY_ENCRYPTED_USER_KEY_EPHEMERAL, + pinKeyEncryptedUserKeyEphemeral.encryptedString, + mockUserId, + ); + }); }); }); @@ -243,16 +285,6 @@ describe("PinService", () => { }); }); - describe("createPinKeyEncryptedUserKey()", () => { - it("should create a pinKeyEncryptedUserKey", async () => { - sut.makePinKey = jest.fn().mockResolvedValue(mockPinKey); - - await sut.createPinKeyEncryptedUserKey(mockPin, mockUserKey, mockUserId); - - expect(encryptService.encrypt).toHaveBeenCalledWith(mockUserKey.key, mockPinKey); - }); - }); - describe("makePinKey()", () => { it("should make a PinKey", async () => { keyGenerationService.deriveKeyFromPassword.mockResolvedValue(mockPinKey);