|
| 1 | +"""Unit Tests for openedx_authz migrations.""" |
| 2 | + |
| 3 | +from django.contrib.auth import get_user_model |
| 4 | +from django.contrib.auth.models import Group |
| 5 | +from django.test import TestCase |
| 6 | + |
| 7 | +from openedx_authz.api.users import batch_unassign_role_from_users, get_user_role_assignments_in_scope |
| 8 | +from openedx_authz.constants.roles import LIBRARY_ADMIN, LIBRARY_USER |
| 9 | +from openedx_authz.engine.enforcer import AuthzEnforcer |
| 10 | +from openedx_authz.engine.utils import migrate_legacy_permissions |
| 11 | +from openedx_authz.tests.stubs.models import ContentLibrary, ContentLibraryPermission, Organization |
| 12 | + |
| 13 | +User = get_user_model() |
| 14 | + |
| 15 | +# Specify a unique prefix to avoid collisions with existing data |
| 16 | +OBJECT_PREFIX = "tmlp_" |
| 17 | + |
| 18 | +org_name = f"{OBJECT_PREFIX}org" |
| 19 | +lib_name = f"{OBJECT_PREFIX}library" |
| 20 | +group_name = f"{OBJECT_PREFIX}test_group" |
| 21 | +user_names = [f"{OBJECT_PREFIX}user{i}" for i in range(3)] |
| 22 | +group_user_names = [f"{OBJECT_PREFIX}guser{i}" for i in range(3)] |
| 23 | +error_user_name = f"{OBJECT_PREFIX}error_user" |
| 24 | +error_group_name = f"{OBJECT_PREFIX}error_group" |
| 25 | +empty_group_name = f"{OBJECT_PREFIX}empty_group" |
| 26 | + |
| 27 | + |
| 28 | +class TestLegacyPermissionsMigration(TestCase): |
| 29 | + """Test cases for migrating legacy permissions.""" |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + """ |
| 33 | + Set up test data: |
| 34 | +
|
| 35 | + What this does: |
| 36 | + 1. Creates an Org and a ContentLibrary |
| 37 | + 2. Create Users and Groups |
| 38 | + 3. Assign legacy permissions using ContentLibraryPermission |
| 39 | + 4. Create invalid permissions for user and group |
| 40 | + """ |
| 41 | + # Create ContentLibrary |
| 42 | + |
| 43 | + org = Organization.objects.create(name=org_name, short_name=org_name) |
| 44 | + library = ContentLibrary.objects.create(org=org, slug=lib_name) |
| 45 | + |
| 46 | + # Create Users and Groups |
| 47 | + users = [ |
| 48 | + User.objects.create_user(username=user_name, email=f"{user_name}@example.com") for user_name in user_names |
| 49 | + ] |
| 50 | + |
| 51 | + group_users = [ |
| 52 | + User.objects.create_user(username=user_name, email=f"{user_name}@example.com") |
| 53 | + for user_name in group_user_names |
| 54 | + ] |
| 55 | + group = Group.objects.create(name=group_name) |
| 56 | + group.user_set.set(group_users) |
| 57 | + |
| 58 | + error_user = User.objects.create_user(username=error_user_name, email=f"{error_user_name}@example.com") |
| 59 | + error_group = Group.objects.create(name=error_group_name) |
| 60 | + error_group.user_set.set([error_user]) |
| 61 | + |
| 62 | + empty_group = Group.objects.create(name=empty_group_name) |
| 63 | + |
| 64 | + # Assign legacy permissions for users and group |
| 65 | + for user in users: |
| 66 | + ContentLibraryPermission.objects.create( |
| 67 | + user=user, |
| 68 | + library=library, |
| 69 | + access_level=ContentLibraryPermission.ADMIN_LEVEL, |
| 70 | + ) |
| 71 | + |
| 72 | + ContentLibraryPermission.objects.create( |
| 73 | + group=group, |
| 74 | + library=library, |
| 75 | + access_level=ContentLibraryPermission.READ_LEVEL, |
| 76 | + ) |
| 77 | + |
| 78 | + # Create invalid permissions for testing error logging |
| 79 | + ContentLibraryPermission.objects.create( |
| 80 | + user=error_user, |
| 81 | + library=library, |
| 82 | + access_level="invalid", |
| 83 | + ) |
| 84 | + ContentLibraryPermission.objects.create( |
| 85 | + group=error_group, |
| 86 | + library=library, |
| 87 | + access_level="invalid", |
| 88 | + ) |
| 89 | + |
| 90 | + # Edge case: empty group with no users |
| 91 | + ContentLibraryPermission.objects.create( |
| 92 | + group=empty_group, |
| 93 | + library=library, |
| 94 | + access_level=ContentLibraryPermission.READ_LEVEL, |
| 95 | + ) |
| 96 | + |
| 97 | + def tearDown(self): |
| 98 | + """ |
| 99 | + Clean up test data created for the migration test. |
| 100 | + """ |
| 101 | + super().tearDown() |
| 102 | + |
| 103 | + AuthzEnforcer.get_enforcer().load_policy() |
| 104 | + batch_unassign_role_from_users( |
| 105 | + users=user_names, |
| 106 | + role_external_key=LIBRARY_ADMIN.external_key, |
| 107 | + scope_external_key=f"lib:{org_name}:{lib_name}", |
| 108 | + ) |
| 109 | + batch_unassign_role_from_users( |
| 110 | + users=group_user_names, |
| 111 | + role_external_key=LIBRARY_USER.external_key, |
| 112 | + scope_external_key=f"lib:{org_name}:{lib_name}", |
| 113 | + ) |
| 114 | + |
| 115 | + ContentLibrary.objects.filter(slug=lib_name).delete() |
| 116 | + Organization.objects.filter(name=org_name).delete() |
| 117 | + Group.objects.filter(name=group_name).delete() |
| 118 | + Group.objects.filter(name=error_group_name).delete() |
| 119 | + Group.objects.filter(name=empty_group_name).delete() |
| 120 | + for user_name in user_names + group_user_names + [error_user_name]: |
| 121 | + User.objects.filter(username=user_name).delete() |
| 122 | + |
| 123 | + def test_migration(self): |
| 124 | + """Test the migration of legacy permissions. |
| 125 | + 1. Rus the migration to migrate legacy permissions. |
| 126 | + 2. Check that each user has the expected role in the new model. |
| 127 | + 3. Check that the group users have the expected role in the new model. |
| 128 | + 4. Check that invalid permissions were identified correctly as errors. |
| 129 | + """ |
| 130 | + |
| 131 | + permissions_with_errors = migrate_legacy_permissions(ContentLibraryPermission) |
| 132 | + |
| 133 | + AuthzEnforcer.get_enforcer().load_policy() |
| 134 | + for user_name in user_names: |
| 135 | + assignments = get_user_role_assignments_in_scope( |
| 136 | + user_external_key=user_name, scope_external_key=f"lib:{org_name}:{lib_name}" |
| 137 | + ) |
| 138 | + self.assertEqual(len(assignments), 1) |
| 139 | + self.assertEqual(assignments[0].roles[0], LIBRARY_ADMIN) |
| 140 | + for group_user_name in group_user_names: |
| 141 | + assignments = get_user_role_assignments_in_scope( |
| 142 | + user_external_key=group_user_name, scope_external_key=f"lib:{org_name}:{lib_name}" |
| 143 | + ) |
| 144 | + self.assertEqual(len(assignments), 1) |
| 145 | + self.assertEqual(assignments[0].roles[0], LIBRARY_USER) |
| 146 | + |
| 147 | + self.assertEqual(len(permissions_with_errors), 2) |
0 commit comments