test_validators.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from unittest.mock import Mock
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from misago.users.models import Ban
  5. from misago.users.testutils import create_test_user
  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. class ValidateEmailAvailableTests(TestCase):
  18. def setUp(self):
  19. self.user = create_test_user("User", "user@example.com")
  20. def test_valid_email(self):
  21. """validate_email_available allows available emails"""
  22. validate_email_available("other@example.com")
  23. validate_email_available(self.user.email, exclude=self.user)
  24. def test_invalid_email(self):
  25. """validate_email_available disallows unvailable emails"""
  26. with self.assertRaises(ValidationError):
  27. validate_email_available(self.user.email)
  28. class ValidateEmailBannedTests(TestCase):
  29. def setUp(self):
  30. Ban.objects.create(check_type=Ban.EMAIL, banned_value="ban@test.com")
  31. def test_unbanned_name(self):
  32. """unbanned email passes validation"""
  33. validate_email_banned("noban@test.com")
  34. def test_banned_name(self):
  35. """banned email fails validation"""
  36. with self.assertRaises(ValidationError):
  37. validate_email_banned("ban@test.com")
  38. class ValidateEmailTests(TestCase):
  39. def test_validate_email(self):
  40. """validate_email has no crashes"""
  41. validate_email("user@example.com")
  42. with self.assertRaises(ValidationError):
  43. validate_email("*")
  44. class ValidateUsernameTests(TestCase):
  45. def test_validate_username(self):
  46. """validate_username has no crashes"""
  47. settings = Mock(username_length_min=1, username_length_max=5)
  48. validate_username(settings, "LeBob")
  49. with self.assertRaises(ValidationError):
  50. validate_username(settings, "*")
  51. class ValidateUsernameAvailableTests(TestCase):
  52. def setUp(self):
  53. self.user = create_test_user("User", "user@example.com")
  54. def test_valid_name(self):
  55. """validate_username_available allows available names"""
  56. validate_username_available("OtherUser")
  57. validate_username_available(self.user.username, exclude=self.user)
  58. def test_invalid_name(self):
  59. """validate_username_available disallows unvailable names"""
  60. with self.assertRaises(ValidationError):
  61. validate_username_available(self.user.username)
  62. class ValidateUsernameBannedTests(TestCase):
  63. def setUp(self):
  64. Ban.objects.create(check_type=Ban.USERNAME, banned_value="Ban")
  65. def test_unbanned_name(self):
  66. """unbanned name passes validation"""
  67. validate_username_banned("User")
  68. def test_banned_name(self):
  69. """banned name fails validation"""
  70. with self.assertRaises(ValidationError):
  71. validate_username_banned("Ban")
  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("User")
  77. validate_username_content("User123")
  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("User!")
  84. with self.assertRaises(ValidationError):
  85. validate_username_content("John Doe")
  86. with self.assertRaises(ValidationError):
  87. validate_username_content("Rafał")
  88. with self.assertRaises(ValidationError):
  89. validate_username_content("初音 ミク")
  90. class ValidateUsernameLengthTests(TestCase):
  91. def test_valid_name(self):
  92. """validate_username_length allows valid names"""
  93. settings = Mock(username_length_min=1, username_length_max=5)
  94. validate_username_length(settings, "a" * settings.username_length_min)
  95. validate_username_length(settings, "a" * settings.username_length_max)
  96. def test_invalid_name(self):
  97. """validate_username_length disallows invalid names"""
  98. settings = Mock(username_length_min=1, username_length_max=5)
  99. with self.assertRaises(ValidationError):
  100. validate_username_length(settings, "a" * (settings.username_length_min - 1))
  101. with self.assertRaises(ValidationError):
  102. validate_username_length(settings, "a" * (settings.username_length_max + 1))
  103. class ValidateGmailEmailTests(TestCase):
  104. def test_validate_gmail_email(self):
  105. """validate_gmail_email spots spammy gmail address"""
  106. added_errors = {}
  107. def add_errors(field_name, errors):
  108. added_errors[field_name] = errors
  109. validate_gmail_email(None, {}, add_errors)
  110. validate_gmail_email(None, {"email": "invalid-email"}, add_errors)
  111. validate_gmail_email(None, {"email": "the.valid.email@gmail.com"}, add_errors)
  112. validate_gmail_email(None, {"email": "the.valid.email@hotmail.com"}, add_errors)
  113. self.assertFalse(added_errors)
  114. validate_gmail_email(
  115. None, {"email": "the.s.p.a.m.my.e.ma.il@gmail.com"}, add_errors
  116. )
  117. self.assertTrue(added_errors)