From 0fcea5ae5e1047f1b10e95ede94305620df434b6 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 11 Aug 2023 18:40:06 +0800 Subject: [PATCH] test: add sad paths for register with identifiers flow (#4299) --- .../happy-path.test.ts} | 0 .../register-with-identifier/sad-path.test.ts | 124 ++++++++++++++++++ 2 files changed, 124 insertions(+) rename packages/integration-tests/src/tests/api/interaction/{register-with-identifier.test.ts => register-with-identifier/happy-path.test.ts} (100%) create mode 100644 packages/integration-tests/src/tests/api/interaction/register-with-identifier/sad-path.test.ts diff --git a/packages/integration-tests/src/tests/api/interaction/register-with-identifier.test.ts b/packages/integration-tests/src/tests/api/interaction/register-with-identifier/happy-path.test.ts similarity index 100% rename from packages/integration-tests/src/tests/api/interaction/register-with-identifier.test.ts rename to packages/integration-tests/src/tests/api/interaction/register-with-identifier/happy-path.test.ts diff --git a/packages/integration-tests/src/tests/api/interaction/register-with-identifier/sad-path.test.ts b/packages/integration-tests/src/tests/api/interaction/register-with-identifier/sad-path.test.ts new file mode 100644 index 000000000..28704d54a --- /dev/null +++ b/packages/integration-tests/src/tests/api/interaction/register-with-identifier/sad-path.test.ts @@ -0,0 +1,124 @@ +import { ConnectorType, InteractionEvent, SignInMode } from '@logto/schemas'; + +import { + patchInteractionIdentifiers, + putInteraction, + sendVerificationCode, +} from '#src/api/interaction.js'; +import { updateSignInExperience } from '#src/api/sign-in-experience.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 { enableAllPasswordSignInMethods } from '#src/helpers/sign-in-experience.js'; +import { generateNewUserProfile } from '#src/helpers/user.js'; +import { generatePassword, generateUsername } from '#src/utils.js'; + +describe('Register with identifiers sad path', () => { + it('Should fail to register if sign-in mode is sign-in only', async () => { + await updateSignInExperience({ signInMode: SignInMode.SignIn }); + const client = await initClient(); + + await expectRejects( + client.send(putInteraction, { + event: InteractionEvent.Register, + }), + { + code: 'auth.forbidden', + statusCode: 403, + } + ); + + // Reset + await updateSignInExperience({ signInMode: SignInMode.SignInAndRegister }); + }); + + describe('Should fail to register with identifiers if sign-up settings are not enabled', () => { + beforeAll(async () => { + // This function call will disable all sign-up settings by default + await enableAllPasswordSignInMethods(); + }); + + it('Should fail to register with username and password', async () => { + const client = await initClient(); + + await expectRejects( + client.send(putInteraction, { + event: InteractionEvent.Register, + profile: { + username: generateUsername(), + password: generatePassword(), + }, + }), + { + code: 'user.sign_in_method_not_enabled', + statusCode: 422, + } + ); + }); + + it('Should fail to register with email', async () => { + await setEmailConnector(); + const { primaryEmail } = generateNewUserProfile({ primaryEmail: true }); + const client = await initClient(); + + await client.successSend(putInteraction, { + event: InteractionEvent.Register, + }); + + await client.successSend(sendVerificationCode, { + email: primaryEmail, + }); + + const { code: verificationCode } = await readVerificationCode(); + + await expectRejects( + client.send(patchInteractionIdentifiers, { + email: primaryEmail, + verificationCode, + }), + { + code: 'user.sign_in_method_not_enabled', + statusCode: 422, + } + ); + + // Clear + await clearConnectorsByTypes([ConnectorType.Email]); + }); + + it('Should fail to register with phone', async () => { + await setSmsConnector(); + + const { primaryPhone } = generateNewUserProfile({ primaryPhone: true }); + const client = await initClient(); + + await client.successSend(putInteraction, { + event: InteractionEvent.Register, + }); + + await client.successSend(sendVerificationCode, { + phone: primaryPhone, + }); + + const { code: verificationCode } = await readVerificationCode(); + + await expectRejects( + client.send(patchInteractionIdentifiers, { + phone: primaryPhone, + verificationCode, + }), + { + code: 'user.sign_in_method_not_enabled', + statusCode: 422, + } + ); + + // Clear + await clearConnectorsByTypes([ConnectorType.Sms]); + }); + }); +});