@@ -298,9 +298,11 @@ def setUp(self):
298
298
super ().setUp ()
299
299
# Insert many tokens, both expired and not, and grants.
300
300
self .num_tokens = 100
301
- now = timezone .now ()
302
- earlier = now - timedelta (seconds = 100 )
303
- later = now + timedelta (seconds = 100 )
301
+ self .delta_secs = 1000
302
+ self .now = timezone .now ()
303
+ self .earlier = self .now - timedelta (seconds = self .delta_secs )
304
+ self .later = self .now + timedelta (seconds = self .delta_secs )
305
+
304
306
app = Application .objects .create (
305
307
name = "test_app" ,
306
308
redirect_uris = "http://localhost http://example.com http://example.org" ,
@@ -309,58 +311,54 @@ def setUp(self):
309
311
authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
310
312
)
311
313
# make 200 access tokens, half current and half expired.
312
- expired_access_tokens = AccessToken . objects . bulk_create (
313
- AccessToken (token = "expired AccessToken {}" .format (i ), expires = earlier )
314
+ expired_access_tokens = [
315
+ AccessToken (token = "expired AccessToken {}" .format (i ), expires = self . earlier )
314
316
for i in range (self .num_tokens )
315
- )
316
- current_access_tokens = AccessToken .objects .bulk_create (
317
- AccessToken (token = f"current AccessToken { i } " , expires = later ) for i in range (self .num_tokens )
318
- )
317
+ ]
318
+ for a in expired_access_tokens :
319
+ a .save ()
320
+
321
+ current_access_tokens = [
322
+ AccessToken (token = f"current AccessToken { i } " , expires = self .later ) for i in range (self .num_tokens )
323
+ ]
324
+ for a in current_access_tokens :
325
+ a .save ()
326
+
319
327
# Give the first half of the access tokens a refresh token,
320
328
# alternating between current and expired ones.
321
- RefreshToken . objects . bulk_create (
329
+ for i in range ( 0 , len ( expired_access_tokens ) // 2 , 2 ):
322
330
RefreshToken (
323
331
token = f"expired AT's refresh token { i } " ,
324
332
application = app ,
325
- access_token = expired_access_tokens [i ]. pk ,
333
+ access_token = expired_access_tokens [i ],
326
334
user = self .user ,
327
- )
328
- for i in range (0 , len (expired_access_tokens ) // 2 , 2 )
329
- )
330
- RefreshToken .objects .bulk_create (
335
+ ).save ()
336
+
337
+ for i in range (1 , len (current_access_tokens ) // 2 , 2 ):
331
338
RefreshToken (
332
339
token = f"current AT's refresh token { i } " ,
333
340
application = app ,
334
- access_token = current_access_tokens [i ]. pk ,
341
+ access_token = current_access_tokens [i ],
335
342
user = self .user ,
336
- )
337
- for i in range (1 , len (current_access_tokens ) // 2 , 2 )
338
- )
343
+ ).save ()
344
+
339
345
# Make some grants, half of which are expired.
340
- Grant . objects . bulk_create (
346
+ for i in range ( self . num_tokens ):
341
347
Grant (
342
348
user = self .user ,
343
349
code = f"old grant code { i } " ,
344
350
application = app ,
345
- expires = earlier ,
351
+ expires = self . earlier ,
346
352
redirect_uri = "https://localhost/redirect" ,
347
- )
348
- for i in range (self .num_tokens )
349
- )
350
- Grant .objects .bulk_create (
353
+ ).save ()
354
+ for i in range (self .num_tokens ):
351
355
Grant (
352
356
user = self .user ,
353
357
code = f"new grant code { i } " ,
354
358
application = app ,
355
- expires = later ,
359
+ expires = self . later ,
356
360
redirect_uri = "https://localhost/redirect" ,
357
- )
358
- for i in range (self .num_tokens )
359
- )
360
-
361
- def test_clear_expired_tokens (self ):
362
- self .oauth2_settings .REFRESH_TOKEN_EXPIRE_SECONDS = 60
363
- assert clear_expired () is None
361
+ ).save ()
364
362
365
363
def test_clear_expired_tokens_incorect_timetype (self ):
366
364
self .oauth2_settings .REFRESH_TOKEN_EXPIRE_SECONDS = "A"
@@ -372,19 +370,61 @@ def test_clear_expired_tokens_incorect_timetype(self):
372
370
def test_clear_expired_tokens_with_tokens (self ):
373
371
self .oauth2_settings .CLEAR_EXPIRED_TOKENS_BATCH_SIZE = 10
374
372
self .oauth2_settings .CLEAR_EXPIRED_TOKENS_BATCH_INTERVAL = 0.0
375
- at_count = AccessToken .objects .count ()
376
- assert at_count == 2 * self .num_tokens , f"{ 2 * self .num_tokens } access tokens should exist."
377
- rt_count = RefreshToken .objects .count ()
378
- assert rt_count == self .num_tokens // 2 , f"{ self .num_tokens // 2 } refresh tokens should exist."
379
- gt_count = Grant .objects .count ()
380
- assert gt_count == self .num_tokens * 2 , f"{ self .num_tokens * 2 } grants should exist."
373
+ self .oauth2_settings .REFRESH_TOKEN_EXPIRE_SECONDS = self .delta_secs // 2
374
+
375
+ # before clear_expired(), confirm setup as expected
376
+ initial_at_count = AccessToken .objects .count ()
377
+ assert initial_at_count == 2 * self .num_tokens , f"{ 2 * self .num_tokens } access tokens should exist."
378
+ initial_expired_at_count = AccessToken .objects .filter (expires__lte = self .now ).count ()
379
+ assert (
380
+ initial_expired_at_count == self .num_tokens
381
+ ), f"{ self .num_tokens } expired access tokens should exist."
382
+ initial_current_at_count = AccessToken .objects .filter (expires__gt = self .now ).count ()
383
+ assert (
384
+ initial_current_at_count == self .num_tokens
385
+ ), f"{ self .num_tokens } current access tokens should exist."
386
+ initial_rt_count = RefreshToken .objects .count ()
387
+ assert (
388
+ initial_rt_count == self .num_tokens // 2
389
+ ), f"{ self .num_tokens // 2 } refresh tokens should exist."
390
+ initial_rt_expired_at_count = RefreshToken .objects .filter (access_token__expires__lte = self .now ).count ()
391
+ assert (
392
+ initial_rt_expired_at_count == initial_rt_count / 2
393
+ ), "half the refresh tokens should be for expired access tokens."
394
+ initial_rt_current_at_count = RefreshToken .objects .filter (access_token__expires__gt = self .now ).count ()
395
+ assert (
396
+ initial_rt_current_at_count == initial_rt_count / 2
397
+ ), "half the refresh tokens should be for current access tokens."
398
+ initial_gt_count = Grant .objects .count ()
399
+ assert initial_gt_count == self .num_tokens * 2 , f"{ self .num_tokens * 2 } grants should exist."
400
+
381
401
clear_expired ()
382
- at_count = AccessToken .objects .count ()
383
- assert at_count == self .num_tokens , "Half the access tokens should not have been deleted."
384
- rt_count = RefreshToken .objects .count ()
385
- assert rt_count == self .num_tokens // 2 , "Half of the refresh tokens should have been deleted."
386
- gt_count = Grant .objects .count ()
387
- assert gt_count == self .num_tokens , "Half the grants should have been deleted."
402
+
403
+ # after clear_expired():
404
+ remaining_at_count = AccessToken .objects .count ()
405
+ assert (
406
+ remaining_at_count == initial_at_count // 2
407
+ ), "half the initial access tokens should still exist."
408
+ remaining_expired_at_count = AccessToken .objects .filter (expires__lte = self .now ).count ()
409
+ assert remaining_expired_at_count == 0 , "no remaining expired access tokens should still exist."
410
+ remaining_current_at_count = AccessToken .objects .filter (expires__gt = self .now ).count ()
411
+ assert (
412
+ remaining_current_at_count == initial_current_at_count
413
+ ), "all current access tokens should still exist."
414
+ remaining_rt_count = RefreshToken .objects .count ()
415
+ assert remaining_rt_count == initial_rt_count // 2 , "half the refresh tokens should still exist."
416
+ remaining_rt_expired_at_count = RefreshToken .objects .filter (
417
+ access_token__expires__lte = self .now
418
+ ).count ()
419
+ assert remaining_rt_expired_at_count == 0 , "no refresh tokens for expired AT's should still exist."
420
+ remaining_rt_current_at_count = RefreshToken .objects .filter (
421
+ access_token__expires__gt = self .now
422
+ ).count ()
423
+ assert (
424
+ remaining_rt_current_at_count == initial_rt_current_at_count
425
+ ), "all the refresh tokens for current access tokens should still exist."
426
+ remaining_gt_count = Grant .objects .count ()
427
+ assert remaining_gt_count == initial_gt_count // 2 , "half the remaining grants should still exist."
388
428
389
429
390
430
@pytest .mark .django_db
0 commit comments