Skip to content

Commit

Permalink
add additional properties tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Aug 17, 2019
1 parent 6d7b9bd commit 6644d20
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
35 changes: 29 additions & 6 deletions test/additional.props.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
Expand All @@ -9,23 +11,35 @@ describe(packageJson.name, () => {
let basePath = null;

before(async () => {
app = await createApp(
{ apiSpec: './test/resources/additional.properties.yaml' },
3005,
// Set up the express app
const apiSpec = path.join(
'test',
'resources',
'additional.properties.yaml',
);
app = await createApp({ apiSpec }, 3005);
basePath = app.basePath;

// Define new coercion routes
app.use(
`${basePath}/additional_props`,
express
.Router()
.post(`/false`, (req, res) => res.json(req.body))
.post(`/true`, (req, res) => res.json(req.body)),
);
});

after(() => {
app.server.close();
});

it('should return 400 when post has extra props', async () =>
it('should return 400 if additionalProperties=false, but extra props sent', async () =>
request(app)
.post(`${basePath}/pets`)
.post(`${basePath}/additional_props/false`)
.send({
name: 'test',
unknown_prop: 'test',
extra_prop: 'test',
})
.expect(400)
.then(r => {
Expand All @@ -36,4 +50,13 @@ describe(packageJson.name, () => {
'should NOT have additional properties',
);
}));

it('should return 200 if additonalProperities=true and extra props are sent', async () =>
request(app)
.post(`${basePath}/additional_props/true`)
.send({
name: 'test',
extra_prop: 'test',
})
.expect(200));
});
37 changes: 33 additions & 4 deletions test/resources/additional.properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ servers:
- url: /v1/

paths:
/pets:
/additional_props/false:
post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
Expand All @@ -18,18 +18,37 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/NewPet'
$ref: '#/components/schemas/PetAdditionalFalse'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/NewPet'
$ref: '#/components/schemas/PetAdditionalFalse'

/additional_props/true:
post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetAdditionalTrue'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/PetAdditionalTrue'

components:
schemas:
NewPet:
PetAdditionalFalse:
additionalProperties: false
required:
- name
Expand All @@ -38,3 +57,13 @@ components:
type: string
tag:
type: string

PetAdditionalTrue:
additionalProperties: true
required:
- name
properties:
name:
type: string
tag:
type: string

0 comments on commit 6644d20

Please sign in to comment.