diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cca84522..f827f987 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -61,6 +61,7 @@ Changed * **Breaking**: ``GET /api/authz/v1/scopes/`` no longer accepts ``course`` and ``library`` as ``scope_type`` values. Use ``course-v1`` and ``lib`` instead. Requests with the old values now return a 400 validation error, the same as ``/roles/``. +* **Breaking**: ``CourseScope`` references its course only through the course key in ``external_key`` (ADR 0035). Migration ``0013_remove_coursescope_course_overview`` copies the course key of older course scopes into ``external_key`` and removes the ``course_overview`` foreign key, so ORM filters on ``course_overview``, creating a scope with a ``course_overview`` argument and ``CourseOverview.authz_scopes`` no longer work. ``CourseScope.course_overview`` remains as a read-only property, and deleting a course still deletes its course scope and role assignments. Removed ======= diff --git a/docs/how-tos/index.rst b/docs/how-tos/index.rst index 20f845d2..8e442934 100644 --- a/docs/how-tos/index.rst +++ b/docs/how-tos/index.rst @@ -5,3 +5,4 @@ How-tos :maxdepth: 1 use-built-in-constants + resolve-course-scope-key-conflicts diff --git a/docs/how-tos/resolve-course-scope-key-conflicts.rst b/docs/how-tos/resolve-course-scope-key-conflicts.rst new file mode 100644 index 00000000..5e9ebc61 --- /dev/null +++ b/docs/how-tos/resolve-course-scope-key-conflicts.rst @@ -0,0 +1,39 @@ +Resolve Course Scopes That ``0013_remove_coursescope_course_overview`` Cannot Update +#################################################################################### + +Migration ``0013_remove_coursescope_course_overview`` copies the course key of each course scope created before ``0010_scope_external_key`` into ``external_key``, and then removes ``course_overview_id``. ``external_key`` is unique across all scopes, and MySQL compares it with the table collation, which ignores case and accents in ``utf8mb4_unicode_ci`` and ``utf8mb4_0900_ai_ci``, so a course key can collide with a key that another scope already holds. When that happens, the migration copies every other key and stops before it removes the column, with an error like this: + +.. code-block:: text + + CourseScopeKeyConflictError: Cannot copy the course key of these course scopes into external_key, because another scope already holds an equal key: 12: course-v1:OpenedX+DemoX+2024. Resolve them as the openedx-authz how-to "Resolve Course Scopes That 0013_remove_coursescope_course_overview Cannot Update" describes, then run migrate again. + +Each ``: `` pair names a course scope whose key is still only in ``course_overview_id``. Since the database treats both keys as equal, both scopes refer to the same course, so the role assignments of the older scope move to the scope that holds the key, and the older scope is removed. Follow these steps for every pair in the error. + +#. Back up the database. + +#. Open a MySQL shell on the LMS database and find the scope that holds the key. This query returns its ``id``: + + .. code-block:: sql + + SELECT id, external_key FROM openedx_authz_scope + WHERE external_key = 'course-v1:OpenedX+DemoX+2024'; + +#. List the role assignments of both scopes, to know what moves. Replace ``12`` with the scope from the error and ``34`` with the ``id`` from the previous step: + + .. code-block:: sql + + SELECT r.scope_id, c.v0 AS subject, c.v1 AS role, c.v2 AS scope + FROM openedx_authz_extendedcasbinrule r + JOIN casbin_rule c ON c.id = r.casbin_rule_id + WHERE r.scope_id IN (12, 34); + +#. Move the role assignments of the older scope to the scope that holds the key, and remove the older scope: + + .. code-block:: sql + + UPDATE openedx_authz_extendedcasbinrule SET scope_id = 34 WHERE scope_id = 12; + DELETE FROM openedx_authz_coursescope WHERE scope_ptr_id = 12; + DELETE FROM openedx_authz_scope WHERE id = 12; + +#. Run ``./manage.py lms migrate`` again, which copies only the keys that are still missing and then removes ``course_overview_id``. + diff --git a/src/openedx_authz/handlers.py b/src/openedx_authz/handlers.py index bab1c6ec..5ccae256 100644 --- a/src/openedx_authz/handlers.py +++ b/src/openedx_authz/handlers.py @@ -143,36 +143,40 @@ def handle_org_waffle_flag_change(sender, instance, **kwargs) -> None: post_save.connect(handle_org_waffle_flag_change, sender=WaffleFlagOrgOverrideModel) -def backfill_course_scope(sender, instance, **kwargs): # pylint: disable=unused-argument +def delete_course_scope_on_course_deletion(sender, instance, **kwargs): # pylint: disable=unused-argument """ - Link a pending CourseScope to the CourseOverview that was just created for it. + Delete the course scope of a CourseOverview that was just deleted. - get_or_create_for_external_key() (openedx_authz/models/scopes.py) allows assigning a - role for a course key before its CourseOverview exists, leaving course_overview null. - This backfills that FK once the course shows up, e.g. after a course rerun finishes - cloning. See CourseScope.link_pending_scope() for the actual lookup/update. + Course scopes reference their course only by its key, so nothing in the database deletes + the scope with its course. Deleting the scope deletes its ExtendedCasbinRule rows through + their foreign key to the scope, and delete_casbin_rule_on_extended_rule_deletion() then + deletes the matching CasbinRule rows, so the role assignments on the course go away and a + course created later with the same key does not inherit them. - This runs on every CourseOverview save, so errors are logged rather than raised: - a problem backfilling a pending scope must not break the caller's save(). The query - runs in its own savepoint so a failure doesn't poison the caller's transaction. + Args: + sender: The CourseOverview model class. + instance: The CourseOverview that was deleted, for example the one with id + ``course-v1:OpenedX+DemoX+2024``. + **kwargs: Other arguments Django sends with ``post_delete``. + + Returns: + None """ - try: - with transaction.atomic(): - CourseScope.link_pending_scope(instance) - except Exception as exc: # pylint: disable=broad-exception-caught - logger.exception( - "Error linking pending CourseScope to CourseOverview %s", - instance.pk, - exc_info=exc, - ) + CourseScope.objects.filter(external_key=CourseScope.external_key_for_object(instance)).delete() def backfill_content_library_scope(sender, instance, **kwargs): # pylint: disable=unused-argument """ Link a pending ContentLibraryScope to the ContentLibrary that was just created for it. - See backfill_course_scope() above; same idea for content libraries, including the - broad exception handling and the isolating savepoint. + get_or_create_for_external_key() (openedx_authz/models/core.py) allows assigning a role + for a library key before its ContentLibrary exists, leaving content_library null. This + backfills that FK once the library shows up. See Scope.link_pending_scope() for the + actual lookup/update. + + This runs on every ContentLibrary save, so errors are logged rather than raised: + a problem backfilling a pending scope must not break the caller's save(). The query + runs in its own savepoint so a failure doesn't poison the caller's transaction. """ try: with transaction.atomic(): @@ -187,7 +191,7 @@ def backfill_content_library_scope(sender, instance, **kwargs): # pylint: disab # Only register the handlers if the models are available (i.e., running in Open edX) if CourseOverview is not None: - post_save.connect(backfill_course_scope, sender=CourseOverview) + post_delete.connect(delete_course_scope_on_course_deletion, sender=CourseOverview) if ContentLibrary is not None: post_save.connect(backfill_content_library_scope, sender=ContentLibrary) diff --git a/src/openedx_authz/migrations/0013_remove_coursescope_course_overview.py b/src/openedx_authz/migrations/0013_remove_coursescope_course_overview.py new file mode 100644 index 00000000..dd312a4f --- /dev/null +++ b/src/openedx_authz/migrations/0013_remove_coursescope_course_overview.py @@ -0,0 +1,68 @@ +from django.db import IntegrityError, migrations, transaction + +KEY_CONFLICT_MESSAGE = ( + "Cannot copy the course key of these course scopes into external_key, because another scope " + "already holds an equal key: {conflicts}. Resolve them as the openedx-authz how-to " + '"Resolve Course Scopes That 0013_remove_coursescope_course_overview Cannot Update" describes, ' + "then run migrate again." +) + + +class CourseScopeKeyConflictError(Exception): + """Raised when a course key cannot be copied into external_key because another scope holds it.""" + + +def copy_course_keys_to_external_key(apps, schema_editor): + """Copy the course key of each course scope without an external_key into external_key. + + Course scopes reference their course only by external_key once this migration removes + course_overview. Scopes created before migration 0010_scope_external_key have no + external_key, so their course key exists only in course_overview_id until it is copied. + Each scope is updated in its own transaction, because external_key is unique and the + database decides which keys are equal under its own collation. When any copy is rejected, + the other scopes keep their copied key and the migration stops before the column is + removed, so no course key is lost and running migrate again copies only the keys still + missing. Nothing is updated when every course scope already has an external_key. + + Args: + apps: The historical app registry of this migration. + schema_editor: The schema editor running this migration. + + Returns: + None + + Raises: + CourseScopeKeyConflictError: When one or more course keys collide with an existing key, + for example ``12: course-v1:OpenedX+DemoX+2024``. + """ + course_scope_model = apps.get_model("openedx_authz", "CourseScope") + scopes_without_key = course_scope_model.objects.filter( + external_key__isnull=True, + course_overview__isnull=False, + ).values_list("pk", "course_overview_id") + conflicts = [] + for scope_id, course_key in scopes_without_key: + try: + with transaction.atomic(using=schema_editor.connection.alias): + course_scope_model.objects.filter(pk=scope_id).update(external_key=str(course_key)) + except IntegrityError: + conflicts.append(f"{scope_id}: {course_key}") + if conflicts: + raise CourseScopeKeyConflictError(KEY_CONFLICT_MESSAGE.format(conflicts=", ".join(conflicts))) + + +class Migration(migrations.Migration): + # Not atomic, so the keys copied before a conflict stay copied when the migration stops. + atomic = False + + dependencies = [ + ("openedx_authz", "0012_move_builtin_schema_sources_to_applets"), + ] + + operations = [ + migrations.RunPython(copy_course_keys_to_external_key, migrations.RunPython.noop), + migrations.RemoveField( + model_name="coursescope", + name="course_overview", + ), + ] diff --git a/src/openedx_authz/models/core.py b/src/openedx_authz/models/core.py index 9b4491da..4e951e48 100644 --- a/src/openedx_authz/models/core.py +++ b/src/openedx_authz/models/core.py @@ -118,9 +118,9 @@ class Scope(BaseRegistryModel): objects = ScopeManager() - # Name of the subclass's FK field pointing at its backing object (e.g. "content_library", - # "course_overview"), used by get_or_create_for_external_key()/link_pending_scope() below - # to work generically across scope types. + # Name of the subclass's FK field pointing at its backing object (e.g. "content_library"), + # used by get_or_create_for_external_key()/link_pending_scope() below to work generically + # across scope types that keep such a FK. LINKED_OBJECT_FIELD: ClassVar[str] = None # Canonical string form of the scope's key (e.g. a course-v1 course id), set on creation diff --git a/src/openedx_authz/models/scopes.py b/src/openedx_authz/models/scopes.py index 11d110bc..c41f430c 100644 --- a/src/openedx_authz/models/scopes.py +++ b/src/openedx_authz/models/scopes.py @@ -92,25 +92,35 @@ class CourseScope(Scope): """ NAMESPACE = "course-v1" - LINKED_OBJECT_FIELD = "course_overview" - # Link to the actual course, if applicable. In other cases, this could be null. - # Piggybacking on the existing CourseOverview model to keep the ExtendedCasbinRule up to date - # by deleting the Scope, and thus the ExtendedCasbinRule, when the CourseOverview is deleted. - # - # When course_overviews IS available, the on_delete=CASCADE will still work at the - # application level through Django's signal handlers. - # Use a string reference to the external app's model so Django won't try - # to import it at model import time. The migration already records the - # dependency on `course_overviews` when the app is present. - course_overview = models.ForeignKey( - settings.OPENEDX_AUTHZ_COURSE_OVERVIEW_MODEL, - on_delete=models.CASCADE, - null=True, - blank=True, - related_name="authz_scopes", - swappable=True, - ) + @classmethod + def get_or_create_for_external_key(cls, scope) -> "CourseScope": + """Get or create the course scope for a course key. + + Course scopes reference their course only by ``external_key``, so the scope is the same + whether the course exists yet or not, for example during a course rerun. + + Args: + scope: ScopeData object with an external_key attribute, for example + ``CourseOverviewData(external_key="course-v1:OpenedX+DemoX+2024")``. + + Returns: + CourseScope: The (possibly newly created) course scope for that key. + """ + course_scope, _ = cls.objects.get_or_create(external_key=scope.external_key) + return course_scope + + @property + def course_overview(self): + """Return the course this scope references through its course key. + + Returns: + CourseOverview | None: The CourseOverview whose id is ``external_key``, or ``None`` + when that course does not exist or the CourseOverview model is not installed. + """ + if CourseOverview is None: + return None + return CourseOverview.objects.filter(id=self.external_key).first() @classmethod def external_key_for_object(cls, linked_object) -> str: diff --git a/src/openedx_authz/tests/integration/test_models.py b/src/openedx_authz/tests/integration/test_models.py index 13ffc664..801f21d7 100644 --- a/src/openedx_authz/tests/integration/test_models.py +++ b/src/openedx_authz/tests/integration/test_models.py @@ -1454,21 +1454,3 @@ def test_content_library_scope_backfills_when_library_created(self): scope.refresh_from_db() self.assertEqual(scope.content_library, content_library) - - def test_scope_created_before_external_key_field_is_reused_not_duplicated(self): - """A Scope row created before this fix (FK set, no external_key) isn't duplicated on reassignment. - - Simulates a pre-existing row the way get_or_create_for_external_key used to create it: - keyed only by the FK, with no external_key. - """ - course_key = CourseLocator("LegacyOrg", "LegacyCourse", "2024_T1") - course_overview = CourseOverviewFactory(id=course_key) - legacy_scope = CourseScope.objects.create(course_overview=course_overview) - self.assertIsNone(legacy_scope.external_key) - - scope_data = CourseOverviewData(external_key=str(course_key)) - scope = Scope.objects.get_or_create_for_external_key(scope_data) - - self.assertEqual(scope.id, legacy_scope.id) - self.assertEqual(scope.external_key, str(course_key)) - self.assertEqual(CourseScope.objects.filter(course_overview=course_overview).count(), 1) diff --git a/src/openedx_authz/tests/test_handlers.py b/src/openedx_authz/tests/test_handlers.py index 0e448a9c..8d7f534a 100644 --- a/src/openedx_authz/tests/test_handlers.py +++ b/src/openedx_authz/tests/test_handlers.py @@ -14,7 +14,6 @@ from casbin_adapter.models import CasbinRule from ddt import data, ddt, unpack from django.test import TestCase, override_settings -from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import LibraryLocatorV2 from openedx_events.authz.data import RoleAssignmentData @@ -27,6 +26,7 @@ ) from openedx_authz.models.authz_migration import MigrationType, ScopeType from openedx_authz.models.core import ExtendedCasbinRule, RoleAssignmentAudit, Scope, Subject +from openedx_authz.models.scopes import CourseScope from openedx_authz.models.subjects import UserSubject from openedx_authz.tests.stubs.models import ( ContentLibrary, @@ -249,32 +249,102 @@ def test_cascade_deletion_with_scope_deletion(self): self.assertTrue(Subject.objects.filter(id=subject_id).exists()) -class TestBackfillPendingScopeSignalHandlers(TestCase): - """Confirm the pending-scope backfill handlers stay resilient to failures. +class TestDeleteCourseScopeOnCourseDeletion(TestCase): + """Deleting a CourseOverview deletes the course scope with its key and the role assignments on it.""" - A failure linking a pending CourseScope/ContentLibraryScope must not break the - CourseOverview/ContentLibrary save() that triggered it (see openedx_authz/handlers.py - backfill_course_scope/backfill_content_library_scope). - """ + def create_course_with_assignment(self, course_id): + """Create a CourseOverview, its course scope and a role assignment on that scope. + + Args: + course_id: The course key as a string, for example ``"course-v1:TestOrg+Delete+2024"``. + + Returns: + tuple: (course_overview, course_scope, casbin_rule, extended_rule) + """ + course_overview = CourseOverview.get_from_id(course_id) + course_scope = CourseScope.objects.create(external_key=course_id) + casbin_rule, extended_rule = create_casbin_rule_with_extended( + ptype="g", + v0="user^course_staff_user", + v1="role^course_staff", + v2=f"course-v1^{course_id}", + v3="", + scope=course_scope, + ) + return course_overview, course_scope, casbin_rule, extended_rule - def test_course_scope_backfill_logs_exception_without_raising(self): - """A failure in CourseScope.link_pending_scope() should be logged, not raised. + def test_deleting_course_deletes_its_scope_and_role_assignments(self): + """Deleting a CourseOverview removes its course scope, ExtendedCasbinRule and CasbinRule rows, + leaves other courses' scopes, and a course created again with the same key has no assignments. Expected Result: - - Logger captures the exception raised by link_pending_scope(). - - CourseOverview.get_from_id() (the caller) still returns normally. + - The deleted course's scope, extended rule and Casbin rule no longer exist. + - The other course's scope still exists. + - The recreated course has no course scope. """ - with ( - patch("openedx_authz.handlers.logger") as mock_logger, - patch("openedx_authz.handlers.CourseScope.link_pending_scope") as mock_link, - ): - mock_link.side_effect = RuntimeError("link failed") + course_id = "course-v1:TestOrg+Delete+2024" + course_overview, course_scope, casbin_rule, extended_rule = self.create_course_with_assignment(course_id) + _, other_scope, _, _ = self.create_course_with_assignment("course-v1:TestOrg+Keep+2024") - course_overview = CourseOverview.get_from_id(CourseKey.from_string("course-v1:TestOrg+Resilient+2024")) + course_overview.delete() - mock_link.assert_called_once_with(course_overview) - mock_logger.exception.assert_called_once() - self.assertIn("Error linking pending CourseScope", mock_logger.exception.call_args[0][0]) + self.assertFalse(CourseScope.objects.filter(id=course_scope.id).exists()) + self.assertFalse(ExtendedCasbinRule.objects.filter(id=extended_rule.id).exists()) + self.assertFalse(CasbinRule.objects.filter(id=casbin_rule.id).exists()) + self.assertTrue(CourseScope.objects.filter(id=other_scope.id).exists()) + CourseOverview.get_from_id(course_id) + self.assertFalse(CourseScope.objects.filter(external_key=course_id).exists()) + + def test_deleting_course_without_scope_deletes_nothing_else(self): + """Deleting a CourseOverview that no course scope references leaves every scope in place. + + Expected Result: + - The number of scopes is unchanged. + """ + self.create_course_with_assignment("course-v1:TestOrg+Other+2024") + course_overview = CourseOverview.get_from_id("course-v1:TestOrg+NoScope+2024") + scope_count = Scope.objects.count() + + course_overview.delete() + + self.assertEqual(Scope.objects.count(), scope_count) + + def test_queryset_delete_deletes_the_scope_of_each_course(self): + """Deleting several CourseOverviews in one queryset delete removes each course scope. + + Expected Result: + - No course scope remains for any of the deleted courses. + """ + course_ids = ["course-v1:TestOrg+Bulk1+2024", "course-v1:TestOrg+Bulk2+2024"] + for course_id in course_ids: + self.create_course_with_assignment(course_id) + + CourseOverview.objects.filter(id__in=course_ids).delete() + + self.assertFalse(CourseScope.objects.filter(external_key__in=course_ids).exists()) + + def test_raw_delete_leaves_the_scope_and_role_assignments(self): + """Removing a CourseOverview row without Django signals leaves its scope and role assignments. + + Expected Result: + - The course scope and its extended rule still exist after the raw delete. + """ + course_id = "course-v1:TestOrg+Raw+2024" + _, course_scope, _, extended_rule = self.create_course_with_assignment(course_id) + queryset = CourseOverview.objects.filter(id=course_id) + + queryset._raw_delete(queryset.db) # pylint: disable=protected-access + + self.assertTrue(CourseScope.objects.filter(id=course_scope.id).exists()) + self.assertTrue(ExtendedCasbinRule.objects.filter(id=extended_rule.id).exists()) + + +class TestBackfillPendingScopeSignalHandlers(TestCase): + """Confirm the pending-scope backfill handler stays resilient to failures. + + A failure linking a pending ContentLibraryScope must not break the ContentLibrary save() + that triggered it (see openedx_authz/handlers.py backfill_content_library_scope). + """ def test_content_library_scope_backfill_logs_exception_without_raising(self): """A failure in ContentLibraryScope.link_pending_scope() should be logged, not raised. diff --git a/src/openedx_authz/tests/test_migrations.py b/src/openedx_authz/tests/test_migrations.py index 78e3d23a..e4656255 100644 --- a/src/openedx_authz/tests/test_migrations.py +++ b/src/openedx_authz/tests/test_migrations.py @@ -8,7 +8,9 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.core.management import CommandError, call_command -from django.test import TestCase +from django.db import connection +from django.db.migrations.executor import MigrationExecutor +from django.test import TestCase, TransactionTestCase from opaque_keys.edx.django.models import CourseKeyField from openedx_authz.api.data import ( @@ -87,6 +89,176 @@ empty_group_name = f"{OBJECT_PREFIX}empty_group" schema_source_migration = import_module("openedx_authz.migrations.0012_move_builtin_schema_sources_to_applets") +COURSE_SCOPE_KEY_MIGRATION_MODULE = "openedx_authz.migrations.0013_remove_coursescope_course_overview" + +APP_LABEL = "openedx_authz" +BEFORE_COURSE_SCOPE_KEY_MIGRATION = [(APP_LABEL, "0012_move_builtin_schema_sources_to_applets")] +COURSE_SCOPE_KEY_MIGRATION = [(APP_LABEL, "0013_remove_coursescope_course_overview")] +COURSE_SCOPE_TABLE = "openedx_authz_coursescope" +COURSE_OVERVIEW_ID_COLUMN = "course_overview_id" + + +def get_column_names(table_name): + """Return the column names of a database table. + + Args: + table_name: The table to describe, for example ``"openedx_authz_coursescope"``. + + Returns: + set[str]: The names of the table's columns. + """ + with connection.cursor() as cursor: + return {column.name for column in connection.introspection.get_table_description(cursor, table_name)} + + +class TestCourseScopeTable(TestCase): + """The course scope table has no column for the CourseOverview foreign key.""" + + def test_course_scope_table_has_no_course_overview_column(self): + """Migrating an empty database ends with no course_overview_id column. + + Expected Result: + - The column is not in openedx_authz_coursescope. + """ + self.assertNotIn(COURSE_OVERVIEW_ID_COLUMN, get_column_names(COURSE_SCOPE_TABLE)) + + +class TestCourseScopeKeyMigration(TransactionTestCase): + """Migration 0013 copies course keys into external_key and removes course_overview_id.""" + + def setUp(self): + """Migrate the database back to 0012, where course scopes still have course_overview_id. + + The migration loader reloads migration modules, so the module is imported after it runs + to get the CourseScopeKeyConflictError class the migration raises. + """ + executor = MigrationExecutor(connection) + executor.migrate(BEFORE_COURSE_SCOPE_KEY_MIGRATION) + self.key_migration = import_module(COURSE_SCOPE_KEY_MIGRATION_MODULE) + self.old_apps = executor.loader.project_state(BEFORE_COURSE_SCOPE_KEY_MIGRATION).apps + self.old_course_scope_model = self.old_apps.get_model(APP_LABEL, "CourseScope") + + def tearDown(self): + """Remove the scopes each test created and migrate forward to the latest migrations again.""" + self.old_apps.get_model(APP_LABEL, "Scope").objects.all().delete() + executor = MigrationExecutor(connection) + executor.migrate(executor.loader.graph.leaf_nodes()) + + def migrate_to_course_scope_key_migration(self): + """Apply migration 0013_remove_coursescope_course_overview.""" + executor = MigrationExecutor(connection) + executor.migrate(COURSE_SCOPE_KEY_MIGRATION) + + def create_old_course_scope(self, course_id=None, external_key=None): + """Create a course scope with the model of migration 0012. + + Args: + course_id: The course key stored in course_overview_id, for example + ``"course-v1:TestOrg+Legacy+2024"``, or None. + external_key: The scope's external_key, or None. + + Returns: + int: The id of the new course scope. + """ + if course_id: + CourseOverview.get_from_id(course_id) + return self.old_course_scope_model.objects.create( + course_overview_id=course_id, + external_key=external_key, + ).pk + + def get_external_keys(self): + """Return the external_key of every scope by id. + + Returns: + dict[int, str | None]: The external_key of each scope. + """ + return dict(self.old_apps.get_model(APP_LABEL, "Scope").objects.values_list("pk", "external_key")) + + def test_backfill_copies_missing_keys_and_removes_the_column(self): + """Scopes with a course and no key get the course key, other scopes keep theirs, and the column goes. + + Expected Result: + - A scope with course_overview_id and no external_key gets the course key. + - Scopes that have a key, or have neither a course nor a key, are unchanged. + - openedx_authz_coursescope has no course_overview_id column afterwards. + """ + legacy_id = self.create_old_course_scope(course_id="course-v1:TestOrg+Legacy+2024") + keyed_id = self.create_old_course_scope( + course_id="course-v1:TestOrg+Rerun+2025", + external_key="course-v1:TestOrg+Rerun+2025", + ) + pending_id = self.create_old_course_scope(external_key="course-v1:TestOrg+Future+2026") + empty_id = self.create_old_course_scope() + + self.migrate_to_course_scope_key_migration() + + self.assertEqual( + self.get_external_keys(), + { + legacy_id: "course-v1:TestOrg+Legacy+2024", + keyed_id: "course-v1:TestOrg+Rerun+2025", + pending_id: "course-v1:TestOrg+Future+2026", + empty_id: None, + }, + ) + self.assertNotIn(COURSE_OVERVIEW_ID_COLUMN, get_column_names(COURSE_SCOPE_TABLE)) + + def test_backfill_reports_conflicts_and_copies_the_other_keys(self): + """A course key that another scope already holds is reported, while the other keys are copied. + + Expected Result: + - The backfill raises CourseScopeKeyConflictError naming the conflicting scope and key. + - The scope without a conflict gets its course key. + - The conflicting scope keeps no external_key. + """ + conflicting_id = self.create_old_course_scope(course_id="course-v1:TestOrg+Taken+2024") + self.create_old_course_scope(external_key="course-v1:TestOrg+Taken+2024") + copied_id = self.create_old_course_scope(course_id="course-v1:TestOrg+Free+2024") + + with self.assertRaises(self.key_migration.CourseScopeKeyConflictError) as raised: + with connection.schema_editor(atomic=False) as schema_editor: + self.key_migration.copy_course_keys_to_external_key(self.old_apps, schema_editor) + + self.assertIn(f"{conflicting_id}: course-v1:TestOrg+Taken+2024", str(raised.exception)) + external_keys = self.get_external_keys() + self.assertEqual(external_keys[copied_id], "course-v1:TestOrg+Free+2024") + self.assertIsNone(external_keys[conflicting_id]) + + def test_migration_keeps_the_copied_keys_when_it_stops_on_a_conflict(self): + """Keys copied before a conflict stay copied when migration 0013 stops. + + Expected Result: + - Migrating raises CourseScopeKeyConflictError. + - The scope without a conflict keeps its copied course key. + """ + copied_id = self.create_old_course_scope(course_id="course-v1:TestOrg+Free+2024") + self.create_old_course_scope(course_id="course-v1:TestOrg+Taken+2024") + self.old_course_scope_model.objects.create(external_key="course-v1:TestOrg+Taken+2024") + + with self.assertRaises(self.key_migration.CourseScopeKeyConflictError): + self.migrate_to_course_scope_key_migration() + + self.assertEqual(self.get_external_keys()[copied_id], "course-v1:TestOrg+Free+2024") + + def test_migration_stops_on_conflict_and_finishes_once_resolved(self): + """A conflict stops migration 0013 before the column is removed, and a later run finishes. + + Expected Result: + - Migrating raises CourseScopeKeyConflictError and course_overview_id still exists. + - After the conflicting scope is removed, migrating copies the key and removes the column. + """ + legacy_id = self.create_old_course_scope(course_id="course-v1:TestOrg+Taken+2024") + holder = self.old_course_scope_model.objects.create(external_key="course-v1:TestOrg+Taken+2024") + + with self.assertRaises(self.key_migration.CourseScopeKeyConflictError): + self.migrate_to_course_scope_key_migration() + + self.assertIn(COURSE_OVERVIEW_ID_COLUMN, get_column_names(COURSE_SCOPE_TABLE)) + holder.delete() + self.migrate_to_course_scope_key_migration() + self.assertEqual(self.get_external_keys()[legacy_id], "course-v1:TestOrg+Taken+2024") + self.assertNotIn(COURSE_OVERVIEW_ID_COLUMN, get_column_names(COURSE_SCOPE_TABLE)) class TestBuiltinSchemaSourceMigration(TestCase): @@ -586,7 +758,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_no_deletion(self): # Capture the state of permissions before rollback to verify that rollback restores the original state original_state_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -630,7 +802,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_no_deletion(self): self.assertEqual(len(permissions_with_no_errors), 12) # 3 users for each of the 4 roles = 12 total entries state_after_migration_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -740,7 +912,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_with_deletion(self): # Capture the state of permissions before rollback to verify that rollback restores the original state original_state_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -780,7 +952,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_with_deletion(self): self.assertEqual(len(permissions_with_no_errors), 12) state_after_migration_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -821,7 +993,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_with_no_new_role_equi # Capture the state of permissions before rollback to verify that rollback restores the original state original_state_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -875,7 +1047,7 @@ def test_migrate_legacy_course_roles_to_authz_and_rollback_with_no_new_role_equi self.assertEqual(len(permissions_with_errors), 9) state_after_migration_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -955,7 +1127,7 @@ def test_migrate_legacy_course_roles_to_authz_using_org_id(self): # Capture the state of permissions before rollback to verify that rollback restores the original state original_state_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") @@ -995,7 +1167,7 @@ def test_migrate_legacy_course_roles_to_authz_using_org_id(self): self.assertEqual(len(permissions_with_no_errors), 12) state_after_migration_user_subjects = list( - UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False) + UserSubject.objects.filter(casbin_rules__scope__coursescope__isnull=False) .distinct() .order_by("id") .values("id", "user_id") diff --git a/src/openedx_authz/tests/test_models.py b/src/openedx_authz/tests/test_models.py index f8aefdd3..c17ddc12 100644 --- a/src/openedx_authz/tests/test_models.py +++ b/src/openedx_authz/tests/test_models.py @@ -339,6 +339,41 @@ def test_existing_fk_only_scope_gets_external_key_backfilled(self): self.assertEqual(ContentLibraryScope.objects.filter(content_library=content_library).count(), 1) +class TestCourseScopeReferenceByKey(TestCase): + """CourseScope references its course only through external_key.""" + + def test_course_overview_follows_the_course_key(self): + """A scope created before its course returns no course, and returns it once it exists. + + Expected Result: + - course_overview is None while the course does not exist. + - course_overview returns the course created later with the same key. + """ + course_id = "course-v1:TestOrg+Rerun+2025" + scope = CourseScope.get_or_create_for_external_key(CourseOverviewData(external_key=course_id)) + self.assertIsNone(scope.course_overview) + + course_overview = CourseOverview.get_from_id(course_id) + + self.assertEqual(scope.course_overview, course_overview) + + def test_existing_course_reuses_one_scope(self): + """Getting the scope of an existing course twice returns the same row. + + Expected Result: + - Both calls return the same scope, and one scope exists for the key. + """ + course_id = "course-v1:TestOrg+Existing+2024" + CourseOverview.get_from_id(course_id) + scope_data = CourseOverviewData(external_key=course_id) + + first_scope = CourseScope.get_or_create_for_external_key(scope_data) + second_scope = CourseScope.get_or_create_for_external_key(scope_data) + + self.assertEqual(first_scope.id, second_scope.id) + self.assertEqual(CourseScope.objects.filter(external_key=course_id).count(), 1) + + class TestPolicyCacheControlModel(TestCase): """Test cases for the PolicyCacheControl model."""