strapi/packages/plugins/documentation/server/services/__tests__/build-component-schema.test.js

762 lines
19 KiB
JavaScript
Raw Normal View History

2023-03-09 11:38:01 +05:30
'use strict';
const _ = require('lodash/fp');
2023-03-09 11:38:01 +05:30
const buildComponentSchema = require('../helpers/build-component-schema');
2023-03-14 11:10:55 +01:00
const strapi = {
plugins: {
'users-permissions': {
contentTypes: {
role: {
attributes: {
2023-04-17 14:11:03 +02:00
test: {
2023-03-14 11:10:55 +01:00
type: 'string',
},
},
},
},
routes: {
'content-api': {
routes: [],
},
},
},
},
api: {
restaurant: {
contentTypes: {
restaurant: {
attributes: {
2023-04-17 14:11:03 +02:00
test: {
2023-03-14 11:10:55 +01:00
type: 'string',
},
},
},
},
routes: {
restaurant: { routes: [] },
},
},
},
contentType: () => ({ info: {}, attributes: { test: { type: 'string' } } }),
};
2023-03-09 11:38:01 +05:30
2023-04-21 16:53:49 +02:00
describe('Documentation plugin | Build component schema', () => {
2023-03-09 11:38:01 +05:30
beforeEach(() => {
// Reset the mocked strapi instance
global.strapi = _.cloneDeep(strapi);
2023-03-14 11:10:55 +01:00
});
afterAll(() => {
// Teardown the mocked strapi instance
global.strapi = {};
2023-03-09 11:38:01 +05:30
});
it('builds the Response schema', () => {
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
let schemas = {};
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const expectedSchemas = {
2023-04-17 14:11:03 +02:00
UsersPermissionsRole: {
type: 'object',
properties: {
test: { type: 'string' },
},
},
2023-03-09 11:38:01 +05:30
UsersPermissionsRoleResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/UsersPermissionsRole',
2023-03-09 11:38:01 +05:30
},
},
},
UsersPermissionsRoleResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
$ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
},
meta: {
type: 'object',
},
},
},
2023-04-17 14:11:03 +02:00
Restaurant: {
type: 'object',
properties: {
test: { type: 'string' },
},
},
2023-03-09 11:38:01 +05:30
RestaurantResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/Restaurant',
2023-03-09 11:38:01 +05:30
},
},
},
RestaurantResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
$ref: '#/components/schemas/RestaurantResponseDataObject',
},
meta: {
type: 'object',
},
},
},
};
expect(schemas).toStrictEqual(expectedSchemas);
});
it('builds the ResponseList schema', () => {
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'GET', path: '/test', handler: 'test.find' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'GET', path: '/test', handler: 'test.find' },
];
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
let schemas = {};
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const expectedSchemas = {
2023-04-17 14:11:03 +02:00
UsersPermissionsRole: {
type: 'object',
properties: {
test: { type: 'string' },
},
},
2023-03-09 11:38:01 +05:30
UsersPermissionsRoleListResponseDataItem: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/UsersPermissionsRole',
2023-03-09 11:38:01 +05:30
},
},
},
UsersPermissionsRoleListResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItem',
},
},
meta: {
type: 'object',
properties: {
pagination: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
UsersPermissionsRoleResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/UsersPermissionsRole',
2023-03-09 11:38:01 +05:30
},
},
},
UsersPermissionsRoleResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
$ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
},
meta: {
type: 'object',
},
},
},
RestaurantListResponseDataItem: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/Restaurant',
2023-03-09 11:38:01 +05:30
},
},
},
RestaurantListResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/RestaurantListResponseDataItem',
},
},
meta: {
type: 'object',
properties: {
pagination: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
RestaurantResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
2023-04-17 14:11:03 +02:00
$ref: '#/components/schemas/Restaurant',
2023-03-09 11:38:01 +05:30
},
},
},
RestaurantResponse: {
2023-04-17 14:11:03 +02:00
type: 'object',
2023-03-09 11:38:01 +05:30
properties: {
data: {
$ref: '#/components/schemas/RestaurantResponseDataObject',
},
meta: {
type: 'object',
},
},
},
2023-04-17 14:11:03 +02:00
Restaurant: {
type: 'object',
properties: {
test: { type: 'string' },
},
},
2023-03-09 11:38:01 +05:30
};
expect(schemas).toStrictEqual(expectedSchemas);
});
it('builds the Request schema', () => {
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'POST', path: '/test', handler: 'test.create' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'POST', path: '/test', handler: 'test.create' },
];
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
let schemas = {};
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
// Just get the request objects
const requestObjectsSchemas = Object.entries(schemas).reduce((acc, curr) => {
const [key, val] = curr;
if (key.endsWith('Request')) {
acc[key] = val;
}
return acc;
}, {});
const expectedSchemas = {
UsersPermissionsRoleRequest: {
type: 'object',
required: ['data'],
properties: {
data: {
type: 'object',
properties: {
test: {
type: 'string',
},
},
},
},
},
RestaurantRequest: {
type: 'object',
required: ['data'],
properties: {
data: {
type: 'object',
properties: {
test: {
type: 'string',
},
},
},
},
},
};
expect(requestObjectsSchemas).toStrictEqual(expectedSchemas);
});
it('builds the LocalizationResponse schema', () => {
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'GET', path: '/localizations', handler: 'test' },
2023-04-17 14:11:03 +02:00
{ method: 'GET', path: '/test', handler: 'test.find' },
2023-03-09 11:38:01 +05:30
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'GET', path: '/localizations', handler: 'test' },
2023-04-17 14:11:03 +02:00
{ method: 'GET', path: '/test', handler: 'test.find' },
2023-03-09 11:38:01 +05:30
];
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
let schemas = {};
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
2023-04-17 14:11:03 +02:00
const expectedSchemas = {
UsersPermissionsRole: {
type: 'object',
properties: {
test: { type: 'string' },
},
},
UsersPermissionsRoleListResponseDataItem: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/UsersPermissionsRole',
},
},
},
UsersPermissionsRoleListResponseDataItemLocalized: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/UsersPermissionsRole',
},
},
},
UsersPermissionsRoleListResponse: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItem',
},
},
meta: {
type: 'object',
properties: {
pagination: {
type: 'object',
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
UsersPermissionsRoleLocalizationListResponse: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItemLocalized',
},
},
meta: {
type: 'object',
properties: {
pagination: {
type: 'object',
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
UsersPermissionsRoleResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/UsersPermissionsRole',
},
},
},
UsersPermissionsRoleResponseDataObjectLocalized: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/UsersPermissionsRole',
},
},
},
UsersPermissionsRoleResponse: {
type: 'object',
properties: {
data: {
$ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
},
meta: {
type: 'object',
},
},
},
UsersPermissionsRoleLocalizationResponse: {
type: 'object',
properties: {
data: {
$ref: '#/components/schemas/UsersPermissionsRoleResponseDataObjectLocalized',
},
meta: {
type: 'object',
},
},
},
RestaurantListResponseDataItem: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/Restaurant',
},
},
},
RestaurantListResponseDataItemLocalized: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/Restaurant',
},
},
},
RestaurantListResponse: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/RestaurantListResponseDataItem',
},
},
meta: {
type: 'object',
properties: {
pagination: {
type: 'object',
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
RestaurantLocalizationListResponse: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
$ref: '#/components/schemas/RestaurantListResponseDataItemLocalized',
},
},
meta: {
type: 'object',
properties: {
pagination: {
type: 'object',
properties: {
page: {
type: 'integer',
},
pageSize: {
type: 'integer',
minimum: 25,
},
pageCount: {
type: 'integer',
maximum: 1,
},
total: {
type: 'integer',
},
},
},
},
},
},
},
RestaurantResponseDataObject: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/Restaurant',
},
},
},
RestaurantResponseDataObjectLocalized: {
type: 'object',
properties: {
id: {
type: 'number',
},
attributes: {
$ref: '#/components/schemas/Restaurant',
},
},
},
RestaurantLocalizationResponse: {
type: 'object',
properties: {
data: {
$ref: '#/components/schemas/RestaurantResponseDataObjectLocalized',
},
meta: {
type: 'object',
},
},
},
RestaurantResponse: {
type: 'object',
properties: {
data: {
$ref: '#/components/schemas/RestaurantResponseDataObject',
},
meta: {
type: 'object',
},
},
},
Restaurant: {
type: 'object',
properties: {
test: { type: 'string' },
},
2023-03-09 11:38:01 +05:30
},
};
2023-04-17 14:11:03 +02:00
expect(schemas).toStrictEqual(expectedSchemas);
2023-03-09 11:38:01 +05:30
});
it('builds the LocalizationRequest schema', () => {
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'POST', path: '/localizations', handler: 'test' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'POST', path: '/localizations', handler: 'test' },
];
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
let schemas = {};
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const schemaNames = Object.keys(schemas);
const pluginListResponseLocalizationRequest = schemas.UsersPermissionsRoleLocalizationRequest;
const apiListResponseLocalizationRequest = schemas.RestaurantLocalizationRequest;
const expectedShape = {
type: 'object',
required: ['locale'],
properties: { test: { type: 'string' } },
};
expect(schemaNames.includes('UsersPermissionsRoleLocalizationRequest')).toBe(true);
expect(schemaNames.includes('RestaurantLocalizationRequest')).toBe(true);
expect(pluginListResponseLocalizationRequest).toStrictEqual(expectedShape);
expect(apiListResponseLocalizationRequest).toStrictEqual(expectedShape);
});
it('creates the correct name given multiple content types', () => {
const apiMock = {
name: 'users-permissions',
getter: 'plugin',
ctNames: ['permission', 'role', 'user'],
};
const schemas = buildComponentSchema(apiMock);
const schemaNames = Object.keys(schemas);
expect(schemaNames).toStrictEqual([
2023-04-17 14:11:03 +02:00
'UsersPermissionsPermission',
2023-03-09 11:38:01 +05:30
'UsersPermissionsPermissionResponseDataObject',
'UsersPermissionsPermissionResponse',
2023-04-17 14:11:03 +02:00
'UsersPermissionsRole',
2023-03-09 11:38:01 +05:30
'UsersPermissionsRoleResponseDataObject',
'UsersPermissionsRoleResponse',
2023-04-17 14:11:03 +02:00
'UsersPermissionsUser',
2023-03-09 11:38:01 +05:30
'UsersPermissionsUserResponseDataObject',
'UsersPermissionsUserResponse',
]);
});
});