authentik.core.tests.test_password_migration
Existing passwords move to password devices.
1"""Existing passwords move to password devices.""" 2 3from datetime import timedelta 4from importlib import import_module 5 6from django.contrib.auth.hashers import make_password 7from django.db import connection 8from django.db.migrations.loader import MigrationLoader 9from django.test import TestCase 10from django.utils.timezone import now 11 12from authentik.core.models import User 13from authentik.lib.generators import generate_id 14from authentik.stages.password.models import PasswordDevice 15 16MIGRATION = ("authentik_stages_password", "0011_password_devices") 17 18 19class TestPasswordMigration(TestCase): 20 """Run the data copy against the historical models and the retained columns.""" 21 22 def test_create_password_devices(self): 23 usable, unusable, empty = (User.objects.create(username=generate_id()) for _ in range(3)) 24 password = make_password(generate_id()) 25 changed_at = now() - timedelta(days=3) 26 with connection.cursor() as cursor: 27 # Before the migration, every user row has a password. 28 cursor.execute("UPDATE authentik_core_user SET password = '' WHERE password IS NULL") 29 cursor.executemany( 30 "UPDATE authentik_core_user SET password = %s, password_change_date = %s " 31 "WHERE id = %s", 32 [ 33 (password, changed_at, usable.pk), 34 (make_password(None), changed_at, unusable.pk), 35 ("", changed_at, empty.pk), 36 ], 37 ) 38 apps = MigrationLoader(connection).project_state(MIGRATION).apps 39 migration = import_module(f"authentik.stages.password.migrations.{MIGRATION[1]}") 40 41 with connection.schema_editor() as schema_editor: 42 migration.create_password_devices(apps, schema_editor) 43 44 device = PasswordDevice.objects.get(user=usable) 45 self.assertEqual(device.password, password) 46 self.assertEqual(device.password_change_date, changed_at) 47 self.assertEqual(device.failed_attempts, 0) 48 self.assertIsNone(device.locked_at) 49 self.assertFalse(PasswordDevice.objects.filter(user__in=[unusable, empty]).exists())
MIGRATION =
('authentik_stages_password', '0011_password_devices')
class
TestPasswordMigration(django.test.testcases.TestCase):
20class TestPasswordMigration(TestCase): 21 """Run the data copy against the historical models and the retained columns.""" 22 23 def test_create_password_devices(self): 24 usable, unusable, empty = (User.objects.create(username=generate_id()) for _ in range(3)) 25 password = make_password(generate_id()) 26 changed_at = now() - timedelta(days=3) 27 with connection.cursor() as cursor: 28 # Before the migration, every user row has a password. 29 cursor.execute("UPDATE authentik_core_user SET password = '' WHERE password IS NULL") 30 cursor.executemany( 31 "UPDATE authentik_core_user SET password = %s, password_change_date = %s " 32 "WHERE id = %s", 33 [ 34 (password, changed_at, usable.pk), 35 (make_password(None), changed_at, unusable.pk), 36 ("", changed_at, empty.pk), 37 ], 38 ) 39 apps = MigrationLoader(connection).project_state(MIGRATION).apps 40 migration = import_module(f"authentik.stages.password.migrations.{MIGRATION[1]}") 41 42 with connection.schema_editor() as schema_editor: 43 migration.create_password_devices(apps, schema_editor) 44 45 device = PasswordDevice.objects.get(user=usable) 46 self.assertEqual(device.password, password) 47 self.assertEqual(device.password_change_date, changed_at) 48 self.assertEqual(device.failed_attempts, 0) 49 self.assertIsNone(device.locked_at) 50 self.assertFalse(PasswordDevice.objects.filter(user__in=[unusable, empty]).exists())
Run the data copy against the historical models and the retained columns.
def
test_create_password_devices(self):
23 def test_create_password_devices(self): 24 usable, unusable, empty = (User.objects.create(username=generate_id()) for _ in range(3)) 25 password = make_password(generate_id()) 26 changed_at = now() - timedelta(days=3) 27 with connection.cursor() as cursor: 28 # Before the migration, every user row has a password. 29 cursor.execute("UPDATE authentik_core_user SET password = '' WHERE password IS NULL") 30 cursor.executemany( 31 "UPDATE authentik_core_user SET password = %s, password_change_date = %s " 32 "WHERE id = %s", 33 [ 34 (password, changed_at, usable.pk), 35 (make_password(None), changed_at, unusable.pk), 36 ("", changed_at, empty.pk), 37 ], 38 ) 39 apps = MigrationLoader(connection).project_state(MIGRATION).apps 40 migration = import_module(f"authentik.stages.password.migrations.{MIGRATION[1]}") 41 42 with connection.schema_editor() as schema_editor: 43 migration.create_password_devices(apps, schema_editor) 44 45 device = PasswordDevice.objects.get(user=usable) 46 self.assertEqual(device.password, password) 47 self.assertEqual(device.password_change_date, changed_at) 48 self.assertEqual(device.failed_attempts, 0) 49 self.assertIsNone(device.locked_at) 50 self.assertFalse(PasswordDevice.objects.filter(user__in=[unusable, empty]).exists())