482 lines
17 KiB
JavaScript
Raw Normal View History

2023-03-14 11:10:55 +01:00
'use strict';
const _ = require('lodash/fp');
2023-03-14 11:10:55 +01:00
const fse = require('fs-extra');
const SwaggerParser = require('@apidevtools/swagger-parser');
const { api, plugins, components, contentTypes } = require('../__mocks__/mock-strapi-data');
const documentation = require('../documentation');
const override = require('../override');
2023-03-14 11:10:55 +01:00
const defaultConfig = require('../../config/default-plugin-config');
const mockStrapiInstance = {
dirs: {
app: {
api: './',
extensions: './',
},
},
contentTypes,
components,
api,
plugins,
config: {
get: () => defaultConfig,
2023-03-14 11:10:55 +01:00
},
log: {
info: jest.fn(),
warn: jest.fn(),
},
2023-03-14 11:10:55 +01:00
};
jest.mock('fs-extra', () => ({
...jest.requireActual('fs-extra'),
writeJson: jest.fn(),
ensureFile: jest.fn(),
}));
describe('Documentation service', () => {
beforeAll(() => {
global.strapi = mockStrapiInstance;
global.strapi.contentType = jest.fn((uid) => {
// Only deal with mocked data, return empty attributes for unmocked relations
if (!global.strapi.contentTypes[uid]) return { attributes: {} };
return global.strapi.contentTypes[uid];
});
global.strapi.plugin = jest.fn((name) => global.strapi.plugins[name]);
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: override({ strapi: global.strapi }),
};
return mockServices[name];
}),
};
2023-03-14 11:10:55 +01:00
});
afterAll(() => {
// Teardown the mocked strapi instance
global.strapi = {};
});
afterEach(() => {
// Reset the mocked strapi config
global.strapi.config.get = () => defaultConfig;
});
2023-03-14 11:10:55 +01:00
it('generates a valid openapi schema', async () => {
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
// The final documenation is read only, clone deep for this test
const validatePromise = SwaggerParser.validate(_.cloneDeep(mockFinalDoc));
2023-03-14 11:10:55 +01:00
await expect(validatePromise).resolves.not.toThrow();
});
it('generates the correct response component schema for a single type', async () => {
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
const expected = {
description: 'OK',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/HomepageResponse',
},
},
},
};
expect(mockFinalDoc.paths['/homepage'].get.responses['200']).toEqual(expected);
expect(mockFinalDoc.paths['/homepage'].put.responses['200']).toEqual(expected);
expect(mockFinalDoc.paths['/homepage'].post.responses['200']).toEqual(expected);
});
it('generates the correct response component schema for a collection type', async () => {
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
const expectedList = {
description: 'OK',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/KitchensinkListResponse',
},
},
},
};
const expectedOne = {
description: 'OK',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/KitchensinkResponse',
},
},
},
};
expect(mockFinalDoc.paths['/kitchensinks'].get.responses['200']).toEqual(expectedList);
expect(mockFinalDoc.paths['/kitchensinks'].post.responses['200']).toEqual(expectedOne);
expect(mockFinalDoc.paths['/kitchensinks/{id}'].get.responses['200']).toEqual(expectedOne);
expect(mockFinalDoc.paths['/kitchensinks/{id}'].put.responses['200']).toEqual(expectedOne);
});
describe('Determines the plugins that need documentation', () => {
it('generates documentation for the default plugins if the user provided nothing in the config', async () => {
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
2023-03-15 11:01:09 +01:00
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual(['upload', 'users-permissions']);
});
it("generates documentation only for plugins in the user's config", async () => {
global.strapi.config.get = () => ({
...defaultConfig,
2023-03-14 18:11:26 +01:00
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: ['upload'] },
});
2023-03-15 11:01:09 +01:00
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
2023-03-14 18:11:26 +01:00
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual(['upload']);
});
it('does not generate documentation for any plugins', async () => {
global.strapi.config.get = () => ({
...defaultConfig,
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: [] },
});
2023-03-15 11:01:09 +01:00
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual([]);
});
});
2023-03-15 11:01:09 +01:00
describe('Handles user config and overrides', () => {
it('replaces default config with the user config', async () => {
const userConfig = {
info: {
version: '4.0.0',
title: 'custom-documentation',
description: 'custom description',
termsOfService: 'custom terms of service',
contact: {
name: 'custom-team',
email: 'custom-contact-email@something.io',
url: 'custom-mywebsite.io',
},
license: {
name: 'custom Apache 2.0',
url: 'custom https://www.apache.org/licenses/LICENSE-2.0.html',
},
},
'x-strapi-config': {
path: 'custom-documentation',
plugins: [],
},
2023-03-20 15:49:30 +01:00
servers: [{ server: 'custom-server' }],
2023-03-15 11:01:09 +01:00
externalDocs: {
description: 'custom Find out more',
url: 'custom-doc-url',
},
webhooks: {
test: {},
},
security: [
{
bearerAuth: ['custom'],
},
],
};
2023-03-22 11:30:48 +01:00
global.strapi.config.get = () => ({ ...userConfig });
2023-03-15 11:01:09 +01:00
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
2023-03-22 11:30:48 +01:00
// The generation data is dynamically added, it cannot be modified by the user
const { 'x-generation-date': generationConfig, ...mockFinalDocInfo } = mockFinalDoc.info;
expect(mockFinalDocInfo).toEqual(userConfig.info);
2023-03-15 11:01:09 +01:00
expect(mockFinalDoc['x-strapi-config']).toEqual(userConfig['x-strapi-config']);
expect(mockFinalDoc.externalDocs).toEqual(userConfig.externalDocs);
expect(mockFinalDoc.security).toEqual(userConfig.security);
expect(mockFinalDoc.webhooks).toEqual(userConfig.webhooks);
2023-03-20 15:49:30 +01:00
expect(mockFinalDoc.servers).toEqual(userConfig.servers);
2023-03-15 11:01:09 +01:00
});
it("does not apply an override if the plugin providing the override isn't specified in the x-strapi-config.plugins", async () => {
global.strapi.config.get = () => ({
...defaultConfig,
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: [] },
});
const docService = documentation({ strapi: global.strapi });
2023-03-20 15:49:30 +01:00
const overrideService = override({ strapi: global.strapi });
2023-03-20 15:49:30 +01:00
overrideService.registerOverride(
{
2023-03-15 09:42:51 +01:00
paths: {
'/test': {
get: {
tags: ['Users-Permissions - Users & Roles'],
summary: 'Get list of users',
responses: {},
},
},
},
},
2023-03-20 15:49:30 +01:00
{ pluginOrigin: 'users-permissions' }
);
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.paths['/test']).toBeUndefined();
});
2023-03-15 11:01:09 +01:00
it('overrides (extends) Tags', async () => {
2023-03-20 15:49:30 +01:00
const overrideService = override({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
// Simulate override from users-permissions plugin
2023-03-20 15:49:30 +01:00
overrideService.registerOverride(
2023-03-15 11:01:09 +01:00
{
tags: ['users-permissions-tag'],
},
2023-03-20 15:49:30 +01:00
{ pluginOrigin: 'users-permissions' }
2023-03-15 11:01:09 +01:00
);
// Simulate override from upload plugin
2023-03-20 15:49:30 +01:00
overrideService.registerOverride(
2023-03-15 11:01:09 +01:00
{
tags: ['upload-tag'],
},
2023-03-20 15:49:30 +01:00
{ pluginOrigin: 'upload' }
2023-03-15 11:01:09 +01:00
);
2023-03-20 15:49:30 +01:00
// Use the override service in the documentation service
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: overrideService,
};
return mockServices[name];
}),
};
const docService = documentation({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.tags).toEqual(['users-permissions-tag', 'upload-tag']);
});
it('overrides (replaces existing or adds new) Paths', async () => {
2023-03-20 15:49:30 +01:00
const overrideService = override({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
// Simulate override from upload plugin
2023-03-20 15:49:30 +01:00
overrideService.registerOverride(
2023-03-15 11:01:09 +01:00
{
paths: {
// This path exists after generating with mock data, replace it
'/upload/files': {
get: {
responses: ['existing-path-test'],
},
},
// This path does not exist after generating with mock data, add it
'/upload/new-path': {
get: {
responses: ['new-path-test'],
},
},
},
},
2023-03-20 15:49:30 +01:00
{ pluginOrigin: 'upload' }
2023-03-15 11:01:09 +01:00
);
2023-03-20 15:49:30 +01:00
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: overrideService,
};
return mockServices[name];
}),
};
const docService = documentation({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.paths['/upload/files'].get.responses).toEqual(['existing-path-test']);
expect(Object.keys(mockFinalDoc.paths['/upload/files'].get)).toEqual(['responses']);
expect(mockFinalDoc.paths['/upload/new-path'].get.responses).toEqual(['new-path-test']);
});
it('overrides (replaces existing or adds new) Components', async () => {
2023-03-20 15:49:30 +01:00
const overrideService = override({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
// Simulate override from upload plugin
2023-03-20 15:49:30 +01:00
overrideService.registerOverride(
2023-03-15 11:01:09 +01:00
{
components: {
schemas: {
// This component schema exists after generating with mock data, replace it
UploadFileResponse: {
properties: {
data: { $ref: 'test-existing-component' },
meta: { type: 'object' },
},
},
// This component schema does not exist after generating with mock data, add it
UploadFileMockCompo: {
properties: {
data: { $ref: 'test-new-component' },
meta: { type: 'object' },
},
},
},
},
},
2023-03-20 15:49:30 +01:00
{ pluginOrigin: 'upload' }
2023-03-15 11:01:09 +01:00
);
2023-03-20 15:49:30 +01:00
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: overrideService,
};
return mockServices[name];
}),
};
const docService = documentation({ strapi: global.strapi });
2023-03-15 11:01:09 +01:00
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.components.schemas.UploadFileResponse.properties.data.$ref).toEqual(
'test-existing-component'
);
expect(mockFinalDoc.components.schemas.UploadFileMockCompo.properties.data.$ref).toEqual(
'test-new-component'
);
});
2023-03-22 11:30:48 +01:00
it('overrides only the specified version', async () => {
const overrideService = override({ strapi: global.strapi });
// Simulate override from upload plugin
overrideService.registerOverride(
{
// Only override for version 1.0.0
info: { version: '1.0.0' },
components: {
schemas: {
// This component schema exists after generating with mock data, replace it
ShouldNotBeAdded: {},
},
},
},
{ pluginOrigin: 'upload' }
);
// Simulate override from upload plugin
overrideService.registerOverride(
{
// Only override for version 2.0.0
info: { version: '2.0.0' },
components: {
schemas: {
// This component schema exists after generating with mock data, replace it
ShouldBeAdded: {},
},
},
},
{ pluginOrigin: 'upload' }
);
// Simulate override from upload plugin
overrideService.registerOverride(
{
components: {
schemas: {
// This component schema exists after generating with mock data, replace it
ShouldAlsoBeAdded: {},
},
},
},
{ pluginOrigin: 'upload' }
);
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: overrideService,
};
return mockServices[name];
}),
};
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc('2.0.0');
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.components.schemas.ShouldNotBeAdded).toBeUndefined();
expect(mockFinalDoc.components.schemas.ShouldBeAdded).toBeDefined();
expect(mockFinalDoc.components.schemas.ShouldAlsoBeAdded).toBeDefined();
});
it('excludes apis and plugins from generation', async () => {
const overrideService = override({ strapi: global.strapi });
overrideService.excludeFromGeneration('kitchensink');
global.strapi.plugins.documentation = {
service: jest.fn((name) => {
const mockServices = {
override: overrideService,
};
return mockServices[name];
}),
};
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(
Object.keys(mockFinalDoc.paths).find((path) => path.includes('kitchensink'))
).toBeUndefined();
expect(
Object.keys(mockFinalDoc.components.schemas).find((compo) => compo.includes('Kitchensink'))
).toBeUndefined();
});
2023-03-22 11:30:48 +01:00
it("applies a user's mutateDocumentation function", async () => {
global.strapi.config.get = () => ({
...defaultConfig,
'x-strapi-config': {
...defaultConfig['x-strapi-config'],
2023-03-22 11:30:48 +01:00
mutateDocumentation(draft) {
draft.paths['/kitchensinks'] = { get: { responses: { 200: { description: 'test' } } } };
},
},
});
const docService = documentation({ strapi: global.strapi });
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.paths['/kitchensinks']).toEqual({
get: { responses: { 200: { description: 'test' } } },
});
});
});
2023-03-14 11:10:55 +01:00
});