diff --git a/seahub/api2/endpoints/user.py b/seahub/api2/endpoints/user.py index e203423c03..af9cc5d5e8 100644 --- a/seahub/api2/endpoints/user.py +++ b/seahub/api2/endpoints/user.py @@ -88,18 +88,23 @@ class User(APIView): # argument check for contact_email contact_email = request.data.get("contact_email", None) if contact_email: - if not ENABLE_USER_SET_CONTACT_EMAIL: - error_msg = _(u'Feature disabled.') - return api_error(status.HTTP_403_FORBIDDEN, error_msg) + profile = Profile.objects.get_profile_by_contact_email(contact_email) + if not profile: + # update contact email + if not ENABLE_USER_SET_CONTACT_EMAIL: + error_msg = _(u'Feature disabled.') + return api_error(status.HTTP_403_FORBIDDEN, error_msg) - contact_email = contact_email.strip() - if not is_valid_email(contact_email): - error_msg = 'contact_email invalid.' - return api_error(status.HTTP_400_BAD_REQUEST, error_msg) - - if Profile.objects.get_profile_by_contact_email(contact_email): - error_msg = _('Contact email %s already exists.' % contact_email) - return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + contact_email = contact_email.strip() + if not is_valid_email(contact_email): + error_msg = 'contact_email invalid.' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + else: + # if profile is other user(contact_email already exists) + # else: input same contact email of this user, let it pass + if profile.user != email: + error_msg = _('Contact email %s already exists.' % contact_email) + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # agrument check for telephone telephone = request.data.get('telephone', None) diff --git a/tests/api/endpoints/test_user.py b/tests/api/endpoints/test_user.py index a3613cda6a..186825ecfd 100644 --- a/tests/api/endpoints/test_user.py +++ b/tests/api/endpoints/test_user.py @@ -122,6 +122,7 @@ class AccountTest(BaseTestCase): resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded') self.assertEqual(400, resp.status_code) + @patch('seahub.api2.endpoints.user.ENABLE_USER_SET_CONTACT_EMAIL', False) def test_update_user_contact_email_feature_disabled(self): self.login_as(self.user) Profile.objects.add_or_update(self.user_name, contact_email='2@2.com') @@ -147,6 +148,14 @@ class AccountTest(BaseTestCase): json_resp = json.loads(resp.content) assert json_resp['contact_email'] == random_contact_email + # same contact email as his/her own contact email + contact_email = Profile.objects.get_contact_email_by_user(self.user_name) + data = 'contact_email=%s' % contact_email + resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded') + json_resp = json.loads(resp.content) + self.assertEqual(200, resp.status_code) + assert json_resp['contact_email'] == contact_email + # test invalid contact email random_contact_email = generate_random_parammeter(0, 0, 'contact_email_invalid') data = 'contact_email=%s' % random_contact_email