2022-10-21 00:14:17 -05:00
|
|
|
import type { Resource, CreateResource } from '@logto/schemas';
|
2022-12-12 00:43:23 -05:00
|
|
|
import { mockEsm, pickDefault } from '@logto/shared/esm';
|
2022-02-21 03:42:48 -05:00
|
|
|
|
2022-11-21 03:38:24 -05:00
|
|
|
import { mockResource } from '#src/__mocks__/index.js';
|
|
|
|
import { createRequester } from '#src/utils/test-utils.js';
|
2022-02-21 03:42:48 -05:00
|
|
|
|
2022-12-12 00:43:23 -05:00
|
|
|
const { jest } = import.meta;
|
2022-02-21 03:42:48 -05:00
|
|
|
|
2022-12-12 00:43:23 -05:00
|
|
|
mockEsm('#src/queries/resource.js', () => ({
|
|
|
|
findTotalNumberOfResources: async () => ({ count: 10 }),
|
|
|
|
findAllResources: async (): Promise<Resource[]> => [mockResource],
|
|
|
|
findResourceById: async (): Promise<Resource> => mockResource,
|
|
|
|
insertResource: async (body: CreateResource): Promise<Resource> => ({
|
|
|
|
...mockResource,
|
|
|
|
...body,
|
|
|
|
}),
|
|
|
|
updateResourceById: async (_: unknown, data: Partial<CreateResource>): Promise<Resource> => ({
|
|
|
|
...mockResource,
|
|
|
|
...data,
|
|
|
|
}),
|
2022-02-21 03:42:48 -05:00
|
|
|
deleteResourceById: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-19 21:25:47 -05:00
|
|
|
// Cannot use `mockEsm()` here, pending investigation.
|
|
|
|
jest.unstable_mockModule('@logto/core-kit', () => ({
|
2022-02-21 03:42:48 -05:00
|
|
|
// eslint-disable-next-line unicorn/consistent-function-scoping
|
2022-12-12 00:43:23 -05:00
|
|
|
buildIdGenerator: () => () => 'randomId',
|
2022-02-21 03:42:48 -05:00
|
|
|
}));
|
|
|
|
|
2022-12-12 00:43:23 -05:00
|
|
|
const resourceRoutes = await pickDefault(import('./resource.js'));
|
|
|
|
|
2022-02-21 03:42:48 -05:00
|
|
|
describe('resource routes', () => {
|
2022-02-22 22:09:26 -05:00
|
|
|
const resourceRequest = createRequester({ authedRoutes: resourceRoutes });
|
2022-02-21 03:42:48 -05:00
|
|
|
|
|
|
|
it('GET /resources', async () => {
|
|
|
|
const response = await resourceRequest.get('/resources');
|
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
expect(response.body).toEqual([mockResource]);
|
|
|
|
expect(response.header).toHaveProperty('total-number', '10');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /resources', async () => {
|
|
|
|
const name = 'user api';
|
|
|
|
const indicator = 'logto.dev/user';
|
|
|
|
const accessTokenTtl = 60;
|
|
|
|
|
|
|
|
const response = await resourceRequest
|
|
|
|
.post('/resources')
|
|
|
|
.send({ name, indicator, accessTokenTtl });
|
|
|
|
|
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
id: 'randomId',
|
|
|
|
name,
|
|
|
|
indicator,
|
|
|
|
accessTokenTtl,
|
|
|
|
scopes: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /resources should throw with invalid input body', async () => {
|
|
|
|
const name = 'user api';
|
|
|
|
const indicator = 'logto.dev/user';
|
|
|
|
|
|
|
|
await expect(resourceRequest.post('/resources')).resolves.toHaveProperty('status', 400);
|
|
|
|
await expect(resourceRequest.post('/resources').send({ name })).resolves.toHaveProperty(
|
|
|
|
'status',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
await expect(resourceRequest.post('/resources').send({ indicator })).resolves.toHaveProperty(
|
|
|
|
'status',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('GET /resources/:id', async () => {
|
|
|
|
const response = await resourceRequest.get('/resources/foo');
|
|
|
|
|
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
...mockResource,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('PATCH /resources/:id', async () => {
|
|
|
|
const name = 'user api';
|
|
|
|
const indicator = 'logto.dev/user';
|
|
|
|
const accessTokenTtl = 60;
|
|
|
|
|
|
|
|
const response = await resourceRequest
|
|
|
|
.patch('/resources/foo')
|
|
|
|
.send({ name, indicator, accessTokenTtl });
|
|
|
|
|
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
...mockResource,
|
|
|
|
name,
|
|
|
|
indicator,
|
|
|
|
accessTokenTtl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('PATCH /resources/:id should throw with invalid propreties', async () => {
|
|
|
|
const response = await resourceRequest.patch('/resources/foo').send({ indicator: 12 });
|
|
|
|
expect(response.status).toEqual(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /resources/:id', async () => {
|
|
|
|
await expect(resourceRequest.delete('/resources/foo')).resolves.toHaveProperty('status', 204);
|
|
|
|
});
|
|
|
|
});
|