58 lines
3.4 KiB
Python
58 lines
3.4 KiB
Python
|
|
"""Office candidate-scope migration rehearsal in Django's disposable test DB."""
|
||
|
|
|
||
|
|
from django.db import connection
|
||
|
|
from django.db.migrations.executor import MigrationExecutor
|
||
|
|
from django.test import TransactionTestCase
|
||
|
|
|
||
|
|
|
||
|
|
class OfficeScopeMigrationTests(TransactionTestCase):
|
||
|
|
def test_old_boundaries_survive_forward_reverse_and_new_default(self):
|
||
|
|
self.assertTrue(str(connection.settings_dict["NAME"]).startswith(
|
||
|
|
("test_", "file:memory")
|
||
|
|
), "Migration rehearsals must only target the disposable test database")
|
||
|
|
old_target = [("control", "0013_node_office_permissions")]
|
||
|
|
new_target = [("control", "0014_node_office_access_scope")]
|
||
|
|
executor = MigrationExecutor(connection)
|
||
|
|
latest_targets = executor.loader.graph.leaf_nodes()
|
||
|
|
try:
|
||
|
|
executor.migrate(old_target)
|
||
|
|
old_apps = executor.loader.project_state(old_target).apps
|
||
|
|
OldUser = old_apps.get_model("accounts", "User")
|
||
|
|
OldNode = old_apps.get_model("control", "Node")
|
||
|
|
user = OldUser.objects.create(username="office-migration", email="migration@example.invalid")
|
||
|
|
common = dict(region="test", server="192.0.2.1", port=11081,
|
||
|
|
protocol="vless_reality", status="online", subscription_enabled=True)
|
||
|
|
listed = OldNode.objects.create(name="listed", slug="listed", office_only=True,
|
||
|
|
total_upload_bytes=123, **common)
|
||
|
|
empty = OldNode.objects.create(name="empty", slug="empty", office_only=True, **common)
|
||
|
|
public = OldNode.objects.create(name="public", slug="public", office_only=False, **common)
|
||
|
|
listed.office_users.add(user)
|
||
|
|
old_ids = [listed.pk, empty.pk, public.pk]
|
||
|
|
before = list(OldNode.objects.filter(pk__in=old_ids).order_by("pk").values())
|
||
|
|
|
||
|
|
forward = MigrationExecutor(connection)
|
||
|
|
forward.migrate(new_target)
|
||
|
|
NewNode = forward.loader.project_state(new_target).apps.get_model("control", "Node")
|
||
|
|
self.assertEqual(list(NewNode.objects.filter(pk__in=old_ids).order_by("pk").values(
|
||
|
|
*before[0].keys()
|
||
|
|
)), before)
|
||
|
|
self.assertEqual(set(NewNode.objects.filter(pk__in=old_ids).values_list(
|
||
|
|
"office_access_scope", flat=True
|
||
|
|
)), {"explicit_users"})
|
||
|
|
self.assertEqual(list(NewNode.objects.get(pk=listed.pk).office_users.values_list("pk", flat=True)), [user.pk])
|
||
|
|
self.assertFalse(NewNode.objects.get(pk=empty.pk).office_users.exists())
|
||
|
|
|
||
|
|
new = NewNode.objects.create(name="new-site", slug="new-site", office_only=True, **common)
|
||
|
|
self.assertEqual(new.office_access_scope, "company_network")
|
||
|
|
self.assertFalse(new.office_users.exists())
|
||
|
|
|
||
|
|
MigrationExecutor(connection).migrate(old_target)
|
||
|
|
self.assertEqual(list(OldNode.objects.filter(pk__in=old_ids).order_by("pk").values()), before)
|
||
|
|
self.assertEqual(list(OldNode.objects.get(pk=listed.pk).office_users.values_list("pk", flat=True)), [user.pk])
|
||
|
|
self.assertTrue(OldNode.objects.get(pk=new.pk).office_only)
|
||
|
|
# Re-applying after schema rollback must not silently broaden any node.
|
||
|
|
MigrationExecutor(connection).migrate(new_target)
|
||
|
|
self.assertEqual(set(NewNode.objects.values_list("office_access_scope", flat=True)), {"explicit_users"})
|
||
|
|
finally:
|
||
|
|
MigrationExecutor(connection).migrate(latest_targets)
|