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:
parent
c67d4a382f
commit
a17c0ae81f
5 changed files with 83 additions and 84 deletions
57
packages/core/src/lib/sign-in-experience/index.test.ts
Normal file
57
packages/core/src/lib/sign-in-experience/index.test.ts
Normal 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'));
|
||||
});
|
||||
});
|
20
packages/core/src/lib/sign-in-experience/index.ts
Normal file
20
packages/core/src/lib/sign-in-experience/index.ts
Normal 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'
|
||||
);
|
||||
};
|
|
@ -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();
|
|
@ -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 = (
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue