test_validators.py 2.1 KB

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