|
| 1 | +package authzquery_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/coder/coder/coderd/database" |
| 8 | + "github.com/coder/coder/coderd/database/dbgen" |
| 9 | + "github.com/coder/coder/coderd/rbac" |
| 10 | +) |
| 11 | + |
| 12 | +func (suite *MethodTestSuite) TestAPIKey() { |
| 13 | + suite.Run("DeleteAPIKeyByID", func() { |
| 14 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 15 | + key, _ := dbgen.APIKey(t, db, database.APIKey{}) |
| 16 | + return methodCase(inputs(key.ID), asserts(key, rbac.ActionDelete)) |
| 17 | + }) |
| 18 | + }) |
| 19 | + suite.Run("GetAPIKeyByID", func() { |
| 20 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 21 | + key, _ := dbgen.APIKey(t, db, database.APIKey{}) |
| 22 | + return methodCase(inputs(key.ID), asserts(key, rbac.ActionRead)) |
| 23 | + }) |
| 24 | + }) |
| 25 | + suite.Run("GetAPIKeysByLoginType", func() { |
| 26 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 27 | + a, _ := dbgen.APIKey(t, db, database.APIKey{LoginType: database.LoginTypePassword}) |
| 28 | + b, _ := dbgen.APIKey(t, db, database.APIKey{LoginType: database.LoginTypePassword}) |
| 29 | + _, _ = dbgen.APIKey(t, db, database.APIKey{LoginType: database.LoginTypeGithub}) |
| 30 | + return methodCase(inputs(database.LoginTypePassword), asserts(a, rbac.ActionRead, b, rbac.ActionRead)) |
| 31 | + }) |
| 32 | + }) |
| 33 | + suite.Run("GetAPIKeysLastUsedAfter", func() { |
| 34 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 35 | + a, _ := dbgen.APIKey(t, db, database.APIKey{LastUsed: time.Now().Add(time.Hour)}) |
| 36 | + b, _ := dbgen.APIKey(t, db, database.APIKey{LastUsed: time.Now().Add(time.Hour)}) |
| 37 | + _, _ = dbgen.APIKey(t, db, database.APIKey{LastUsed: time.Now().Add(-time.Hour)}) |
| 38 | + return methodCase(inputs(time.Now()), asserts(a, rbac.ActionRead, b, rbac.ActionRead)) |
| 39 | + }) |
| 40 | + }) |
| 41 | + suite.Run("InsertAPIKey", func() { |
| 42 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 43 | + u := dbgen.User(t, db, database.User{}) |
| 44 | + return methodCase(inputs(database.InsertAPIKeyParams{ |
| 45 | + UserID: u.ID, |
| 46 | + LoginType: database.LoginTypePassword, |
| 47 | + Scope: database.APIKeyScopeAll, |
| 48 | + }), asserts(rbac.ResourceAPIKey.WithOwner(u.ID.String()), rbac.ActionCreate)) |
| 49 | + }) |
| 50 | + }) |
| 51 | + suite.Run("UpdateAPIKeyByID", func() { |
| 52 | + suite.RunMethodTest(func(t *testing.T, db database.Store) MethodCase { |
| 53 | + a, _ := dbgen.APIKey(t, db, database.APIKey{}) |
| 54 | + return methodCase(inputs(database.UpdateAPIKeyByIDParams{ |
| 55 | + ID: a.ID, |
| 56 | + LastUsed: time.Now(), |
| 57 | + }), asserts(a, rbac.ActionUpdate)) |
| 58 | + }) |
| 59 | + }) |
| 60 | +} |
0 commit comments