@@ -8,20 +8,12 @@ import (
8
8
"github.com/coder/coder/coderd/coderdtest"
9
9
"github.com/coder/coder/coderd/rbac"
10
10
"github.com/coder/coder/codersdk"
11
- "github.com/google/uuid"
12
11
"github.com/stretchr/testify/require"
13
12
)
14
13
15
14
func TestListRoles (t * testing.T ) {
16
15
t .Parallel ()
17
16
18
- requireUnauthorized := func (t * testing.T , err error ) {
19
- var apiErr * codersdk.Error
20
- require .ErrorAs (t , err , & apiErr )
21
- require .Equal (t , http .StatusUnauthorized , apiErr .StatusCode ())
22
- require .Contains (t , apiErr .Message , "unauthorized" )
23
- }
24
-
25
17
ctx := context .Background ()
26
18
client := coderdtest .New (t , nil )
27
19
// Create admin, member, and org admin
@@ -41,73 +33,78 @@ func TestListRoles(t *testing.T) {
41
33
)
42
34
require .NoError (t , err , "update org member roles" )
43
35
36
+ otherOrg , err := client .CreateOrganization (ctx , admin .UserID , codersdk.CreateOrganizationRequest {
37
+ Name : "other" ,
38
+ })
39
+ require .NoError (t , err , "create org" )
40
+
41
+ const unauth = "unauthorized"
42
+ const notMember = "not a member of the organization"
43
+
44
44
testCases := []struct {
45
- Name string
46
- Client * codersdk.Client
47
- APICall func () ([]string , error )
48
- ExpectedRoles []string
49
- Authorized bool
45
+ Name string
46
+ Client * codersdk.Client
47
+ APICall func () ([]string , error )
48
+ ExpectedRoles []string
49
+ AuthorizedError string
50
50
}{
51
51
{
52
52
Name : "MemberListSite" ,
53
53
APICall : func () ([]string , error ) {
54
54
x , err := member .ListSiteRoles (ctx )
55
55
return x , err
56
56
},
57
- Authorized : false ,
57
+ AuthorizedError : unauth ,
58
58
},
59
59
{
60
60
Name : "OrgMemberListOrg" ,
61
61
APICall : func () ([]string , error ) {
62
62
return member .ListOrganizationRoles (ctx , admin .OrganizationID )
63
63
},
64
- Authorized : false ,
64
+ AuthorizedError : unauth ,
65
65
},
66
66
{
67
67
Name : "NonOrgMemberListOrg" ,
68
68
APICall : func () ([]string , error ) {
69
- return member .ListOrganizationRoles (ctx , uuid . New () )
69
+ return member .ListOrganizationRoles (ctx , otherOrg . ID )
70
70
},
71
- Authorized : false ,
71
+ AuthorizedError : notMember ,
72
72
},
73
73
// Org admin
74
74
{
75
75
Name : "OrgAdminListSite" ,
76
76
APICall : func () ([]string , error ) {
77
77
return orgAdmin .ListSiteRoles (ctx )
78
78
},
79
- Authorized : false ,
79
+ AuthorizedError : unauth ,
80
80
},
81
81
{
82
82
Name : "OrgAdminListOrg" ,
83
83
APICall : func () ([]string , error ) {
84
84
return orgAdmin .ListOrganizationRoles (ctx , admin .OrganizationID )
85
85
},
86
- Authorized : true ,
87
86
ExpectedRoles : rbac .OrganizationRoles (admin .OrganizationID ),
88
87
},
89
88
{
90
89
Name : "OrgAdminListOtherOrg" ,
91
90
APICall : func () ([]string , error ) {
92
- return orgAdmin .ListOrganizationRoles (ctx , uuid . New () )
91
+ return orgAdmin .ListOrganizationRoles (ctx , otherOrg . ID )
93
92
},
94
- Authorized : false ,
93
+ AuthorizedError : notMember ,
95
94
},
96
95
// Admin
97
96
{
98
97
Name : "AdminListSite" ,
99
98
APICall : func () ([]string , error ) {
100
99
return client .ListSiteRoles (ctx )
101
100
},
102
- Authorized : true ,
103
101
ExpectedRoles : rbac .SiteRoles (),
104
102
},
105
103
{
106
104
Name : "AdminListOrg" ,
107
105
APICall : func () ([]string , error ) {
108
106
return client .ListOrganizationRoles (ctx , admin .OrganizationID )
109
107
},
110
- Authorized : true ,
111
108
ExpectedRoles : rbac .OrganizationRoles (admin .OrganizationID ),
112
109
},
113
110
}
@@ -117,8 +114,11 @@ func TestListRoles(t *testing.T) {
117
114
t .Run (c .Name , func (t * testing.T ) {
118
115
t .Parallel ()
119
116
roles , err := c .APICall ()
120
- if ! c .Authorized {
121
- requireUnauthorized (t , err )
117
+ if c .AuthorizedError != "" {
118
+ var apiErr * codersdk.Error
119
+ require .ErrorAs (t , err , & apiErr )
120
+ require .Equal (t , http .StatusUnauthorized , apiErr .StatusCode ())
121
+ require .Contains (t , apiErr .Message , c .AuthorizedError )
122
122
} else {
123
123
require .NoError (t , err )
124
124
require .Equal (t , c .ExpectedRoles , roles )
0 commit comments