From e1ccf4b43e95eb03a6048a3f572a2e31681738ce Mon Sep 17 00:00:00 2001 From: Douglas McConnachie Date: Sat, 18 Apr 2020 11:38:49 +0100 Subject: [PATCH] refactor: apply feedback apply server route in test, relocate test RE: integration Signed-off-by: Douglas McConnachie --- .../integration/rest.server.integration.ts | 160 ++++++++++++++- .../rest.server.open-api-spec.unit.ts | 192 +----------------- 2 files changed, 163 insertions(+), 189 deletions(-) diff --git a/packages/rest/src/__tests__/integration/rest.server.integration.ts b/packages/rest/src/__tests__/integration/rest.server.integration.ts index c6ae6da178ab..4c58b422fb6a 100644 --- a/packages/rest/src/__tests__/integration/rest.server.integration.ts +++ b/packages/rest/src/__tests__/integration/rest.server.integration.ts @@ -4,7 +4,11 @@ // License text available at https://opensource.org/licenses/MIT import {Application} from '@loopback/core'; -import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder'; +import { + aComponentsSpec, + anOpenApiSpec, + anOperationSpec, +} from '@loopback/openapi-spec-builder'; import { createClientForHandler, createRestAppClient, @@ -875,6 +879,160 @@ paths: await server.stop(); }); + it('disables consolidator if openApiSpec.consolidate option is set to false', async () => { + const options = {openApiSpec: {consolidate: false}}; + const server = await givenAServer({rest: options}); + + const EXPECTED_SPEC = anOpenApiSpec() + .withOperation( + 'get', + '/', + anOperationSpec().withResponse(200, { + description: 'Example', + content: { + 'application/json': { + schema: { + title: 'loopback.example', + properties: { + test: { + type: 'string', + }, + }, + }, + }, + }, + }), + ) + .build(); + + server.route('get', '/', EXPECTED_SPEC.paths['/'].get, () => {}); + + await server.start(); + const spec = await server.getApiSpec(); + expect(spec).to.eql(EXPECTED_SPEC); + await server.stop(); + }); + + it('runs consolidator if openApiSpec.consolidate option is set to true', async () => { + const options = {openApiSpec: {consolidate: true}}; + const server = await givenAServer({rest: options}); + + const EXPECTED_SPEC = anOpenApiSpec() + .withOperation( + 'get', + '/', + anOperationSpec().withResponse(200, { + description: 'Example', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/loopback.example', + }, + }, + }, + }), + ) + .withComponents( + aComponentsSpec().withSchema('loopback.example', { + title: 'loopback.example', + properties: { + test: { + type: 'string', + }, + }, + }), + ) + .build(); + + server.route( + 'get', + '/', + anOperationSpec() + .withResponse(200, { + description: 'Example', + content: { + 'application/json': { + schema: { + title: 'loopback.example', + properties: { + test: { + type: 'string', + }, + }, + }, + }, + }, + }) + .build(), + () => {}, + ); + + await server.start(); + const spec = await server.getApiSpec(); + expect(spec).to.eql(EXPECTED_SPEC); + await server.stop(); + }); + + it('runs consolidator if openApiSpec.consolidate option is undefined', async () => { + const options = {openApiSpec: {consolidate: undefined}}; + const server = await givenAServer({rest: options}); + + const EXPECTED_SPEC = anOpenApiSpec() + .withOperation( + 'get', + '/', + anOperationSpec().withResponse(200, { + description: 'Example', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/loopback.example', + }, + }, + }, + }), + ) + .withComponents( + aComponentsSpec().withSchema('loopback.example', { + title: 'loopback.example', + properties: { + test: { + type: 'string', + }, + }, + }), + ) + .build(); + + server.route( + 'get', + '/', + anOperationSpec() + .withResponse(200, { + description: 'Example', + content: { + 'application/json': { + schema: { + title: 'loopback.example', + properties: { + test: { + type: 'string', + }, + }, + }, + }, + }, + }) + .build(), + () => {}, + ); + + await server.start(); + const spec = await server.getApiSpec(); + expect(spec).to.eql(EXPECTED_SPEC); + await server.stop(); + }); + it('registers controller routes under routes.*', async () => { const server = await givenAServer(); server.controller(DummyController); diff --git a/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts b/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts index 1218a15aada7..3266d941d807 100644 --- a/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts +++ b/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts @@ -4,25 +4,16 @@ // License text available at https://opensource.org/licenses/MIT import {Application, createBindingFromClass} from '@loopback/core'; -import { - aComponentsSpec, - anOpenApiSpec, - anOperationSpec, -} from '@loopback/openapi-spec-builder'; -import {get, OpenApiSpec, post, requestBody} from '@loopback/openapi-v3'; +import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder'; +import {get, post, requestBody} from '@loopback/openapi-v3'; import {model, property} from '@loopback/repository'; -import { - expect, - givenHttpServerConfig, - validateApiSpec, -} from '@loopback/testlab'; +import {expect, validateApiSpec} from '@loopback/testlab'; import { createControllerFactoryForClass, RestComponent, RestServer, - RestServerConfig, } from '../../..'; -import {RestBindings, RestTags} from '../../../keys'; +import {RestTags} from '../../../keys'; import {ConsolidationEnhancer} from '../../../spec-enhancers/consolidate.spec-enhancer'; import {TestInfoSpecEnhancer} from './fixtures/info.spec.extension'; @@ -408,181 +399,6 @@ describe('RestServer.getApiSpec()', () => { expect(spec.info).to.eql(EXPECTED_SPEC_INFO); }); - context('options', () => { - afterEach(async () => server.stop()); - it('disables consolidator if consolidate is set to false', async () => { - await givenAppWithRestConfig({ - rest: {openApiSpec: {consolidate: false}}, - }); - - const INPUT_SPEC = anOpenApiSpec() - .withOperation( - 'get', - '/', - anOperationSpec().withResponse(200, { - description: 'Example', - content: { - 'application/json': { - schema: { - title: 'loopback.example', - properties: { - test: { - type: 'string', - }, - }, - }, - }, - }, - }), - ) - .build(); - - server.api(INPUT_SPEC); - - server.OASEnhancer.spec = await server.get( - RestBindings.API_SPEC, - ); - await server.OASEnhancer.applyEnhancerByName('consolidate'); - const spec = await server.get(RestBindings.API_SPEC); - expect(spec).to.eql(INPUT_SPEC); - }); - - it('runs consolidator if consolidate is set to true', async () => { - await givenAppWithRestConfig({ - rest: {openApiSpec: {consolidate: true}}, - }); - - server.api( - anOpenApiSpec() - .withOperation( - 'get', - '/', - anOperationSpec().withResponse(200, { - description: 'Example', - content: { - 'application/json': { - schema: { - title: 'loopback.example', - properties: { - test: { - type: 'string', - }, - }, - }, - }, - }, - }), - ) - .build(), - ); - - const EXPECTED_SPEC = anOpenApiSpec() - .withOperation( - 'get', - '/', - anOperationSpec().withResponse(200, { - description: 'Example', - content: { - 'application/json': { - schema: { - $ref: '#/components/schemas/loopback.example', - }, - }, - }, - }), - ) - .withComponents( - aComponentsSpec().withSchema('loopback.example', { - title: 'loopback.example', - properties: { - test: { - type: 'string', - }, - }, - }), - ) - .build(); - - server.OASEnhancer.spec = await server.get( - RestBindings.API_SPEC, - ); - await server.OASEnhancer.applyEnhancerByName('consolidate'); - const spec = await server.get(RestBindings.API_SPEC); - expect(spec).to.eql(EXPECTED_SPEC); - }); - - it('runs consolidator if consolidate is undefined', async () => { - await givenAppWithRestConfig({ - rest: {openApiSpec: {consolidate: undefined}}, - }); - - server.api( - anOpenApiSpec() - .withOperation( - 'get', - '/', - anOperationSpec().withResponse(200, { - description: 'Example', - content: { - 'application/json': { - schema: { - title: 'loopback.example', - properties: { - test: { - type: 'string', - }, - }, - }, - }, - }, - }), - ) - .build(), - ); - - const EXPECTED_SPEC = anOpenApiSpec() - .withOperation( - 'get', - '/', - anOperationSpec().withResponse(200, { - description: 'Example', - content: { - 'application/json': { - schema: { - $ref: '#/components/schemas/loopback.example', - }, - }, - }, - }), - ) - .withComponents( - aComponentsSpec().withSchema('loopback.example', { - title: 'loopback.example', - properties: { - test: { - type: 'string', - }, - }, - }), - ) - .build(); - - server.OASEnhancer.spec = await server.get( - RestBindings.API_SPEC, - ); - await server.OASEnhancer.applyEnhancerByName('consolidate'); - const spec = await server.get(RestBindings.API_SPEC); - expect(spec).to.eql(EXPECTED_SPEC); - }); - }); - - async function givenAppWithRestConfig(options: {rest: RestServerConfig}) { - options.rest = givenHttpServerConfig(options.rest); - app = new Application(options); - app.component(RestComponent); - server = await app.getServer(RestServer); - await server.start(); - } async function givenApplication() { app = new Application(); app.component(RestComponent);