test_comments_api.py 3.0 KB

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