test_api.py 2.4 KB

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