0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00
logto/packages/core/src/oidc/adapter.test.ts
simeng-li fb6a1dc236
test(core): add ut for queires (#287)
* test(core): add ut for queires

add ut for queries

* test(core): add user query ut

add user query ut

* fix(core): remove test code

remove console log
2022-02-28 14:30:27 +08:00

110 lines
3.3 KiB
TypeScript

import { Application } from '@logto/schemas';
import snakecaseKeys from 'snakecase-keys';
import {
consumeInstanceById,
destroyInstanceById,
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(),
destroyInstanceById: 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(destroyInstanceById).toBeCalledWith(modelName, id);
await adapter.revokeByGrantId(grantId);
expect(revokeInstanceByGrantId).toBeCalledWith(modelName, grantId);
});
});