You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_forms.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536
  1. from unittest import mock
  2. from django.core import mail
  3. from django.core.exceptions import ValidationError
  4. from django.test import TestCase
  5. from oscar.apps.customer.forms import EmailUserCreationForm, PasswordResetForm
  6. from oscar.test.factories import UserFactory
  7. class TestEmailUserCreationForm(TestCase):
  8. @mock.patch('oscar.apps.customer.forms.validate_password')
  9. def test_validator_passed_populated_user(self, mocked_validate):
  10. mocked_validate.side_effect = ValidationError('That password is rubbish')
  11. form = EmailUserCreationForm(data={'email': 'terry@boom.com', 'password1': 'terry', 'password2': 'terry'})
  12. self.assertFalse(form.is_valid())
  13. mocked_validate.assert_called_once_with('terry', form.instance)
  14. self.assertEqual(mocked_validate.call_args[0][1].email, 'terry@boom.com')
  15. self.assertEqual(form.errors['password2'], ['That password is rubbish'])
  16. class TestPasswordResetForm(TestCase):
  17. def test_user_email_unicode_collision(self):
  18. # Regression test for CVE-2019-19844, which Oscar's PasswordResetForm
  19. # was vulnerable to because it had overridden the save() method.
  20. UserFactory(username='mike123', email='mike@example.org')
  21. UserFactory(username='mike456', email='mıke@example.org')
  22. form = PasswordResetForm({'email': 'mıke@example.org'})
  23. self.assertTrue(form.is_valid())
  24. form.save()
  25. self.assertEqual(len(mail.outbox), 1)
  26. self.assertEqual(mail.outbox[0].to, ['mıke@example.org'])