SubtestWithDB is a helper function that returns a function that can be passed to s.Run(). This function will run the test case for the method that is being tested. The check parameter is used to assert the results of the method. If the caller does not use the `check` parameter, the test will fail.
(db database.Store, testCaseF func(db database.Store, check *expects))
| 187 | // tested. The check parameter is used to assert the results of the method. |
| 188 | // If the caller does not use the `check` parameter, the test will fail. |
| 189 | func (s *MethodTestSuite) SubtestWithDB(db database.Store, testCaseF func(db database.Store, check *expects)) func() { |
| 190 | return func() { |
| 191 | t := s.T() |
| 192 | testName := s.T().Name() |
| 193 | names := strings.Split(testName, "/") |
| 194 | methodName := names[len(names)-1] |
| 195 | s.methodAccounting[methodName]++ |
| 196 | |
| 197 | fakeAuthorizer := &coderdtest.FakeAuthorizer{} |
| 198 | rec := &coderdtest.RecordingAuthorizer{ |
| 199 | Wrapped: fakeAuthorizer, |
| 200 | } |
| 201 | az := dbauthz.New(db, rec, slog.Make(), coderdtest.AccessControlStorePointer()) |
| 202 | actor := rbac.Subject{ |
| 203 | ID: testActorID.String(), |
| 204 | Roles: rbac.RoleIdentifiers{rbac.RoleOwner()}, |
| 205 | Groups: []string{}, |
| 206 | Scope: rbac.ScopeAll, |
| 207 | } |
| 208 | ctx := dbauthz.As(context.Background(), actor) |
| 209 | |
| 210 | var testCase expects |
| 211 | testCaseF(db, &testCase) |
| 212 | // Check the developer added assertions. If there are no assertions, |
| 213 | // an empty list should be passed. |
| 214 | s.Require().False(testCase.assertions == nil, "rbac assertions not set, use the 'check' parameter") |
| 215 | |
| 216 | // Find the method with the name of the test. |
| 217 | var callMethod func(ctx context.Context) ([]reflect.Value, error) |
| 218 | azt := reflect.TypeOf(az) |
| 219 | MethodLoop: |
| 220 | for i := 0; i < azt.NumMethod(); i++ { |
| 221 | method := azt.Method(i) |
| 222 | if method.Name == methodName { |
| 223 | methodF := reflect.ValueOf(az).Method(i) |
| 224 | |
| 225 | callMethod = func(ctx context.Context) ([]reflect.Value, error) { |
| 226 | resp := methodF.Call(append([]reflect.Value{reflect.ValueOf(ctx)}, testCase.inputs...)) |
| 227 | return splitResp(t, resp) |
| 228 | } |
| 229 | break MethodLoop |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | require.NotNil(t, callMethod, "method %q does not exist", methodName) |
| 234 | |
| 235 | if len(testCase.assertions) > 0 { |
| 236 | // Only run these tests if we know the underlying call makes |
| 237 | // rbac assertions. |
| 238 | s.NotAuthorizedErrorTest(ctx, fakeAuthorizer, testCase, callMethod) |
| 239 | } |
| 240 | |
| 241 | if len(testCase.assertions) > 0 || |
| 242 | slice.Contains([]string{ |
| 243 | "GetAuthorizedWorkspaces", |
| 244 | "GetAuthorizedTemplates", |
| 245 | "GetDefaultChatModelConfig", |
| 246 | }, methodName) { |
no test coverage detected