Skip to content

Commit

Permalink
add/fix tests for request/response allOf
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Oct 25, 2020
1 parent d1ebf76 commit 80e84a3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
7 changes: 7 additions & 0 deletions test/all.of.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ describe(packageJson.name, () => {
const e = r.body;
expect(e.message).to.contain("required property 'name'");
}));

// it('should fail if array is sent when object expected', async () =>
// request(app)
// .post(`${app.basePath}/all_of`)
// .send([{ id: 1, name: 'jim' }])
// .expect(400)
// .then((r: any) => expect(r.body.message).to.contain('should be object')));
});
11 changes: 11 additions & 0 deletions test/request.bodies.ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ describe(packageJson.name, () => {
expect(body).to.have.property('testProperty');
}));

it('should return 400 if array is passed (instead of object) and the array includes an object that meets requirements', async () =>
request(app)
.post(`${app.basePath}/request_bodies_ref`)
.send([
{
testProperty: 'abc',
},
])
.expect(400)
.then((r) => expect(r.body.message).to.include('should be object')));

it('should return 200 if a json suffex is used for content-type', async () =>
request(app)
.post(`${app.basePath}/request_bodies_ref`)
Expand Down
1 change: 1 addition & 0 deletions test/resources/all.of.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ components:
- $ref: "#/components/schemas/NewPet"
- required:
- id
type: object
properties:
id:
type: integer
Expand Down
25 changes: 7 additions & 18 deletions test/resources/response.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,13 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Object'
# type: object
# required:
# - name
# - id
# properties:
# id:
# type: integer
# format: int64
# bought_at:
# type: string
# format: date-time
# nullable: true
# name:
# type: string
# nullable: true
# tag:
# type: string
post:
operationId: postObject
parameters:
- name: mode
in: query
schema:
type: string
requestBody:
content:
application/json:
Expand Down Expand Up @@ -223,7 +211,8 @@ components:
Pet:
allOf:
- $ref: '#/components/schemas/NewPet'
- required:
- type: object # need to specify type object or e.g. array will pass validation
required:
- id
properties:
id:
Expand Down
24 changes: 15 additions & 9 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ describe(packageJson.name, () => {
});
app.get(`${app.basePath}/boolean`, (req, res) => {
return res.json(req.query.value);
})
});
app.get(`${app.basePath}/object`, (req, res) => {
return res.json([
{ id: 1, name: 'name', tag: 'tag', bought_at: null },
]);
});
app.post(`${app.basePath}/object`, (req, res) => {
return res.json(req.body);
return req.query.mode === 'array'
? res.json([req.body])
: res.json(req.body);
});
app.get(`${app.basePath}/users`, (req, res) => {
const json = ['user1', 'user2', 'user3'];
Expand Down Expand Up @@ -109,23 +111,27 @@ describe(packageJson.name, () => {
expect(r.body).to.have.property('code').that.equals(500);
}));

// TODO fix me - fails for response and request validation what allOf is in use
it.skip('should fail if request is array when expecting object', async () =>
it.skip('should fail if response expects object (using allOf), but got array', async () =>
request(app)
.post(`${app.basePath}/object`)
.send([{ id: 1, name: 'name', tag: 'tag', bought_at: null }])
.expect(400)
.post(`${app.basePath}/object?mode=array`)
.send({ id: 1, name: 'fido' })
.expect(500)
.then((r: any) => {
expect(r.body.message).to.contain('should be object');
expect(r.body).to.have.property('code').that.equals(500);
}));

it('should fail if response is response is empty object', async () =>
it('should return 200 if returns expect object (using allOf) with type object', async () =>
request(app)
.post(`${app.basePath}/object`)
.send({ id: 1, name: 'fido' })
.expect(200));

it('should fail if response is empty object', async () =>
request(app)
.get(`${app.basePath}/pets?mode=empty_object`)
.expect(500)
.then((r: any) => {
console.log(r.body);
expect(r.body.message).to.contain('should be array');
expect(r.body).to.have.property('code').that.equals(500);
}));
Expand Down

0 comments on commit 80e84a3

Please sign in to comment.