From 7b727cb0f2b61a52cbcc9038aa7988749f9e3800 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Mon, 14 Oct 2019 13:27:08 -0400 Subject: [PATCH] create tests for read only support --- test/read.only.spec.ts | 58 ++++++++++++++++++++++++++++++++ test/resources/read.only.yaml | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 test/read.only.spec.ts create mode 100644 test/resources/read.only.yaml diff --git a/test/read.only.spec.ts b/test/read.only.spec.ts new file mode 100644 index 00000000..4076ad2f --- /dev/null +++ b/test/read.only.spec.ts @@ -0,0 +1,58 @@ +import * as path from 'path'; +import * as express from 'express'; +import { expect } from 'chai'; +import * as request from 'supertest'; +import { createApp } from './common/app'; + +const packageJson = require('../package.json'); + +describe.only(packageJson.name, () => { + let app = null; + + before(async () => { + // Set up the express app + const apiSpec = path.join('test', 'resources', 'read.only.yaml'); + app = await createApp({ apiSpec, validateResponses: true }, 3005, app => + app + .post(`${app.basePath}/products`, (req, res) => res.json(req.body)) + .get(`${app.basePath}/products`, (req, res) => + res.json([ + { + id: 'id_1', + name: 'name_1', + price: 9.99, + created_at: new Date().toISOString(), + }, + ]), + ), + ); + }); + + after(() => { + app.server.close(); + }); + + it('should not allow read only properties in requests', async () => + request(app) + .get(`${app.basePath}/products`) + .query({ + id: 'id_1', + name: 'some name', + price: 10.99, + created_at: new Date().toUTCString(), + }) + .expect(400) + .then(r => { + const body = r.body; + })); + + it('should allow read only properties in responses', async () => + request(app) + .get(`${app.basePath}/products`) + .expect(200) + .then(r => { + expect(r.body) + .to.be.an('array') + .with.length(1); + })); +}); diff --git a/test/resources/read.only.yaml b/test/resources/read.only.yaml new file mode 100644 index 00000000..4b9868ce --- /dev/null +++ b/test/resources/read.only.yaml @@ -0,0 +1,62 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: Swagger Petstore + description: A sample API + termsOfService: http://swagger.io/terms/ + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html +servers: + - url: http://petstore.swagger.io/v1 + +paths: + /products: + get: + description: get products + operationId: getProducts + responses: + '200': + description: pet response + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Product' + + post: + description: create products + operationId: createProducts + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/Product' + responses: + '200': + description: pet response + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Product' + +components: + schemas: + Product: + type: object + properties: + id: + type: string + readOnly: true + name: + type: string + price: + type: number + created_at: + type: string + format: date-time + readOnly: true