# 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


def test_migration_custom_user_0028_user_email_verified_date(transactional_db, migration):
    old_apps = migration.before([('custom_user', '0027_user_deactivation_reason')])

    User = old_apps.get_model('custom_user', 'User')
    User.objects.create(email='john.doe@example.com', email_verified=True)

    new_apps = migration.apply([('custom_user', '0028_user_email_verified_date')])
    User = new_apps.get_model('custom_user', 'User')
    user = User.objects.get()
    assert user.email_verified_date == user.date_joined


def test_migration_custom_user_0047_initialize_services_runtime_settings(transactional_db, migration):
    old_apps = migration.before([('authentic2', '0046_runtimesetting')])

    Setting = old_apps.get_model('authentic2', 'Setting')
    assert Setting.objects.count() == 0

    new_apps = migration.apply([('authentic2', '0047_initialize_services_runtime_settings')])
    Setting = new_apps.get_model('authentic2', 'Setting')
    assert Setting.objects.count() == 4
    assert Setting.objects.filter(key__startswith='sso:').count() == 4
    for setting in Setting.objects.filter(key__startswith='sso:'):
        assert setting.value == ''


def test_migration_custom_user_0050_initialize_users_advanced_configuration(transactional_db, migration):
    old_apps = migration.before([('authentic2', '0049_apiclient_allowed_user_attributes')])
    Setting = old_apps.get_model('authentic2', 'Setting')
    before = Setting.objects.count()

    new_apps = migration.apply([('authentic2', '0050_initialize_users_advanced_configuration')])
    Setting = new_apps.get_model('authentic2', 'Setting')
    assert Setting.objects.count() == before + 1
    assert Setting.objects.filter(key__startswith='users:').count() == 1
    assert Setting.objects.get(key='users:backoffice_sidebar_template').value == ''
