test_validators.py 5.4 KB

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