-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_migrations.py
More file actions
1241 lines (1055 loc) · 56.3 KB
/
test_migrations.py
File metadata and controls
1241 lines (1055 loc) · 56.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit Tests for openedx_authz migrations."""
from unittest.mock import patch
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 openedx_authz.api.users import batch_unassign_role_from_users, get_user_role_assignments_in_scope
from openedx_authz.constants.roles import (
COURSE_ADMIN,
COURSE_DATA_RESEARCHER,
COURSE_LIMITED_STAFF,
COURSE_STAFF,
LEGACY_COURSE_ROLE_EQUIVALENCES,
LIBRARY_ADMIN,
LIBRARY_USER,
)
from openedx_authz.engine.enforcer import AuthzEnforcer
from openedx_authz.engine.utils import (
migrate_authz_to_legacy_course_roles,
migrate_legacy_course_roles_to_authz,
migrate_legacy_permissions,
)
from openedx_authz.models.subjects import UserSubject
from openedx_authz.tests.stubs.models import (
ContentLibrary,
ContentLibraryPermission,
CourseAccessRole,
CourseOverview,
Organization,
)
User = get_user_model()
# Specify a unique prefix to avoid collisions with existing data
OBJECT_PREFIX = "tmlp_"
org_name = f"{OBJECT_PREFIX}org_full_name"
org_short_name = f"{OBJECT_PREFIX}org"
lib_name = f"{OBJECT_PREFIX}library"
group_name = f"{OBJECT_PREFIX}test_group"
user_names = [f"{OBJECT_PREFIX}user{i}" for i in range(3)]
group_user_names = [f"{OBJECT_PREFIX}guser{i}" for i in range(3)]
error_user_name = f"{OBJECT_PREFIX}error_user"
error_group_name = f"{OBJECT_PREFIX}error_group"
empty_group_name = f"{OBJECT_PREFIX}empty_group"
class TestLegacyContentLibraryPermissionsMigration(TestCase):
"""Test cases for migrating legacy content library permissions."""
def setUp(self):
"""
Set up test data:
What this does:
1. Creates an Org and a ContentLibrary
2. Create Users and Groups
3. Assign legacy permissions using ContentLibraryPermission
4. Create invalid permissions for user and group
"""
# Create ContentLibrary
org = Organization.objects.create(name=org_name, short_name=org_short_name)
library = ContentLibrary.objects.create(org=org, slug=lib_name)
# Create Users and Groups
users = [
User.objects.create_user(username=user_name, email=f"{user_name}@example.com") for user_name in user_names
]
group_users = [
User.objects.create_user(username=user_name, email=f"{user_name}@example.com")
for user_name in group_user_names
]
group = Group.objects.create(name=group_name)
group.user_set.set(group_users)
error_user = User.objects.create_user(username=error_user_name, email=f"{error_user_name}@example.com")
error_group = Group.objects.create(name=error_group_name)
error_group.user_set.set([error_user])
empty_group = Group.objects.create(name=empty_group_name)
# Assign legacy permissions for users and group
for user in users:
ContentLibraryPermission.objects.create(
user=user,
library=library,
access_level=ContentLibraryPermission.ADMIN_LEVEL,
)
ContentLibraryPermission.objects.create(
group=group,
library=library,
access_level=ContentLibraryPermission.READ_LEVEL,
)
# Create invalid permissions for testing error logging
ContentLibraryPermission.objects.create(
user=error_user,
library=library,
access_level="invalid",
)
ContentLibraryPermission.objects.create(
group=error_group,
library=library,
access_level="invalid",
)
# Edge case: empty group with no users
ContentLibraryPermission.objects.create(
group=empty_group,
library=library,
access_level=ContentLibraryPermission.READ_LEVEL,
)
def tearDown(self):
"""
Clean up test data created for the migration test.
"""
super().tearDown()
AuthzEnforcer.get_enforcer().load_policy()
batch_unassign_role_from_users(
users=user_names,
role_external_key=LIBRARY_ADMIN.external_key,
scope_external_key=f"lib:{org_short_name}:{lib_name}",
)
batch_unassign_role_from_users(
users=group_user_names,
role_external_key=LIBRARY_USER.external_key,
scope_external_key=f"lib:{org_short_name}:{lib_name}",
)
ContentLibrary.objects.filter(slug=lib_name).delete()
Organization.objects.filter(name=org_name).delete()
Group.objects.filter(name=group_name).delete()
Group.objects.filter(name=error_group_name).delete()
Group.objects.filter(name=empty_group_name).delete()
for user_name in user_names + group_user_names + [error_user_name]:
User.objects.filter(username=user_name).delete()
def test_migration(self):
"""Test the migration of legacy permissions.
1. Rus the migration to migrate legacy permissions.
2. Check that each user has the expected role in the new model.
3. Check that the group users have the expected role in the new model.
4. Check that invalid permissions were identified correctly as errors.
"""
permissions_with_errors = migrate_legacy_permissions(ContentLibraryPermission)
AuthzEnforcer.get_enforcer().load_policy()
for user_name in user_names:
assignments = get_user_role_assignments_in_scope(
user_external_key=user_name, scope_external_key=f"lib:{org_short_name}:{lib_name}"
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], LIBRARY_ADMIN)
for group_user_name in group_user_names:
assignments = get_user_role_assignments_in_scope(
user_external_key=group_user_name, scope_external_key=f"lib:{org_short_name}:{lib_name}"
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], LIBRARY_USER)
self.assertEqual(len(permissions_with_errors), 2)
class TestLegacyCourseAuthoringPermissionsMigration(TestCase):
"""Test cases for migrating legacy course authoring permissions."""
def setUp(self):
"""
Set up test data:
What this does:
1. Defines an Org and a CourseKey for the test course
2. Create Users for each legacy role and an additional user for testing invalid permissions
3. Assign legacy permissions using CourseAccessRole for each user and role combination
4. Create invalid permissions for user to test error logging
- Notes:
- CourseAccessRole does not have a group concept, so we are only assigning
permissions to individual users in this test.
- The only roles we are migrating are instructor, staff, limited_staff and data_researcher,
any other role in CourseAccessRole will be considered invalid for the purpose of this test.
"""
# Defining course identifiers
self.org = org_short_name
self.course_id = f"course-v1:{self.org}+{OBJECT_PREFIX}course+2024"
default_course_fields = {
"org": self.org,
"course_id": self.course_id,
}
self.invalid_course = f"ccx-v1:{self.org}+{OBJECT_PREFIX}+2026_01+ccx@2"
self.course_overview = CourseOverview.objects.create(
id=self.course_id, org=self.org, display_name=f"{OBJECT_PREFIX} Course"
)
# Create lists to hold legacy role objects for cleanup and verification purposes
self.admin_legacy_roles = []
self.staff_legacy_roles = []
self.limited_staff_legacy_roles = []
self.data_researcher_legacy_roles = []
# Create users for each legacy role and an additional user for testing invalid permissions
self.admin_users = [
User.objects.create_user(username=f"admin_{user_name}", email=f"admin_{user_name}@example.com")
for user_name in user_names
]
self.staff_users = [
User.objects.create_user(username=f"staff_{user_name}", email=f"staff_{user_name}@example.com")
for user_name in user_names
]
self.limited_staff = [
User.objects.create_user(
username=f"limited_staff_{user_name}", email=f"limited_staff_{user_name}@example.com"
)
for user_name in user_names
]
self.data_researcher = [
User.objects.create_user(
username=f"data_researcher_{user_name}", email=f"data_researcher_{user_name}@example.com"
)
for user_name in user_names
]
self.error_user = User.objects.create_user(username=error_user_name, email=f"{error_user_name}@example.com")
# Assign legacy permissions for users based on their role
for user in self.admin_users:
leg_role = CourseAccessRole.objects.create(
**default_course_fields,
user=user,
role="instructor",
)
self.admin_legacy_roles.append(leg_role)
for user in self.staff_users:
leg_role = CourseAccessRole.objects.create(
**default_course_fields,
user=user,
role="staff",
)
self.staff_legacy_roles.append(leg_role)
for user in self.limited_staff:
leg_role = CourseAccessRole.objects.create(
**default_course_fields,
user=user,
role="limited_staff",
)
self.limited_staff_legacy_roles.append(leg_role)
for user in self.data_researcher:
leg_role = CourseAccessRole.objects.create(
**default_course_fields,
user=user,
role="data_researcher",
)
self.data_researcher_legacy_roles.append(leg_role)
# Create invalid permission for testing error logging
CourseAccessRole.objects.create(
**default_course_fields,
user=self.error_user,
role="invalid-legacy-role",
)
class MockPermission:
"""Mock class to simulate CourseAccessRole entries for testing the rollback migration."""
def __init__(self, user, role, course_id, id_in):
self.user = user
self.role = role
self.course_id = course_id
self.id = id_in
class MockUser:
"""Mock class to simulate User objects for testing the rollback migration."""
def __init__(self, username):
self.username = username
class MockQuerySet:
"""Mock class to simulate QuerySet behavior for testing the rollback migration."""
def __init__(self, permissions):
self.permissions = permissions
def filter(self, **kwargs):
return self
def select_related(self, *args, **kwargs):
return self
def all(self):
return self.permissions
def get_or_create(self):
raise Exception("Unexpected error mock")
class MockCourseAccessRole:
"""Mock class to simulate CourseAccessRole manager for testing the rollback migration."""
objects = MockQuerySet(
[
MockPermission(MockUser("testuser"), "instructor", "course-v1:test", 1),
]
)
self.mock_course_access_role = MockCourseAccessRole
def tearDown(self):
"""
Clean up test data created for the migration test.
"""
super().tearDown()
AuthzEnforcer.get_enforcer().load_policy()
admin_users_names = [user.username for user in self.admin_users]
staff_users_names = [user.username for user in self.staff_users]
limited_staff_users_names = [user.username for user in self.limited_staff]
data_researcher_users_names = [user.username for user in self.data_researcher]
batch_unassign_role_from_users(
users=admin_users_names,
role_external_key=COURSE_ADMIN.external_key,
scope_external_key=self.course_id,
)
batch_unassign_role_from_users(
users=staff_users_names,
role_external_key=COURSE_STAFF.external_key,
scope_external_key=self.course_id,
)
batch_unassign_role_from_users(
users=limited_staff_users_names,
role_external_key=COURSE_LIMITED_STAFF.external_key,
scope_external_key=self.course_id,
)
batch_unassign_role_from_users(
users=data_researcher_users_names,
role_external_key=COURSE_DATA_RESEARCHER.external_key,
scope_external_key=self.course_id,
)
def test_legacy_course_role_equivalences_mapping(self):
"""Test that the LEGACY_COURSE_ROLE_EQUIVALENCES mapping contains no duplicate values."""
legacy_roles = LEGACY_COURSE_ROLE_EQUIVALENCES.keys()
new_roles = LEGACY_COURSE_ROLE_EQUIVALENCES.values()
# Check that there are no duplicate values in the mapping
self.assertEqual(
len(legacy_roles), len(set(new_roles)), "LEGACY_COURSE_ROLE_EQUIVALENCES contains duplicate values"
)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_and_rollback_no_deletion(self):
"""Test the migration of legacy permissions from CourseAccessRole to the new Casbin-based model
and the rollback functionality without deletion.
1. Run the migration to migrate legacy permissions from CourseAccessRole to the
new model with delete_after_migration set to False.
- Notes:
- The migration function should correctly map legacy roles to
the new roles based on the defined mapping in the migration function.
- Any legacy role that does not have a defined mapping should be logged as an error
and not migrated.
- After migration, all legacy CourseAccessRole entries should not be deleted
since we set delete_after_migration to False.
2. Check that each user has the expected role in the new model.
3. Check that invalid permissions were identified correctly as errors.
4. Rollback the migration and check that each user has the expected legacy role and
that all migrated permissions were rolled back successfully.
"""
# Capture the old state of permissions for rollback testing
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
self.assertEqual(
len(user_names), 3
) # Sanity check to ensure we have the expected number of users for each role
self.assertEqual(
len(original_state_access_roles), 13
) # 3 users for each of the 4 roles + 1 invalid role = 13 total entries
# Migrate from legacy CourseAccessRole to new Casbin-based model
course_id_list = [self.course_id]
permissions_with_errors, permissions_with_no_errors = migrate_legacy_course_roles_to_authz(
CourseAccessRole, course_id_list=course_id_list, org_id=None, delete_after_migration=False
)
AuthzEnforcer.get_enforcer().load_policy()
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_ADMIN)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_STAFF)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_LIMITED_STAFF)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_DATA_RESEARCHER)
self.assertEqual(len(permissions_with_errors), 1)
self.assertEqual(permissions_with_errors[0].user.username, self.error_user.username)
self.assertEqual(permissions_with_errors[0].role, "invalid-legacy-role")
self.assertEqual(len(permissions_with_no_errors), 12) # 3 users for each of the 4 roles = 12 total entries
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# 3 users for each of the 4 roles + 1 invalid role = 13 total entries
self.assertEqual(len(after_migrate_state_access_roles), 13)
# Must be the same before and after migration since we set delete_after_migration to False
self.assertEqual(original_state_access_roles, after_migrate_state_access_roles)
# Now let's rollback
# 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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
permissions_with_errors, permissions_with_no_errors = migrate_authz_to_legacy_course_roles(
CourseAccessRole, UserSubject, course_id_list=course_id_list, org_id=None, delete_after_migration=False
)
# Check that each user has the expected legacy role after rollback and that errors
# are logged for any permissions that could not be rolled back
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_ADMIN)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_STAFF)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_LIMITED_STAFF)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_DATA_RESEARCHER)
self.assertEqual(len(permissions_with_errors), 0)
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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# The number of CourseAccessRole entries should be the same as the original state
# since we are not deleting any entries in this test.
self.assertEqual(len(original_state_access_roles), 13)
# All original entries should still be there since we are not deleting any entries
# and when creating new entries for the users that were migrated back to legacy roles,
# we are creating them with get_or_create which will not create duplicates if an entry
# with the same user, org, course_id and role already exists.
self.assertEqual(len(after_migrate_state_access_roles), 13)
# Sanity check to ensure we have the expected number of UserSubjects related to
# the course permissions before migration (3 users * 4 roles = 12)
self.assertEqual(len(original_state_user_subjects), 12)
# After rollback, we should have the same 12 UserSubjects related to the course permissions
# since we are not deleting any entries in this test,
self.assertEqual(len(state_after_migration_user_subjects), 12)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_and_rollback_with_deletion(self):
"""Test the migration of legacy permissions from CourseAccessRole to
the new Casbin-based model with deletion of legacy permissions after migration.
1. Run the migration to migrate legacy permissions from CourseAccessRole to the
new model with delete_after_migration set to True.
- Notes:
- The migration function should correctly map legacy roles to
the new roles based on the defined mapping in the migration function.
- Any legacy role that does not have a defined mapping should be logged as an error
and not migrated.
- After migration, all legacy CourseAccessRole entries should be deleted
since we set delete_after_migration to True.
2. Check that each user has the expected role in the new model.
3. Check that invalid permissions were identified correctly as errors.
4. Rollback the migration and check that each user has the expected legacy role and
that all migrated permissions were rolled back successfully.
"""
# Capture the old state of permissions for rollback testing
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
self.assertEqual(
len(user_names), 3
) # Sanity check to ensure we have the expected number of users for each role
self.assertEqual(
len(original_state_access_roles), 13
) # 3 users for each of the 4 roles + 1 invalid role = 13 total entries
course_id_list = [self.course_id]
# Migrate from legacy CourseAccessRole to new Casbin-based model
permissions_with_errors, permissions_with_no_errors = migrate_legacy_course_roles_to_authz(
CourseAccessRole, course_id_list=course_id_list, org_id=None, delete_after_migration=True
)
AuthzEnforcer.get_enforcer().load_policy()
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_ADMIN)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_STAFF)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_LIMITED_STAFF)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_DATA_RESEARCHER)
self.assertEqual(len(permissions_with_errors), 1)
self.assertEqual(permissions_with_errors[0].user.username, self.error_user.username)
self.assertEqual(permissions_with_errors[0].role, "invalid-legacy-role")
self.assertEqual(len(permissions_with_no_errors), 12) # 3 users for each of the 4 roles = 12 total entries
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
self.assertEqual(len(original_state_access_roles), 13)
# Only the invalid role entry should remain since we set delete_after_migration to True
self.assertEqual(len(after_migrate_state_access_roles), 1)
# Must be different before and after migration since we set delete_after_migration
# to True and we are deleting all
self.assertNotEqual(original_state_access_roles, after_migrate_state_access_roles)
# Now let's rollback
# 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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
permissions_with_errors, permissions_with_no_errors = migrate_authz_to_legacy_course_roles(
CourseAccessRole, UserSubject, course_id_list=course_id_list, org_id=None, delete_after_migration=True
)
# Check that each user has the expected legacy role after rollback
# and that errors are logged for any permissions that could not be rolled back
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
self.assertEqual(len(permissions_with_errors), 0)
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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# Before the rollback, we should only have the 1 invalid role entry
# since we set delete_after_migration to True in the migration.
self.assertEqual(len(original_state_access_roles), 1)
# All original entries + 3 users * 4 roles = 12
# plus the original invalid entry = 1 + 12 = 13 total entries
self.assertEqual(len(after_migrate_state_access_roles), 1 + 12)
# Sanity check to ensure we have the expected number of UserSubjects related to
# the course permissions before migration (3 users * 4 roles = 12)
self.assertEqual(len(original_state_user_subjects), 12)
# After rollback, we should have 0 UserSubjects related to the course permissions
self.assertEqual(len(state_after_migration_user_subjects), 0)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_and_rollback_with_no_new_role_equivalent(self):
"""Test the migration of legacy course roles to the new Casbin-based model
and the rollback when there is no equivalent new role.
"""
# Migrate from legacy CourseAccessRole to new Casbin-based model
course_id_list = [self.course_id]
permissions_with_errors, _ = migrate_legacy_course_roles_to_authz(
CourseAccessRole, course_id_list=course_id_list, org_id=None, delete_after_migration=True
)
AuthzEnforcer.get_enforcer().load_policy()
# Now let's rollback
# 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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# Mock the COURSE_ROLE_EQUIVALENCES mapping to only include a mapping
# for COURSE_ADMIN to simulate the scenario where the staff, limited_staff
# and data_researcher roles do not have a legacy role equivalent and
# therefore cannot be migrated back to legacy roles during the rollback.
with patch(
"openedx_authz.engine.utils.COURSE_ROLE_EQUIVALENCES",
{COURSE_ADMIN.external_key: "instructor"},
):
permissions_with_errors, _ = migrate_authz_to_legacy_course_roles(
CourseAccessRole, UserSubject, course_id_list=course_id_list, org_id=None, delete_after_migration=True
)
# Check that each user has the expected legacy role after rollback
# and that errors are logged for any permissions that could not be rolled back
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
# Since we are mocking the COURSE_ROLE_EQUIVALENCES mapping to only include a mapping for COURSE_ADMIN,
# the staff role will not have a legacy role equivalent and therefore should not be migrated back
self.assertEqual(len(assignments), 1)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
# Since we are mocking the COURSE_ROLE_EQUIVALENCES mapping to only include a mapping for COURSE_ADMIN,
# the limited_staff role will not have a legacy role equivalent and therefore should not be migrated back
self.assertEqual(len(assignments), 1)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
# Since we are mocking the COURSE_ROLE_EQUIVALENCES mapping to only include a mapping for COURSE_ADMIN,
# the data_researcher role will not have a legacy role equivalent and therefore should not be migrated back
self.assertEqual(len(assignments), 1)
# 3 staff + 3 limited_staff + 3 data_researcher = 9 entries with no legacy role equivalent
self.assertEqual(len(permissions_with_errors), 9)
state_after_migration_user_subjects = list(
UserSubject.objects.filter(casbin_rules__scope__coursescope__course_overview__isnull=False)
.distinct()
.order_by("id")
.values("id", "user_id")
)
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# Before the rollback, we should only have the 1 invalid role entry
# since we set delete_after_migration to True in the migration.
self.assertEqual(len(original_state_access_roles), 1)
# All original entries (1) + 3 users * 1 roles = 4
self.assertEqual(len(after_migrate_state_access_roles), 1 + 3)
# Before the rollback, we should have the 12 UserSubjects related to the course permissions
# since we had 3 users with 4 roles each in the original state.
self.assertEqual(len(original_state_user_subjects), 12)
# After rollback, we should have 9 UserSubjects related to the course permissions
# since the users with staff, limited_staff and data_researcher roles will not be
# migrated back to legacy roles due to our mocked COURSE_ROLE_EQUIVALENCES mapping.
self.assertEqual(len(state_after_migration_user_subjects), 9)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_using_org_id(self):
"""Test the migration of legacy course roles to the new Casbin-based model
and the rollback when there is no equivalent new role.
"""
# Migrate from legacy CourseAccessRole to new Casbin-based model
permissions_with_errors, permissions_with_no_errors = migrate_legacy_course_roles_to_authz(
CourseAccessRole, course_id_list=None, org_id=self.org, delete_after_migration=True
)
AuthzEnforcer.get_enforcer().load_policy()
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_ADMIN)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_STAFF)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_LIMITED_STAFF)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 1)
self.assertEqual(assignments[0].roles[0], COURSE_DATA_RESEARCHER)
self.assertEqual(len(permissions_with_errors), 1)
self.assertEqual(permissions_with_errors[0].user.username, self.error_user.username)
self.assertEqual(permissions_with_errors[0].role, "invalid-legacy-role")
self.assertEqual(len(permissions_with_no_errors), 12) # 3 users for each of the 4 roles = 12 total entries
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# self.assertEqual(len(original_state_access_roles), 13)
# Only the invalid role entry should remain since we set delete_after_migration to True
self.assertEqual(len(after_migrate_state_access_roles), 1)
# Must be different before and after migration since we set delete_after_migration
# to True and we are deleting all
# Now let's rollback
# 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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
original_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
permissions_with_errors, permissions_with_no_errors = migrate_authz_to_legacy_course_roles(
CourseAccessRole, UserSubject, course_id_list=None, org_id=self.org, delete_after_migration=True
)
# Check that each user has the expected legacy role after rollback
# and that errors are logged for any permissions that could not be rolled back
for user in self.admin_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.staff_users:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.limited_staff:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
for user in self.data_researcher:
assignments = get_user_role_assignments_in_scope(
user_external_key=user.username, scope_external_key=self.course_id
)
self.assertEqual(len(assignments), 0)
self.assertEqual(len(permissions_with_errors), 0)
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)
.distinct()
.order_by("id")
.values("id", "user_id")
)
after_migrate_state_access_roles = list(
CourseAccessRole.objects.all().order_by("id").values("id", "user_id", "org", "course_id", "role")
)
# Before the rollback, we should only have the 1 invalid role entry
# since we set delete_after_migration to True in the migration.
self.assertEqual(len(original_state_access_roles), 1)
# All original entries + 3 users * 4 roles = 12
# plus the original invalid entry = 1 + 12 = 13 total entries
self.assertEqual(len(after_migrate_state_access_roles), 1 + 12)
# Sanity check to ensure we have the expected number of UserSubjects related to
# the course permissions before migration (3 users * 4 roles = 12)
self.assertEqual(len(original_state_user_subjects), 12)
# After rollback, we should have 0 UserSubjects related to the course permissions
self.assertEqual(len(state_after_migration_user_subjects), 0)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_authz_to_legacy_course_roles_with_no_org_and_courses(self):
# Migrate from legacy CourseAccessRole to new Casbin-based model
with self.assertRaises(ValueError):
migrate_authz_to_legacy_course_roles(
CourseAccessRole, UserSubject, course_id_list=None, org_id=None, delete_after_migration=True
)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_authz_to_legacy_course_roles_with_invalid_courses(self):
with self.assertRaises(ValueError):
migrate_authz_to_legacy_course_roles(
CourseAccessRole,
UserSubject,
course_id_list=[self.invalid_course],
org_id=None,
delete_after_migration=True,
)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_with_no_org_and_courses(self):
# Migrate from legacy CourseAccessRole to new Casbin-based model
with self.assertRaises(ValueError):
migrate_legacy_course_roles_to_authz(
CourseAccessRole, course_id_list=None, org_id=None, delete_after_migration=True
)
@patch("openedx_authz.api.data.CourseOverview", CourseOverview)
def test_migrate_legacy_course_roles_to_authz_with_invalid_courses(self):
with self.assertRaises(ValueError):
migrate_legacy_course_roles_to_authz(
CourseAccessRole,
course_id_list=[self.invalid_course],
org_id=None,
delete_after_migration=True,
)
@patch("openedx_authz.management.commands.authz_migrate_course_authoring.CourseAccessRole", CourseAccessRole)
@patch("openedx_authz.management.commands.authz_migrate_course_authoring.migrate_legacy_course_roles_to_authz")
def test_authz_migrate_course_authoring_command(self, mock_migrate):
"""
Verify that the authz_migrate_course_authoring command
calls migrate_legacy_course_roles_to_authz with the correct arguments.
"""
mock_migrate.return_value = [], [] # Return empty lists for permissions with and without errors
# Run without --delete
call_command("authz_migrate_course_authoring", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
args, kwargs = mock_migrate.call_args
self.assertEqual(kwargs["delete_after_migration"], False)
mock_migrate.reset_mock()
# Run with --delete
with patch("builtins.input", return_value="yes"):
call_command("authz_migrate_course_authoring", "--delete", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
args, kwargs = mock_migrate.call_args
self.assertEqual(kwargs["delete_after_migration"], True)
@patch("openedx_authz.management.commands.authz_migrate_course_authoring.CourseAccessRole", CourseAccessRole)
@patch("openedx_authz.management.commands.authz_migrate_course_authoring.migrate_legacy_course_roles_to_authz")
def test_authz_migrate_course_authoring_command_mixed_success(self, mock_migrate):
"""
Verify that the authz_migrate_course_authoring command outputs without errors
for mixed success operations.
"""
mock_migrate.return_value = (
["course-v1:fail"],
[self.course_id],
) # Return one success and one failure
call_command("authz_migrate_course_authoring", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
# Return only one success
mock_migrate.reset_mock()
mock_migrate.return_value = (
[],
[self.course_id],
)
call_command("authz_migrate_course_authoring", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
# Return only one failure
mock_migrate.reset_mock()
mock_migrate.return_value = (
[self.course_id],
[],
)
call_command("authz_migrate_course_authoring", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
# Return only no successes or failures
mock_migrate.reset_mock()
mock_migrate.return_value = (
[],
[],
)
call_command("authz_migrate_course_authoring", "--course-id-list", self.course_id)
mock_migrate.assert_called_once()
@patch("openedx_authz.management.commands.authz_rollback_course_authoring.CourseAccessRole", CourseAccessRole)
@patch("openedx_authz.management.commands.authz_rollback_course_authoring.migrate_authz_to_legacy_course_roles")
def test_authz_rollback_course_authoring_command(self, mock_rollback):
"""
Verify that the authz_rollback_course_authoring command
calls migrate_authz_to_legacy_course_roles correctly.
"""
mock_rollback.return_value = [], [] # Return empty lists for permissions with and without errors
# Run without --delete