test_api.py 2.5 KB

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