@@ -157,7 +157,7 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
157
157
if len (testCase .assertions ) > 0 {
158
158
// Only run these tests if we know the underlying call makes
159
159
// rbac assertions.
160
- s .NotAuthorizedErrorTest (ctx , fakeAuthorizer , callMethod )
160
+ s .NotAuthorizedErrorTest (ctx , fakeAuthorizer , testCase , callMethod )
161
161
}
162
162
163
163
if len (testCase .assertions ) > 0 ||
@@ -230,7 +230,7 @@ func (s *MethodTestSuite) NoActorErrorTest(callMethod func(ctx context.Context)
230
230
231
231
// NotAuthorizedErrorTest runs the given method with an authorizer that will fail authz.
232
232
// Asserts that the error returned is a NotAuthorizedError.
233
- func (s * MethodTestSuite ) NotAuthorizedErrorTest (ctx context.Context , az * coderdtest.FakeAuthorizer , callMethod func (ctx context.Context ) ([]reflect.Value , error )) {
233
+ func (s * MethodTestSuite ) NotAuthorizedErrorTest (ctx context.Context , az * coderdtest.FakeAuthorizer , testCase expects , callMethod func (ctx context.Context ) ([]reflect.Value , error )) {
234
234
s .Run ("NotAuthorized" , func () {
235
235
az .AlwaysReturn = rbac .ForbiddenWithInternal (xerrors .New ("Always fail authz" ), rbac.Subject {}, "" , rbac.Object {}, nil )
236
236
@@ -242,9 +242,14 @@ func (s *MethodTestSuite) NotAuthorizedErrorTest(ctx context.Context, az *coderd
242
242
// This is unfortunate, but if we are using `Filter` the error returned will be nil. So filter out
243
243
// any case where the error is nil and the response is an empty slice.
244
244
if err != nil || ! hasEmptySliceResponse (resp ) {
245
- s .ErrorContainsf (err , "unauthorized" , "error string should have a good message" )
246
- s .Errorf (err , "method should an error with disallow authz" )
247
- s .ErrorAs (err , & dbauthz.NotAuthorizedError {}, "error should be NotAuthorizedError" )
245
+ // Expect the default error
246
+ if testCase .notAuthorizedExpect == "" {
247
+ s .ErrorContainsf (err , "unauthorized" , "error string should have a good message" )
248
+ s .Errorf (err , "method should an error with disallow authz" )
249
+ s .ErrorAs (err , & dbauthz.NotAuthorizedError {}, "error should be NotAuthorizedError" )
250
+ } else {
251
+ s .ErrorContains (err , testCase .notAuthorizedExpect )
252
+ }
248
253
}
249
254
})
250
255
@@ -263,8 +268,13 @@ func (s *MethodTestSuite) NotAuthorizedErrorTest(ctx context.Context, az *coderd
263
268
// This is unfortunate, but if we are using `Filter` the error returned will be nil. So filter out
264
269
// any case where the error is nil and the response is an empty slice.
265
270
if err != nil || ! hasEmptySliceResponse (resp ) {
266
- s .Errorf (err , "method should an error with cancellation" )
267
- s .ErrorIsf (err , context .Canceled , "error should match context.Canceled" )
271
+ if testCase .cancelledCtxExpect == "" {
272
+ s .Errorf (err , "method should an error with cancellation" )
273
+ s .ErrorIsf (err , context .Canceled , "error should match context.Canceled" )
274
+ } else {
275
+ s .ErrorContains (err , testCase .cancelledCtxExpect )
276
+ }
277
+
268
278
}
269
279
})
270
280
}
@@ -308,6 +318,23 @@ type expects struct {
308
318
// outputs is optional. Can assert non-error return values.
309
319
outputs []reflect.Value
310
320
err error
321
+
322
+ // Optional override of the default error checks.
323
+ // By default, we search for the expected error strings.
324
+ // If these strings are present, these strings will be searched
325
+ // instead.
326
+ notAuthorizedExpect string
327
+ cancelledCtxExpect string
328
+ }
329
+
330
+ func (m * expects ) WithNotAuthorized (contains string ) * expects {
331
+ m .notAuthorizedExpect = contains
332
+ return m
333
+ }
334
+
335
+ func (m * expects ) WithCancelled (contains string ) * expects {
336
+ m .cancelledCtxExpect = contains
337
+ return m
311
338
}
312
339
313
340
// Asserts is required. Asserts the RBAC authorize calls that should be made.
0 commit comments