test_comments_api.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.shortcuts import reverse
  2. from model_bakery import baker
  3. from glitchtip.test_utils.test_case import GlitchTipTestCase
  4. # Create your tests here.
  5. class CommentsApiTestCase(GlitchTipTestCase):
  6. def setUp(self):
  7. self.create_user_and_project()
  8. self.issue = baker.make("issue_events.Issue", project=self.project)
  9. self.url = reverse("api:list_comments", kwargs={"issue_id": self.issue.id})
  10. def test_comment_creation(self):
  11. data = {"data": {"text": "Test"}}
  12. not_my_issue = baker.make("issue_events.Issue")
  13. res = self.client.post(self.url, data, format="json")
  14. self.assertEqual(res.status_code, 201)
  15. self.assertEqual(res.json()["data"]["text"], "Test")
  16. url = reverse(
  17. "api:list_comments",
  18. kwargs={"issue_id": not_my_issue.id},
  19. )
  20. res = self.client.post(url, data, format="json")
  21. self.assertEqual(res.status_code, 400)
  22. def test_comments_list(self):
  23. comments = baker.make(
  24. "issue_events.Comment",
  25. issue=self.issue,
  26. user=self.user,
  27. _fill_optional=["text"],
  28. _quantity=3,
  29. )
  30. not_my_issue = baker.make("issue_events.Issue")
  31. baker.make(
  32. "issue_events.Comment", issue=not_my_issue, _fill_optional=["text"]
  33. )
  34. res = self.client.get(self.url)
  35. self.assertContains(res, comments[2].text)
  36. url = reverse("api:list_comments", kwargs={"issue_id": not_my_issue.id})
  37. res = self.client.get(url)
  38. self.assertEqual(len(res.json()), 0)
  39. def test_comment_update(self):
  40. comment = baker.make(
  41. "issue_events.Comment",
  42. issue=self.issue,
  43. user=self.user,
  44. _fill_optional=["text"],
  45. )
  46. url = reverse(
  47. "api:update_comment",
  48. kwargs={"issue_id": self.issue.id, "comment_id": comment.id},
  49. )
  50. data = {"data": {"text": "Test"}}
  51. res = self.client.put(url, data, format="json")
  52. self.assertEqual(res.json()["data"]["text"], "Test")
  53. def test_comment_delete(self):
  54. comment = baker.make(
  55. "issue_events.Comment",
  56. issue=self.issue,
  57. user=self.user,
  58. _fill_optional=["text"],
  59. )
  60. url = reverse(
  61. "api:delete_comment",
  62. kwargs={"issue_id": self.issue.id, "comment_id": comment.id},
  63. )
  64. self.client.delete(url)
  65. res = self.client.get(self.url)
  66. self.assertEqual(len(res.json()), 0)