Skip to content

Commit

Permalink
fix(core): fix the test
Browse files Browse the repository at this point in the history
fix the test
  • Loading branch information
simeng-li committed Jun 21, 2024
1 parent f2caa40 commit 5d2d15b
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { type ToZodObject } from '@logto/schemas/lib/utils/zod.js';
import { generateStandardId } from '@logto/shared';
import { z } from 'zod';

import { type createPasscodeLibrary } from '#src/libraries/passcode.js';
import type Libraries from '#src/tenants/Libraries.js';
import type Queries from '#src/tenants/Queries.js';
import assertThat from '#src/utils/assert-that.js';
Expand Down Expand Up @@ -56,6 +57,12 @@ export const codeVerificationRecordDataGuard = z.object({
verified: z.boolean(),
}) satisfies ToZodObject<CodeVerificationRecordData>;

/** This util method convert the interaction identifier to passcode library payload format */
const getPasscodeIdentifierPayload = (
identifier: VerificationCodeIdentifier
): Parameters<ReturnType<typeof createPasscodeLibrary>['createPasscode']>[2] =>

Check warning on line 63 in packages/core/src/routes/experience/classes/verifications/code-verification.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/verifications/code-verification.ts#L62-L63

Added lines #L62 - L63 were not covered by tests
identifier.type === 'email' ? { email: identifier.value } : { phone: identifier.value };

/**
* CodeVerification is a verification type that verifies a given identifier by sending a verification code
* to the user's email or phone.
Expand Down Expand Up @@ -123,16 +130,13 @@ export class CodeVerification implements Verification {
/**
* Verify the `identifier` with the given code
*
* @remark The identifier must match the current identifier of the verification record.
* The code will be verified by checking the passcode record in the DB.
* @remark The identifier and code will be verified in the passcode library.
* No need to verify the identifier before calling this method.
*
* - `isVerified` will be set to true if the code is verified successfully.
* - `verifiedUserId` will be set if the `identifier` matches any existing user's record.
*/
async verify(identifier: VerificationCodeIdentifier, code?: string) {
// Throw code not found error if the input identifier does not match the current identifier
assertThat(identifier === this.identifier, 'verification_code.not_found');

// Throw code not found error if the code is not provided
assertThat(code, 'verification_code.not_found');

Expand All @@ -142,7 +146,7 @@ export class CodeVerification implements Verification {
this.id,
getTemplateTypeByEvent(this.interactionEvent),
code,
this.codeIdentifierPayload
getPasscodeIdentifierPayload(identifier)
);

this.verified = true;
Expand All @@ -163,13 +167,6 @@ export class CodeVerification implements Verification {
};
}

Check warning on line 168 in packages/core/src/routes/experience/classes/verifications/code-verification.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/classes/verifications/code-verification.ts#L160-L168

Added lines #L160 - L168 were not covered by tests

/** Format the `identifier` data for passcode library method use */
private get codeIdentifierPayload() {
return this.identifier.type === 'email'
? { email: this.identifier.value }
: { phone: this.identifier.value };
}

/**
* Send the verification code to the current `identifier`
*
Expand All @@ -183,7 +180,7 @@ export class CodeVerification implements Verification {
const verificationCode = await createPasscode(
this.id,
getTemplateTypeByEvent(this.interactionEvent),
this.codeIdentifierPayload
getPasscodeIdentifierPayload(this.identifier)
);

await sendPasscode(verificationCode);
Expand Down

0 comments on commit 5d2d15b

Please sign in to comment.