test_api.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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", 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", 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", status_code=400)
  26. response = self.client.post(self.api_link, 'malformed', content_type="application/json")
  27. self.assertContains(response, "JSON parse error", 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. # regression test for #929
  33. response = self.client.post(self.api_link, {'post': '\n'})
  34. self.assertContains(response, "You have to enter a message.", status_code=400)
  35. def test_invalid_post(self):
  36. """api handles invalid post type"""
  37. response = self.client.post(self.api_link, {'post': 123})
  38. self.assertContains(
  39. response,
  40. "Posted message should be at least 5 characters long (it has 3).",
  41. status_code=400
  42. )
  43. def test_valid_post(self):
  44. """api returns parsed markup for valid post"""
  45. response = self.client.post(self.api_link, {'post': 'Lorem ipsum dolor met!'})
  46. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")