test_validators.py 5.8 KB

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