@@ -16,6 +16,7 @@ import (
16
16
"github.com/coder/coder/buildinfo"
17
17
"github.com/coder/coder/coderd/coderdtest"
18
18
"github.com/coder/coder/coderd/rbac"
19
+ "github.com/coder/coder/codersdk"
19
20
)
20
21
21
22
func TestMain (m * testing.M ) {
@@ -34,6 +35,7 @@ func TestBuildInfo(t *testing.T) {
34
35
// TestAuthorizeAllEndpoints will check `authorize` is called on every endpoint registered.
35
36
func TestAuthorizeAllEndpoints (t * testing.T ) {
36
37
t .Parallel ()
38
+ ctx := context .Background ()
37
39
38
40
authorizer := & fakeAuthorizer {}
39
41
srv ,client ,_ := coderdtest .NewWithServer (t ,& coderdtest.Options {
@@ -50,6 +52,8 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
50
52
template := coderdtest .CreateTemplate (t ,client ,admin .OrganizationID ,version .ID )
51
53
workspace := coderdtest .CreateWorkspace (t ,client ,admin .OrganizationID ,template .ID )
52
54
coderdtest .AwaitWorkspaceBuildJob (t ,client ,workspace .LatestBuild .ID )
55
+ file ,err := client .Upload (ctx ,codersdk .ContentTypeTar ,make ([]byte ,1024 ))
56
+ require .NoError (t ,err ,"upload file" )
53
57
54
58
// Always fail auth from this point forward
55
59
authorizer .AlwaysReturn = rbac .ForbiddenWithInternal (xerrors .New ("fake implementation" ),nil ,nil )
@@ -121,8 +125,6 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
121
125
122
126
"POST:/api/v2/users/{user}/organizations" : {NoAuthorize :true },
123
127
124
- "POST:/api/v2/files" : {NoAuthorize :true },
125
- "GET:/api/v2/files/{hash}" : {NoAuthorize :true },
126
128
"GET:/api/v2/workspaces/{workspace}/watch" : {NoAuthorize :true },
127
129
128
130
// These endpoints have more assertions. This is good, add more endpoints to assert if you can!
@@ -184,6 +186,10 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
184
186
AssertObject :workspaceRBACObj ,
185
187
},
186
188
189
+ "POST:/api/v2/files" : {AssertAction :rbac .ActionCreate ,AssertObject :rbac .ResourceFile },
190
+ "GET:/api/v2/files/{fileHash}" : {AssertAction :rbac .ActionRead ,
191
+ AssertObject :rbac .ResourceFile .WithOwner (admin .UserID .String ()).WithID (file .Hash )},
192
+
187
193
// These endpoints need payloads to get to the auth part. Payloads will be required
188
194
"PUT:/api/v2/users/{user}/roles" : {StatusCode :http .StatusBadRequest ,NoAuthorize :true },
189
195
"POST:/api/v2/workspaces/{workspace}/builds" : {StatusCode :http .StatusBadRequest ,NoAuthorize :true },
@@ -220,6 +226,7 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
220
226
route = strings .ReplaceAll (route ,"{workspacebuild}" ,workspace .LatestBuild .ID .String ())
221
227
route = strings .ReplaceAll (route ,"{workspacename}" ,workspace .Name )
222
228
route = strings .ReplaceAll (route ,"{workspacebuildname}" ,workspace .LatestBuild .Name )
229
+ route = strings .ReplaceAll (route ,"{hash}" ,file .Hash )
223
230
224
231
resp ,err := client .Request (context .Background (),method ,route ,nil )
225
232
require .NoError (t ,err ,"do req" )