Exemple #1
0
 def test_form_valid(self):
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertTrue(form.is_valid())
Exemple #2
0
 def test_emails_must_match(self):
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertFalse(form.is_valid())
     self.assertIn('new_email2', form._errors)
Exemple #3
0
 def test_new_email2_required(self):
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertFalse(form.is_valid())
     self.assertIn('new_email2', form._errors)
Exemple #4
0
 def test_new_email_already_used(self):
     """
     Test that the new email is not already associated with a user
     """
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertFalse(form.is_valid())
     self.assertIn('new_email2', form._errors)
Exemple #5
0
 def post_change_email(self, request, *args, **kwargs):
     """Handle a POST request with the email change form submitted"""
     form = ChangeUsernameEmailForm(self.request.user, request.POST)
     if form.is_valid():
         messages.success(request, _("Your email address has been changed")) 
         user = form.save()
         # Send an email notification to the new email 
         send_email_change_email(user, user.email, request=self.request)
         # Send an email notification to the previous email
         send_email_change_email(user, form.previous_data['email'], request=self.request)
         return self.render_to_response(self.get_context_data())
     else:
         return self.render_to_response(self.get_context_data(change_email_form=form))
Exemple #6
0
 def test_incorrect_password(self):
     """
     Test that the form is invalid if the password field doesn't match
     the user's password.
     """
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertFalse(form.is_valid())
     self.assertIn('password', form._errors)
Exemple #7
0
 def test_original_email_saved(self):
     """
     Test that the original email is saved in an attribute of the 
     form after the form is saved.
     """
     old_email = self.user.email
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertTrue(form.is_valid())
     form.save()
     self.assertEqual(form.previous_data['email'], old_email)
Exemple #8
0
 def test_save(self):
     """
     Test that the user object is updated when the form is saved.
     """
     data = {
         'password': '******',
         'new_email1': '*****@*****.**',
         'new_email2': '*****@*****.**',
     }
     form = ChangeUsernameEmailForm(self.user, data)
     self.assertTrue(form.is_valid())
     form.save()
     user = User.objects.get(pk=self.user.pk)
     self.assertEqual(user.email, data['new_email1'])
     self.assertEqual(user.username, data['new_email1'])
Exemple #9
0
 def get_context_data(self, change_email_form=None, **kwargs):
     context = super(AccountSummaryView, self).get_context_data(**kwargs)
     if change_email_form is None:
         change_email_form = ChangeUsernameEmailForm(self.request.user)
     context['change_email_form'] = change_email_form 
     return context