chore: enforcement of dbauthz tests was broken (#11218)

* chore: enforcement of dbauthz tests was broken

Implemented missing tests to catch back up

---------

Co-authored-by: Cian Johnston <cian@coder.com>
This commit is contained in:
Steven Masley
2023-12-15 12:30:21 -06:00
committed by GitHub
parent 0801760956
commit e63de9a259
4 changed files with 509 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package dbauthz_test
import (
"context"
"errors"
"fmt"
"reflect"
"sort"
@ -27,10 +28,14 @@ import (
"github.com/coder/coder/v2/coderd/util/slice"
)
var errMatchAny = errors.New("match any error")
var skipMethods = map[string]string{
"InTx": "Not relevant",
"Ping": "Not relevant",
"Wrappers": "Not relevant",
"InTx": "Not relevant",
"Ping": "Not relevant",
"Wrappers": "Not relevant",
"AcquireLock": "Not relevant",
"TryAcquireLock": "Not relevant",
}
// TestMethodTestSuite runs MethodTestSuite.
@ -62,7 +67,8 @@ func (s *MethodTestSuite) SetupSuite() {
mockStore.EXPECT().Wrappers().Return([]string{}).AnyTimes()
az := dbauthz.New(mockStore, nil, slog.Make(), coderdtest.AccessControlStorePointer())
// Take the underlying type of the interface.
azt := reflect.TypeOf(az).Elem()
azt := reflect.TypeOf(az)
require.Greater(s.T(), azt.NumMethod(), 0, "no methods found on querier")
s.methodAccounting = make(map[string]int)
for i := 0; i < azt.NumMethod(); i++ {
method := azt.Method(i)
@ -168,7 +174,16 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
fakeAuthorizer.AlwaysReturn = nil
outputs, err := callMethod(ctx)
s.NoError(err, "method %q returned an error", methodName)
if testCase.err == nil {
s.NoError(err, "method %q returned an error", methodName)
} else {
if errors.Is(testCase.err, errMatchAny) {
// This means we do not care exactly what the error is.
s.Error(err, "method %q returned an error", methodName)
} else {
s.EqualError(err, testCase.err.Error(), "method %q returned an unexpected error", methodName)
}
}
// Some tests may not care about the outputs, so we only assert if
// they are provided.
@ -289,6 +304,7 @@ type expects struct {
assertions []AssertRBAC
// outputs is optional. Can assert non-error return values.
outputs []reflect.Value
err error
}
// Asserts is required. Asserts the RBAC authorize calls that should be made.
@ -313,6 +329,12 @@ func (m *expects) Returns(rets ...any) *expects {
return m
}
// Errors is optional. If it is never called, it will not be asserted.
func (m *expects) Errors(err error) *expects {
m.err = err
return m
}
// AssertRBAC contains the object and actions to be asserted.
type AssertRBAC struct {
Object rbac.Object