Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubskopal committed Apr 1, 2020
1 parent 1289af2 commit f67efe6
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/framework/types.ts
Expand Up @@ -62,7 +62,7 @@ export interface OpenApiValidatorOpts {
mode: 'bundle' | 'dereference';
};
operationHandlers?: false | string;
formatValidation?: false | 'fast' | 'fail';
formatValidation?: false | 'fast' | 'full';
}

export namespace OpenAPIV3 {
Expand Down
101 changes: 101 additions & 0 deletions test/datetime.validation.spec.ts
@@ -0,0 +1,101 @@
import * as path from 'path';
import {expect} from 'chai';
import * as request from 'supertest';
import {createApp} from './common/app';

describe("datetime.validation", () => {
let app = null;

async function setupServer(formatValidation?: false | "full" | "fast") {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'datetime.validation.yaml');
app = await createApp(
{
apiSpec,
validateResponses: true,
formatValidation
},
3005,
app => {
// Define new coercion routes
app.post(`${app.basePath}/date-time-validation`, (req, res) => {
res.json(req.body);
});
},
true,
);
}

beforeEach(() => {
app = null;
});

afterEach(async () => {
if (app) {
await new Promise(resolve => app.server.close(resolve));
}
});


it('should return 200 if testDateTimeProperty is provided with invalid, but correctly formatted date time and default validation is enabled (past compatibility)', async () => {
await setupServer();
await request(app)
.post(`${app.basePath}/date-time-validation`)
.send({
testDateTimeProperty: '2000-13-03T12:13:14Z',
})
.expect(200)
.then(r => {
const {body} = r;
expect(body).to.have.property('testDateTimeProperty');
});
});

it('should return 400 if testDateTimeProperty is provided with incorrectly formatted date time and default validation enabled (past compatibility)', async () => {
await setupServer();
await request(app)
.post(`${app.basePath}/date-time-validation`)
.send({
testDateTimeProperty: 'wrong',
})
.expect(400);
});

it('should return 200 if testDateTimeProperty is provided with incorrectly formatted date time and format validation disabled', async () => {
await setupServer(false);
await request(app)
.post(`${app.basePath}/date-time-validation`)
.send({
testDateTimeProperty: 'blah-blah',
})
.expect(200)
.then(r => {
const {body} = r;
expect(body).to.have.property('testDateTimeProperty');
});
});

it('should return 200 if testDateTimeProperty is provided with valid date time and full validation enabled', async () => {
await setupServer("full");
await request(app)
.post(`${app.basePath}/date-time-validation`)
.send({
testDateTimeProperty: '2000-02-03T12:13:14Z',
})
.expect(200)
.then(r => {
const {body} = r;
expect(body).to.have.property('testDateTimeProperty');
});
});

it('should return 400 if testDateTimeProperty is provided with invalid date time and full validation enabled', async () => {
await setupServer("full");
await request(app)
.post(`${app.basePath}/date-time-validation`)
.send({
testDateTimeProperty: '2000-13-03T12:13:14Z',
})
.expect(400);
});
});
39 changes: 39 additions & 0 deletions test/resources/datetime.validation.yaml
@@ -0,0 +1,39 @@
openapi: '3.0.2'
info:
version: 1.0.0
title: date-time validation test
description: date-time validation test

servers:
- url: /v1/

paths:
/date-time-validation:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Test'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Test'
'400':
description: Bad Request

components:
schemas:
Test:
type: object
additionalProperties: false
properties:
testDateTimeProperty:
type: string
format: date-time
required:
- testDateTimeProperty

0 comments on commit f67efe6

Please sign in to comment.