From 5823e063d1020746e89b609f8596788186465930 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Fri, 14 Oct 2022 13:26:04 +0800 Subject: [PATCH] feat(core): update sie (#2128) --- .../core/src/__mocks__/sign-in-experience.ts | 4 ++ .../src/routes/sign-in-experience.test.ts | 47 +++++-------------- .../core/src/routes/sign-in-experience.ts | 10 +++- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/packages/core/src/__mocks__/sign-in-experience.ts b/packages/core/src/__mocks__/sign-in-experience.ts index a11510927..bd65c4740 100644 --- a/packages/core/src/__mocks__/sign-in-experience.ts +++ b/packages/core/src/__mocks__/sign-in-experience.ts @@ -102,3 +102,7 @@ export const mockSignInMethod: SignIn['methods'][0] = { verificationCode: false, isPasswordPrimary: true, }; + +export const mockSignIn = { + methods: [mockSignInMethod], +}; diff --git a/packages/core/src/routes/sign-in-experience.test.ts b/packages/core/src/routes/sign-in-experience.test.ts index 5873e2efe..1f02bb081 100644 --- a/packages/core/src/routes/sign-in-experience.test.ts +++ b/packages/core/src/routes/sign-in-experience.test.ts @@ -14,9 +14,13 @@ import { mockSignInMethods, mockWechatConnector, mockColor, + mockSignUp, + mockSignIn, } from '@/__mocks__'; import * as signInExpLib from '@/lib/sign-in-experience'; +import * as signInLib from '@/lib/sign-in-experience/sign-in'; import * as signInMethodsLib from '@/lib/sign-in-experience/sign-in-methods'; +import * as signUpLib from '@/lib/sign-in-experience/sign-up'; import { createRequester } from '@/utils/test-utils'; import signInExperiencesRoutes from './sign-in-experience'; @@ -65,39 +69,6 @@ describe('GET /sign-in-exp', () => { }); describe('PATCH /sign-in-exp', () => { - it('should not update social connector targets when social sign-in is disabled', async () => { - const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Disabled }; - const response = await signInExperienceRequester.patch('/sign-in-exp').send({ - signInMethods, - socialSignInConnectorTargets: ['facebook'], - }); - expect(response).toMatchObject({ - status: 200, - body: { - ...mockSignInExperience, - signInMethods, - }, - }); - }); - - it('should update enabled social connector targets only when social sign-in is enabled', async () => { - const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Secondary }; - const socialSignInConnectorTargets = ['facebook']; - const signInExperience = { - signInMethods, - socialSignInConnectorTargets, - }; - const response = await signInExperienceRequester.patch('/sign-in-exp').send(signInExperience); - expect(response).toMatchObject({ - status: 200, - body: { - ...mockSignInExperience, - signInMethods, - socialSignInConnectorTargets, - }, - }); - }); - it('should update social connector targets in correct sorting order', async () => { const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Secondary }; const socialSignInConnectorTargets = ['github', 'facebook']; @@ -141,6 +112,8 @@ describe('PATCH /sign-in-exp', () => { const validateBranding = jest.spyOn(signInExpLib, 'validateBranding'); const validateTermsOfUse = jest.spyOn(signInExpLib, 'validateTermsOfUse'); const validateSignInMethods = jest.spyOn(signInMethodsLib, 'validateSignInMethods'); + const validateSignIn = jest.spyOn(signInLib, 'validateSignIn'); + const validateSignUp = jest.spyOn(signUpLib, 'validateSignUp'); const response = await signInExperienceRequester.patch('/sign-in-exp').send({ color: mockColor, @@ -148,15 +121,21 @@ describe('PATCH /sign-in-exp', () => { termsOfUse, signInMethods: mockSignInMethods, socialSignInConnectorTargets, + signUp: mockSignUp, + signIn: mockSignIn, + forgotPassword: true, }); + const connectors = [mockFacebookConnector, mockGithubConnector, mockWechatConnector]; expect(validateBranding).toHaveBeenCalledWith(mockBranding); expect(validateTermsOfUse).toHaveBeenCalledWith(termsOfUse); expect(validateSignInMethods).toHaveBeenCalledWith( mockSignInMethods, socialSignInConnectorTargets, - [mockFacebookConnector, mockGithubConnector, mockWechatConnector] + connectors ); + expect(validateSignUp).toHaveBeenCalledWith(mockSignUp, connectors); + expect(validateSignIn).toHaveBeenCalledWith(mockSignIn, mockSignUp, connectors); expect(response).toMatchObject({ status: 200, diff --git a/packages/core/src/routes/sign-in-experience.ts b/packages/core/src/routes/sign-in-experience.ts index 021fe146a..89776ec03 100644 --- a/packages/core/src/routes/sign-in-experience.ts +++ b/packages/core/src/routes/sign-in-experience.ts @@ -7,6 +7,7 @@ import { validateSignInMethods, isEnabled, validateSignUp, + validateSignIn, } from '@/lib/sign-in-experience'; import koaGuard from '@/middleware/koa-guard'; import { @@ -34,7 +35,7 @@ export default function signInExperiencesRoutes(router: }), async (ctx, next) => { const { socialSignInConnectorTargets, ...rest } = ctx.guard.body; - const { branding, termsOfUse, signInMethods, signUp } = rest; + const { branding, termsOfUse, signInMethods, signUp, signIn } = rest; if (branding) { validateBranding(branding); @@ -67,6 +68,13 @@ export default function signInExperiencesRoutes(router: validateSignUp(signUp, enabledConnectors); } + if (signIn && signUp) { + validateSignIn(signIn, signUp, enabledConnectors); + } else if (signIn) { + const signInExperience = await findDefaultSignInExperience(); + validateSignIn(signIn, signInExperience.signUp, enabledConnectors); + } + // Update socialSignInConnectorTargets only when social sign-in is enabled. const signInExperience = signInMethods && isEnabled(signInMethods.social)