Skip to content

Commit

Permalink
Add test for processDeviceVerificationResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-livefront committed Jan 15, 2025
1 parent 84cc367 commit 0cae6f4
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion libs/auth/src/common/login-strategies/login.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/for
import { PasswordTokenRequest } from "@bitwarden/common/auth/models/request/identity-token/password-token.request";
import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/identity-token/token-two-factor.request";
import { IdentityCaptchaResponse } from "@bitwarden/common/auth/models/response/identity-captcha.response";
import { IdentityDeviceVerificationResponse } from "@bitwarden/common/auth/models/response/identity-device-verification.response";
import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response";
import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response";
import { MasterPasswordPolicyResponse } from "@bitwarden/common/auth/models/response/master-password-policy.response";
Expand Down Expand Up @@ -381,7 +382,9 @@ describe("LoginStrategy", () => {
expect(tokenService.clearTwoFactorToken).toHaveBeenCalled();

const expected = new AuthResult();
expected.twoFactorProviders = { 0: null };
expected.twoFactorProviders = { 0: null } as unknown as Partial<
Record<TwoFactorProviderType, Record<string, string>>
>;
expected.email = "";
expected.ssoEmail2FaSessionToken = undefined;
expect(result).toEqual(expected);
Expand Down Expand Up @@ -511,4 +514,54 @@ describe("LoginStrategy", () => {
);
});
});

describe("Device verification", () => {
it("processes device verification response", async () => {
const captchaToken = "test-captcha-token";
const deviceVerificationResponse = new IdentityDeviceVerificationResponse({
error: "invalid_grant",
error_description: "Device verification required.",
email: "[email protected]",
deviceVerificationRequest: true,
captchaToken: captchaToken,
});

apiService.postIdentityToken.mockResolvedValue(deviceVerificationResponse);

cache = new PasswordLoginStrategyData();
cache.tokenRequest = new PasswordTokenRequest(
email,
masterPasswordHash,
"",
new TokenTwoFactorRequest(),
);

passwordLoginStrategy = new PasswordLoginStrategy(
cache,
passwordStrengthService,
policyService,
loginStrategyService,
accountService as AccountService,
masterPasswordService,
keyService,
encryptService,
apiService,
tokenService,
appIdService,
platformUtilsService,
messagingService,
logService,
stateService,
twoFactorService,
userDecryptionOptionsService,
billingAccountProfileStateService,
vaultTimeoutSettingsService,
kdfConfigService,
);

const result = await passwordLoginStrategy.logIn(credentials);

expect(result.requiresDeviceVerification).toBe(true);
});
});
});

0 comments on commit 0cae6f4

Please sign in to comment.