From 1d9499e1ad35da1dded86713b5dea220dcfeb73c Mon Sep 17 00:00:00 2001 From: Awais Jibran Date: Mon, 20 Apr 2015 14:28:37 +0500 Subject: [PATCH] Fixed change email address error from student dashboard TNL-1745 --- common/djangoapps/student/tests/test_email.py | 26 +++++++++++++++++++ common/djangoapps/student/views.py | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/common/djangoapps/student/tests/test_email.py b/common/djangoapps/student/tests/test_email.py index c18952113981..29e8be59133b 100644 --- a/common/djangoapps/student/tests/test_email.py +++ b/common/djangoapps/student/tests/test_email.py @@ -243,6 +243,32 @@ def test_invalid_password(self): self.request.POST['password'] = 'wrong' self.assertFailedRequest(self.run_request(), 'Invalid password') + @patch('student.views.render_to_string', Mock(side_effect=mock_render_to_string, autospec=True)) + def test_duplicate_activation_key(self): + """Assert that if two users change Email address simultaneously, server should return 200""" + + # New emails for the users + user1_new_email = "valid_user1_email@example.com" + user2_new_email = "valid_user2_email@example.com" + + # Set new email for user1. + self.request.POST['new_email'] = user1_new_email + + # Create a another user 'user2' & make request for change email + user2 = UserFactory.create(email=self.new_email, password="test2") + user2_request = self.req_factory.post('unused_url', data={ + 'password': 'test2', + 'new_email': user2_new_email + }) + user2_request.user = user2 + + # Send requests & check if response was successful + user1_response = change_email_request(self.request) + user2_response = change_email_request(user2_request) + + self.assertEqual(user1_response.status_code, 200) + self.assertEqual(user2_response.status_code, 200) + def test_invalid_emails(self): for email in ('bad_email', 'bad_email@', '@bad_email'): self.request.POST['new_email'] = email diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index c27356681bad..278df30d887b 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -2025,7 +2025,7 @@ def validate_new_email(user, new_email): raise ValueError(_('An account with this e-mail already exists.')) -def do_email_change_request(user, new_email, activation_key=uuid.uuid4().hex): +def do_email_change_request(user, new_email, activation_key=None): """ Given a new email for a user, does some basic verification of the new address and sends an activation message to the new address. If any issues are encountered with verification or sending the message, a ValueError will @@ -2038,6 +2038,10 @@ def do_email_change_request(user, new_email, activation_key=uuid.uuid4().hex): else: pec = pec_list[0] + # if activation_key is not passing as an argument, generate a random key + if not activation_key: + activation_key = uuid.uuid4().hex + pec.new_email = new_email pec.activation_key = activation_key pec.save()