TestNoopNoDiff verifies if no role change occurs, no database call is taken per organization. This limits the number of db calls to O(1) if there are no changes. Which is the usual case, as user's roles do not change often.
(t *testing.T)
| 293 | // per organization. This limits the number of db calls to O(1) if there |
| 294 | // are no changes. Which is the usual case, as user's roles do not change often. |
| 295 | func TestNoopNoDiff(t *testing.T) { |
| 296 | t.Parallel() |
| 297 | |
| 298 | ctx := context.Background() |
| 299 | ctrl := gomock.NewController(t) |
| 300 | mDB := dbmock.NewMockStore(ctrl) |
| 301 | |
| 302 | mgr := runtimeconfig.NewManager() |
| 303 | s := idpsync.NewAGPLSync(slogtest.Make(t, &slogtest.Options{}), mgr, idpsync.DeploymentSyncSettings{ |
| 304 | SiteRoleField: "", |
| 305 | SiteRoleMapping: nil, |
| 306 | SiteDefaultRoles: nil, |
| 307 | }) |
| 308 | |
| 309 | userID := uuid.New() |
| 310 | orgID := uuid.New() |
| 311 | siteRoles := []string{rbac.RoleTemplateAdmin().Name, rbac.RoleAuditor().Name} |
| 312 | orgRoles := []string{rbac.RoleOrgAuditor(), rbac.RoleOrgAdmin()} |
| 313 | // The DB mock expects. |
| 314 | // If this test fails, feel free to add more expectations. |
| 315 | // The primary expectations to avoid is 'UpdateUserRoles' |
| 316 | // and 'UpdateMemberRoles'. |
| 317 | mDB.EXPECT().InTx( |
| 318 | gomock.Any(), gomock.Any(), |
| 319 | ).DoAndReturn(func(f func(database.Store) error, _ *database.TxOptions) error { |
| 320 | err := f(mDB) |
| 321 | return err |
| 322 | }) |
| 323 | |
| 324 | mDB.EXPECT().OrganizationMembers(gomock.Any(), database.OrganizationMembersParams{ |
| 325 | UserID: userID, |
| 326 | }).Return([]database.OrganizationMembersRow{ |
| 327 | { |
| 328 | OrganizationMember: database.OrganizationMember{ |
| 329 | UserID: userID, |
| 330 | OrganizationID: orgID, |
| 331 | Roles: orgRoles, |
| 332 | }, |
| 333 | }, |
| 334 | }, nil) |
| 335 | |
| 336 | mDB.EXPECT().GetRuntimeConfig(gomock.Any(), gomock.Any()).Return( |
| 337 | string(must(json.Marshal(idpsync.RoleSyncSettings{ |
| 338 | Field: "roles", |
| 339 | Mapping: nil, |
| 340 | }))), nil) |
| 341 | |
| 342 | err := s.SyncRoles(ctx, mDB, database.User{ |
| 343 | ID: userID, |
| 344 | Email: "alice@email.com", |
| 345 | Username: "alice", |
| 346 | Status: database.UserStatusActive, |
| 347 | RBACRoles: siteRoles, |
| 348 | LoginType: database.LoginTypePassword, |
| 349 | }, idpsync.RoleParams{ |
| 350 | SyncEntitled: true, |
| 351 | SyncSiteWide: true, |
| 352 | SiteWideRoles: siteRoles, |
nothing calls this directly
no test coverage detected