mirror of
https://github.com/bitwarden/clients.git
synced 2026-11-16 07:03:29 +00:00
Updating tests coverage for default set initial password service
This commit is contained in:
@@ -355,8 +355,7 @@ export class DefaultSetInitialPasswordService implements SetInitialPasswordServi
|
||||
organization_sso_identifier: orgSsoIdentifier,
|
||||
user_id: asUuid<SdkUserId>(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,
|
||||
});
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user