Skip to content

Commit

Permalink
add readonly required test using allOf
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Oct 10, 2020
1 parent cae389c commit cbd6292
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 25 deletions.
65 changes: 42 additions & 23 deletions test/read.only.spec.ts
Expand Up @@ -10,7 +10,7 @@ describe(packageJson.name, () => {
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 = await createApp({ apiSpec, validateResponses: true }, 3005, (app) =>
app
.post(`${app.basePath}/products`, (req, res) => res.json(req.body))
.get(`${app.basePath}/products`, (req, res) =>
Expand Down Expand Up @@ -42,11 +42,19 @@ describe(packageJson.name, () => {
const body = req.body;
body.id = 'test';
body.created_at = new Date().toISOString();
body.reviews = body.reviews.map(r => ({
body.reviews = body.reviews.map((r) => ({
id: 99,
rating: r.rating ?? 2,
}));
res.json(body);
})
.post(`${app.basePath}/readonly_required_allof`, (req, res) => {
const json = {
name: 'My Name',
...(req.query.include_id ? { id: 'test_id' } : {}),
};
console.log(json);
res.json(json);
}),
);
});
Expand All @@ -66,7 +74,7 @@ describe(packageJson.name, () => {
created_at: new Date().toISOString(),
})
.expect(400)
.then(r => {
.then((r) => {
const body = r.body;
// id is a readonly property and should not be allowed in the request
expect(body.message).to.contain('id');
Expand All @@ -76,10 +84,8 @@ describe(packageJson.name, () => {
request(app)
.get(`${app.basePath}/products`)
.expect(200)
.then(r => {
expect(r.body)
.to.be.an('array')
.with.length(1);
.then((r) => {
expect(r.body).to.be.an('array').with.length(1);
}));

it('should not allow read only inlined properties in requests', async () =>
Expand All @@ -93,7 +99,7 @@ describe(packageJson.name, () => {
created_at: new Date().toUTCString(),
})
.expect(400)
.then(r => {
.then((r) => {
const body = r.body;
// id is a readonly property and should not be allowed in the request
expect(body.message).to.contain('id');
Expand All @@ -114,7 +120,7 @@ describe(packageJson.name, () => {
},
})
.expect(400)
.then(r => {
.then((r) => {
const body = r.body;
// id is a readonly property and should not be allowed in the request
expect(body.message).to.contain('id');
Expand All @@ -135,7 +141,7 @@ describe(packageJson.name, () => {
],
})
.expect(400)
.then(r => {
.then((r) => {
const body = r.body;
// id is a readonly property and should not be allowed in the request
expect(body.message).to.contain('request.body.reviews[0].id');
Expand All @@ -152,10 +158,8 @@ describe(packageJson.name, () => {
username: 'test',
})
.expect(200)
.then(r => {
expect(r.body)
.to.be.an('object')
.with.property('id');
.then((r) => {
expect(r.body).to.be.an('object').with.property('id');
expect(r.body).to.have.property('username');
}));

Expand All @@ -170,10 +174,8 @@ describe(packageJson.name, () => {
username: 'test',
})
.expect(200)
.then(r => {
expect(r.body)
.to.be.an('object')
.with.property('id');
.then((r) => {
expect(r.body).to.be.an('object').with.property('id');
expect(r.body).to.have.property('username');
}));

Expand All @@ -188,10 +190,8 @@ describe(packageJson.name, () => {
username: 'test',
})
.expect(200)
.then(r => {
expect(r.body)
.to.be.an('object')
.with.property('id');
.then((r) => {
expect(r.body).to.be.an('object').with.property('id');
expect(r.body).to.have.property('username');
}));

Expand All @@ -203,9 +203,28 @@ describe(packageJson.name, () => {
username: 'test',
})
.expect(500)
.then(r => {
.then((r) => {
expect(r.body.errors[0])
.to.have.property('message')
.equals("should have required property 'id'");
}));

it('should require readonly required property in response', async () =>
request(app)
.post(`${app.basePath}/readonly_required_allof`)
.query({ include_id: true })
.send({ optional: 'test' })
.set('content-type', 'application/json')
.expect(200));

it('should return 500 if readonly required property is missing from response', async () =>
request(app)
.post(`${app.basePath}/readonly_required_allof`)
.query({ include_id: false })
.send({ optional: 'test' })
.set('content-type', 'application/json')
.expect(500)
.then((r) => {
expect(r.body.message).includes("should have required property 'id'");
}));
});
49 changes: 47 additions & 2 deletions test/resources/read.only.yaml
Expand Up @@ -13,8 +13,8 @@ servers:
paths:
/user_inlined:
post:
description: get user
operationId: getUser
description: get user inlined
operationId: getUserInlined
parameters:
- name: include_id
in: query
Expand Down Expand Up @@ -145,6 +145,32 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProductNested'

/readonly_required_allof:
post:
description: readonly required
parameters:
- name: include_id
in: query
schema:
type: boolean
required: true
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
optional:
type: string
responses:
'200':
description: readonly required
content:
application/json:
schema:
$ref: '#/components/schemas/Item'

components:
schemas:
Expand Down Expand Up @@ -206,3 +232,22 @@ components:
type: string
name:
type: string

Item:
allOf:
- $ref: '#/components/schemas/Id'
- $ref: '#/components/schemas/Name'
Id:
properties:
id:
type: string
readOnly: true
required:
- id

Name:
properties:
name:
type: string
required:
- name

0 comments on commit cbd6292

Please sign in to comment.