@@ -218,25 +218,16 @@ func (s *MethodTestSuite) TestAPIKey() {
218
218
dbm .EXPECT ().GetAPIKeyByID (gomock .Any (),key .ID ).Return (key ,nil ).AnyTimes ()
219
219
check .Args (key .ID ).Asserts (key ,policy .ActionRead ).Returns (key )
220
220
}))
221
- s .Run ("GetAPIKeyByName" ,s .Subtest (func (db database.Store ,check * expects ) {
222
- dbtestutil .DisableForeignKeysAndTriggers (s .T (),db )
223
- key ,_ := dbgen .APIKey (s .T (),db , database.APIKey {
224
- TokenName :"marge-cat" ,
225
- LoginType :database .LoginTypeToken ,
226
- })
227
- check .Args (database.GetAPIKeyByNameParams {
228
- TokenName :key .TokenName ,
229
- UserID :key .UserID ,
230
- }).Asserts (key ,policy .ActionRead ).Returns (key )
221
+ s .Run ("GetAPIKeyByName" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
222
+ key := testutil .Fake (s .T (),faker , database.APIKey {LoginType :database .LoginTypeToken ,TokenName :"marge-cat" })
223
+ dbm .EXPECT ().GetAPIKeyByName (gomock .Any (), database.GetAPIKeyByNameParams {TokenName :key .TokenName ,UserID :key .UserID }).Return (key ,nil ).AnyTimes ()
224
+ check .Args (database.GetAPIKeyByNameParams {TokenName :key .TokenName ,UserID :key .UserID }).Asserts (key ,policy .ActionRead ).Returns (key )
231
225
}))
232
- s .Run ("GetAPIKeysByLoginType" ,s .Subtest (func (db database.Store ,check * expects ) {
233
- dbtestutil .DisableForeignKeysAndTriggers (s .T (),db )
234
- a ,_ := dbgen .APIKey (s .T (),db , database.APIKey {LoginType :database .LoginTypePassword })
235
- b ,_ := dbgen .APIKey (s .T (),db , database.APIKey {LoginType :database .LoginTypePassword })
236
- _ ,_ = dbgen .APIKey (s .T (),db , database.APIKey {LoginType :database .LoginTypeGithub })
237
- check .Args (database .LoginTypePassword ).
238
- Asserts (a ,policy .ActionRead ,b ,policy .ActionRead ).
239
- Returns (slice .New (a ,b ))
226
+ s .Run ("GetAPIKeysByLoginType" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
227
+ a := testutil .Fake (s .T (),faker , database.APIKey {LoginType :database .LoginTypePassword })
228
+ b := testutil .Fake (s .T (),faker , database.APIKey {LoginType :database .LoginTypePassword })
229
+ dbm .EXPECT ().GetAPIKeysByLoginType (gomock .Any (),database .LoginTypePassword ).Return ([]database.APIKey {a ,b },nil ).AnyTimes ()
230
+ check .Args (database .LoginTypePassword ).Asserts (a ,policy .ActionRead ,b ,policy .ActionRead ).Returns (slice .New (a ,b ))
240
231
}))
241
232
s .Run ("GetAPIKeysByUserID" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
242
233
u1 := testutil .Fake (s .T (),faker , database.User {})
@@ -248,57 +239,44 @@ func (s *MethodTestSuite) TestAPIKey() {
248
239
Asserts (keyA ,policy .ActionRead ,keyB ,policy .ActionRead ).
249
240
Returns (slice .New (keyA ,keyB ))
250
241
}))
251
- s .Run ("GetAPIKeysLastUsedAfter" ,s .Subtest (func (db database.Store ,check * expects ) {
252
- dbtestutil .DisableForeignKeysAndTriggers (s .T (),db )
253
- a ,_ := dbgen .APIKey (s .T (),db , database.APIKey {LastUsed :time .Now ().Add (time .Hour )})
254
- b ,_ := dbgen .APIKey (s .T (),db , database.APIKey {LastUsed :time .Now ().Add (time .Hour )})
255
- _ ,_ = dbgen .APIKey (s .T (),db , database.APIKey {LastUsed :time .Now ().Add (- time .Hour )})
256
- check .Args (time .Now ()).
257
- Asserts (a ,policy .ActionRead ,b ,policy .ActionRead ).
258
- Returns (slice .New (a ,b ))
242
+ s .Run ("GetAPIKeysLastUsedAfter" ,s .Mocked (func (dbm * dbmock.MockStore ,_ * gofakeit.Faker ,check * expects ) {
243
+ now := time .Now ()
244
+ a := database.APIKey {LastUsed :now .Add (time .Hour )}
245
+ b := database.APIKey {LastUsed :now .Add (time .Hour )}
246
+ dbm .EXPECT ().GetAPIKeysLastUsedAfter (gomock .Any (),gomock .Any ()).Return ([]database.APIKey {a ,b },nil ).AnyTimes ()
247
+ check .Args (now ).Asserts (a ,policy .ActionRead ,b ,policy .ActionRead ).Returns (slice .New (a ,b ))
259
248
}))
260
- s .Run ("InsertAPIKey" ,s .Subtest (func (db database.Store ,check * expects ) {
261
- u := dbgen .User (s .T (),db , database.User {})
262
-
263
- check .Args (database.InsertAPIKeyParams {
264
- UserID :u .ID ,
265
- LoginType :database .LoginTypePassword ,
266
- Scope :database .APIKeyScopeAll ,
267
- IPAddress :defaultIPAddress (),
268
- }).Asserts (rbac .ResourceApiKey .WithOwner (u .ID .String ()),policy .ActionCreate )
249
+ s .Run ("InsertAPIKey" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
250
+ u := testutil .Fake (s .T (),faker , database.User {})
251
+ arg := database.InsertAPIKeyParams {UserID :u .ID ,LoginType :database .LoginTypePassword ,Scope :database .APIKeyScopeAll ,IPAddress :defaultIPAddress ()}
252
+ ret := testutil .Fake (s .T (),faker , database.APIKey {UserID :u .ID ,LoginType :database .LoginTypePassword })
253
+ dbm .EXPECT ().InsertAPIKey (gomock .Any (),arg ).Return (ret ,nil ).AnyTimes ()
254
+ check .Args (arg ).Asserts (rbac .ResourceApiKey .WithOwner (u .ID .String ()),policy .ActionCreate )
269
255
}))
270
- s .Run ("UpdateAPIKeyByID" ,s .Subtest (func (db database.Store ,check * expects ) {
271
- u := dbgen .User (s .T (),db , database.User {})
272
- a ,_ := dbgen .APIKey (s .T (),db , database.APIKey {UserID :u .ID ,IPAddress :defaultIPAddress ()})
273
- check .Args (database.UpdateAPIKeyByIDParams {
274
- ID :a .ID ,
275
- IPAddress :defaultIPAddress (),
276
- LastUsed :time .Now (),
277
- ExpiresAt :time .Now ().Add (time .Hour ),
278
- }).Asserts (a ,policy .ActionUpdate ).Returns ()
279
- }))
280
- s .Run ("DeleteApplicationConnectAPIKeysByUserID" ,s .Subtest (func (db database.Store ,check * expects ) {
281
- dbtestutil .DisableForeignKeysAndTriggers (s .T (),db )
282
- a ,_ := dbgen .APIKey (s .T (),db , database.APIKey {
283
- Scope :database .APIKeyScopeApplicationConnect ,
284
- })
256
+ s .Run ("UpdateAPIKeyByID" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
257
+ u := testutil .Fake (s .T (),faker , database.User {})
258
+ a := testutil .Fake (s .T (),faker , database.APIKey {UserID :u .ID ,IPAddress :defaultIPAddress ()})
259
+ arg := database.UpdateAPIKeyByIDParams {ID :a .ID ,IPAddress :defaultIPAddress (),LastUsed :time .Now (),ExpiresAt :time .Now ().Add (time .Hour )}
260
+ dbm .EXPECT ().GetAPIKeyByID (gomock .Any (),a .ID ).Return (a ,nil ).AnyTimes ()
261
+ dbm .EXPECT ().UpdateAPIKeyByID (gomock .Any (),arg ).Return (nil ).AnyTimes ()
262
+ check .Args (arg ).Asserts (a ,policy .ActionUpdate ).Returns ()
263
+ }))
264
+ s .Run ("DeleteApplicationConnectAPIKeysByUserID" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
265
+ a := testutil .Fake (s .T (),faker , database.APIKey {Scope :database .APIKeyScopeApplicationConnect })
266
+ dbm .EXPECT ().DeleteApplicationConnectAPIKeysByUserID (gomock .Any (),a .UserID ).Return (nil ).AnyTimes ()
285
267
check .Args (a .UserID ).Asserts (rbac .ResourceApiKey .WithOwner (a .UserID .String ()),policy .ActionDelete ).Returns ()
286
268
}))
287
- s .Run ("DeleteExternalAuthLink" ,s .Subtest (func (db database.Store ,check * expects ) {
288
- a := dbgen .ExternalAuthLink (s .T (),db , database.ExternalAuthLink {})
289
- check .Args (database.DeleteExternalAuthLinkParams {
290
- ProviderID :a .ProviderID ,
291
- UserID :a .UserID ,
292
- }).Asserts (rbac .ResourceUserObject (a .UserID ),policy .ActionUpdatePersonal ).Returns ()
293
- }))
294
- s .Run ("GetExternalAuthLinksByUserID" ,s .Subtest (func (db database.Store ,check * expects ) {
295
- a := dbgen .ExternalAuthLink (s .T (),db , database.ExternalAuthLink {})
296
- b := dbgen .ExternalAuthLink (s .T (),db , database.ExternalAuthLink {
297
- UserID :a .UserID ,
298
- })
299
- check .Args (a .UserID ).Asserts (
300
- rbac .ResourceUserObject (a .UserID ),policy .ActionReadPersonal ,
301
- rbac .ResourceUserObject (b .UserID ),policy .ActionReadPersonal )
269
+ s .Run ("DeleteExternalAuthLink" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
270
+ a := testutil .Fake (s .T (),faker , database.ExternalAuthLink {})
271
+ dbm .EXPECT ().GetExternalAuthLink (gomock .Any (), database.GetExternalAuthLinkParams {ProviderID :a .ProviderID ,UserID :a .UserID }).Return (a ,nil ).AnyTimes ()
272
+ dbm .EXPECT ().DeleteExternalAuthLink (gomock .Any (), database.DeleteExternalAuthLinkParams {ProviderID :a .ProviderID ,UserID :a .UserID }).Return (nil ).AnyTimes ()
273
+ check .Args (database.DeleteExternalAuthLinkParams {ProviderID :a .ProviderID ,UserID :a .UserID }).Asserts (rbac .ResourceUserObject (a .UserID ),policy .ActionUpdatePersonal ).Returns ()
274
+ }))
275
+ s .Run ("GetExternalAuthLinksByUserID" ,s .Mocked (func (dbm * dbmock.MockStore ,faker * gofakeit.Faker ,check * expects ) {
276
+ a := testutil .Fake (s .T (),faker , database.ExternalAuthLink {})
277
+ b := testutil .Fake (s .T (),faker , database.ExternalAuthLink {UserID :a .UserID })
278
+ dbm .EXPECT ().GetExternalAuthLinksByUserID (gomock .Any (),a .UserID ).Return ([]database.ExternalAuthLink {a ,b },nil ).AnyTimes ()
279
+ check .Args (a .UserID ).Asserts (rbac .ResourceUserObject (a .UserID ),policy .ActionReadPersonal ,rbac .ResourceUserObject (b .UserID ),policy .ActionReadPersonal )
302
280
}))
303
281
}
304
282