Skip to content

Commit

Permalink
create tests for read only support
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Oct 14, 2019
1 parent 9c5ac4a commit 7b727cb
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 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);
}));
});
62 changes: 62 additions & 0 deletions 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

0 comments on commit 7b727cb

Please sign in to comment.