Skip to content

Commit d6c0d2b

Browse files
committed
Add unit test
1 parent a79bb89 commit d6c0d2b

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

coderd/database/dbgen/dbgen.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"net"
11+
"strings"
1112
"testing"
1213
"time"
1314

@@ -817,6 +818,19 @@ func OAuth2ProviderAppToken(t testing.TB, db database.Store, seed database.OAuth
817818
return token
818819
}
819820

821+
func CustomRole(t testing.TB, db database.Store, seed database.CustomRole) database.CustomRole {
822+
role, err := db.UpsertCustomRole(genCtx, database.UpsertCustomRoleParams{
823+
Name: takeFirst(seed.Name, strings.ToLower(namesgenerator.GetRandomName(1))),
824+
DisplayName: namesgenerator.GetRandomName(1),
825+
OrganizationID: seed.OrganizationID,
826+
SitePermissions: takeFirstSlice(seed.SitePermissions, []byte("[]")),
827+
OrgPermissions: takeFirstSlice(seed.SitePermissions, []byte("{}")),
828+
UserPermissions: takeFirstSlice(seed.SitePermissions, []byte("[]")),
829+
})
830+
require.NoError(t, err, "insert custom role")
831+
return role
832+
}
833+
820834
func must[V any](v V, err error) V {
821835
if err != nil {
822836
panic(err)

coderd/database/dbmem/dbmem.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,10 @@ func (q *FakeQuerier) CustomRoles(_ context.Context, arg database.CustomRolesPar
11971197
continue
11981198
}
11991199

1200+
if arg.OrganizationID != uuid.Nil && role.OrganizationID.UUID != arg.OrganizationID {
1201+
continue
1202+
}
1203+
12001204
found = append(found, role)
12011205
}
12021206

@@ -8377,6 +8381,7 @@ func (q *FakeQuerier) UpsertCustomRole(_ context.Context, arg database.UpsertCus
83778381
for i := range q.customRoles {
83788382
if strings.EqualFold(q.customRoles[i].Name, arg.Name) {
83798383
q.customRoles[i].DisplayName = arg.DisplayName
8384+
q.customRoles[i].OrganizationID = arg.OrganizationID
83808385
q.customRoles[i].SitePermissions = arg.SitePermissions
83818386
q.customRoles[i].OrgPermissions = arg.OrgPermissions
83828387
q.customRoles[i].UserPermissions = arg.UserPermissions
@@ -8388,6 +8393,7 @@ func (q *FakeQuerier) UpsertCustomRole(_ context.Context, arg database.UpsertCus
83888393
role := database.CustomRole{
83898394
Name: arg.Name,
83908395
DisplayName: arg.DisplayName,
8396+
OrganizationID: arg.OrganizationID,
83918397
SitePermissions: arg.SitePermissions,
83928398
OrgPermissions: arg.OrgPermissions,
83938399
UserPermissions: arg.UserPermissions,

coderd/roles_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package coderd_test
33
import (
44
"context"
55
"net/http"
6+
"slices"
67
"testing"
78

89
"github.com/stretchr/testify/require"
910

1011
"github.com/coder/coder/v2/coderd/coderdtest"
1112
"github.com/coder/coder/v2/coderd/database/db2sdk"
13+
"github.com/coder/coder/v2/coderd/database/dbgen"
1214
"github.com/coder/coder/v2/coderd/rbac"
15+
"github.com/coder/coder/v2/coderd/rbac/policy"
16+
"github.com/coder/coder/v2/coderd/rbac/rolestore"
1317
"github.com/coder/coder/v2/codersdk"
1418
"github.com/coder/coder/v2/testutil"
1519
)
@@ -156,6 +160,41 @@ func TestListRoles(t *testing.T) {
156160
}
157161
}
158162

163+
func TestListCustomRoles(t *testing.T) {
164+
t.Parallel()
165+
166+
t.Run("Organizations", func(t *testing.T) {
167+
client, db := coderdtest.NewWithDatabase(t, nil)
168+
owner := coderdtest.CreateFirstUser(t, client)
169+
170+
const roleName = "random_role"
171+
dbgen.CustomRole(t, db, must(rolestore.ConvertRoleToDB(rbac.Role{
172+
Name: rbac.RoleName(roleName, owner.OrganizationID.String()),
173+
DisplayName: "Random Role",
174+
Site: nil,
175+
Org: map[string][]rbac.Permission{
176+
owner.OrganizationID.String(): {
177+
{
178+
Negate: false,
179+
ResourceType: rbac.ResourceWorkspace.Type,
180+
Action: policy.ActionRead,
181+
},
182+
},
183+
},
184+
User: nil,
185+
})))
186+
187+
ctx := testutil.Context(t, testutil.WaitShort)
188+
roles, err := client.ListOrganizationRoles(ctx, owner.OrganizationID)
189+
require.NoError(t, err)
190+
191+
found := slices.ContainsFunc(roles, func(element codersdk.AssignableRoles) bool {
192+
return element.Name == roleName && element.OrganizationID == owner.OrganizationID.String()
193+
})
194+
require.Truef(t, found, "custom organization role listed")
195+
})
196+
}
197+
159198
func convertRole(roleName string) codersdk.Role {
160199
role, _ := rbac.RoleByName(roleName)
161200
return db2sdk.Role(role)

0 commit comments

Comments
 (0)