test_validators.py 5.2 KB

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