@@ -6,18 +6,17 @@ import (
6
6
"reflect"
7
7
"testing"
8
8
9
+ "github.com/google/uuid"
9
10
"github.com/stretchr/testify/require"
10
-
11
- "cdr.dev/slog/sloggers/slogtest"
12
-
13
11
"golang.org/x/xerrors"
14
12
15
- "github.com/google/uuid"
16
-
17
13
"cdr.dev/slog"
14
+ "cdr.dev/slog/sloggers/slogtest"
18
15
"github.com/coder/coder/coderd/authzquery"
19
16
"github.com/coder/coder/coderd/coderdtest"
17
+ "github.com/coder/coder/coderd/database"
20
18
"github.com/coder/coder/coderd/database/dbfake"
19
+ "github.com/coder/coder/coderd/database/dbgen"
21
20
"github.com/coder/coder/coderd/rbac"
22
21
)
23
22
@@ -36,6 +35,15 @@ func TestNotAuthorizedError(t *testing.T) {
36
35
require .ErrorAs (t , err , & authErr , "must be a NotAuthorizedError" )
37
36
require .ErrorIs (t , authErr .Err , testErr , "internal error must match" )
38
37
})
38
+
39
+ t .Run ("MissingActor" , func (t * testing.T ) {
40
+ q := authzquery .NewAuthzQuerier (dbfake .New (), & coderdtest.RecordingAuthorizer {
41
+ Wrapped : & coderdtest.FakeAuthorizer {AlwaysReturn : nil },
42
+ }, slog .Make ())
43
+ // This should fail because the actor is missing.
44
+ _ , err := q .GetWorkspaceByID (context .Background (), uuid .New ())
45
+ require .ErrorIs (t , err , authzquery .NoActorError , "must be a NoActorError" )
46
+ })
39
47
}
40
48
41
49
// TestAuthzQueryRecursive is a simple test to search for infinite recursion
@@ -72,6 +80,40 @@ func TestAuthzQueryRecursive(t *testing.T) {
72
80
}
73
81
}
74
82
83
+ func TestPing (t * testing.T ) {
84
+ t .Parallel ()
85
+
86
+ q := authzquery .NewAuthzQuerier (dbfake .New (), & coderdtest.RecordingAuthorizer {}, slog .Make ())
87
+ _ , err := q .Ping (context .Background ())
88
+ require .NoError (t , err , "must not error" )
89
+ }
90
+
91
+ // TestInTX is not perfect, just checks that it properly checks auth.
92
+ func TestInTX (t * testing.T ) {
93
+ t .Parallel ()
94
+
95
+ db := dbfake .New ()
96
+ q := authzquery .NewAuthzQuerier (db , & coderdtest.RecordingAuthorizer {
97
+ Wrapped : & coderdtest.FakeAuthorizer {AlwaysReturn : xerrors .New ("custom error" )},
98
+ }, slog .Make ())
99
+ actor := rbac.Subject {
100
+ ID : uuid .NewString (),
101
+ Roles : rbac.RoleNames {rbac .RoleOwner ()},
102
+ Groups : []string {},
103
+ Scope : rbac .ScopeAll ,
104
+ }
105
+
106
+ w := dbgen .Workspace (t , db , database.Workspace {})
107
+ ctx := authzquery .WithAuthorizeContext (context .Background (), actor )
108
+ err := q .InTx (func (tx database.Store ) error {
109
+ // The inner tx should use the parent's authz
110
+ _ , err := tx .GetWorkspaceByID (ctx , w .ID )
111
+ return err
112
+ }, nil )
113
+ require .Error (t , err , "must error" )
114
+ require .ErrorAs (t , err , & authzquery.NotAuthorizedError {}, "must be an authorized error" )
115
+ }
116
+
75
117
func must [T any ](value T , err error ) T {
76
118
if err != nil {
77
119
panic (err )
0 commit comments