test_validators.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from unittest.mock import Mock
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from ..models import Ban
  5. from ..test import create_test_user
  6. from ..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. validate_username(settings, "LeB_b")
  50. with self.assertRaises(ValidationError):
  51. validate_username(settings, "*")
  52. with self.assertRaises(ValidationError):
  53. validate_username(settings, "___")
  54. class ValidateUsernameAvailableTests(TestCase):
  55. def setUp(self):
  56. self.user = create_test_user("User", "user@example.com")
  57. def test_valid_name(self):
  58. """validate_username_available allows available names"""
  59. validate_username_available("OtherUser")
  60. validate_username_available(self.user.username, exclude=self.user)
  61. def test_invalid_name(self):
  62. """validate_username_available disallows unvailable names"""
  63. with self.assertRaises(ValidationError):
  64. validate_username_available(self.user.username)
  65. class ValidateUsernameBannedTests(TestCase):
  66. def setUp(self):
  67. Ban.objects.create(check_type=Ban.USERNAME, banned_value="Ban")
  68. def test_unbanned_name(self):
  69. """unbanned name passes validation"""
  70. validate_username_banned("User")
  71. def test_banned_name(self):
  72. """banned name fails validation"""
  73. with self.assertRaises(ValidationError):
  74. validate_username_banned("Ban")
  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("User")
  80. validate_username_content("User123")
  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("User!")
  87. with self.assertRaises(ValidationError):
  88. validate_username_content("John Doe")
  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.valid.email@gmail.com"}, add_errors)
  115. validate_gmail_email(None, {"email": "the.valid.email@hotmail.com"}, add_errors)
  116. self.assertFalse(added_errors)
  117. validate_gmail_email(
  118. None, {"email": "the.s.p.a.m.my.e.ma.il@gmail.com"}, add_errors
  119. )
  120. self.assertTrue(added_errors)