From 80e84a340b5f3677deb7d6a36f9c74be9b0b4c7d Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sat, 24 Oct 2020 21:58:53 -0400 Subject: [PATCH] add/fix tests for request/response allOf --- test/all.of.spec.ts | 7 +++++++ test/request.bodies.ref.spec.ts | 11 +++++++++++ test/resources/all.of.yaml | 1 + test/resources/response.validation.yaml | 25 +++++++------------------ test/response.validation.spec.ts | 24 +++++++++++++++--------- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/test/all.of.spec.ts b/test/all.of.spec.ts index d3c3236a..3c71930a 100644 --- a/test/all.of.spec.ts +++ b/test/all.of.spec.ts @@ -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'))); }); diff --git a/test/request.bodies.ref.spec.ts b/test/request.bodies.ref.spec.ts index cfea33ba..525a1d3f 100644 --- a/test/request.bodies.ref.spec.ts +++ b/test/request.bodies.ref.spec.ts @@ -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`) diff --git a/test/resources/all.of.yaml b/test/resources/all.of.yaml index 26ff52fc..7110589d 100644 --- a/test/resources/all.of.yaml +++ b/test/resources/all.of.yaml @@ -40,6 +40,7 @@ components: - $ref: "#/components/schemas/NewPet" - required: - id + type: object properties: id: type: integer diff --git a/test/resources/response.validation.yaml b/test/resources/response.validation.yaml index 50c0e3a2..ad25e0ce 100644 --- a/test/resources/response.validation.yaml +++ b/test/resources/response.validation.yaml @@ -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: @@ -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: diff --git a/test/response.validation.spec.ts b/test/response.validation.spec.ts index 67db365a..a099fa5a 100644 --- a/test/response.validation.spec.ts +++ b/test/response.validation.spec.ts @@ -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']; @@ -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); }));