test_parsing_api.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from django.urls import reverse
  2. api_link = reverse("misago:api:parse-markup")
  3. def test_api_rejects_unauthenticated_user(db, client):
  4. response = client.post(api_link)
  5. assert response.status_code == 403
  6. def test_api_rejects_request_without_data(user_client):
  7. response = user_client.post(api_link)
  8. assert response.status_code == 400
  9. assert response.json() == {"detail": "You have to enter a message."}
  10. def test_api_rejects_request_with_invalid_shaped_data(user_client):
  11. response = user_client.post(api_link, "[]", content_type="application/json")
  12. assert response.status_code == 400
  13. assert response.json() == {
  14. "detail": "Invalid data. Expected a dictionary, but got list."
  15. }
  16. response = user_client.post(api_link, "123", content_type="application/json")
  17. assert response.status_code == 400
  18. assert response.json() == {
  19. "detail": "Invalid data. Expected a dictionary, but got int."
  20. }
  21. response = user_client.post(api_link, '"string"', content_type="application/json")
  22. assert response.status_code == 400
  23. assert response.json() == {
  24. "detail": "Invalid data. Expected a dictionary, but got str."
  25. }
  26. def test_api_rejects_request_with_malformed_data(user_client):
  27. response = user_client.post(api_link, "malformed", content_type="application/json")
  28. assert response.status_code == 400
  29. assert response.json() == {
  30. "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)"
  31. }
  32. def test_api_validates_that_post_has_content(user_client):
  33. response = user_client.post(api_link, json={"post": ""})
  34. assert response.status_code == 400
  35. assert response.json() == {"detail": "You have to enter a message."}
  36. # regression test for #929
  37. def test_api_strips_whitespace_from_post_before_validating_length(user_client):
  38. response = user_client.post(api_link, json={"post": "\n"})
  39. assert response.status_code == 400
  40. assert response.json() == {"detail": "You have to enter a message."}
  41. def test_api_casts_post_value_to_string(user_client):
  42. response = user_client.post(api_link, json={"post": 123})
  43. assert response.status_code == 400
  44. assert response.json() == {
  45. "detail": "Posted message should be at least 5 characters long (it has 3)."
  46. }
  47. def test_api_returns_parsed_value(user_client):
  48. response = user_client.post(api_link, json={"post": "Hello world!"})
  49. assert response.status_code == 200
  50. assert response.json() == {"parsed": "<p>Hello world!</p>"}