MCPcopy Index your code
hub / github.com/coder/coder / TestGroupsAuth

Function TestGroupsAuth

coderd/database/dbauthz/groupsauth_test.go:22–166  ·  view source on GitHub ↗

nolint:tparallel

(t *testing.T)

Source from the content-addressed store, hash-verified

20
21// nolint:tparallel
22func TestGroupsAuth(t *testing.T) {
23 t.Parallel()
24
25 authz := rbac.NewAuthorizer(prometheus.NewRegistry())
26 store, _ := dbtestutil.NewDB(t)
27 db := dbauthz.New(store, authz, slogtest.Make(t, &slogtest.Options{
28 IgnoreErrors: true,
29 }), coderdtest.AccessControlStorePointer())
30
31 ownerCtx := dbauthz.As(context.Background(), rbac.Subject{
32 ID: "owner",
33 Roles: rbac.Roles(must(rbac.RoleIdentifiers{rbac.RoleOwner()}.Expand())),
34 Groups: []string{},
35 Scope: rbac.ExpandableScope(rbac.ScopeAll),
36 })
37
38 org := dbgen.Organization(t, db, database.Organization{})
39 group := dbgen.Group(t, db, database.Group{
40 OrganizationID: org.ID,
41 })
42
43 var users []database.User
44 for i := 0; i < 5; i++ {
45 user := dbgen.User(t, db, database.User{})
46 users = append(users, user)
47 err := db.InsertGroupMember(ownerCtx, database.InsertGroupMemberParams{
48 UserID: user.ID,
49 GroupID: group.ID,
50 })
51 require.NoError(t, err)
52 }
53
54 totalMembers := len(users)
55 testCases := []struct {
56 Name string
57 Subject rbac.Subject
58 ReadGroup bool
59 ReadMembers bool
60 MembersExpected int
61 }{
62 {
63 Name: "Owner",
64 Subject: rbac.Subject{
65 ID: "owner",
66 Roles: rbac.Roles(must(rbac.RoleIdentifiers{rbac.RoleOwner()}.Expand())),
67 Groups: []string{},
68 Scope: rbac.ExpandableScope(rbac.ScopeAll),
69 },
70 ReadGroup: true,
71 ReadMembers: true,
72 MembersExpected: totalMembers,
73 },
74 {
75 Name: "UserAdmin",
76 Subject: rbac.Subject{
77 ID: "useradmin",
78 Roles: rbac.Roles(must(rbac.RoleIdentifiers{rbac.RoleUserAdmin()}.Expand())),
79 Groups: []string{},

Callers

nothing calls this directly

Calls 15

NewAuthorizerFunction · 0.92
NewDBFunction · 0.92
NewFunction · 0.92
AsFunction · 0.92
RolesTypeAlias · 0.92
RoleOwnerFunction · 0.92
ExpandableScopeInterface · 0.92
OrganizationFunction · 0.92
GroupFunction · 0.92
UserFunction · 0.92
RoleUserAdminFunction · 0.92

Tested by

no test coverage detected