|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
ruleTester.run("allow-fields-must-be-global-middleware", rule, { |
|
7
|
valid: [ |
|
8
|
// Global middleware: no method scoping. |
|
9
|
{ |
|
10
|
code: ` |
|
11
|
import { |
|
12
|
allowFields, |
|
13
|
defineMiddlewares, |
|
14
|
} from "@medusajs/framework/http" |
|
15
|
export default defineMiddlewares({ |
|
16
|
routes: [ |
|
17
|
{ |
|
18
|
matcher: "/store/products", |
|
19
|
middlewares: [allowFields("brand")], |
|
20
|
}, |
|
21
|
], |
|
22
|
}) |
|
23
|
`, |
|
24
|
}, |
|
25
|
// Global middleware writing to req.allowed. |
|
26
|
{ |
|
27
|
code: ` |
|
28
|
export default defineMiddlewares({ |
|
29
|
routes: [ |
|
30
|
{ |
|
31
|
matcher: "/store/products", |
|
32
|
middlewares: [ |
|
33
|
(req, res, next) => { |
|
34
|
req.allowed.push("brand") |
|
35
|
next() |
|
36
|
}, |
|
37
|
], |
|
38
|
}, |
|
39
|
], |
|
40
|
}) |
|
41
|
`, |
|
42
|
}, |
|
43
|
// A method-scoped middleware that doesn't touch allowed fields. |
|
44
|
{ |
|
45
|
code: ` |
|
46
|
export default defineMiddlewares({ |
|
47
|
routes: [ |
|
48
|
{ |
|
49
|
matcher: "/store/products", |
|
50
|
method: "GET", |
|
51
|
middlewares: [(req, res, next) => { next() }], |
|
52
|
}, |
|
53
|
], |
|
54
|
}) |
|
55
|
`, |
|
56
|
}, |
|
57
|
// An `allowed` property on something other than the request param. |
|
58
|
{ |
|
59
|
code: ` |
|
60
|
export default defineMiddlewares({ |
|
61
|
routes: [ |
|
62
|
{ |
|
63
|
matcher: "/store/products", |
|
64
|
method: "GET", |
|
65
|
middlewares: [ |
|
66
|
(req, res, next) => { |
|
67
|
const config = { allowed: [] } |
|
68
|
config.allowed.push("brand") |
|
69
|
next() |
|
70
|
}, |
|
71
|
], |
|
72
|
}, |
|
73
|
], |
|
74
|
}) |
|
75
|
`, |
|
76
|
}, |
|
77
|
// Global middleware replacing the disallowed fields. |
|
78
|
{ |
|
79
|
code: ` |
|
80
|
export default defineMiddlewares({ |
|
81
|
routes: [ |
|
82
|
{ |
|
83
|
matcher: "/store/products", |
|
84
|
middlewares: [ |
|
85
|
(req, res, next) => { |
|
86
|
req.disallowed = ["variants.prices"] |
|
87
|
next() |
|
88
|
}, |
|
89
|
], |
|
90
|
}, |
|
91
|
], |
|
92
|
}) |
|
93
|
`, |
|
94
|
}, |
|
95
|
// A `disallowed` property on something other than the request param. |
|
96
|
{ |
|
97
|
code: ` |
|
98
|
export default defineMiddlewares({ |
|
99
|
routes: [ |
|
100
|
{ |
|
101
|
matcher: "/store/products", |
|
102
|
method: "GET", |
|
103
|
middlewares: [ |
|
104
|
(req, res, next) => { |
|
105
|
const config = { disallowed: [] } |
|
106
|
config.disallowed.push("variants.prices") |
|
107
|
next() |
|
108
|
}, |
|
109
|
], |
|
110
|
}, |
|
111
|
], |
|
112
|
}) |
|
113
|
`, |
|
114
|
}, |
|
115
|
// A method-scoped middleware validating the body. |
|
116
|
{ |
|
117
|
code: ` |
|
118
|
export default defineMiddlewares({ |
|
119
|
routes: [ |
|
120
|
{ |
|
121
|
matcher: "/store/products", |
|
122
|
methods: ["POST"], |
|
123
|
middlewares: [validateAndTransformBody(schema)], |
|
124
|
}, |
|
125
|
], |
|
126
|
}) |
|
127
|
`, |
|
128
|
}, |
|
129
|
], |
|
130
|
invalid: [ |
|
131
|
// allowFields under `method`. |
|
132
|
{ |
|
133
|
code: ` |
|
134
|
export default defineMiddlewares({ |
|
135
|
routes: [ |
|
136
|
{ |
|
137
|
matcher: "/store/products", |
|
138
|
method: "GET", |
|
139
|
middlewares: [allowFields("brand")], |
|
140
|
}, |
|
141
|
], |
|
142
|
}) |
|
143
|
`, |
|
144
|
errors: [ |
|
145
|
{ |
|
146
|
messageId: "methodScoped", |
|
147
|
data: { source: "allowFields", key: "method" }, |
|
148
|
}, |
|
149
|
], |
|
150
|
}, |
|
151
|
// allowFields under `methods`. |
|
152
|
{ |
|
153
|
code: ` |
|
154
|
export default defineMiddlewares({ |
|
155
|
routes: [ |
|
156
|
{ |
|
157
|
matcher: "/store/products", |
|
158
|
methods: ["GET"], |
|
159
|
middlewares: [allowFields("brand")], |
|
160
|
}, |
|
161
|
], |
|
162
|
}) |
|
163
|
`, |
|
164
|
errors: [ |
|
165
|
{ |
|
166
|
messageId: "methodScoped", |
|
167
|
data: { source: "allowFields", key: "methods" }, |
|
168
|
}, |
|
169
|
], |
|
170
|
}, |
|
171
|
// req.allowed mutation under `method`. |
|
172
|
{ |
|
173
|
code: ` |
|
174
|
export default defineMiddlewares({ |
|
175
|
routes: [ |
|
176
|
{ |
|
177
|
matcher: "/store/products", |
|
178
|
method: "GET", |
|
179
|
middlewares: [ |
|
180
|
(req, res, next) => { |
|
181
|
(req.allowed ??= []).push("brand") |
|
182
|
next() |
|
183
|
}, |
|
184
|
], |
|
185
|
}, |
|
186
|
], |
|
187
|
}) |
|
188
|
`, |
|
189
|
errors: [ |
|
190
|
{ |
|
191
|
messageId: "methodScoped", |
|
192
|
data: { source: "req.allowed", key: "method" }, |
|
193
|
}, |
|
194
|
], |
|
195
|
}, |
|
196
|
// req.disallowed assignment under `method`. |
|
197
|
{ |
|
198
|
code: ` |
|
199
|
export default defineMiddlewares({ |
|
200
|
routes: [ |
|
201
|
{ |
|
202
|
matcher: "/store/products", |
|
203
|
method: "GET", |
|
204
|
middlewares: [ |
|
205
|
(req, res, next) => { |
|
206
|
req.disallowed = ["variants.prices"] |
|
207
|
next() |
|
208
|
}, |
|
209
|
], |
|
210
|
}, |
|
211
|
], |
|
212
|
}) |
|
213
|
`, |
|
214
|
errors: [ |
|
215
|
{ |
|
216
|
messageId: "methodScoped", |
|
217
|
data: { source: "req.disallowed", key: "method" }, |
|
218
|
}, |
|
219
|
], |
|
220
|
}, |
|
221
|
// req.disallowed push under `methods`. |
|
222
|
{ |
|
223
|
code: ` |
|
224
|
export default defineMiddlewares({ |
|
225
|
routes: [ |
|
226
|
{ |
|
227
|
matcher: "/store/products", |
|
228
|
methods: ["GET"], |
|
229
|
middlewares: [ |
|
230
|
(req, res, next) => { |
|
231
|
req.disallowed.push("variants.prices") |
|
232
|
next() |
|
233
|
}, |
|
234
|
], |
|
235
|
}, |
|
236
|
], |
|
237
|
}) |
|
238
|
`, |
|
239
|
errors: [ |
|
240
|
{ |
|
241
|
messageId: "methodScoped", |
|
242
|
data: { source: "req.disallowed", key: "methods" }, |
|
243
|
}, |
|
244
|
], |
|
245
|
}, |
|
246
|
// A middleware setting both properties reports each one. |
|
247
|
{ |
|
248
|
code: ` |
|
249
|
export default defineMiddlewares({ |
|
250
|
routes: [ |
|
251
|
{ |
|
252
|
matcher: "/store/products", |
|
253
|
method: "GET", |
|
254
|
middlewares: [ |
|
255
|
(req, res, next) => { |
|
256
|
req.allowed.push("brand") |
|
257
|
req.disallowed = ["variants.prices"] |
|
258
|
next() |
|
259
|
}, |
|
260
|
], |
|
261
|
}, |
|
262
|
], |
|
263
|
}) |
|
264
|
`, |
|
265
|
errors: [ |
|
266
|
{ |
|
267
|
messageId: "methodScoped", |
|
268
|
data: { source: "req.allowed", key: "method" }, |
|
269
|
}, |
|
270
|
{ |
|
271
|
messageId: "methodScoped", |
|
272
|
data: { source: "req.disallowed", key: "method" }, |
|
273
|
}, |
|
274
|
], |
|
275
|
}, |
|
276
|
// Only the method-scoped route is reported. |
|
277
|
{ |
|
278
|
code: ` |
|
279
|
export default defineMiddlewares({ |
|
280
|
routes: [ |
|
281
|
{ |
|
282
|
matcher: "/store/products", |
|
283
|
middlewares: [allowFields("brand")], |
|
284
|
}, |
|
285
|
{ |
|
286
|
matcher: "/store/carts", |
|
287
|
method: "GET", |
|
288
|
middlewares: [allowFields("custom")], |
|
289
|
}, |
|
290
|
], |
|
291
|
}) |
|
292
|
`, |
|
293
|
errors: [ |
|
294
|
{ |
|
295
|
messageId: "methodScoped", |
|
296
|
data: { source: "allowFields", key: "method" }, |
|
297
|
}, |
|
298
|
], |
|
299
|
}, |
|
300
|
], |
|
301
|
}) |