Skip to content

Commit d37379d

Browse files
committed
Merge remote-tracking branch 'origin/authzquerier_layer' into authzquerier_layer
2 parents 9f7d276 + 300f6dc commit d37379d

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

coderd/authzquery/methods_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,44 @@ import (
2020
"github.com/coder/coder/coderd/rbac"
2121
)
2222

23+
var (
24+
skipMethods = map[string]any{
25+
"InTx": struct{}{},
26+
"Ping": struct{}{},
27+
}
28+
)
29+
2330
// Define the suite, and absorb the built-in basic suite
2431
// functionality from testify - including a T() method which
2532
// returns the current testing context
2633
type MethodTestSuite struct {
2734
suite.Suite
35+
// methodAccounting counts all methods called by a 'RunMethodTest'
36+
methodAccounting map[string]int
2837
}
2938

30-
func (suite *MethodTestSuite) SetupTest() {
39+
func (suite *MethodTestSuite) SetupSuite() {
40+
az := &authzquery.AuthzQuerier{}
41+
azt := reflect.TypeOf(az)
42+
suite.methodAccounting = make(map[string]int)
43+
for i := 0; i < azt.NumMethod(); i++ {
44+
method := azt.Method(i)
45+
if _, ok := skipMethods[method.Name]; ok {
46+
continue
47+
}
48+
suite.methodAccounting[method.Name] = 0
49+
}
3150
}
3251

33-
func (suite *MethodTestSuite) TearDownTest() {
52+
func (suite *MethodTestSuite) TearDownSuite() {
53+
suite.Run("Accounting", func() {
54+
t := suite.T()
55+
for m, c := range suite.methodAccounting {
56+
if c <= 0 {
57+
t.Errorf("Method %q never called", m)
58+
}
59+
}
60+
})
3461
}
3562

3663
// In order for 'go test' to run this suite, we need to create
@@ -49,10 +76,12 @@ type AssertRBAC struct {
4976
Actions []rbac.Action
5077
}
5178

52-
func (suite *MethodTestSuite) RunMethodTest(t *testing.T, testCaseF func(t *testing.T, db database.Store) MethodCase) {
79+
func (suite *MethodTestSuite) RunMethodTest(testCaseF func(t *testing.T, db database.Store) MethodCase) {
80+
t := suite.T()
5381
testName := suite.T().Name()
5482
names := strings.Split(testName, "/")
5583
methodName := names[len(names)-1]
84+
suite.methodAccounting[methodName]++
5685

5786
db := databasefake.New()
5887
rec := &coderdtest.RecordingAuthorizer{

coderd/authzquery/template_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package authzquery_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/coder/coderd/database"
7+
"github.com/coder/coder/coderd/database/dbgen"
8+
"github.com/coder/coder/coderd/rbac"
9+
)
10+
11+
func (suite *MethodTestSuite) TestTemplate() {
12+
suite.Run("GetTemplateByID", func() {
13+
suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase {
14+
obj := dbgen.Template(t, db, database.Template{})
15+
return MethodCase{
16+
Inputs: methodInputs(obj.ID),
17+
Assertions: asserts(obj, rbac.ActionRead),
18+
}
19+
})
20+
})
21+
}

coderd/authzquery/workspace_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
)
1010

1111
func (suite *MethodTestSuite) TestWorkspace() {
12-
t := suite.T()
1312
suite.Run("GetWorkspaceByID", func() {
14-
suite.RunMethodTest(t, func(t *testing.T, db database.Store) MethodCase {
13+
suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase {
1514
workspace := dbgen.Workspace(t, db, database.Workspace{})
1615
return MethodCase{
1716
Inputs: methodInputs(workspace.ID),

0 commit comments

Comments
 (0)