@@ -10,30 +10,37 @@ describe(packageJson.name, () => {
10
10
before ( async ( ) => {
11
11
// Set up the express app
12
12
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 ) =>
14
14
app
15
15
. post ( `${ app . basePath } /products/inlined` , ( req , res ) => {
16
16
const body = req . body ;
17
17
const excludeWriteOnly = req . query . exclude_write_only ;
18
18
if ( excludeWriteOnly ) {
19
19
delete body . role ;
20
20
}
21
- res . json ( body ) ;
21
+ res . json ( {
22
+ ...body ,
23
+ } ) ;
22
24
} )
23
25
. post ( `${ app . basePath } /products/nested` , ( req , res ) => {
24
26
const body = req . body ;
25
27
const excludeWriteOnly = req . query . exclude_write_only ;
26
28
body . id = 'test' ;
27
29
body . created_at = new Date ( ) . toISOString ( ) ;
28
- body . reviews = body . reviews . map ( r => ( {
30
+ body . reviews = body . reviews . map ( ( r ) => ( {
29
31
...( excludeWriteOnly ? { } : { role_x : 'admin' } ) ,
30
32
rating : r . rating ?? 2 ,
31
33
} ) ) ;
32
34
33
35
if ( excludeWriteOnly ) {
34
36
delete body . role ;
37
+ delete body . password ;
35
38
}
36
- res . json ( body ) ;
39
+ res . json ( {
40
+ // id: 'xxxxx',
41
+ // created_at: '2024-02-09T17:32:28Z',
42
+ ...body ,
43
+ } ) ;
37
44
} ) ,
38
45
) ;
39
46
} ) ;
@@ -52,7 +59,7 @@ describe(packageJson.name, () => {
52
59
created_at : new Date ( ) . toUTCString ( ) ,
53
60
} )
54
61
. expect ( 400 )
55
- . then ( r => {
62
+ . then ( ( r ) => {
56
63
const body = r . body ;
57
64
// id is a readonly property and should not be allowed in the request
58
65
expect ( body . message ) . to . contain ( 'created_at' ) ;
@@ -68,7 +75,7 @@ describe(packageJson.name, () => {
68
75
price : 10.99 ,
69
76
} )
70
77
. expect ( 500 )
71
- . then ( r => {
78
+ . then ( ( r ) => {
72
79
const body = r . body ;
73
80
expect ( body . message ) . to . contain ( 'role' ) ;
74
81
expect ( body . errors [ 0 ] . path ) . to . contain ( '/response/role' ) ;
@@ -86,10 +93,35 @@ describe(packageJson.name, () => {
86
93
name : 'some name' ,
87
94
role : 'admin' ,
88
95
price : 10.99 ,
89
- password : 'password_value'
96
+ password : 'password_value' ,
90
97
} )
91
98
. expect ( 200 ) ) ;
92
99
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
+
93
125
it ( 'should not allow write only properties in responses (nested schema $refs)' , async ( ) =>
94
126
request ( app )
95
127
. post ( `${ app . basePath } /products/nested` )
@@ -101,12 +133,12 @@ describe(packageJson.name, () => {
101
133
reviews : [
102
134
{
103
135
rating : 5 ,
104
- review_password : 'review_password_value'
136
+ review_password : 'review_password_value' ,
105
137
} ,
106
138
] ,
107
139
} )
108
140
. expect ( 500 )
109
- . then ( r => {
141
+ . then ( ( r ) => {
110
142
const body = r . body ;
111
143
expect ( body . message ) . to . contain ( 'role_x' ) ;
112
144
expect ( body . errors [ 0 ] . path ) . to . contain ( '/response/reviews/0/role_x' ) ;
@@ -132,7 +164,7 @@ describe(packageJson.name, () => {
132
164
] ,
133
165
} )
134
166
. expect ( 400 )
135
- . then ( r => {
167
+ . then ( ( r ) => {
136
168
const body = r . body ;
137
169
expect ( body . message ) . to . contain ( 'request/body/reviews/0/id' ) ;
138
170
} ) ) ;
0 commit comments