add test of the DeletedUserManager.cleanup() method

This commit is contained in:
Benjamin Dauvergne 2016-07-06 16:33:03 +02:00
parent e3d190d9a2
commit accc74d7c4
2 changed files with 16 additions and 2 deletions

View File

@ -35,9 +35,9 @@ class DeletedUserManager(models.Manager):
user.save()
self.get_or_create(user=user)
def cleanup(self, threshold=600):
def cleanup(self, threshold=600, timestamp=None):
'''Delete all deleted users for more than 10 minutes.'''
not_after = now() - timedelta(seconds=threshold)
not_after = (timestamp or now()) - timedelta(seconds=threshold)
for deleted_user in self.filter(creation__lte=not_after):
user = deleted_user.user
deleted_user.delete()

14
tests/test_cleanup.py Normal file
View File

@ -0,0 +1,14 @@
import datetime
from authentic2.models import DeletedUser
from django.contrib.auth import get_user_model
from django.utils.timezone import now
def test_deleted_user_cleanup(db):
User = get_user_model()
u = User.objects.create(username='john.doe')
assert User.objects.count() == 1
DeletedUser.objects.delete_user(u)
DeletedUser.objects.cleanup(timestamp=now() + datetime.timedelta(seconds=700))
assert User.objects.count() == 0