mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
test(oidc): add oidc adapter test case (#266)
add oidc adapter test case
This commit is contained in:
parent
4a80773b2d
commit
279318780e
4 changed files with 145 additions and 0 deletions
110
packages/core/src/oidc/adapter.test.ts
Normal file
110
packages/core/src/oidc/adapter.test.ts
Normal file
|
@ -0,0 +1,110 @@
|
|||
import { Application } from '@logto/schemas';
|
||||
import snakecaseKeys from 'snakecase-keys';
|
||||
|
||||
import {
|
||||
consumeInstanceById,
|
||||
destoryInstanceById,
|
||||
findPayloadById,
|
||||
findPayloadByPayloadField,
|
||||
revokeInstanceByGrantId,
|
||||
upsertInstance,
|
||||
} from '@/queries/oidc-model-instance';
|
||||
import { mockApplication } from '@/utils/mock';
|
||||
|
||||
import postgresAdapter from './adapter';
|
||||
import { getApplicationTypeString } from './utils';
|
||||
|
||||
jest.mock('@/queries/application', () => ({
|
||||
findApplicationById: jest.fn(async (): Promise<Application> => mockApplication),
|
||||
}));
|
||||
|
||||
jest.mock('@/queries/oidc-model-instance', () => ({
|
||||
upsertInstance: jest.fn(),
|
||||
findPayloadById: jest.fn(),
|
||||
findPayloadByPayloadField: jest.fn(),
|
||||
consumeInstanceById: jest.fn(),
|
||||
destoryInstanceById: jest.fn(),
|
||||
revokeInstanceByGrantId: jest.fn(),
|
||||
}));
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
jest.mock(
|
||||
'dayjs',
|
||||
// eslint-disable-next-line unicorn/consistent-function-scoping
|
||||
jest.fn(() => () => ({
|
||||
add: jest.fn((delta: number) => new Date(now + delta * 1000)),
|
||||
}))
|
||||
);
|
||||
|
||||
describe('postgres Adapter', () => {
|
||||
it('Client Modal', async () => {
|
||||
const rejectError = new Error('Not implemented');
|
||||
const adapter = postgresAdapter('Client');
|
||||
|
||||
await expect(adapter.upsert('client', {}, 0)).rejects.toMatchError(rejectError);
|
||||
await expect(adapter.findByUserCode('foo')).rejects.toMatchError(rejectError);
|
||||
await expect(adapter.findByUid('foo')).rejects.toMatchError(rejectError);
|
||||
await expect(adapter.consume('foo')).rejects.toMatchError(rejectError);
|
||||
await expect(adapter.destroy('foo')).rejects.toMatchError(rejectError);
|
||||
await expect(adapter.revokeByGrantId('foo')).rejects.toMatchError(rejectError);
|
||||
|
||||
const application = await adapter.find('foo');
|
||||
|
||||
const {
|
||||
id: client_id,
|
||||
name: client_name,
|
||||
type,
|
||||
oidcClientMetadata,
|
||||
customClientMetadata,
|
||||
} = mockApplication;
|
||||
|
||||
expect(application).toEqual({
|
||||
client_id,
|
||||
client_name,
|
||||
application_type: getApplicationTypeString(type),
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
token_endpoint_auth_method: 'none',
|
||||
...snakecaseKeys(oidcClientMetadata),
|
||||
...customClientMetadata,
|
||||
});
|
||||
});
|
||||
|
||||
it('Access Token Model', async () => {
|
||||
const modelName = 'Access Token';
|
||||
const uid = 'fooUser';
|
||||
const userCode = 'fooCode';
|
||||
const id = 'fooId';
|
||||
const grantId = 'grandId';
|
||||
const expireAt = 60;
|
||||
const adapter = postgresAdapter(modelName);
|
||||
|
||||
await adapter.upsert(id, { uid, userCode }, expireAt);
|
||||
expect(upsertInstance).toBeCalledWith({
|
||||
modelName,
|
||||
id,
|
||||
payload: { uid, userCode },
|
||||
expiresAt: now + expireAt * 1000,
|
||||
});
|
||||
|
||||
await adapter.find(id);
|
||||
expect(findPayloadById).toBeCalledWith(modelName, id);
|
||||
|
||||
await adapter.findByUserCode(userCode);
|
||||
expect(findPayloadByPayloadField).toBeCalledWith(modelName, 'userCode', userCode);
|
||||
|
||||
jest.clearAllMocks();
|
||||
|
||||
await adapter.findByUid(uid);
|
||||
expect(findPayloadByPayloadField).toBeCalledWith(modelName, 'uid', uid);
|
||||
|
||||
await adapter.consume(id);
|
||||
expect(consumeInstanceById).toBeCalledWith(modelName, id);
|
||||
|
||||
await adapter.destroy(id);
|
||||
expect(destoryInstanceById).toBeCalledWith(modelName, id);
|
||||
|
||||
await adapter.revokeByGrantId(grantId);
|
||||
expect(revokeInstanceByGrantId).toBeCalledWith(modelName, grantId);
|
||||
});
|
||||
});
|
12
packages/core/src/oidc/init.test.ts
Normal file
12
packages/core/src/oidc/init.test.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import Koa from 'koa';
|
||||
|
||||
import initOidc from './init';
|
||||
|
||||
describe('oidc provider init', () => {
|
||||
it('init should not throw', async () => {
|
||||
const app = new Koa();
|
||||
await expect(initOidc(app)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
// TODO:
|
||||
});
|
|
@ -1,3 +1,5 @@
|
|||
/* istanbul ignore file */
|
||||
|
||||
import { customClientMetadataGuard, CustomClientMetadataType } from '@logto/schemas';
|
||||
import { fromKeyLike } from 'jose/jwk/from_key_like';
|
||||
import Koa from 'koa';
|
||||
|
|
21
packages/core/src/oidc/utils.test.ts
Normal file
21
packages/core/src/oidc/utils.test.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { ApplicationType } from '@logto/schemas';
|
||||
|
||||
import { getApplicationTypeString, buildOidcClientMetadata } from './utils';
|
||||
|
||||
describe('oidc utils method', () => {
|
||||
it('getApplicationTypeString', () => {
|
||||
expect(getApplicationTypeString(ApplicationType.SPA)).toEqual('web');
|
||||
expect(getApplicationTypeString(ApplicationType.Native)).toEqual('native');
|
||||
expect(getApplicationTypeString(ApplicationType.Traditional)).toEqual('web');
|
||||
});
|
||||
|
||||
it('buildOidcClientMetadata', () => {
|
||||
const metadata = {
|
||||
redirectUris: ['logto.dev'],
|
||||
postLogoutRedirectUris: ['logto.dev'],
|
||||
logoUri: 'logto.pnf',
|
||||
};
|
||||
expect(buildOidcClientMetadata()).toEqual({ redirectUris: [], postLogoutRedirectUris: [] });
|
||||
expect(buildOidcClientMetadata(metadata)).toEqual(metadata);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue