0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

refactor(core): split sign in experience lib

This commit is contained in:
wangsijie 2022-10-10 15:27:25 +08:00
parent c67d4a382f
commit a17c0ae81f
No known key found for this signature in database
GPG key ID: C72642FE24F7D42B
5 changed files with 83 additions and 84 deletions

View file

@ -0,0 +1,57 @@
import { BrandingStyle } from '@logto/schemas';
import { mockBranding } from '@/__mocks__';
import RequestError from '@/errors/RequestError';
import { validateBranding, validateTermsOfUse } from '@/lib/sign-in-experience';
describe('validate branding', () => {
test('should throw when the UI style contains the slogan and slogan is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo_Slogan,
slogan: '',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_slogan'));
});
test('should throw when the logo is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo,
logoUrl: ' ',
slogan: '',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_logo'));
});
test('should throw when the UI style contains the slogan and slogan is blank', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo_Slogan,
slogan: ' \t\n',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_slogan'));
});
test('should not throw when the UI style does not contain the slogan and slogan is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo,
});
}).not.toThrow();
});
});
describe('validate terms of use', () => {
test('should throw when terms of use is enabled and content URL is empty', () => {
expect(() => {
validateTermsOfUse({
enabled: true,
});
}).toMatchError(new RequestError('sign_in_experiences.empty_content_url_of_terms_of_use'));
});
});

View file

@ -0,0 +1,20 @@
import { Branding, BrandingStyle, TermsOfUse } from '@logto/schemas';
import assertThat from '@/utils/assert-that';
export * from './sign-in-methods';
export const validateBranding = (branding: Branding) => {
if (branding.style === BrandingStyle.Logo_Slogan) {
assertThat(branding.slogan?.trim(), 'sign_in_experiences.empty_slogan');
}
assertThat(branding.logoUrl.trim(), 'sign_in_experiences.empty_logo');
};
export const validateTermsOfUse = (termsOfUse: TermsOfUse) => {
assertThat(
!termsOfUse.enabled || termsOfUse.contentUrl,
'sign_in_experiences.empty_content_url_of_terms_of_use'
);
};

View file

@ -1,74 +1,16 @@
import { BrandingStyle, SignInMethodState, ConnectorType } from '@logto/schemas';
import { SignInMethodState, ConnectorType } from '@logto/schemas';
import {
mockAliyunDmConnector,
mockFacebookConnector,
mockGithubConnector,
mockBranding,
mockSignInMethods,
} from '@/__mocks__';
import RequestError from '@/errors/RequestError';
import {
isEnabled,
validateBranding,
validateSignInMethods,
validateTermsOfUse,
} from '@/lib/sign-in-experience';
import { isEnabled, validateSignInMethods } from '@/lib/sign-in-experience';
const enabledConnectors = [mockFacebookConnector, mockGithubConnector];
describe('validate branding', () => {
test('should throw when the UI style contains the slogan and slogan is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo_Slogan,
slogan: '',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_slogan'));
});
test('should throw when the logo is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo,
logoUrl: ' ',
slogan: '',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_logo'));
});
test('should throw when the UI style contains the slogan and slogan is blank', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo_Slogan,
slogan: ' \t\n',
});
}).toMatchError(new RequestError('sign_in_experiences.empty_slogan'));
});
test('should not throw when the UI style does not contain the slogan and slogan is empty', () => {
expect(() => {
validateBranding({
...mockBranding,
style: BrandingStyle.Logo,
});
}).not.toThrow();
});
});
describe('validate terms of use', () => {
test('should throw when terms of use is enabled and content URL is empty', () => {
expect(() => {
validateTermsOfUse({
enabled: true,
});
}).toMatchError(new RequestError('sign_in_experiences.empty_content_url_of_terms_of_use'));
});
});
describe('check whether the social sign-in method state is enabled', () => {
it('should be truthy when sign-in method state is primary', () => {
expect(isEnabled(SignInMethodState.Primary)).toBeTruthy();

View file

@ -1,31 +1,10 @@
import {
Branding,
BrandingStyle,
SignInMethods,
SignInMethodState,
TermsOfUse,
} from '@logto/schemas';
import { ConnectorType, SignInMethods, SignInMethodState } from '@logto/schemas';
import { Optional } from '@silverhand/essentials';
import { ConnectorType, LogtoConnector } from '@/connectors/types';
import { LogtoConnector } from '@/connectors/types';
import RequestError from '@/errors/RequestError';
import assertThat from '@/utils/assert-that';
export const validateBranding = (branding: Branding) => {
if (branding.style === BrandingStyle.Logo_Slogan) {
assertThat(branding.slogan?.trim(), 'sign_in_experiences.empty_slogan');
}
assertThat(branding.logoUrl.trim(), 'sign_in_experiences.empty_logo');
};
export const validateTermsOfUse = (termsOfUse: TermsOfUse) => {
assertThat(
!termsOfUse.enabled || termsOfUse.contentUrl,
'sign_in_experiences.empty_content_url_of_terms_of_use'
);
};
export const isEnabled = (state: SignInMethodState) => state !== SignInMethodState.Disabled;
export const validateSignInMethods = (

View file

@ -16,6 +16,7 @@ import {
mockColor,
} from '@/__mocks__';
import * as signInExpLib from '@/lib/sign-in-experience';
import * as signInMethodsLib from '@/lib/sign-in-experience/sign-in-methods';
import { createRequester } from '@/utils/test-utils';
import signInExperiencesRoutes from './sign-in-experience';
@ -139,7 +140,7 @@ describe('PATCH /sign-in-exp', () => {
const validateBranding = jest.spyOn(signInExpLib, 'validateBranding');
const validateTermsOfUse = jest.spyOn(signInExpLib, 'validateTermsOfUse');
const validateSignInMethods = jest.spyOn(signInExpLib, 'validateSignInMethods');
const validateSignInMethods = jest.spyOn(signInMethodsLib, 'validateSignInMethods');
const response = await signInExperienceRequester.patch('/sign-in-exp').send({
color: mockColor,