@@ -2,12 +2,9 @@ package dbauthz_test
2
2
3
3
import (
4
4
"context"
5
- "database/sql"
6
5
"reflect"
7
6
"testing"
8
7
9
- "cdr.dev/slog/sloggers/slogtest"
10
-
11
8
"github.com/google/uuid"
12
9
"github.com/stretchr/testify/require"
13
10
"golang.org/x/xerrors"
@@ -55,31 +52,31 @@ func TestInTX(t *testing.T) {
55
52
require .ErrorAs (t , err , & dbauthz.NotAuthorizedError {}, "must be an authorized error" )
56
53
}
57
54
58
- func TestNotAuthorizedError (t * testing.T ) {
55
+ // TestNew should not double wrap a querier.
56
+ func TestNew (t * testing.T ) {
59
57
t .Parallel ()
60
58
61
- t .Run ("Is404" , func (t * testing.T ) {
62
- t .Parallel ()
63
-
64
- testErr := xerrors .New ("custom error" )
59
+ var (
60
+ db = dbfake .New ()
61
+ exp = dbgen .Workspace (t , db , database.Workspace {})
62
+ rec = & coderdtest.RecordingAuthorizer {
63
+ Wrapped : & coderdtest.FakeAuthorizer {AlwaysReturn : nil },
64
+ }
65
+ subj = rbac.Subject {}
66
+ ctx = dbauthz .WithAuthorizeContext (context .Background (), rbac.Subject {})
67
+ )
65
68
66
- err := dbauthz .logNotAuthorizedError (context .Background (), slogtest .Make (t , nil ), testErr )
67
- require .ErrorIs (t , err , sql .ErrNoRows , "must be a sql.ErrNoRows" )
69
+ // Double wrap should not cause an actual double wrap. So only 1 rbac call
70
+ // should be made.
71
+ az := dbauthz .New (db , rec , slog .Make ())
72
+ az = dbauthz .New (az , rec , slog .Make ())
68
73
69
- var authErr dbauthz.NotAuthorizedError
70
- require .ErrorAs (t , err , & authErr , "must be a NotAuthorizedError" )
71
- require .ErrorIs (t , authErr .Err , testErr , "internal error must match" )
72
- })
74
+ w , err := az .GetWorkspaceByID (ctx , exp .ID )
75
+ require .NoError (t , err , "must not error" )
76
+ require .Equal (t , exp , w , "must be equal" )
73
77
74
- t .Run ("MissingActor" , func (t * testing.T ) {
75
- t .Parallel ()
76
- q := dbauthz .New (dbfake .New (), & coderdtest.RecordingAuthorizer {
77
- Wrapped : & coderdtest.FakeAuthorizer {AlwaysReturn : nil },
78
- }, slog .Make ())
79
- // This should fail because the actor is missing.
80
- _ , err := q .GetWorkspaceByID (context .Background (), uuid .New ())
81
- require .ErrorIs (t , err , dbauthz .NoActorError , "must be a NoActorError" )
82
- })
78
+ rec .AssertActor (t , subj , rec .Pair (rbac .ActionRead , exp ))
79
+ require .NoError (t , rec .AllAsserted (), "should only be 1 rbac call" )
83
80
}
84
81
85
82
// TestDBAuthzRecursive is a simple test to search for infinite recursion
0 commit comments