Skip to content

Commit

Permalink
fix: partial clean up not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
shadinaif committed Oct 22, 2024
1 parent 6a74d43 commit ca6f77a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion futurex_openedx_extensions/helpers/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,12 +1275,15 @@ def _clean_course_access_roles_partial(
if not roles_to_restore:
return

CourseAccessRole.objects.bulk_create(roles_to_restore)
creator_orgs = []
non_creator_roles = []
for role in roles_to_restore:
if role.role == cs.COURSE_CREATOR_ROLE_TENANT:
creator_orgs.append(role.org)
else:
non_creator_roles.append(role)

CourseAccessRole.objects.bulk_create(non_creator_roles)
add_org_course_creator(caller, user, creator_orgs)


Expand Down
22 changes: 22 additions & 0 deletions tests/test_helpers/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2354,3 +2354,25 @@ def test_clean_course_access_roles_partial(
_clean_course_access_roles_partial(None, user, tenant_ids, roles_to_keep)
mock_delete.assert_called_once_with(tenant_ids, user)
assert mock_create.call_count == 2


@pytest.mark.django_db
def test_clean_course_access_roles_partial_course_creator(
roles_authorize_caller, base_data,
): # pylint: disable=unused-argument
"""Verify that _clean_course_access_roles_partial correctly restore course creator roles."""
user = get_user_model().objects.get(username='user70')
assert CourseAccessRole.objects.filter(user=user).count() == 0
tenant_ids = [1, 2]
roles = []
for org in ['org1', 'org2']:
Organization.objects.create(short_name=org)
roles.append(CourseAccessRole(user=user, role=cs.COURSE_CREATOR_ROLE_TENANT, org=org))
add_org_course_creator(None, user, ['org1', 'org2'])

_clean_course_access_roles_partial(None, user, tenant_ids, roles)
assert CourseCreator.objects.filter(user=user).exists()
assert CourseCreator.objects.get(user=user).organizations.count() == 2
assert CourseCreator.objects.get(user=user).all_organizations is False
assert CourseAccessRole.objects.filter(user=user).count() == 2
assert CourseAccessRole.objects.filter(user=user, role=cs.COURSE_CREATOR_ROLE_TENANT).count() == 2

0 comments on commit ca6f77a

Please sign in to comment.