Skip to content

Commit f0bbaaf

Browse files
committed
Add unit test to cover prepareSQL error case
1 parent bbe4f18 commit f0bbaaf

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func fetchWithPostFilter[
369369
func prepareSQLFilter(ctx context.Context, authorizer rbac.Authorizer, action rbac.Action, resourceType string) (rbac.PreparedAuthorized, error) {
370370
act, ok := ActorFromContext(ctx)
371371
if !ok {
372-
return nil, xerrors.Errorf("no authorization actor in context")
372+
return nil, NoActorError
373373
}
374374

375375
return authorizer.Prepare(ctx, act, action, resourceType)

coderd/database/dbauthz/querier_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ func (s *MethodTestSuite) TestProvsionerJob() {
214214
_ = dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{JobID: j.ID, WorkspaceID: w.ID})
215215
check.Args(database.UpdateProvisionerJobWithCancelByIDParams{ID: j.ID}).Asserts(w, rbac.ActionUpdate).Returns()
216216
}))
217+
s.Run("BuildFalseCancel/UpdateProvisionerJobWithCancelByID", s.Subtest(func(db database.Store, check *expects) {
218+
tpl := dbgen.Template(s.T(), db, database.Template{AllowUserCancelWorkspaceJobs: false})
219+
w := dbgen.Workspace(s.T(), db, database.Workspace{TemplateID: tpl.ID})
220+
j := dbgen.ProvisionerJob(s.T(), db, database.ProvisionerJob{
221+
Type: database.ProvisionerJobTypeWorkspaceBuild,
222+
})
223+
_ = dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{JobID: j.ID, WorkspaceID: w.ID})
224+
check.Args(database.UpdateProvisionerJobWithCancelByIDParams{ID: j.ID}).Asserts(w, rbac.ActionUpdate).Returns()
225+
}))
217226
s.Run("TemplateVersion/UpdateProvisionerJobWithCancelByID", s.Subtest(func(db database.Store, check *expects) {
218227
j := dbgen.ProvisionerJob(s.T(), db, database.ProvisionerJob{
219228
Type: database.ProvisionerJobTypeTemplateVersionImport,

coderd/database/dbauthz/setup_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111

1212
"golang.org/x/xerrors"
1313

14-
"github.com/coder/coder/coderd/rbac/regosql"
15-
1614
"github.com/google/uuid"
1715
"github.com/stretchr/testify/require"
1816
"github.com/stretchr/testify/suite"
@@ -23,6 +21,8 @@ import (
2321
"github.com/coder/coder/coderd/database/dbauthz"
2422
"github.com/coder/coder/coderd/database/dbfake"
2523
"github.com/coder/coder/coderd/rbac"
24+
"github.com/coder/coder/coderd/rbac/regosql"
25+
"github.com/coder/coder/coderd/util/slice"
2626
)
2727

2828
var (
@@ -140,12 +140,21 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
140140

141141
require.NotNil(t, callMethod, "method %q does not exist", methodName)
142142

143-
// Run tests that are only run if the method makes rbac assertions.
144-
// These tests assert the error conditions of the method.
145143
if len(testCase.assertions) > 0 {
146144
// Only run these tests if we know the underlying call makes
147145
// rbac assertions.
148146
s.NotAuthorizedErrorTest(ctx, fakeAuthorizer, callMethod)
147+
}
148+
149+
if len(testCase.assertions) > 0 ||
150+
slice.Contains([]string{
151+
"GetAuthorizedWorkspaces",
152+
"GetAuthorizedTemplates",
153+
}, methodName) {
154+
155+
// Some methods do no make rbac assertions because they use
156+
// SQL. We still want to test that they return an error if the
157+
// actor is not set.
149158
s.NoActorErrorTest(callMethod)
150159
}
151160

coderd/database/dbgen/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Template(t testing.TB, db database.Store, seed database.Template) database.
6666
UserACL: seed.UserACL,
6767
GroupACL: seed.GroupACL,
6868
DisplayName: takeFirst(seed.DisplayName, namesgenerator.GetRandomName(1)),
69-
AllowUserCancelWorkspaceJobs: takeFirst(seed.AllowUserCancelWorkspaceJobs, true),
69+
AllowUserCancelWorkspaceJobs: seed.AllowUserCancelWorkspaceJobs,
7070
})
7171
require.NoError(t, err, "insert template")
7272
return template

0 commit comments

Comments
 (0)