|
3 | 3 | # pylint: disable=redefined-outer-name |
4 | 4 | import itertools |
5 | 5 | from datetime import timedelta |
| 6 | +from unittest.mock import patch |
6 | 7 | from urllib.parse import parse_qsl |
7 | 8 |
|
8 | 9 | import factory |
|
43 | 44 | unsubscribe_from_edx_course_emails, |
44 | 45 | update_edx_user_email, |
45 | 46 | update_edx_user_name, |
| 47 | + update_edx_user_profile, |
46 | 48 | validate_username_email_with_edx, |
47 | 49 | ) |
48 | 50 | from openedx.constants import ( |
|
57 | 59 | EdxApiEmailSettingsErrorException, |
58 | 60 | EdxApiEnrollErrorException, |
59 | 61 | EdxApiRegistrationValidationException, |
| 62 | + EdxApiUserUpdateError, |
60 | 63 | OpenEdxUserMissingError, |
61 | 64 | UnknownEdxApiEmailSettingsException, |
62 | 65 | UnknownEdxApiEnrollException, |
@@ -1139,3 +1142,54 @@ def test_reconcile_edx_username_conflict(): |
1139 | 1142 |
|
1140 | 1143 | assert user.edx_username in new_user.edx_username |
1141 | 1144 | assert user.edx_username != new_user.edx_username |
| 1145 | + |
| 1146 | + |
| 1147 | +@patch("openedx.api.get_valid_edx_api_auth") |
| 1148 | +@patch("openedx.api.requests.Session") |
| 1149 | +def test_update_edx_user_profile_success(mock_session, mock_get_auth, mocker, user): |
| 1150 | + """ |
| 1151 | + Test that update_edx_user_profile makes a call to update the user profile in Open edX via an API client |
| 1152 | + """ |
| 1153 | + mock_auth = mocker.MagicMock() |
| 1154 | + mock_auth.access_token = "token" # noqa: S105 |
| 1155 | + mock_get_auth.return_value = mock_auth |
| 1156 | + |
| 1157 | + mock_resp = mocker.MagicMock() |
| 1158 | + mock_resp.status_code = 200 |
| 1159 | + mock_session.return_value.patch.return_value = mock_resp |
| 1160 | + |
| 1161 | + update_edx_user_profile(user) |
| 1162 | + mock_session.return_value.patch.assert_called_once() |
| 1163 | + |
| 1164 | + |
| 1165 | +@patch("openedx.api.get_valid_edx_api_auth") |
| 1166 | +@patch("openedx.api.requests.Session") |
| 1167 | +def test_update_edx_user_profile_no_openedx_user( |
| 1168 | + mock_session, mock_get_auth, user, caplog |
| 1169 | +): |
| 1170 | + """ |
| 1171 | + Test that update_edx_user_profile does not attempt to update the user profile in Open edX when Open edX user is not synced |
| 1172 | + """ |
| 1173 | + user.openedx_users.all().delete() |
| 1174 | + update_edx_user_profile(user) |
| 1175 | + assert "Skipping user profile update" in caplog.text |
| 1176 | + |
| 1177 | + |
| 1178 | +@patch("openedx.api.get_valid_edx_api_auth") |
| 1179 | +@patch("openedx.api.requests.Session") |
| 1180 | +def test_update_edx_user_profile_error(mock_session, mock_get_auth, mocker, user): |
| 1181 | + """ |
| 1182 | + Test that update_edx_user_profile raises an EdxApiUserUpdateError if the request fails |
| 1183 | + """ |
| 1184 | + mock_auth = mocker.MagicMock() |
| 1185 | + mock_auth.access_token = "token" # noqa: S105 |
| 1186 | + mock_get_auth.return_value = mock_auth |
| 1187 | + |
| 1188 | + mock_resp = mocker.MagicMock() |
| 1189 | + mock_resp.status_code = 400 |
| 1190 | + mock_session.return_value.patch.return_value = mock_resp |
| 1191 | + |
| 1192 | + with patch("openedx.api.get_error_response_summary", return_value="error summary"): |
| 1193 | + with pytest.raises(EdxApiUserUpdateError) as exc: |
| 1194 | + update_edx_user_profile(user) |
| 1195 | + assert "Error updating Open edX user" in str(exc.value) |
0 commit comments