test_validators.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from django.contrib.auth import get_user_model
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from misago.conf import settings
  5. from misago.users.models import Ban
  6. from misago.users.validators import (
  7. validate_email, validate_email_available, validate_email_banned, validate_gmail_email,
  8. validate_username, validate_username_available, validate_username_banned,
  9. validate_username_content, validate_username_length)
  10. UserModel = get_user_model()
  11. class ValidateEmailAvailableTests(TestCase):
  12. def setUp(self):
  13. self.test_user = UserModel.objects.create_user('EricTheFish', 'eric@test.com', 'pass123')
  14. def test_valid_email(self):
  15. """validate_email_available allows available emails"""
  16. validate_email_available('bob@boberson.com')
  17. validate_email_available(self.test_user.email, exclude=self.test_user)
  18. def test_invalid_email(self):
  19. """validate_email_available disallows unvailable emails"""
  20. with self.assertRaises(ValidationError):
  21. validate_email_available(self.test_user.email)
  22. class ValidateEmailBannedTests(TestCase):
  23. def setUp(self):
  24. Ban.objects.create(
  25. check_type=Ban.EMAIL,
  26. banned_value="ban@test.com",
  27. )
  28. def test_unbanned_name(self):
  29. """unbanned email passes validation"""
  30. validate_email_banned('noban@test.com')
  31. def test_banned_name(self):
  32. """banned email fails validation"""
  33. with self.assertRaises(ValidationError):
  34. validate_email_banned('ban@test.com')
  35. class ValidateEmailTests(TestCase):
  36. def test_validate_email(self):
  37. """validate_email has no crashes"""
  38. validate_email('bob@boberson.com')
  39. with self.assertRaises(ValidationError):
  40. validate_email('*')
  41. class ValidateUsernameTests(TestCase):
  42. def test_validate_username(self):
  43. """validate_username has no crashes"""
  44. validate_username('LeBob')
  45. with self.assertRaises(ValidationError):
  46. validate_username('*')
  47. class ValidateUsernameAvailableTests(TestCase):
  48. def setUp(self):
  49. self.test_user = UserModel.objects.create_user('EricTheFish', 'eric@test.com', 'pass123')
  50. def test_valid_name(self):
  51. """validate_username_available allows available names"""
  52. validate_username_available('BobBoberson')
  53. validate_username_available(self.test_user.username, exclude=self.test_user)
  54. def test_invalid_name(self):
  55. """validate_username_available disallows unvailable names"""
  56. with self.assertRaises(ValidationError):
  57. validate_username_available(self.test_user.username)
  58. class ValidateUsernameBannedTests(TestCase):
  59. def setUp(self):
  60. Ban.objects.create(
  61. check_type=Ban.USERNAME,
  62. banned_value="Bob",
  63. )
  64. def test_unbanned_name(self):
  65. """unbanned name passes validation"""
  66. validate_username_banned('Luke')
  67. def test_banned_name(self):
  68. """banned name fails validation"""
  69. with self.assertRaises(ValidationError):
  70. validate_username_banned('Bob')
  71. class ValidateUsernameContentTests(TestCase):
  72. def test_valid_name(self):
  73. """validate_username_content allows valid names"""
  74. validate_username_content('123')
  75. validate_username_content('Bob')
  76. validate_username_content('Bob123')
  77. def test_invalid_name(self):
  78. """validate_username_content disallows invalid names"""
  79. with self.assertRaises(ValidationError):
  80. validate_username_content('!')
  81. with self.assertRaises(ValidationError):
  82. validate_username_content('Bob!')
  83. with self.assertRaises(ValidationError):
  84. validate_username_content('Bob Boberson')
  85. with self.assertRaises(ValidationError):
  86. validate_username_content('Rafał')
  87. with self.assertRaises(ValidationError):
  88. validate_username_content('初音 ミク')
  89. class ValidateUsernameLengthTests(TestCase):
  90. def test_valid_name(self):
  91. """validate_username_length allows valid names"""
  92. validate_username_length('a' * settings.username_length_min)
  93. validate_username_length('a' * settings.username_length_max)
  94. def test_invalid_name(self):
  95. """validate_username_length disallows invalid names"""
  96. with self.assertRaises(ValidationError):
  97. validate_username_length('a' * (settings.username_length_min - 1))
  98. with self.assertRaises(ValidationError):
  99. validate_username_length('a' * (settings.username_length_max + 1))
  100. class ValidateGmailEmailTests(TestCase):
  101. def test_validate_gmail_email(self):
  102. """validate_gmail_email spots spammy gmail address"""
  103. added_errors = {}
  104. def add_errors(field_name, errors):
  105. added_errors[field_name] = errors
  106. validate_gmail_email(None, {}, add_errors)
  107. validate_gmail_email(None, {'email': 'invalid-email'}, add_errors)
  108. validate_gmail_email(None, {'email': 'the.bob.boberson@gmail.com'}, add_errors)
  109. validate_gmail_email(None, {'email': 'the.bob.boberson@hotmail.com'}, add_errors)
  110. self.assertFalse(added_errors)
  111. validate_gmail_email(None, {'email': 'the.b.o.b.b.ob.e.r.son@gmail.com'}, add_errors)
  112. self.assertTrue(added_errors)