test_validators.py 5.3 KB

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