Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed change email address error from student dashboard #7753

Merged
merged 1 commit into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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