|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import datetime |
7 | | -import itertools |
8 | 7 | import unicodedata |
9 | 8 | from unittest.mock import Mock, patch |
10 | 9 |
|
|
18 | 17 | from django.test.client import RequestFactory |
19 | 18 | from django.urls import reverse |
20 | 19 | from pytz import UTC |
21 | | -from social_django.models import UserSocialAuth |
22 | 20 |
|
23 | 21 | from common.djangoapps.student.models import ( |
24 | 22 | AccountRecovery, |
@@ -104,10 +102,12 @@ def setUp(self): |
104 | 102 | self.staff_user = UserFactory(is_staff=True, password=self.password) |
105 | 103 | self.reset_tracker() |
106 | 104 |
|
107 | | - enterprise_patcher = patch('openedx.features.enterprise_support.api.enterprise_customer_for_request') |
108 | | - enterprise_learner_patcher = enterprise_patcher.start() |
109 | | - enterprise_learner_patcher.return_value = {} |
110 | | - self.addCleanup(enterprise_learner_patcher.stop) |
| 105 | + filter_patcher = patch( |
| 106 | + 'openedx.core.djangoapps.user_api.accounts.api.AccountSettingsReadOnlyFieldsRequested.run_filter', |
| 107 | + return_value=(set(), None), |
| 108 | + ) |
| 109 | + filter_patcher.start() |
| 110 | + self.addCleanup(filter_patcher.stop) |
111 | 111 |
|
112 | 112 | def test_get_username_provided(self): |
113 | 113 | """Test the difference in behavior when a username is supplied to get_account_settings.""" |
@@ -248,73 +248,19 @@ def test_update_success_for_enterprise(self): |
248 | 248 | account_settings = get_account_settings(self.default_request)[0] |
249 | 249 | assert level_of_education == account_settings['level_of_education'] |
250 | 250 |
|
251 | | - @patch('openedx.features.enterprise_support.api.enterprise_customer_for_request') |
252 | | - @patch('openedx.features.enterprise_support.utils.third_party_auth.provider.Registry.get') |
253 | | - @ddt.data( |
254 | | - *itertools.product( |
255 | | - # field_name_value values |
256 | | - (("email", "new_email@example.com"), ("name", "new name"), ("country", "IN")), |
257 | | - # is_enterprise_user |
258 | | - (True, False), |
259 | | - # is_synch_learner_profile_data |
260 | | - (True, False), |
261 | | - # has `UserSocialAuth` record |
262 | | - (True, False), |
263 | | - ) |
| 251 | + @patch( |
| 252 | + 'openedx.core.djangoapps.user_api.accounts.api.AccountSettingsReadOnlyFieldsRequested.run_filter', |
| 253 | + return_value=({'country'}, None), |
264 | 254 | ) |
265 | | - @ddt.unpack |
266 | | - def test_update_validation_error_for_enterprise( |
267 | | - self, |
268 | | - field_name_value, |
269 | | - is_enterprise_user, |
270 | | - is_synch_learner_profile_data, |
271 | | - has_user_social_auth_record, |
272 | | - mock_auth_provider, |
273 | | - mock_customer, |
274 | | - ): |
275 | | - idp_backend_name = 'tpa-saml' |
276 | | - mock_customer.return_value = {} |
277 | | - if is_enterprise_user: |
278 | | - mock_customer.return_value.update({ |
279 | | - 'uuid': 'real-ent-uuid', |
280 | | - 'name': 'Dummy Enterprise', |
281 | | - 'identity_provider': 'saml-ubc', |
282 | | - 'identity_providers': [ |
283 | | - { |
284 | | - "provider_id": "saml-ubc", |
285 | | - } |
286 | | - ], |
287 | | - }) |
288 | | - mock_auth_provider.return_value.sync_learner_profile_data = is_synch_learner_profile_data |
289 | | - mock_auth_provider.return_value.backend_name = idp_backend_name |
290 | | - |
291 | | - update_data = {field_name_value[0]: field_name_value[1]} |
292 | | - |
293 | | - user_fullname_editable = False |
294 | | - if has_user_social_auth_record: |
295 | | - UserSocialAuth.objects.create( |
296 | | - provider=idp_backend_name, |
297 | | - user=self.user |
298 | | - ) |
299 | | - else: |
300 | | - UserSocialAuth.objects.all().delete() |
301 | | - # user's fullname is editable if no `UserSocialAuth` record exists |
302 | | - user_fullname_editable = field_name_value[0] == 'name' |
303 | | - |
304 | | - # prevent actual email change requests |
305 | | - with patch('openedx.core.djangoapps.user_api.accounts.api.student_views.do_email_change_request'): |
306 | | - # expect field un-editability only when all of the following conditions are met |
307 | | - if is_enterprise_user and is_synch_learner_profile_data and not user_fullname_editable: |
308 | | - with pytest.raises(AccountValidationError) as validation_error: |
309 | | - update_account_settings(self.user, update_data) |
310 | | - field_errors = validation_error.value.field_errors |
311 | | - assert 'This field is not editable via this API' == \ |
312 | | - field_errors[field_name_value[0]]['developer_message'] |
313 | | - else: |
314 | | - update_account_settings(self.user, update_data) |
315 | | - account_settings = get_account_settings(self.default_request)[0] |
316 | | - if field_name_value[0] != "email": |
317 | | - assert field_name_value[1] == account_settings[field_name_value[0]] |
| 255 | + def test_readonly_field_from_filter_is_rejected(self, mock_run_filter): # pylint: disable=unused-argument |
| 256 | + """ |
| 257 | + When AccountSettingsReadOnlyFieldsRequested.run_filter returns a field as read-only, |
| 258 | + update_account_settings should raise AccountValidationError for that field. |
| 259 | + """ |
| 260 | + with pytest.raises(AccountValidationError) as exc_info: |
| 261 | + update_account_settings(self.user, {"country": "IN"}) |
| 262 | + field_errors = exc_info.value.field_errors |
| 263 | + assert 'This field is not editable via this API' == field_errors['country']['developer_message'] |
318 | 264 |
|
319 | 265 | def test_update_error_validating(self): |
320 | 266 | """Test that AccountValidationError is thrown if incorrect values are supplied.""" |
|
0 commit comments