|
| 1 | +import * as path from 'path'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import * as request from 'supertest'; |
| 4 | +import { createApp } from './common/app'; |
| 5 | +import * as packageJson from '../package.json'; |
| 6 | + |
| 7 | +describe(packageJson.name, () => { |
| 8 | + let app = null; |
| 9 | + |
| 10 | + before(async () => { |
| 11 | + // Set up the express app |
| 12 | + const apiSpec = path.join('test', 'resources', 'read.only.yaml'); |
| 13 | + app = await createApp({ apiSpec, validateRequests: {removeAdditional:true}, validateResponses: true }, 3005, (app) => |
| 14 | + app |
| 15 | + .post(`${app.basePath}/products`, (req, res) => res.json(req.body)) |
| 16 | + .get(`${app.basePath}/products`, (req, res) => |
| 17 | + res.json([ |
| 18 | + { |
| 19 | + id: 'id_1', |
| 20 | + name: 'name_1', |
| 21 | + price: 9.99, |
| 22 | + created_at: new Date().toISOString(), |
| 23 | + }, |
| 24 | + ]), |
| 25 | + ) |
| 26 | + .post(`${app.basePath}/products/inlined`, (req, res) => |
| 27 | + res.json(req.body), |
| 28 | + ) |
| 29 | + .post(`${app.basePath}/user`, (req, res) => |
| 30 | + res.json({ |
| 31 | + ...req.body, |
| 32 | + ...(req.query.include_id ? { id: 'test_id' } : {}), |
| 33 | + }), |
| 34 | + ) |
| 35 | + .post(`${app.basePath}/user_inlined`, (req, res) => |
| 36 | + res.json({ |
| 37 | + ...req.body, |
| 38 | + ...(req.query.include_id ? { id: 'test_id' } : {}), |
| 39 | + }), |
| 40 | + ) |
| 41 | + .post(`${app.basePath}/products/nested`, (req, res) => { |
| 42 | + const body = req.body; |
| 43 | + body.id = 'test'; |
| 44 | + body.created_at = new Date().toISOString(); |
| 45 | + body.reviews = body.reviews.map((r) => ({ |
| 46 | + id: 99, |
| 47 | + rating: r.rating ?? 2, |
| 48 | + })); |
| 49 | + res.json(body); |
| 50 | + }) |
| 51 | + .post(`${app.basePath}/readonly_required_allof`, (req, res) => { |
| 52 | + const json = { |
| 53 | + name: 'My Name', |
| 54 | + ...(req.query.include_id ? { id: 'test_id' } : {}), |
| 55 | + }; |
| 56 | + res.json(json); |
| 57 | + }), |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + after(() => { |
| 62 | + app.server.close(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should remove read only properties in requests thanks to removeAdditional', async () => |
| 66 | + request(app) |
| 67 | + .post(`${app.basePath}/products`) |
| 68 | + .set('content-type', 'application/json') |
| 69 | + .send({ |
| 70 | + id: 'id_1', |
| 71 | + name: 'some name', |
| 72 | + price: 10.99, |
| 73 | + created_at: new Date().toISOString(), |
| 74 | + }) |
| 75 | + .expect(200) |
| 76 | + .then((r) => { |
| 77 | + const body = r.body; |
| 78 | + // id is a readonly property and should not be allowed in the request |
| 79 | + // but, as removeAdditional is true for requests, it should be deleted before entering in the route |
| 80 | + expect(body.id).to.be.undefined; |
| 81 | + })); |
| 82 | + |
| 83 | + |
| 84 | + it('should allow read only properties in responses', async () => |
| 85 | + request(app) |
| 86 | + .get(`${app.basePath}/products`) |
| 87 | + .expect(200) |
| 88 | + .then((r) => { |
| 89 | + expect(r.body).to.be.an('array').with.length(1); |
| 90 | + })); |
| 91 | + |
| 92 | + it('should remove read only inlined properties in requests thanks to removeAdditional', async () => |
| 93 | + await request(app) |
| 94 | + .post(`${app.basePath}/products/inlined`) |
| 95 | + .set('content-type', 'application/json') |
| 96 | + .send({ |
| 97 | + id: 'id_1', |
| 98 | + name: 'some name', |
| 99 | + price: 10.99, |
| 100 | + created_at: new Date().toISOString(), |
| 101 | + }) |
| 102 | + .expect(200) |
| 103 | + .then((r) => { |
| 104 | + const body = r.body; |
| 105 | + // id is a readonly property and should not not be allowed in the request |
| 106 | + // but, as removeAdditional is true for requests, it should be deleted before entering in the route |
| 107 | + expect(body.id).to.be.undefined; |
| 108 | + })); |
| 109 | + |
| 110 | + |
| 111 | + it('should remove read only properties in requests (nested and deep nested schema $refs) thanks to removeAdditional', async () => |
| 112 | + request(app) |
| 113 | + .post(`${app.basePath}/products/nested`) |
| 114 | + .set('content-type', 'application/json') |
| 115 | + .send({ |
| 116 | + id: 'id_1', |
| 117 | + name: 'some name', |
| 118 | + price: 10.99, |
| 119 | + created_at: new Date().toISOString(), |
| 120 | + reviews: [{ |
| 121 | + id: 10, |
| 122 | + rating: 5, |
| 123 | + }], |
| 124 | + }) |
| 125 | + .expect(200) |
| 126 | + .then((r) => { |
| 127 | + const body = r.body; |
| 128 | + // id is a readonly property and should not not be allowed in the request |
| 129 | + // but, as removeAdditional is true for requests, it should be deleted before entering in the route |
| 130 | + expect(body.id).to.be.equal('test'); |
| 131 | + expect(body.reviews[0].id).to.be.equal(99); |
| 132 | + })); |
| 133 | + |
| 134 | + it('should pass validation if required read only properties to be missing from request ($ref)', async () => |
| 135 | + request(app) |
| 136 | + .post(`${app.basePath}/user`) |
| 137 | + .set('content-type', 'application/json') |
| 138 | + .query({ |
| 139 | + include_id: true, |
| 140 | + }) |
| 141 | + .send({ |
| 142 | + username: 'test', |
| 143 | + }) |
| 144 | + .expect(200) |
| 145 | + .then((r) => { |
| 146 | + expect(r.body).to.be.an('object').with.property('id'); |
| 147 | + expect(r.body).to.have.property('username'); |
| 148 | + })); |
| 149 | + |
| 150 | + it('should pass validation if required read only properties to be missing from request (inlined)', async () => |
| 151 | + request(app) |
| 152 | + .post(`${app.basePath}/user_inlined`) |
| 153 | + .set('content-type', 'application/json') |
| 154 | + .query({ |
| 155 | + include_id: true, |
| 156 | + }) |
| 157 | + .send({ |
| 158 | + username: 'test', |
| 159 | + }) |
| 160 | + .expect(200) |
| 161 | + .then((r) => { |
| 162 | + expect(r.body).to.be.an('object').with.property('id'); |
| 163 | + expect(r.body).to.have.property('username'); |
| 164 | + })); |
| 165 | + |
| 166 | + it('should pass validation if required read only properties to be missing from request (with charset)', async () => |
| 167 | + request(app) |
| 168 | + .post(`${app.basePath}/user_inlined`) |
| 169 | + .set('content-type', 'application/json; charset=utf-8') |
| 170 | + .query({ |
| 171 | + include_id: true, |
| 172 | + }) |
| 173 | + .send({ |
| 174 | + username: 'test', |
| 175 | + }) |
| 176 | + .expect(200) |
| 177 | + .then((r) => { |
| 178 | + expect(r.body).to.be.an('object').with.property('id'); |
| 179 | + expect(r.body).to.have.property('username'); |
| 180 | + })); |
| 181 | + |
| 182 | + it('should fail validation if required read only properties is missing from the response', async () => |
| 183 | + request(app) |
| 184 | + .post(`${app.basePath}/user`) |
| 185 | + .set('content-type', 'application/json') |
| 186 | + .send({ |
| 187 | + username: 'test', |
| 188 | + }) |
| 189 | + .expect(500) |
| 190 | + .then((r) => { |
| 191 | + expect(r.body.errors[0]) |
| 192 | + .to.have.property('message') |
| 193 | + .equals("must have required property 'id'"); |
| 194 | + })); |
| 195 | + |
| 196 | + it('should require readonly required property in response', async () => |
| 197 | + request(app) |
| 198 | + .post(`${app.basePath}/readonly_required_allof`) |
| 199 | + .query({ include_id: true }) |
| 200 | + .send({ optional: 'test' }) |
| 201 | + .set('content-type', 'application/json') |
| 202 | + .expect(200)); |
| 203 | + |
| 204 | + it('should return 500 if readonly required property is missing from response', async () => |
| 205 | + request(app) |
| 206 | + .post(`${app.basePath}/readonly_required_allof`) |
| 207 | + .query({ include_id: false }) |
| 208 | + .send({ optional: 'test' }) |
| 209 | + .set('content-type', 'application/json') |
| 210 | + .expect(500) |
| 211 | + .then((r) => { |
| 212 | + expect(r.body.message).includes("must have required property 'id'"); |
| 213 | + })); |
| 214 | +}); |
0 commit comments