-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
write.only.spec.ts
171 lines (161 loc) 路 4.89 KB
/
write.only.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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: 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;
delete body.password;
}
res.json({
// id: 'xxxxx',
// created_at: '2024-02-09T17:32:28Z',
...body,
});
}),
);
});
after(() => {
app.server.close();
});
it('should not allow ready only inlined properties in requests', async () =>
request(app)
.post(`${app.basePath}/products/inlined`)
.set('content-type', 'application/json')
.send({
name: 'some name',
price: 10.99,
created_at: new Date().toUTCString(),
})
.expect(400)
.then((r) => {
const body = r.body;
// id is a readonly property and should not be allowed in the request
expect(body.message).to.contain('created_at');
}));
it('should not allow write only inlined properties in responses', async () =>
request(app)
.post(`${app.basePath}/products/inlined`)
.set('content-type', 'application/json')
.send({
name: 'some name',
role: 'admin',
price: 10.99,
})
.expect(500)
.then((r) => {
const body = r.body;
expect(body.message).to.contain('role');
expect(body.errors[0].path).to.contain('/response/role');
expect(body.errors[0].message).to.contain('write-only');
}));
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,
password: 'password_value',
})
.expect(200));
it('should return 200 if no write-only properties are in the responses', async () =>
request(app)
.post(`${app.basePath}/products/nested`)
.query({
exclude_write_only: true,
})
.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;
// check that read-only props were not affected and present in the response
expect(body.created_at).to.be;
}));
it('should not allow write only properties in responses (nested schema $refs)', 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(500)
.then((r) => {
const body = r.body;
expect(body.message).to.contain('role_x');
expect(body.errors[0].path).to.contain('/response/reviews/0/role_x');
expect(body.errors[0].message).to.contain('write-only');
}));
it('should not allow read only properties in requests (deep nested schema $refs)', async () =>
request(app)
.post(`${app.basePath}/products/nested`)
.query({
exclude_write_only: true,
})
.set('content-type', 'application/json')
.send({
name: 'some name',
price: 10.99,
reviews: [
{
id: 99,
role_x: 'admin',
rating: 5,
},
],
})
.expect(400)
.then((r) => {
const body = r.body;
expect(body.message).to.contain('request/body/reviews/0/id');
}));
});