Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1289af2
commit f67efe6
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |