test_validators.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 load_post_validators, validate_post_length, validate_title
  6. class LoadPostValidatorsTests(TestCase):
  7. def test_empty_list(self):
  8. """empty validator list returns no validators"""
  9. self.assertEqual(load_post_validators([]), [])
  10. def test_load_validator(self):
  11. """function loads validator from list"""
  12. validators = load_post_validators([
  13. 'misago.core.testproject.validators.test_post_validator',
  14. ])
  15. self.assertEqual(validators, [test_post_validator])
  16. def test_load_nonexistant_validator(self):
  17. """nonexistant validator raises"""
  18. with self.assertRaises(ImportError):
  19. load_post_validators([
  20. 'misago.core.yaddayadda.yaddayadda',
  21. ])
  22. with self.assertRaises(AttributeError):
  23. load_post_validators([
  24. 'misago.core.yaddayadda',
  25. ])
  26. class ValidatePostLengthTests(TestCase):
  27. def test_valid_post(self):
  28. """valid post passes validation"""
  29. validate_post_length("Lorem ipsum dolor met sit amet elit.")
  30. def test_empty_post(self):
  31. """empty post is rejected"""
  32. with self.assertRaises(ValidationError):
  33. validate_post_length("")
  34. def test_too_short_post(self):
  35. """too short post is rejected"""
  36. with self.assertRaises(ValidationError):
  37. post = 'a' * settings.post_length_min
  38. validate_post_length(post[1:])
  39. def test_too_long_post(self):
  40. """too long post is rejected"""
  41. with self.assertRaises(ValidationError):
  42. post = 'a' * settings.post_length_max
  43. validate_post_length(post * 2)
  44. class ValidateTitleTests(TestCase):
  45. def test_valid_titles(self):
  46. """validate_title is ok with valid titles"""
  47. VALID_TITLES = [
  48. 'Lorem ipsum dolor met',
  49. '123 456 789 112'
  50. 'Ugabugagagagagaga',
  51. ]
  52. for title in VALID_TITLES:
  53. validate_title(title)
  54. def test_empty_title(self):
  55. """empty title is rejected"""
  56. with self.assertRaises(ValidationError):
  57. validate_title("")
  58. def test_too_short_title(self):
  59. """too short title is rejected"""
  60. with self.assertRaises(ValidationError):
  61. title = 'a' * settings.thread_title_length_min
  62. validate_title(title[1:])
  63. def test_too_long_title(self):
  64. """too long title is rejected"""
  65. with self.assertRaises(ValidationError):
  66. title = 'a' * settings.thread_title_length_max
  67. validate_title(title * 2)
  68. def test_unsluggable_title(self):
  69. """unsluggable title is rejected"""
  70. with self.assertRaises(ValidationError):
  71. title = '--' * settings.thread_title_length_min
  72. validate_title(title)