authentic/tests/test_cleanup.py

44 lines
1.8 KiB
Python

# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from django.utils.timezone import now
from authentic2.custom_user.models import User, DeletedUser
def test_deleted_user_cleanup(db, freezer):
freezer.move_to('2020-01-01')
u = User.objects.create(username='john.doe', email='john@example.com')
assert User.objects.count() == 1
assert DeletedUser.objects.count() == 0
u.mark_as_deleted()
User.objects.cleanup(timestamp=now() + datetime.timedelta(seconds=700))
assert User.objects.count() == 0
assert DeletedUser.objects.count() == 1
deleted_user = DeletedUser.objects.get(old_user_id=u.id)
assert deleted_user.deleted == u.deleted
assert deleted_user.old_email == u.email.rsplit('#', 1)[0]
assert deleted_user.old_uuid == u.uuid
assert deleted_user.old_data is None
freezer.move_to(datetime.timedelta(days=365))
DeletedUser.cleanup()
assert DeletedUser.objects.count() == 1, 'DeletedUser are deleted after 365 days'
freezer.move_to(datetime.timedelta(seconds=1))
DeletedUser.cleanup()
assert DeletedUser.objects.count() == 0, 'DeletedUser are deleted after 365 days'