From 49d79d0e2ac1d0c78630d7d3227cbcd4743f1dcf Mon Sep 17 00:00:00 2001 From: Ike Kottlowski Date: Thu, 12 Nov 2026 13:11:41 -0500 Subject: [PATCH] Updating tests coverage for default set initial password service --- ...initial-password.service.implementation.ts | 3 +- ...fault-set-initial-password.service.spec.ts | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.implementation.ts b/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.implementation.ts index f3b8b1c6521..27a45bd3f62 100644 --- a/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.implementation.ts +++ b/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.implementation.ts @@ -355,8 +355,7 @@ export class DefaultSetInitialPasswordService implements SetInitialPasswordServi organization_sso_identifier: orgSsoIdentifier, user_id: asUuid(userId), reset_password_enroll: resetPasswordAutoEnroll, - // TODO PM-45010: pass credentials.acceptOrganizationInvite as accept_organization_invite - // after the sdk-internal bump. + accept_organization_invite: credentials.acceptOrganizationInvite, }); }), ), diff --git a/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.spec.ts b/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.spec.ts index 7ea96afe59d..9722f832dc5 100644 --- a/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.spec.ts +++ b/libs/angular/src/auth/password-management/set-initial-password/default-set-initial-password.service.spec.ts @@ -686,6 +686,49 @@ describe("DefaultSetInitialPasswordService", () => { organizationUserApiService.putOrganizationUserResetPasswordEnrollment, ).not.toHaveBeenCalled(); }); + + it("should send the existing local key pair to the new endpoint instead of creating a new key pair", async () => { + // Arrange + credentials.acceptOrganizationInvite = false; + setupMocks({ ...defaultMockConfig, userHasLocalKeyPair: true }); + + // Act + await sut.setInitialPassword(credentials, userType, userId); + + // Assert + const request: SsoJitProvisionFinishPasswordRequest = + masterPasswordApiService.postSsoJitProvisionFinishPassword.mock.calls[0][0]; + expect(request.keys).toEqual({ + publicKey: Utils.fromBufferToB64(existingUserPublicKey), + encryptedPrivateKey: userKeyEncryptedPrivateKey.encryptedString, + }); + expect(legacyCompatKeyService.makeKeyPair).not.toHaveBeenCalled(); + }); + + it("should NOT change local state or enroll in account recovery when the new endpoint fails", async () => { + // Arrange + credentials.acceptOrganizationInvite = true; + credentials.resetPasswordAutoEnroll = true; + setupMocks({ ...defaultMockConfig, resetPasswordAutoEnroll: true }); + const error = new Error("request failed"); + masterPasswordApiService.postSsoJitProvisionFinishPassword.mockRejectedValue(error); + + // Act + const promise = sut.setInitialPassword(credentials, userType, userId); + + // Assert + await expect(promise).rejects.toThrow(error); + expect(masterPasswordService.setForceSetPasswordReason).not.toHaveBeenCalled(); + expect(kdfConfigService.setKdfConfig).not.toHaveBeenCalled(); + expect(masterPasswordService.setMasterPasswordUnlockData).not.toHaveBeenCalled(); + expect( + accountCryptographicStateService.setAccountCryptographicState, + ).not.toHaveBeenCalled(); + expect(unlockService.unlockWithDecryptedUserKey).not.toHaveBeenCalled(); + expect( + organizationUserApiService.putOrganizationUserResetPasswordEnrollment, + ).not.toHaveBeenCalled(); + }); }); }); @@ -695,6 +738,21 @@ describe("DefaultSetInitialPasswordService", () => { setPasswordRequest.keys = null; }); + [true, false].forEach((acceptOrganizationInvite) => { + it(`should use the legacy set-password endpoint when acceptOrganizationInvite is ${acceptOrganizationInvite}, because the new endpoint is only for JIT provisioned users`, async () => { + // Arrange + credentials.acceptOrganizationInvite = acceptOrganizationInvite; + setupMocks({ ...defaultMockConfig, userType }); + + // Act + await sut.setInitialPassword(credentials, userType, userId); + + // Assert + expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest); + expect(masterPasswordApiService.postSsoJitProvisionFinishPassword).not.toHaveBeenCalled(); + }); + }); + it("should NOT generate a keyPair", async () => { // Arrange setupMocks({ ...defaultMockConfig, userType }); @@ -1143,6 +1201,53 @@ describe("DefaultSetInitialPasswordService", () => { ); }); + describe("accept_organization_invite", () => { + it.each([true, false])( + "should pass acceptOrganizationInvite (%s) to the SDK as accept_organization_invite", + async (acceptOrganizationInvite) => { + await sut.initializePasswordJitPasswordUserV2Encryption( + { ...credentials, acceptOrganizationInvite }, + userId, + ); + + expect(mockRegistration).toHaveBeenCalledWith( + expect.objectContaining({ + accept_organization_invite: acceptOrganizationInvite, + }), + ); + }, + ); + + it("should pass accept_organization_invite as undefined to the SDK when acceptOrganizationInvite is undefined", async () => { + await sut.initializePasswordJitPasswordUserV2Encryption( + { ...credentials, acceptOrganizationInvite: undefined }, + userId, + ); + + const request = mockRegistration.mock.calls[0][0]; + expect(request.accept_organization_invite).toBeUndefined(); + }); + + it("should leave the account recovery enrollment decision to the SDK when acceptOrganizationInvite is false", async () => { + await sut.initializePasswordJitPasswordUserV2Encryption( + { ...credentials, resetPasswordAutoEnroll: true, acceptOrganizationInvite: false }, + userId, + ); + + // The SDK skips enrollment when accept_organization_invite is false, so the client + // passes both values unchanged and does not enroll on its own. + expect(mockRegistration).toHaveBeenCalledWith( + expect.objectContaining({ + reset_password_enroll: true, + accept_organization_invite: false, + }), + ); + expect( + organizationUserApiService.putOrganizationUserResetPasswordEnrollment, + ).not.toHaveBeenCalled(); + }); + }); + describe("input validation", () => { it.each([ "newPasswordHint",