-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Carmine DiMascio
committed
Oct 14, 2019
1 parent
9c5ac4a
commit 7b727cb
Showing
2 changed files
with
120 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
})); | ||
}); |
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,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 |