Skip to content

Commit abddeff

Browse files
committed
refactor
1 parent 48c43c2 commit abddeff

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/ol_openedx_lti_utilities/ol_openedx_lti_utilities/views.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66

7+
from django.db import transaction
78
from django.http import Http404, HttpResponseBadRequest
89
from edx_rest_framework_extensions import permissions
910
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
@@ -18,6 +19,7 @@
1819
from rest_framework import status
1920
from rest_framework.response import Response
2021
from rest_framework.views import APIView
22+
from social_django.models import UserSocialAuth
2123

2224
log = logging.getLogger(__name__)
2325

@@ -85,25 +87,34 @@ def post(self, request):
8587

8688
# A user that is created by LTI will always have the same username as
8789
# lti_user_id in LtiUser table.
88-
lti_user = LtiUser.objects.filter(edx_user__email=user_email).first()
89-
if not lti_user:
90-
log.error("No user was found against the given email (%s)", user_email)
91-
raise Http404
92-
if lti_user.lti_user_id != lti_user.edx_user.username:
93-
log.error(
94-
"User with email (%s) does not appear to be an LTI-created user",
95-
user_email,
96-
)
97-
return HttpResponseBadRequest(
98-
"User with the given email does not appear to be an LTI-created user."
99-
)
100-
101-
user = lti_user.edx_user
102-
user.email = user.email.split("@")[0] + "@" + PLACEHOLDER_EMAIL_DOMAIN
103-
user.save()
90+
with transaction.atomic():
91+
lti_user = LtiUser.objects.filter(edx_user__email=user_email).first()
92+
if not lti_user:
93+
log.error("No user was found against the given email (%s)", user_email)
94+
raise Http404
95+
if lti_user.lti_user_id != lti_user.edx_user.username:
96+
log.error(
97+
"User with email (%s) does not appear to be an LTI-created user",
98+
user_email,
99+
)
100+
return HttpResponseBadRequest(
101+
"User with the given email does not appear to be an "
102+
"LTI-created user."
103+
)
104+
105+
user = lti_user.edx_user
106+
user.email = user.email.split("@")[0] + "@" + PLACEHOLDER_EMAIL_DOMAIN
107+
user.save()
108+
# Remove social auth records for this user
109+
UserSocialAuth.objects.filter(user=user).delete()
110+
# Remove the old LTI mapping so that a new one gets created the next time
111+
# users access edX via LTI
112+
lti_user.delete()
113+
104114
# Send the user for retirement and deactivate the account
105115
try:
106116
create_retirement_request_and_deactivate_account(user)
107117
except Exception as e: # noqa: BLE001
108118
log.error("Error retiring and deactivating user: %s", e) # noqa: TRY400
119+
109120
return Response(status=status.HTTP_200_OK)

src/ol_openedx_lti_utilities/tests/test_views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def setUp(self):
3030
@patch(
3131
"ol_openedx_lti_utilities.views.create_retirement_request_and_deactivate_account"
3232
)
33-
def test_successful_lti_user_fix(self, mock_retirement_user, mock_lti_user_model):
33+
@patch("ol_openedx_lti_utilities.views.UserSocialAuth.objects")
34+
def test_successful_lti_user_fix(
35+
self, mock_user_social_auth_objects, mock_retirement_user, mock_lti_user_model
36+
):
3437
"""Test successful LTI user fix."""
3538
# Mock LTI user with bad username
3639
mock_lti_user = Mock()
@@ -50,6 +53,8 @@ def test_successful_lti_user_fix(self, mock_retirement_user, mock_lti_user_model
5053
response = self.client.post(self.url, payload)
5154
mock_edx_user.save.assert_called_once()
5255
mock_retirement_user.assert_called_once_with(mock_edx_user)
56+
mock_qs = mock_user_social_auth_objects.filter.return_value
57+
mock_user_social_auth_objects.filter.return_value.delete.assert_called_once()
5358

5459
assert response.status_code == status.HTTP_200_OK
5560

0 commit comments

Comments
 (0)