test_comments_api.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, content_type="application/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, content_type="application/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("issue_events.Comment", issue=not_my_issue, _fill_optional=["text"])
  32. res = self.client.get(self.url)
  33. self.assertContains(res, comments[2].text)
  34. url = reverse("api:list_comments", kwargs={"issue_id": not_my_issue.id})
  35. res = self.client.get(url)
  36. self.assertEqual(len(res.json()), 0)
  37. def test_comments_list_deleted_user(self):
  38. user2 = baker.make("users.User")
  39. self.organization.add_user(user2)
  40. comment = baker.make(
  41. "issue_events.Comment",
  42. issue=self.issue,
  43. user=user2,
  44. _fill_optional=["text"],
  45. )
  46. user2.delete()
  47. res = self.client.get(self.url)
  48. self.assertContains(res, comment.text)
  49. def test_comment_update(self):
  50. comment = baker.make(
  51. "issue_events.Comment",
  52. issue=self.issue,
  53. user=self.user,
  54. _fill_optional=["text"],
  55. )
  56. url = reverse(
  57. "api:update_comment",
  58. kwargs={"issue_id": self.issue.id, "comment_id": comment.id},
  59. )
  60. data = {"data": {"text": "Test"}}
  61. res = self.client.put(url, data, content_type="application/json")
  62. self.assertEqual(res.json()["data"]["text"], "Test")
  63. def test_comment_delete(self):
  64. comment = baker.make(
  65. "issue_events.Comment",
  66. issue=self.issue,
  67. user=self.user,
  68. _fill_optional=["text"],
  69. )
  70. url = reverse(
  71. "api:delete_comment",
  72. kwargs={"issue_id": self.issue.id, "comment_id": comment.id},
  73. )
  74. self.client.delete(url)
  75. res = self.client.get(self.url)
  76. self.assertEqual(len(res.json()), 0)