0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-24 22:41:28 -05:00

refactor: update helper function names

This commit is contained in:
Gao Sun 2024-01-30 09:44:21 +08:00
parent 6befe60149
commit 87fd9a01e4
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
13 changed files with 75 additions and 63 deletions

View file

@ -23,25 +23,37 @@ export const createUserByAdmin = async (
});
};
type VerificationCodeRecord = {
type ConnectorMessageRecord = {
phone?: string;
address?: string;
code: string;
type: string;
};
export const readVerificationCode = async (
/**
* Read the most recent connector message record from file system that is created by mock connectors.
*
* @param forType The type of connector to read message from.
* @returns A promise that resolves to the connector message record.
*/
export const readConnectorMessage = async (
forType: keyof typeof mockConnectorFilePaths
): Promise<VerificationCodeRecord> => {
): Promise<ConnectorMessageRecord> => {
const buffer = await fs.readFile(mockConnectorFilePaths[forType]);
const content = buffer.toString();
// For test use only
// eslint-disable-next-line no-restricted-syntax
return JSON.parse(content) as VerificationCodeRecord;
return JSON.parse(content) as ConnectorMessageRecord;
};
export const removeVerificationCode = async (
/**
* Remove the connector message record file from file system. If the file does not exist, do nothing.
*
* @param forType The type of connector to remove message from.
* @returns A promise that resolves to void.
*/
export const removeConnectorMessage = async (
forType: keyof typeof mockConnectorFilePaths
): Promise<void> => {
try {

View file

@ -15,7 +15,7 @@ import {
import { generateUserId } from '#src/utils.js';
import { initClient, processSession, logoutClient } from './client.js';
import { expectRejects, readVerificationCode } from './index.js';
import { expectRejects, readConnectorMessage } from './index.js';
import { enableAllPasswordSignInMethods } from './sign-in-experience.js';
import { generateNewUser } from './user.js';
@ -101,7 +101,7 @@ export const resetPassword = async (
...profile,
});
const { code: verificationCode } = await readVerificationCode(
const { code: verificationCode } = await readConnectorMessage(
'email' in profile ? 'Email' : 'Sms'
);
await client.successSend(patchInteractionIdentifiers, {

View file

@ -14,7 +14,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateNewUser } from '#src/helpers/user.js';
import { generatePassword } from '#src/utils.js';
@ -48,7 +48,7 @@ describe('reset password', () => {
email: userProfile.primaryEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: userProfile.primaryEmail,
@ -108,7 +108,7 @@ describe('reset password', () => {
phone: userProfile.primaryPhone,
});
const verificationCodeRecord = await readVerificationCode('Sms');
const verificationCodeRecord = await readConnectorMessage('Sms');
expect(verificationCodeRecord).toMatchObject({
phone: userProfile.primaryPhone,

View file

@ -14,7 +14,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { generateNewUser, generateNewUserProfile } from '#src/helpers/user.js';
import { generatePassword } from '#src/utils.js';
@ -31,7 +31,7 @@ describe('reset password flow sad path', () => {
email: primaryEmail,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: primaryEmail,
verificationCode,
@ -59,7 +59,7 @@ describe('reset password flow sad path', () => {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode('Sms');
const { code: verificationCode } = await readConnectorMessage('Sms');
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,
verificationCode,
@ -95,7 +95,7 @@ describe('reset password flow sad path', () => {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode('Sms');
const { code: verificationCode } = await readConnectorMessage('Sms');
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,
verificationCode,

View file

@ -17,7 +17,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { readVerificationCode, expectRejects } from '#src/helpers/index.js';
import { readConnectorMessage, expectRejects } from '#src/helpers/index.js';
import {
enableAllVerificationCodeSignInMethods,
enableAllPasswordSignInMethods,
@ -79,7 +79,7 @@ describe('register with passwordless identifier', () => {
email: primaryEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: primaryEmail,
@ -125,7 +125,7 @@ describe('register with passwordless identifier', () => {
email: primaryEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
const { code } = verificationCodeRecord;
@ -186,7 +186,7 @@ describe('register with passwordless identifier', () => {
phone: primaryPhone,
});
const verificationCodeRecord = await readVerificationCode('Sms');
const verificationCodeRecord = await readConnectorMessage('Sms');
expect(verificationCodeRecord).toMatchObject({
phone: primaryPhone,
@ -232,7 +232,7 @@ describe('register with passwordless identifier', () => {
phone: primaryPhone,
});
const { code } = await readVerificationCode('Sms');
const { code } = await readConnectorMessage('Sms');
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,
@ -296,7 +296,7 @@ describe('register with passwordless identifier', () => {
email: primaryEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: primaryEmail,
@ -351,7 +351,7 @@ describe('register with passwordless identifier', () => {
phone: primaryPhone,
});
const verificationCodeRecord = await readVerificationCode('Sms');
const verificationCodeRecord = await readConnectorMessage('Sms');
expect(verificationCodeRecord).toMatchObject({
phone: primaryPhone,

View file

@ -13,7 +13,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllPasswordSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateNewUserProfile } from '#src/helpers/user.js';
import { generatePassword, generateUsername } from '#src/utils.js';
@ -85,7 +85,7 @@ describe('Register with identifiers sad path', () => {
email: primaryEmail,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await expectRejects(
client.send(patchInteractionIdentifiers, {
@ -116,7 +116,7 @@ describe('Register with identifiers sad path', () => {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode('Sms');
const { code: verificationCode } = await readConnectorMessage('Sms');
await expectRejects(
client.send(patchInteractionIdentifiers, {

View file

@ -17,7 +17,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateEmail, generateSsoConnectorName } from '#src/utils.js';
@ -32,7 +32,7 @@ const happyPath = async (email: string) => {
email,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: email,
@ -124,7 +124,7 @@ describe('test register with email with SSO feature', () => {
email,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await expectRejects(
client.send(patchInteractionIdentifiers, {

View file

@ -15,7 +15,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateNewUser, generateNewUserProfile } from '#src/helpers/user.js';
import { generateEmail, generatePhone } from '#src/utils.js';
@ -44,7 +44,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
email: userProfile.primaryEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: userProfile.primaryEmail,
@ -77,7 +77,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
phone: userProfile.primaryPhone,
});
const verificationCodeRecord = await readVerificationCode('Sms');
const verificationCodeRecord = await readConnectorMessage('Sms');
expect(verificationCodeRecord).toMatchObject({
phone: userProfile.primaryPhone,
@ -116,7 +116,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
email: newEmail,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
const { code } = verificationCodeRecord;
@ -158,7 +158,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
phone: newPhone,
});
const verificationCodeRecord = await readVerificationCode('Sms');
const verificationCodeRecord = await readConnectorMessage('Sms');
const { code } = verificationCodeRecord;
@ -205,7 +205,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
await client.successSend(sendVerificationCode, {
email: userProfile.primaryEmail,
});
const { code } = await readVerificationCode('Email');
const { code } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: userProfile.primaryEmail,
@ -267,7 +267,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
await client.successSend(sendVerificationCode, {
email: userProfile.primaryEmail,
});
const { code } = await readVerificationCode('Email');
const { code } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: userProfile.primaryEmail,
@ -324,7 +324,7 @@ describe('Sign-in flow using verification-code identifiers', () => {
await client.successSend(sendVerificationCode, {
email: userProfile.primaryEmail,
});
const { code } = await readVerificationCode('Email');
const { code } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: userProfile.primaryEmail,

View file

@ -15,7 +15,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateNewUser } from '#src/helpers/user.js';
import { generateEmail, generatePhone } from '#src/utils.js';
@ -72,7 +72,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
email: primaryEmail,
});
const { code: emailVerificationCode } = await readVerificationCode('Email');
const { code: emailVerificationCode } = await readConnectorMessage('Email');
await expectRejects(
client.send(patchInteractionIdentifiers, {
@ -90,7 +90,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
phone: primaryPhone,
});
const { code: phoneVerificationCode } = await readVerificationCode('Sms');
const { code: phoneVerificationCode } = await readConnectorMessage('Sms');
await expectRejects(
client.send(patchInteractionIdentifiers, {
@ -121,7 +121,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
email: primaryEmail,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await expectRejects(
client.send(patchInteractionIdentifiers, {
@ -160,7 +160,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode('Sms');
const { code: verificationCode } = await readConnectorMessage('Sms');
await expectRejects(
client.send(patchInteractionIdentifiers, {
@ -198,7 +198,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
email: notExistUserEmail,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: notExistUserEmail,
@ -224,7 +224,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
phone: notExistUserPhone,
});
const { code: verificationCode } = await readVerificationCode('Sms');
const { code: verificationCode } = await readConnectorMessage('Sms');
await client.successSend(patchInteractionIdentifiers, {
phone: notExistUserPhone,
@ -254,7 +254,7 @@ describe('Sign-in flow sad path using verification-code identifiers', () => {
email: primaryEmail,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: primaryEmail,

View file

@ -16,7 +16,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
import { generateEmail, generateSsoConnectorName } from '#src/utils.js';
@ -32,7 +32,7 @@ const happyPath = async (email: string) => {
email,
});
const verificationCodeRecord = await readVerificationCode('Email');
const verificationCodeRecord = await readConnectorMessage('Email');
expect(verificationCodeRecord).toMatchObject({
address: email,
@ -91,7 +91,7 @@ describe('test sign-in with email passcode identifier with SSO feature', () => {
email,
});
const { code: verificationCode } = await readVerificationCode('Email');
const { code: verificationCode } = await readConnectorMessage('Email');
await expectRejects(
client.send(patchInteractionIdentifiers, {

View file

@ -13,7 +13,7 @@ import {
setSmsConnector,
setEmailConnector,
} from '#src/helpers/connector.js';
import { readVerificationCode, expectRejects } from '#src/helpers/index.js';
import { readConnectorMessage, expectRejects } from '#src/helpers/index.js';
import {
enableAllPasswordSignInMethods,
enableAllVerificationCodeSignInMethods,
@ -121,7 +121,7 @@ describe('Sign-in flow using password identifiers', () => {
email: primaryEmail,
});
const { code } = await readVerificationCode('Email');
const { code } = await readConnectorMessage('Email');
await client.successSend(patchInteractionIdentifiers, {
email: primaryEmail,
@ -183,7 +183,7 @@ describe('Sign-in flow using password identifiers', () => {
phone: primaryPhone,
});
const { code } = await readVerificationCode('Sms');
const { code } = await readConnectorMessage('Sms');
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,

View file

@ -7,7 +7,7 @@ import {
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode, removeVerificationCode } from '#src/helpers/index.js';
import { expectRejects, readConnectorMessage, removeConnectorMessage } from '#src/helpers/index.js';
import { enableAllVerificationCodeSignInMethods } from '#src/helpers/sign-in-experience.js';
describe('Generic verification code through management API', () => {
@ -26,7 +26,7 @@ describe('Generic verification code through management API', () => {
});
afterEach(async () => {
await Promise.all([removeVerificationCode('Sms'), removeVerificationCode('Email')]);
await Promise.all([removeConnectorMessage('Sms'), removeConnectorMessage('Email')]);
});
it('should create an email verification code on server side', async () => {
@ -34,7 +34,7 @@ describe('Generic verification code through management API', () => {
const response = await requestVerificationCode(payload);
expect(response.statusCode).toBe(204);
const { code, type, address } = await readVerificationCode('Email');
const { code, type, address } = await readConnectorMessage('Email');
expect(type).toBe(TemplateType.Generic);
expect(address).toBe(mockEmail);
@ -46,7 +46,7 @@ describe('Generic verification code through management API', () => {
const response = await requestVerificationCode(payload);
expect(response.statusCode).toBe(204);
const { code, type, phone } = await readVerificationCode('Sms');
const { code, type, phone } = await readConnectorMessage('Sms');
expect(type).toBe(TemplateType.Generic);
expect(phone).toBe(mockPhone);
@ -59,8 +59,8 @@ describe('Generic verification code through management API', () => {
statusCode: 400,
});
await expect(readVerificationCode('Email')).rejects.toThrow();
await expect(readVerificationCode('Sms')).rejects.toThrow();
await expect(readConnectorMessage('Email')).rejects.toThrow();
await expect(readConnectorMessage('Sms')).rejects.toThrow();
});
it('should fail to send a verification code on server side when no email connector has been set', async () => {
@ -114,7 +114,7 @@ describe('Generic verification code through management API', () => {
it('should be able to verify the email verification code', async () => {
await requestVerificationCode({ email: mockEmail });
const { code } = await readVerificationCode('Email');
const { code } = await readConnectorMessage('Email');
await expect(
verifyVerificationCode({ email: mockEmail, verificationCode: code })
@ -124,7 +124,7 @@ describe('Generic verification code through management API', () => {
it('should be able to verify the sms verification code', async () => {
await requestVerificationCode({ phone: mockPhone });
const { code } = await readVerificationCode('Sms');
const { code } = await readConnectorMessage('Sms');
await expect(
verifyVerificationCode({ phone: mockPhone, verificationCode: code })
@ -133,7 +133,7 @@ describe('Generic verification code through management API', () => {
it('should throw when the code is not valid', async () => {
await requestVerificationCode({ phone: mockPhone });
await readVerificationCode('Sms');
await readConnectorMessage('Sms');
await expectRejects(verifyVerificationCode({ phone: mockPhone, verificationCode: '666' }), {
code: 'verification_code.code_mismatch',
statusCode: 400,
@ -144,7 +144,7 @@ describe('Generic verification code through management API', () => {
const phoneToVerify = '666';
const phoneToGetCode = mockPhone;
await requestVerificationCode({ phone: phoneToGetCode });
const { code, phone } = await readVerificationCode('Sms');
const { code, phone } = await readConnectorMessage('Sms');
expect(phoneToGetCode).toEqual(phone);
await expectRejects(verifyVerificationCode({ phone: phoneToVerify, verificationCode: code }), {
code: 'verification_code.not_found',
@ -156,7 +156,7 @@ describe('Generic verification code through management API', () => {
const emailToVerify = 'verify_email@mail.com';
const emailToGetCode = mockEmail;
await requestVerificationCode({ email: emailToGetCode });
const { code, address } = await readVerificationCode('Email');
const { code, address } = await readConnectorMessage('Email');
expect(emailToGetCode).toEqual(address);
await expectRejects(verifyVerificationCode({ email: emailToVerify, verificationCode: code }), {
code: 'verification_code.not_found',

View file

@ -2,7 +2,7 @@ import { type MfaFactor } from '@logto/schemas';
import { appendPath } from '@silverhand/essentials';
import { logtoUrl, mockSocialAuthPageUrl } from '#src/constants.js';
import { readVerificationCode } from '#src/helpers/index.js';
import { readConnectorMessage } from '#src/helpers/index.js';
import { dcls } from '#src/utils.js';
import ExpectPage from './expect-page.js';
@ -133,10 +133,10 @@ export default class ExpectExperience extends ExpectPage {
*/
async toCompleteVerification(
type: ExperienceType,
connectorType: Parameters<typeof readVerificationCode>['0']
connectorType: Parameters<typeof readConnectorMessage>['0']
) {
this.toBeAt(`${type}/verification-code`);
const { code } = await readVerificationCode(connectorType);
const { code } = await readConnectorMessage(connectorType);
await this.toFillVerificationCode(code);
}