# 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/>.

from django.contrib.auth.models import AbstractUser
from django.utils.timezone import now


def test_migration_custom_user_0021_set_unusable_password(transactional_db, migration):
    old_apps = migration.before([('custom_user', '0020_deleteduser')])

    User = old_apps.get_model('custom_user', 'User')
    user = User.objects.create()
    assert user.password == ''

    new_apps = migration.apply([('custom_user', '0021_set_unusable_password')])
    User = new_apps.get_model('custom_user', 'User')
    user = User.objects.get()
    assert not AbstractUser.has_usable_password(user)


def test_migration_custom_user_0026_remove_user_deleted(transactional_db, migration):
    old_apps = migration.before([('custom_user', '0025_user_deactivation')])

    User = old_apps.get_model('custom_user', 'User')
    DeletedUser = old_apps.get_model('custom_user', 'DeletedUser')
    User.objects.create(deleted=now())
    User.objects.create()

    assert User.objects.count() == 2
    assert DeletedUser.objects.count() == 0
    new_apps = migration.apply([('custom_user', '0026_remove_user_deleted')])
    User = new_apps.get_model('custom_user', 'User')
    DeletedUser = new_apps.get_model('custom_user', 'DeletedUser')
    assert User.objects.count() == 1
    assert DeletedUser.objects.count() == 1
