-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
write.only.removeadditional.spec.ts
100 lines (93 loc) 路 2.9 KB
/
write.only.removeadditional.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';
describe(packageJson.name, () => {
let app = null;
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'write.only.yaml');
app = await createApp(
{ apiSpec, validateResponses: { removeAdditional: true } },
3005,
(app) =>
app
.post(`${app.basePath}/products/inlined`, (req, res) => {
const body = req.body;
const excludeWriteOnly = req.query.exclude_write_only;
if (excludeWriteOnly) {
delete body.role;
}
res.json(body);
})
.post(`${app.basePath}/products/nested`, (req, res) => {
const body = req.body;
const excludeWriteOnly = req.query.exclude_write_only;
body.id = 'test';
body.created_at = new Date().toISOString();
body.reviews = body.reviews.map((r) => ({
...(excludeWriteOnly ? {} : { role_x: 'admin' }),
rating: r.rating ?? 2,
}));
if (excludeWriteOnly) {
delete body.role;
}
res.json(body);
}),
);
});
after(() => {
app.server.close();
});
it('should remove write only inlined properties in responses thanks to removeAdditional', async () =>
request(app)
.post(`${app.basePath}/products/inlined`)
.set('content-type', 'application/json')
.send({
name: 'some name',
role: 'admin',
price: 10.99,
})
.expect(200)
.then((r) => {
const body = r.body;
expect(body.message).to.be.undefined;
expect(body.role).to.be.undefined;
expect(body.price).to.be.equal(10.99);
}));
it('should return 200 if no write-only properties are in the responses', async () =>
request(app)
.post(`${app.basePath}/products/inlined`)
.query({
exclude_write_only: true,
})
.set('content-type', 'application/json')
.send({
name: 'some name',
role: 'admin',
price: 10.99,
})
.expect(200));
it('should remove write only properties in responses (nested schema $refs) thanks to removeAdditional', async () =>
request(app)
.post(`${app.basePath}/products/nested`)
.set('content-type', 'application/json')
.send({
name: 'some name',
price: 10.99,
password: 'password_value',
reviews: [
{
rating: 5,
review_password: 'review_password_value',
},
],
})
.expect(200)
.then((r) => {
const body = r.body;
expect(body.reviews[0].role_x).to.be.undefined;
expect(body.reviews[0].rating).to.be.equal(5);
}));
});