Skip to content

Commit

Permalink
Merge pull request #7753 from edx/aj/tnl1745-change-email-on-dashboard
Browse files Browse the repository at this point in the history
Fixed change email address error from student dashboard
  • Loading branch information
awaisdar001 committed Apr 28, 2015
2 parents 70d837a + 1d9499e commit 3bd4535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
26 changes: 26 additions & 0 deletions common/djangoapps/student/tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
user2_new_email = "[email protected]"

# 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
Expand Down
6 changes: 5 additions & 1 deletion common/djangoapps/student/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 3bd4535

Please sign in to comment.