Skip to content

Commit 8c53e58

Browse files
committed
fixes write-only tests
1 parent 4f16ed2 commit 8c53e58

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

test/write.only.removeadditional.spec.ts

+33-31
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@ describe(packageJson.name, () => {
1010
before(async () => {
1111
// Set up the express app
1212
const apiSpec = path.join('test', 'resources', 'write.only.yaml');
13-
app = await createApp({ apiSpec, validateResponses: {removeAdditional : true} }, 3005, app =>
14-
app
15-
.post(`${app.basePath}/products/inlined`, (req, res) => {
16-
const body = req.body;
17-
const excludeWriteOnly = req.query.exclude_write_only;
18-
if (excludeWriteOnly) {
19-
delete body.role;
20-
}
21-
res.json(body);
22-
})
23-
.post(`${app.basePath}/products/nested`, (req, res) => {
24-
const body = req.body;
25-
const excludeWriteOnly = req.query.exclude_write_only;
26-
body.id = 'test';
27-
body.created_at = new Date().toISOString();
28-
body.reviews = body.reviews.map(r => ({
29-
...(excludeWriteOnly ? {} : { role_x: 'admin' }),
30-
rating: r.rating ?? 2,
31-
}));
13+
app = await createApp(
14+
{ apiSpec, validateResponses: { removeAdditional: true } },
15+
3005,
16+
(app) =>
17+
app
18+
.post(`${app.basePath}/products/inlined`, (req, res) => {
19+
const body = req.body;
20+
const excludeWriteOnly = req.query.exclude_write_only;
21+
if (excludeWriteOnly) {
22+
delete body.role;
23+
}
24+
res.json(body);
25+
})
26+
.post(`${app.basePath}/products/nested`, (req, res) => {
27+
const body = req.body;
28+
const excludeWriteOnly = req.query.exclude_write_only;
29+
body.id = 'test';
30+
body.created_at = new Date().toISOString();
31+
body.reviews = body.reviews.map((r) => ({
32+
...(excludeWriteOnly ? {} : { role_x: 'admin' }),
33+
rating: r.rating ?? 2,
34+
}));
3235

33-
if (excludeWriteOnly) {
34-
delete body.role;
35-
}
36-
res.json(body);
37-
}),
36+
if (excludeWriteOnly) {
37+
delete body.role;
38+
}
39+
res.json(body);
40+
}),
3841
);
3942
});
4043

@@ -52,7 +55,7 @@ describe(packageJson.name, () => {
5255
price: 10.99,
5356
})
5457
.expect(200)
55-
.then(r => {
58+
.then((r) => {
5659
const body = r.body;
5760
expect(body.message).to.be.undefined;
5861
expect(body.role).to.be.undefined;
@@ -71,8 +74,7 @@ describe(packageJson.name, () => {
7174
role: 'admin',
7275
price: 10.99,
7376
})
74-
.expect(200)
75-
);
77+
.expect(200));
7678

7779
it('should remove write only properties in responses (nested schema $refs) thanks to removeAdditional', async () =>
7880
request(app)
@@ -81,18 +83,18 @@ describe(packageJson.name, () => {
8183
.send({
8284
name: 'some name',
8385
price: 10.99,
86+
password: 'password_value',
8487
reviews: [
8588
{
86-
rating: 5
89+
rating: 5,
90+
review_password: 'review_password_value',
8791
},
8892
],
8993
})
9094
.expect(200)
91-
.then(r => {
95+
.then((r) => {
9296
const body = r.body;
93-
console.log('%j', r.body);
9497
expect(body.reviews[0].role_x).to.be.undefined;
9598
expect(body.reviews[0].rating).to.be.equal(5);
9699
}));
97-
98100
});

test/write.only.spec.ts

+42-10
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ describe(packageJson.name, () => {
1010
before(async () => {
1111
// Set up the express app
1212
const apiSpec = path.join('test', 'resources', 'write.only.yaml');
13-
app = await createApp({ apiSpec, validateResponses: true }, 3005, app =>
13+
app = await createApp({ apiSpec, validateResponses: true }, 3005, (app) =>
1414
app
1515
.post(`${app.basePath}/products/inlined`, (req, res) => {
1616
const body = req.body;
1717
const excludeWriteOnly = req.query.exclude_write_only;
1818
if (excludeWriteOnly) {
1919
delete body.role;
2020
}
21-
res.json(body);
21+
res.json({
22+
...body,
23+
});
2224
})
2325
.post(`${app.basePath}/products/nested`, (req, res) => {
2426
const body = req.body;
2527
const excludeWriteOnly = req.query.exclude_write_only;
2628
body.id = 'test';
2729
body.created_at = new Date().toISOString();
28-
body.reviews = body.reviews.map(r => ({
30+
body.reviews = body.reviews.map((r) => ({
2931
...(excludeWriteOnly ? {} : { role_x: 'admin' }),
3032
rating: r.rating ?? 2,
3133
}));
3234

3335
if (excludeWriteOnly) {
3436
delete body.role;
37+
delete body.password;
3538
}
36-
res.json(body);
39+
res.json({
40+
// id: 'xxxxx',
41+
// created_at: '2024-02-09T17:32:28Z',
42+
...body,
43+
});
3744
}),
3845
);
3946
});
@@ -52,7 +59,7 @@ describe(packageJson.name, () => {
5259
created_at: new Date().toUTCString(),
5360
})
5461
.expect(400)
55-
.then(r => {
62+
.then((r) => {
5663
const body = r.body;
5764
// id is a readonly property and should not be allowed in the request
5865
expect(body.message).to.contain('created_at');
@@ -68,7 +75,7 @@ describe(packageJson.name, () => {
6875
price: 10.99,
6976
})
7077
.expect(500)
71-
.then(r => {
78+
.then((r) => {
7279
const body = r.body;
7380
expect(body.message).to.contain('role');
7481
expect(body.errors[0].path).to.contain('/response/role');
@@ -86,10 +93,35 @@ describe(packageJson.name, () => {
8693
name: 'some name',
8794
role: 'admin',
8895
price: 10.99,
89-
password: 'password_value'
96+
password: 'password_value',
9097
})
9198
.expect(200));
9299

100+
it('should return 200 if no write-only properties are in the responses', async () =>
101+
request(app)
102+
.post(`${app.basePath}/products/nested`)
103+
.query({
104+
exclude_write_only: true,
105+
})
106+
.set('content-type', 'application/json')
107+
.send({
108+
name: 'some name',
109+
price: 10.99,
110+
password: 'password_value',
111+
reviews: [
112+
{
113+
rating: 5,
114+
review_password: 'review_password_value',
115+
},
116+
],
117+
})
118+
.expect(200)
119+
.then((r) => {
120+
const body = r.body;
121+
// check that read-only props were not affected and present in the response
122+
expect(body.created_at).to.be;
123+
}));
124+
93125
it('should not allow write only properties in responses (nested schema $refs)', async () =>
94126
request(app)
95127
.post(`${app.basePath}/products/nested`)
@@ -101,12 +133,12 @@ describe(packageJson.name, () => {
101133
reviews: [
102134
{
103135
rating: 5,
104-
review_password: 'review_password_value'
136+
review_password: 'review_password_value',
105137
},
106138
],
107139
})
108140
.expect(500)
109-
.then(r => {
141+
.then((r) => {
110142
const body = r.body;
111143
expect(body.message).to.contain('role_x');
112144
expect(body.errors[0].path).to.contain('/response/reviews/0/role_x');
@@ -132,7 +164,7 @@ describe(packageJson.name, () => {
132164
],
133165
})
134166
.expect(400)
135-
.then(r => {
167+
.then((r) => {
136168
const body = r.body;
137169
expect(body.message).to.contain('request/body/reviews/0/id');
138170
}));

0 commit comments

Comments
 (0)