mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
test: start migrating dbauthz tests to mocked db (#19257)
This PR adds a framework to move to a mocked db. And therefore massively speed up these tests.
This commit is contained in:
@@ -11,9 +11,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v7"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/mock/gomock"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
@@ -22,6 +24,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
@@ -204,14 +207,15 @@ func defaultIPAddress() pqtype.Inet {
|
||||
}
|
||||
|
||||
func (s *MethodTestSuite) TestAPIKey() {
|
||||
s.Run("DeleteAPIKeyByID", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
key, _ := dbgen.APIKey(s.T(), db, database.APIKey{})
|
||||
s.Run("DeleteAPIKeyByID", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
key := testutil.Fake(s.T(), faker, database.APIKey{})
|
||||
dbm.EXPECT().GetAPIKeyByID(gomock.Any(), key.ID).Return(key, nil).AnyTimes()
|
||||
dbm.EXPECT().DeleteAPIKeyByID(gomock.Any(), key.ID).Return(nil).AnyTimes()
|
||||
check.Args(key.ID).Asserts(key, policy.ActionDelete).Returns()
|
||||
}))
|
||||
s.Run("GetAPIKeyByID", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
key, _ := dbgen.APIKey(s.T(), db, database.APIKey{})
|
||||
s.Run("GetAPIKeyByID", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
key := testutil.Fake(s.T(), faker, database.APIKey{})
|
||||
dbm.EXPECT().GetAPIKeyByID(gomock.Any(), key.ID).Return(key, nil).AnyTimes()
|
||||
check.Args(key.ID).Asserts(key, policy.ActionRead).Returns(key)
|
||||
}))
|
||||
s.Run("GetAPIKeyByName", s.Subtest(func(db database.Store, check *expects) {
|
||||
@@ -234,14 +238,12 @@ func (s *MethodTestSuite) TestAPIKey() {
|
||||
Asserts(a, policy.ActionRead, b, policy.ActionRead).
|
||||
Returns(slice.New(a, b))
|
||||
}))
|
||||
s.Run("GetAPIKeysByUserID", s.Subtest(func(db database.Store, check *expects) {
|
||||
u1 := dbgen.User(s.T(), db, database.User{})
|
||||
u2 := dbgen.User(s.T(), db, database.User{})
|
||||
|
||||
keyA, _ := dbgen.APIKey(s.T(), db, database.APIKey{UserID: u1.ID, LoginType: database.LoginTypeToken, TokenName: "key-a"})
|
||||
keyB, _ := dbgen.APIKey(s.T(), db, database.APIKey{UserID: u1.ID, LoginType: database.LoginTypeToken, TokenName: "key-b"})
|
||||
_, _ = dbgen.APIKey(s.T(), db, database.APIKey{UserID: u2.ID, LoginType: database.LoginTypeToken})
|
||||
s.Run("GetAPIKeysByUserID", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
u1 := testutil.Fake(s.T(), faker, database.User{})
|
||||
keyA := testutil.Fake(s.T(), faker, database.APIKey{UserID: u1.ID, LoginType: database.LoginTypeToken, TokenName: "key-a"})
|
||||
keyB := testutil.Fake(s.T(), faker, database.APIKey{UserID: u1.ID, LoginType: database.LoginTypeToken, TokenName: "key-b"})
|
||||
|
||||
dbm.EXPECT().GetAPIKeysByUserID(gomock.Any(), gomock.Any()).Return(slice.New(keyA, keyB), nil).AnyTimes()
|
||||
check.Args(database.GetAPIKeysByUserIDParams{LoginType: database.LoginTypeToken, UserID: u1.ID}).
|
||||
Asserts(keyA, policy.ActionRead, keyB, policy.ActionRead).
|
||||
Returns(slice.New(keyA, keyB))
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v7"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/google/uuid"
|
||||
@@ -20,7 +21,6 @@ import (
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/coderdtest"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/coder/v2/coderd/rbac/regosql"
|
||||
"github.com/coder/coder/v2/coderd/util/slice"
|
||||
)
|
||||
@@ -105,11 +106,37 @@ func (s *MethodTestSuite) TearDownSuite() {
|
||||
|
||||
var testActorID = uuid.New()
|
||||
|
||||
// Subtest is a helper function that returns a function that can be passed to
|
||||
// Mocked runs a subtest with a mocked database. Removing the overhead of a real
|
||||
// postgres database resulting in much faster tests.
|
||||
func (s *MethodTestSuite) Mocked(testCaseF func(dmb *dbmock.MockStore, faker *gofakeit.Faker, check *expects)) func() {
|
||||
t := s.T()
|
||||
mDB := dbmock.NewMockStore(gomock.NewController(t))
|
||||
mDB.EXPECT().Wrappers().Return([]string{}).AnyTimes()
|
||||
|
||||
// Use a constant seed to prevent flakes from random data generation.
|
||||
faker := gofakeit.New(0)
|
||||
|
||||
// The usual Subtest assumes the test setup will use a real database to populate
|
||||
// with data. In this mocked case, we want to pass the underlying mocked database
|
||||
// to the test case instead.
|
||||
return s.SubtestWithDB(mDB, func(_ database.Store, check *expects) {
|
||||
testCaseF(mDB, faker, check)
|
||||
})
|
||||
}
|
||||
|
||||
// Subtest starts up a real postgres database for each test case.
|
||||
// Deprecated: Use 'Mocked' instead for much faster tests.
|
||||
func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expects)) func() {
|
||||
t := s.T()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
return s.SubtestWithDB(db, testCaseF)
|
||||
}
|
||||
|
||||
// SubtestWithDB is a helper function that returns a function that can be passed to
|
||||
// s.Run(). This function will run the test case for the method that is being
|
||||
// tested. The check parameter is used to assert the results of the method.
|
||||
// If the caller does not use the `check` parameter, the test will fail.
|
||||
func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expects)) func() {
|
||||
func (s *MethodTestSuite) SubtestWithDB(db database.Store, testCaseF func(db database.Store, check *expects)) func() {
|
||||
return func() {
|
||||
t := s.T()
|
||||
testName := s.T().Name()
|
||||
@@ -117,7 +144,6 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
|
||||
methodName := names[len(names)-1]
|
||||
s.methodAccounting[methodName]++
|
||||
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
fakeAuthorizer := &coderdtest.FakeAuthorizer{}
|
||||
rec := &coderdtest.RecordingAuthorizer{
|
||||
Wrapped: fakeAuthorizer,
|
||||
|
||||
Reference in New Issue
Block a user