Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Oct 10, 2019
1 parent e5165df commit 36c04aa
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 127 deletions.
27 changes: 1 addition & 26 deletions src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export class ResponseValidator {
constructor(openApiSpec, options: any = {}) {
this.spec = openApiSpec;
this.ajv = createResponseAjv(openApiSpec, options);
(<any>mung).onError = function(err, req, res, next) {
// monkey patch mung to rethrow exception
(<any>mung).onError = (err, req, res, next) => {
return next(err);
};
}
Expand Down Expand Up @@ -129,28 +128,4 @@ export class ResponseValidator {
}
return validators;
}

private validateBody(body) {}

private toOpenapiValidationError(error: Ajv.ErrorObject) {
const validationError = {
path: `instance${error.dataPath}`,
errorCode: `${error.keyword}.openapi.responseValidation`,
message: error.message,
};

validationError.path = validationError.path.replace(
/^instance\.(?:response\.)?/,
'',
);

validationError.message =
validationError.path + ' ' + validationError.message;

if (validationError.path === 'response') {
delete validationError.path;
}

return validationError;
}
}
25 changes: 11 additions & 14 deletions test/additional.props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const packageJson = require('../package.json');

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(async () => {
// Set up the express app
Expand All @@ -17,16 +16,14 @@ describe(packageJson.name, () => {
'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)),
app = await createApp({ apiSpec }, 3005, app =>
app.use(
`${app.basePath}/additional_props`,
express
.Router()
.post(`/false`, (req, res) => res.json(req.body))
.post(`/true`, (req, res) => res.json(req.body)),
),
);
});

Expand All @@ -36,22 +33,22 @@ describe(packageJson.name, () => {

it('should return 400 if additionalProperties=false, but extra props sent', async () =>
request(app)
.post(`${basePath}/additional_props/false`)
.post(`${app.basePath}/additional_props/false`)
.send({
name: 'test',
extra_prop: 'test',
})
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.be.an('array');
expect(r.body.errors).to.have.length(1);
const message = r.body.errors[0].message;
expect(message).to.equal('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`)
.post(`${app.basePath}/additional_props/true`)
.send({
name: 'test',
extra_prop: 'test',
Expand Down
25 changes: 11 additions & 14 deletions test/coercion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ const packageJson = require('../package.json');

describe(packageJson.name, () => {
let app = null;
let basePath = null;

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

// Define new coercion routes
app.use(
`${basePath}/coercion`,
express
.Router()
.post(`/pets`, (req, res) => res.json(req.body))
.post(`/pets_string_boolean`, (req, res) => res.json(req.body)),
app = await createApp({ apiSpec }, 3005, app =>
app.use(
`${app.basePath}/coercion`,
express
.Router()
.post(`/pets`, (req, res) => res.json(req.body))
.post(`/pets_string_boolean`, (req, res) => res.json(req.body)),
),
);
});

Expand All @@ -32,7 +29,7 @@ describe(packageJson.name, () => {

it('should coerce is_cat to boolean since it is defined as a boolean in the spec', async () =>
request(app)
.post(`${basePath}/coercion/pets`)
.post(`${app.basePath}/coercion/pets`)
.send({
name: 'test',
is_cat: 'true',
Expand All @@ -45,7 +42,7 @@ describe(packageJson.name, () => {

it('should keep is_cat as boolean', async () =>
request(app)
.post(`${basePath}/coercion/pets`)
.post(`${app.basePath}/coercion/pets`)
.send({
name: 'test',
is_cat: true,
Expand All @@ -58,7 +55,7 @@ describe(packageJson.name, () => {

it('should coerce a is_cat from boolean to string since it is defined as such in the spec', async () =>
request(app)
.post(`${basePath}/coercion/pets_string_boolean`)
.post(`${app.basePath}/coercion/pets_string_boolean`)
.send({
name: 'test',
is_cat: true,
Expand Down
4 changes: 1 addition & 3 deletions test/common/app.common.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as http from 'http';
import * as express from 'express';
const BASE_PATH = '/v1';

export function startServer(app, port): Promise<http.Server> {
return new Promise((resolve, reject) => {
const http = require('http');
const server = http.createServer(app);
app.server = server;
app.basePath = BASE_PATH;
server.listen(port, () => {
console.log(`Listening on port ${port}`);
resolve(server);
Expand All @@ -16,7 +14,7 @@ export function startServer(app, port): Promise<http.Server> {
}

export function routes(app) {
const basePath = BASE_PATH;
const basePath = app.basePath;
const router1 = express
.Router()
.post('/', function(req, res, next) {
Expand Down
4 changes: 1 addition & 3 deletions test/headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(() => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
return createApp({ apiSpec }, 3004).then(a => {
app = a;
basePath = (<any>app).basePath;
});
});

Expand All @@ -22,7 +20,7 @@ describe(packageJson.name, () => {

it('should throw 400 if required header is missing', async () =>
request(app)
.get(`${basePath}/pets/10/attributes`)
.get(`${app.basePath}/pets/10/attributes`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(400)
Expand Down
30 changes: 13 additions & 17 deletions test/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ import { createApp } from './common/app';
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
let basePath = null;
describe(`GET .../pets/:id/photos`, () => {
let app = null;

before(() => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
return createApp({ apiSpec }, 3003).then(a => {
app = a;
basePath = (<any>app).basePath;
before(async () => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
app = await createApp({ apiSpec }, 3003);
});
});

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

describe(`GET ${basePath}/pets/:id/photos`, () => {
it('should throw 400 when required multipart file field', async () =>
request(app)
.post(`${basePath}/pets/10/photos`)
.post(`${app.basePath}/pets/10/photos`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.expect(400)
Expand All @@ -38,15 +34,15 @@ describe(packageJson.name, () => {

it('should throw 400 when required form field is missing during multipart upload', async () =>
request(app)
.post(`${basePath}/pets/10/photos`)
.post(`${app.basePath}/pets/10/photos`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.attach('file', 'package.json')
.expect(400));

it('should validate multipart file and metadata', async () =>
request(app)
.post(`${basePath}/pets/10/photos`)
.post(`${app.basePath}/pets/10/photos`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.attach('file', 'package.json')
Expand All @@ -65,7 +61,7 @@ describe(packageJson.name, () => {

it('should throw 405 get method not allowed', async () =>
request(app)
.get(`${basePath}/pets/10/photos`)
.get(`${app.basePath}/pets/10/photos`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
Expand All @@ -75,7 +71,7 @@ describe(packageJson.name, () => {

it('should throw 415 unsupported media type', async () =>
request(app)
.post(`${basePath}/pets/10/photos`)
.post(`${app.basePath}/pets/10/photos`)
.send({ test: 'test' })
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
Expand Down
25 changes: 13 additions & 12 deletions test/nullable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ describe(packageJson.name, () => {
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'nullable.yaml');
app = await createApp({ apiSpec, coerceTypes: false }, 3005);
basePath = app.basePath;

app.use(
`${basePath}`,
express.Router().post(`/pets/nullable`, (req, res) => res.json(req.body)),
app = await createApp({ apiSpec, coerceTypes: false }, 3005, app =>
app.use(
`${app.basePath}`,
express
.Router()
.post(`/pets/nullable`, (req, res) => res.json(req.body)),
),
);
});

Expand All @@ -28,7 +29,7 @@ describe(packageJson.name, () => {

it('should allow null to be set (name: nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.post(`${app.basePath}/pets/nullable`)
.send({
name: null,
})
Expand All @@ -39,15 +40,15 @@ describe(packageJson.name, () => {

it('should not fill an explicity null with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.post(`${app.basePath}/pets`)
.send({
name: null,
})
.expect(400));

it('should fill unspecified field with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.post(`${app.basePath}/pets`)
.send({
name: 'name',
})
Expand All @@ -58,7 +59,7 @@ describe(packageJson.name, () => {

it('should fail if required and not provided (nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.post(`${app.basePath}/pets/nullable`)
.send({})
.expect(400)
.then(r => {
Expand All @@ -67,7 +68,7 @@ describe(packageJson.name, () => {

it('should fail if required and not provided (nullable false', async () =>
request(app)
.post(`${basePath}/pets`)
.post(`${app.basePath}/pets`)
.send({})
.expect(400)
.then(r => {
Expand All @@ -76,7 +77,7 @@ describe(packageJson.name, () => {

it('should fail if required and provided as null when nullable is false', async () =>
request(app)
.post(`${basePath}/pets`)
.post(`${app.basePath}/pets`)
.send({
name: null,
})
Expand Down
Loading

0 comments on commit 36c04aa

Please sign in to comment.