test_validators.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.core.exceptions import ValidationError
  2. from django.test import TestCase
  3. from misago.conf import settings
  4. from misago.threads.validators import validate_post_length, validate_title
  5. class ValidatePostLengthTests(TestCase):
  6. def test_valid_post(self):
  7. """valid post passes validation"""
  8. validate_post_length("Lorem ipsum dolor met sit amet elit.")
  9. def test_empty_post(self):
  10. """empty post is rejected"""
  11. with self.assertRaises(ValidationError):
  12. validate_post_length("")
  13. def test_too_short_post(self):
  14. """too short post is rejected"""
  15. with self.assertRaises(ValidationError):
  16. post = 'a' * settings.post_length_min
  17. validate_post_length(post[1:])
  18. def test_too_long_post(self):
  19. """too long post is rejected"""
  20. with self.assertRaises(ValidationError):
  21. post = 'a' * settings.post_length_max
  22. validate_post_length(post * 2)
  23. class ValidateTitleTests(TestCase):
  24. def test_valid_titles(self):
  25. """validate_title is ok with valid titles"""
  26. VALID_TITLES = [
  27. 'Lorem ipsum dolor met',
  28. '123 456 789 112'
  29. 'Ugabugagagagagaga',
  30. ]
  31. for title in VALID_TITLES:
  32. validate_title(title)
  33. def test_empty_title(self):
  34. """empty title is rejected"""
  35. with self.assertRaises(ValidationError):
  36. validate_title("")
  37. def test_too_short_title(self):
  38. """too short title is rejected"""
  39. with self.assertRaises(ValidationError):
  40. title = 'a' * settings.thread_title_length_min
  41. validate_title(title[1:])
  42. def test_too_long_title(self):
  43. """too long title is rejected"""
  44. with self.assertRaises(ValidationError):
  45. title = 'a' * settings.thread_title_length_max
  46. validate_title(title * 2)
  47. def test_unsluggable_title(self):
  48. """unsluggable title is rejected"""
  49. with self.assertRaises(ValidationError):
  50. title = '--' * settings.thread_title_length_min
  51. validate_title(title)