0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

test: add sad paths for reset password flow (#4298)

This commit is contained in:
Xiao Yijun 2023-08-10 11:37:23 +08:00 committed by GitHub
parent 74849dde22
commit 24686cf0bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,113 @@
import { ConnectorType } from '@logto/connector-kit';
import { InteractionEvent } from '@logto/schemas';
import { suspendUser } from '#src/api/admin-user.js';
import {
patchInteractionIdentifiers,
putInteraction,
putInteractionProfile,
sendVerificationCode,
} from '#src/api/interaction.js';
import { initClient } from '#src/helpers/client.js';
import {
clearConnectorsByTypes,
setEmailConnector,
setSmsConnector,
} from '#src/helpers/connector.js';
import { expectRejects, readVerificationCode } from '#src/helpers/index.js';
import { generateNewUser, generateNewUserProfile } from '#src/helpers/user.js';
import { generatePassword } from '#src/utils.js';
describe('reset password flow sad path', () => {
it('Should fail to reset password with email if related user is not exist', async () => {
await setEmailConnector();
const { primaryEmail } = generateNewUserProfile({ primaryEmail: true });
const client = await initClient();
await client.successSend(putInteraction, { event: InteractionEvent.ForgotPassword });
await client.successSend(sendVerificationCode, {
email: primaryEmail,
});
const { code: verificationCode } = await readVerificationCode();
await client.successSend(patchInteractionIdentifiers, {
email: primaryEmail,
verificationCode,
});
await client.successSend(putInteractionProfile, { password: generatePassword() });
await expectRejects(client.submitInteraction(), {
code: 'user.user_not_exist',
statusCode: 404,
});
// Clear
await clearConnectorsByTypes([ConnectorType.Email]);
});
it('Should fail to reset password with phone if related user is not exist', async () => {
await setSmsConnector();
const { primaryPhone } = generateNewUserProfile({ primaryPhone: true });
const client = await initClient();
await client.successSend(putInteraction, { event: InteractionEvent.ForgotPassword });
await client.successSend(sendVerificationCode, {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode();
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,
verificationCode,
});
await client.successSend(putInteractionProfile, { password: generatePassword() });
await expectRejects(client.submitInteraction(), {
code: 'user.user_not_exist',
statusCode: 404,
});
// Clear
await clearConnectorsByTypes([ConnectorType.Sms]);
});
it('Should fail to reset password if related user is suspended', async () => {
await setSmsConnector();
const {
user,
userProfile: { primaryPhone },
} = await generateNewUser({
primaryPhone: true,
password: true,
});
await suspendUser(user.id, true);
const client = await initClient();
await client.successSend(putInteraction, { event: InteractionEvent.ForgotPassword });
await client.successSend(sendVerificationCode, {
phone: primaryPhone,
});
const { code: verificationCode } = await readVerificationCode();
await client.successSend(patchInteractionIdentifiers, {
phone: primaryPhone,
verificationCode,
});
await client.successSend(putInteractionProfile, { password: generatePassword() });
await expectRejects(client.submitInteraction(), {
code: 'user.suspended',
statusCode: 401,
});
// Clear
await clearConnectorsByTypes([ConnectorType.Sms]);
});
});