test_validators.py 5.3 KB

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