test_validators.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from unittest.mock import Mock
  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.users.models import Ban
  6. from misago.users.validators import (
  7. validate_email,
  8. validate_email_available,
  9. validate_email_banned,
  10. validate_gmail_email,
  11. validate_username,
  12. validate_username_available,
  13. validate_username_banned,
  14. validate_username_content,
  15. validate_username_length,
  16. )
  17. UserModel = get_user_model()
  18. class ValidateEmailAvailableTests(TestCase):
  19. def setUp(self):
  20. self.test_user = UserModel.objects.create_user(
  21. "EricTheFish", "eric@test.com", "pass123"
  22. )
  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(check_type=Ban.EMAIL, banned_value="ban@test.com")
  34. def test_unbanned_name(self):
  35. """unbanned email passes validation"""
  36. validate_email_banned("noban@test.com")
  37. def test_banned_name(self):
  38. """banned email fails validation"""
  39. with self.assertRaises(ValidationError):
  40. validate_email_banned("ban@test.com")
  41. class ValidateEmailTests(TestCase):
  42. def test_validate_email(self):
  43. """validate_email has no crashes"""
  44. validate_email("bob@boberson.com")
  45. with self.assertRaises(ValidationError):
  46. validate_email("*")
  47. class ValidateUsernameTests(TestCase):
  48. def test_validate_username(self):
  49. """validate_username has no crashes"""
  50. settings = Mock(username_length_min=1, username_length_max=5)
  51. validate_username(settings, "LeBob")
  52. with self.assertRaises(ValidationError):
  53. validate_username(settings, "*")
  54. class ValidateUsernameAvailableTests(TestCase):
  55. def setUp(self):
  56. self.test_user = UserModel.objects.create_user("EricTheFish", "eric@test.com")
  57. def test_valid_name(self):
  58. """validate_username_available allows available names"""
  59. validate_username_available("BobBoberson")
  60. validate_username_available(self.test_user.username, exclude=self.test_user)
  61. def test_invalid_name(self):
  62. """validate_username_available disallows unvailable names"""
  63. with self.assertRaises(ValidationError):
  64. validate_username_available(self.test_user.username)
  65. class ValidateUsernameBannedTests(TestCase):
  66. def setUp(self):
  67. Ban.objects.create(check_type=Ban.USERNAME, banned_value="Bob")
  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("Rafał")
  91. with self.assertRaises(ValidationError):
  92. validate_username_content("初音 ミク")
  93. class ValidateUsernameLengthTests(TestCase):
  94. def test_valid_name(self):
  95. """validate_username_length allows valid names"""
  96. settings = Mock(username_length_min=1, username_length_max=5)
  97. validate_username_length(settings, "a" * settings.username_length_min)
  98. validate_username_length(settings, "a" * settings.username_length_max)
  99. def test_invalid_name(self):
  100. """validate_username_length disallows invalid names"""
  101. settings = Mock(username_length_min=1, username_length_max=5)
  102. with self.assertRaises(ValidationError):
  103. validate_username_length(settings, "a" * (settings.username_length_min - 1))
  104. with self.assertRaises(ValidationError):
  105. validate_username_length(settings, "a" * (settings.username_length_max + 1))
  106. class ValidateGmailEmailTests(TestCase):
  107. def test_validate_gmail_email(self):
  108. """validate_gmail_email spots spammy gmail address"""
  109. added_errors = {}
  110. def add_errors(field_name, errors):
  111. added_errors[field_name] = errors
  112. validate_gmail_email(None, {}, add_errors)
  113. validate_gmail_email(None, {"email": "invalid-email"}, add_errors)
  114. validate_gmail_email(None, {"email": "the.bob.boberson@gmail.com"}, add_errors)
  115. validate_gmail_email(
  116. None, {"email": "the.bob.boberson@hotmail.com"}, add_errors
  117. )
  118. self.assertFalse(added_errors)
  119. validate_gmail_email(
  120. None, {"email": "the.b.o.b.b.ob.e.r.son@gmail.com"}, add_errors
  121. )
  122. self.assertTrue(added_errors)