test_api.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.urls import reverse
  4. from misago.users.testutils import AuthenticatedUserTestCase
  5. class ParseMarkupApiTests(AuthenticatedUserTestCase):
  6. def setUp(self):
  7. super(ParseMarkupApiTests, self).setUp()
  8. self.api_link = reverse('misago:api:parse-markup')
  9. def test_is_anonymous(self):
  10. """api requires authentication"""
  11. self.logout_user()
  12. response = self.client.post(self.api_link)
  13. self.assertContains(response, "This action is not available to guests.", status_code=403)
  14. def test_no_data(self):
  15. """api handles no data"""
  16. response = self.client.post(self.api_link)
  17. self.assertContains(response, "You have to enter a message.", status_code=400)
  18. def test_invalid_data(self):
  19. """api handles post that is invalid type"""
  20. response = self.client.post(self.api_link, '[]', content_type="application/json")
  21. self.assertContains(response, "Invalid data. Expected a dictionary, but got list.", status_code=400)
  22. response = self.client.post(self.api_link, '123', content_type="application/json")
  23. self.assertContains(response, "Invalid data. Expected a dictionary, but got int.", status_code=400)
  24. response = self.client.post(self.api_link, '"string"', content_type="application/json")
  25. self.assertContains(response, "Invalid data. Expected a dictionary, but got unicode.", status_code=400)
  26. response = self.client.post(self.api_link, 'malformed', content_type="application/json")
  27. self.assertContains(response, "JSON parse error - No JSON object could be decoded", status_code=400)
  28. def test_empty_post(self):
  29. """api handles empty post"""
  30. response = self.client.post(self.api_link, {'post': ''})
  31. self.assertContains(response, "You have to enter a message.", status_code=400)
  32. def test_invalid_post(self):
  33. """api handles invalid post type"""
  34. response = self.client.post(self.api_link, {'post': 123})
  35. self.assertContains(
  36. response,
  37. "Posted message should be at least 5 characters long (it has 3).",
  38. status_code=400
  39. )
  40. def test_valid_post(self):
  41. """api returns parsed markup for valid post"""
  42. response = self.client.post(self.api_link, {'post': 'Lorem ipsum dolor met!'})
  43. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")