@@ -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,12 @@ 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
+ }
268
277
}
269
278
})
270
279
}
@@ -308,6 +317,13 @@ type expects struct {
308
317
// outputs is optional. Can assert non-error return values.
309
318
outputs []reflect.Value
310
319
err error
320
+
321
+ // Optional override of the default error checks.
322
+ // By default, we search for the expected error strings.
323
+ // If these strings are present, these strings will be searched
324
+ // instead.
325
+ notAuthorizedExpect string
326
+ cancelledCtxExpect string
311
327
}
312
328
313
329
// Asserts is required. Asserts the RBAC authorize calls that should be made.
@@ -338,6 +354,16 @@ func (m *expects) Errors(err error) *expects {
338
354
return m
339
355
}
340
356
357
+ func (m * expects ) WithNotAuthorized (contains string ) * expects {
358
+ m .notAuthorizedExpect = contains
359
+ return m
360
+ }
361
+
362
+ func (m * expects ) WithCancelled (contains string ) * expects {
363
+ m .cancelledCtxExpect = contains
364
+ return m
365
+ }
366
+
341
367
// AssertRBAC contains the object and actions to be asserted.
342
368
type AssertRBAC struct {
343
369
Object rbac.Object
0 commit comments