test_shortcuts.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.core.urlresolvers import reverse
  2. from django.test import TestCase
  3. from misago.core.shortcuts import validate_slug, OutdatedSlug
  4. from misago.core.testproject.models import Model
  5. class ValidateSlugTests(TestCase):
  6. def test_is_outdated_slug_exception_not_raised_for_valid_slug(self):
  7. """
  8. check_object_slug doesn't raise OutdatedSlug when slugs match
  9. """
  10. model = Model(1, "test-slug")
  11. validate_slug(model, "test-slug")
  12. def test_is_outdated_slug_exception_raised_for_invalid_slug(self):
  13. """
  14. check_object_slug raises OutdatedSlug when slugs mismatch
  15. """
  16. model = Model(1, "test-slug")
  17. with self.assertRaises(OutdatedSlug):
  18. validate_slug(model, "wrong-slug")
  19. def test_is_outdated_slug_exception_raised_with_valid_message(self):
  20. """
  21. check_object_slug raises OutdatedSlug with valid message
  22. """
  23. correct_slug = "test-slug"
  24. model = Model(1, correct_slug)
  25. try:
  26. validate_slug(model, "wrong-slug")
  27. except OutdatedSlug as e:
  28. self.assertEqual(model, e.args[0])
  29. class CheckSlugHandler(TestCase):
  30. urls = 'misago.core.testproject.urls'
  31. def test_valid_slug_handle(self):
  32. """valid slug causes no interruption in view processing"""
  33. test_kwargs = {'model_slug': 'eric-the-fish', 'model_id': 1}
  34. response = self.client.get(
  35. reverse('validate_slug_view', kwargs=test_kwargs))
  36. self.assertIn("Allright", response.content)
  37. def test_invalid_slug_handle(self):
  38. """invalid slug returns in redirect to valid page"""
  39. test_kwargs = {'model_slug': 'lion-the-eric', 'model_id': 1}
  40. response = self.client.get(
  41. reverse('validate_slug_view', kwargs=test_kwargs))
  42. valid_url = "http://testserver/forum/test-valid-slug/eric-the-fish-1/"
  43. self.assertEqual(response['Location'], valid_url)