0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00
logto/packages/core/src/oidc/utils.test.ts

124 lines
4 KiB
TypeScript
Raw Normal View History

2022-09-21 13:06:56 +08:00
import { ApplicationType, CustomClientMetadataKey, GrantType } from '@logto/schemas';
2023-02-09 18:31:14 +08:00
import { mockEnvSet } from '#src/test-utils/env-set.js';
import {
isOriginAllowed,
buildOidcClientMetadata,
2022-09-21 13:06:56 +08:00
getConstantClientMetadata,
validateCustomClientMetadata,
2022-11-21 16:38:24 +08:00
} from './utils.js';
2022-09-21 13:06:56 +08:00
describe('getConstantClientMetadata()', () => {
2023-02-09 18:31:14 +08:00
expect(getConstantClientMetadata(mockEnvSet, ApplicationType.SPA)).toEqual({
2022-09-21 13:06:56 +08:00
application_type: 'web',
2023-11-08 15:44:27 +08:00
grant_types: [GrantType.AuthorizationCode, GrantType.RefreshToken, GrantType.OrganizationToken],
2022-09-21 13:06:56 +08:00
token_endpoint_auth_method: 'none',
});
2023-02-09 18:31:14 +08:00
expect(getConstantClientMetadata(mockEnvSet, ApplicationType.Native)).toEqual({
2022-09-21 13:06:56 +08:00
application_type: 'native',
2023-11-08 15:44:27 +08:00
grant_types: [GrantType.AuthorizationCode, GrantType.RefreshToken, GrantType.OrganizationToken],
2022-09-21 13:06:56 +08:00
token_endpoint_auth_method: 'none',
});
2023-02-09 18:31:14 +08:00
expect(getConstantClientMetadata(mockEnvSet, ApplicationType.Traditional)).toEqual({
2022-09-21 13:06:56 +08:00
application_type: 'web',
2023-11-08 15:44:27 +08:00
grant_types: [GrantType.AuthorizationCode, GrantType.RefreshToken, GrantType.OrganizationToken],
2022-09-21 13:06:56 +08:00
token_endpoint_auth_method: 'client_secret_basic',
});
2023-02-09 18:31:14 +08:00
expect(getConstantClientMetadata(mockEnvSet, ApplicationType.MachineToMachine)).toEqual({
2022-09-21 13:06:56 +08:00
application_type: 'web',
grant_types: [GrantType.ClientCredentials],
token_endpoint_auth_method: 'client_secret_basic',
response_types: [],
});
});
2022-09-21 13:06:56 +08:00
describe('buildOidcClientMetadata()', () => {
const metadata = {
redirectUris: ['logto.dev'],
postLogoutRedirectUris: ['logto.dev'],
logoUri: 'logto.pnf',
};
expect(buildOidcClientMetadata()).toEqual({ redirectUris: [], postLogoutRedirectUris: [] });
expect(buildOidcClientMetadata(metadata)).toEqual(metadata);
});
describe('validateMetadata', () => {
describe('corsAllowedOrigins', () => {
it('should not throw when corsAllowedOrigins is empty', () => {
expect(() => {
validateCustomClientMetadata('corsAllowedOrigins', []);
}).not.toThrow();
});
it('should not throw when corsAllowedOrigins are all valid', () => {
expect(() => {
validateCustomClientMetadata('corsAllowedOrigins', [
'http://localhost:3001',
'https://logto.dev',
]);
}).not.toThrow();
});
it('should throw when corsAllowedOrigins are not all valid', () => {
expect(() => {
validateCustomClientMetadata('corsAllowedOrigins', ['', 'logto.dev']);
}).toThrow();
});
});
describe.each(['idTokenTtl', 'refreshTokenTtl'])('%s', (ttlKey) => {
test(`${ttlKey} should not throw when it is a number`, () => {
expect(() => {
validateCustomClientMetadata(ttlKey, 5000);
}).not.toThrow();
});
test(`${ttlKey} should throw when it is not a number`, () => {
expect(() => {
validateCustomClientMetadata(ttlKey, 'string_value');
}).toThrow();
});
});
});
describe('isOriginAllowed', () => {
it('should return false if there is no corsAllowOrigins', () => {
expect(isOriginAllowed('https://logto.dev', {})).toBeFalsy();
});
it('should return false if corsAllowOrigins is empty', () => {
expect(
isOriginAllowed('https://logto.dev', { [CustomClientMetadataKey.CorsAllowedOrigins]: [] })
).toBeFalsy();
});
it('should return false if corsAllowOrigins do not include the origin', () => {
expect(
isOriginAllowed('http://localhost:3001', {
[CustomClientMetadataKey.CorsAllowedOrigins]: ['https://logto.dev'],
})
).toBeFalsy();
});
it('should return true if corsAllowOrigins include the origin', () => {
expect(
isOriginAllowed('https://logto.dev', {
[CustomClientMetadataKey.CorsAllowedOrigins]: ['https://logto.dev'],
})
).toBeTruthy();
});
it('should return true if redirectUris include the origin', () => {
expect(
isOriginAllowed(
'https://logto.dev',
{
[CustomClientMetadataKey.CorsAllowedOrigins]: [],
},
['https://logto.dev/callback']
)
).toBeTruthy();
});
});