Skip to content

Commit

Permalink
✅ [#390] Test migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Oct 1, 2024
1 parent 473b04e commit 105c247
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
10 changes: 0 additions & 10 deletions backend/src/openarchiefbeheer/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
import factory
from factory.django import DjangoModelFactory

from ..models import Role

User = get_user_model()


class RoleFactory(DjangoModelFactory):
name = factory.Sequence(lambda n: f"Role {n}")

class Meta:
model = Role


class UserFactory(DjangoModelFactory):
username = factory.Sequence(lambda n: f"user-{n}")
first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
password = factory.PostGenerationMethodCall("set_password", "password")
role = factory.SubFactory(RoleFactory)
email = factory.Faker("email")

class Meta:
Expand Down
87 changes: 87 additions & 0 deletions backend/src/openarchiefbeheer/accounts/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from openarchiefbeheer.utils.tests.migrations_tests import TestMigrations


class TestAddGroups(TestMigrations):
app = "accounts"
migrate_from = "0003_role_can_review_final_list"
migrate_to = "0006_remove_user_role_delete_role"

def setUpBeforeMigration(self, apps):
User = apps.get_model("accounts", "User")
Role = apps.get_model("accounts", "Role")

administrator = Role.objects.get(
name="Administrator",
)
record_manager = Role.objects.get(
name="Record Manager",
)
reviewer = Role.objects.get(
name="Reviewer",
)
archivist = Role.objects.get(
name="Archivist",
)

self.user_admin = User.objects.create(username="admin1", role=administrator)
self.user_record_manager = User.objects.create(
username="rm1", role=record_manager
)
self.user_reviewer = User.objects.create(username="r1", role=reviewer)
self.user_archivist = User.objects.create(username="a1", role=archivist)
self.user_no_role = User.objects.create(username="no_role")

def test_groups_and_permissions(self):
self.assertEqual(self.user_admin.groups.get().name, "Administrator")
self.assertEqual(self.user_record_manager.groups.get().name, "Record Manager")
self.assertEqual(self.user_reviewer.groups.get().name, "Reviewer")
self.assertEqual(self.user_archivist.groups.get().name, "Archivist")
self.assertFalse(self.user_no_role.groups.filter().exists())


class TestReAddRoles(TestMigrations):
app = "accounts"
migrate_from = "0006_remove_user_role_delete_role"
migrate_to = "0003_role_can_review_final_list"

def setUpBeforeMigration(self, apps):
User = apps.get_model("accounts", "User")
Group = apps.get_model("auth", "Group")

administrator, _ = Group.objects.get_or_create(
name="Administrator",
)
record_manager, _ = Group.objects.get_or_create(
name="Record Manager",
)
reviewer, _ = Group.objects.get_or_create(
name="Reviewer",
)
archivist, _ = Group.objects.get_or_create(
name="Archivist",
)

self.user_admin = User.objects.create(username="admin1")
self.user_admin.groups.add(administrator)

self.user_record_manager = User.objects.create(username="rm1")
self.user_record_manager.groups.add(record_manager)

self.user_reviewer = User.objects.create(username="r1")
self.user_reviewer.groups.add(reviewer)

self.user_archivist = User.objects.create(username="a1")
self.user_archivist.groups.add(archivist)

self.user_no_role = User.objects.create(username="no_role")

def test_groups_and_permissions(self):
User = self.apps.get_model("accounts", "User")

users = User.objects.all()

self.assertEqual(users.get(username="admin1").role.name, "Administrator")
self.assertEqual(users.get(username="rm1").role.name, "Record Manager")
self.assertEqual(users.get(username="r1").role.name, "Reviewer")
self.assertEqual(users.get(username="a1").role.name, "Archivist")
self.assertIsNone(users.get(username="no_role").role)

0 comments on commit 105c247

Please sign in to comment.