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