Skip to content

Commit 89b1b69

Browse files
committed
Add unit test to test manual role assign
1 parent 3fcb55d commit 89b1b69

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

enterprise/coderd/userauth_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// nolint:bodyclose
2626
func TestUserOIDC(t *testing.T) {
2727
t.Parallel()
28-
t.Run("Roles", func(t *testing.T) {
28+
t.Run("RoleSync", func(t *testing.T) {
2929
t.Parallel()
3030

3131
t.Run("NewUserAndRemoveRoles", func(t *testing.T) {
@@ -78,6 +78,44 @@ func TestUserOIDC(t *testing.T) {
7878

7979
require.Len(t, user.Roles, 0)
8080
})
81+
t.Run("BlockAssignRoles", func(t *testing.T) {
82+
t.Parallel()
83+
84+
ctx := testutil.Context(t, testutil.WaitMedium)
85+
conf := coderdtest.NewOIDCConfig(t, "")
86+
87+
config := conf.OIDCConfig(t, jwt.MapClaims{})
88+
config.AllowSignups = true
89+
config.UserRoleField = "roles"
90+
91+
client, _ := coderdenttest.New(t, &coderdenttest.Options{
92+
Options: &coderdtest.Options{
93+
OIDCConfig: config,
94+
},
95+
LicenseOptions: &coderdenttest.LicenseOptions{
96+
Features: license.Features{codersdk.FeatureTemplateRBAC: 1},
97+
},
98+
})
99+
100+
admin, err := client.User(ctx, "me")
101+
require.NoError(t, err)
102+
require.Len(t, admin.OrganizationIDs, 1)
103+
104+
resp := oidcCallback(t, client, conf.EncodeClaims(t, jwt.MapClaims{
105+
"email": "alice@coder.com",
106+
"roles": []string{},
107+
}))
108+
require.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
109+
// Try to manually update user roles, even though controlled by oidc
110+
// role sync.
111+
_, err = client.UpdateUserRoles(ctx, "alice", codersdk.UpdateRoles{
112+
Roles: []string{
113+
rbac.RoleTemplateAdmin(),
114+
},
115+
})
116+
require.Error(t, err)
117+
require.ErrorContains(t, err, "Cannot modify roles for OIDC users when role sync is enabled.")
118+
})
81119
})
82120

83121
t.Run("Groups", func(t *testing.T) {

0 commit comments

Comments
 (0)