Skip to content

Commit

Permalink
fixes write-only tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Feb 10, 2024
1 parent 4f16ed2 commit 8c53e58
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
64 changes: 33 additions & 31 deletions test/write.only.removeadditional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@ describe(packageJson.name, () => {
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,
}));
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);
}),
if (excludeWriteOnly) {
delete body.role;
}
res.json(body);
}),
);
});

Expand All @@ -52,7 +55,7 @@ describe(packageJson.name, () => {
price: 10.99,
})
.expect(200)
.then(r => {
.then((r) => {
const body = r.body;
expect(body.message).to.be.undefined;
expect(body.role).to.be.undefined;
Expand All @@ -71,8 +74,7 @@ describe(packageJson.name, () => {
role: 'admin',
price: 10.99,
})
.expect(200)
);
.expect(200));

it('should remove write only properties in responses (nested schema $refs) thanks to removeAdditional', async () =>
request(app)
Expand All @@ -81,18 +83,18 @@ describe(packageJson.name, () => {
.send({
name: 'some name',
price: 10.99,
password: 'password_value',
reviews: [
{
rating: 5
rating: 5,
review_password: 'review_password_value',
},
],
})
.expect(200)
.then(r => {
.then((r) => {
const body = r.body;
console.log('%j', r.body);
expect(body.reviews[0].role_x).to.be.undefined;
expect(body.reviews[0].rating).to.be.equal(5);
}));

});
52 changes: 42 additions & 10 deletions test/write.only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@ describe(packageJson.name, () => {
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 = 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);
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 => ({
body.reviews = body.reviews.map((r) => ({
...(excludeWriteOnly ? {} : { role_x: 'admin' }),
rating: r.rating ?? 2,
}));

if (excludeWriteOnly) {
delete body.role;
delete body.password;
}
res.json(body);
res.json({
// id: 'xxxxx',
// created_at: '2024-02-09T17:32:28Z',
...body,
});
}),
);
});
Expand All @@ -52,7 +59,7 @@ describe(packageJson.name, () => {
created_at: new Date().toUTCString(),
})
.expect(400)
.then(r => {
.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');
Expand All @@ -68,7 +75,7 @@ describe(packageJson.name, () => {
price: 10.99,
})
.expect(500)
.then(r => {
.then((r) => {
const body = r.body;
expect(body.message).to.contain('role');
expect(body.errors[0].path).to.contain('/response/role');
Expand All @@ -86,10 +93,35 @@ describe(packageJson.name, () => {
name: 'some name',
role: 'admin',
price: 10.99,
password: 'password_value'
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`)
Expand All @@ -101,12 +133,12 @@ describe(packageJson.name, () => {
reviews: [
{
rating: 5,
review_password: 'review_password_value'
review_password: 'review_password_value',
},
],
})
.expect(500)
.then(r => {
.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');
Expand All @@ -132,7 +164,7 @@ describe(packageJson.name, () => {
],
})
.expect(400)
.then(r => {
.then((r) => {
const body = r.body;
expect(body.message).to.contain('request/body/reviews/0/id');
}));
Expand Down

0 comments on commit 8c53e58

Please sign in to comment.