test_validators.py 5.5 KB

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