test_comments_api.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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("issues.Issue", project=self.project)
  9. self.url = reverse(
  10. "issue-comments-list",
  11. kwargs={"issue_pk": self.issue.pk},
  12. )
  13. def test_comment_creation(self):
  14. data = {"data": {"text": "Test"}}
  15. not_my_issue = baker.make("issues.Issue")
  16. res = self.client.post(self.url, data, format="json")
  17. self.assertEqual(res.status_code, 201)
  18. self.assertEqual(res.data["data"]["text"], "Test")
  19. url = reverse(
  20. "issue-comments-list",
  21. kwargs={"issue_pk": not_my_issue.pk},
  22. )
  23. res = self.client.post(url, data, format="json")
  24. self.assertEqual(res.status_code, 400)
  25. def test_comments_list(self):
  26. comments = baker.make(
  27. "issues.Comment", issue=self.issue, _fill_optional=["text"], _quantity=3
  28. )
  29. not_my_issue = baker.make("issues.Issue")
  30. not_my_comment = baker.make(
  31. "issues.Comment", issue=not_my_issue, _fill_optional=["text"]
  32. )
  33. res = self.client.get(self.url)
  34. self.assertContains(res, comments[2].text)
  35. url = reverse(
  36. "issue-comments-list",
  37. kwargs={"issue_pk": not_my_issue.pk},
  38. )
  39. res = self.client.get(url)
  40. self.assertEqual(len(res.data), 0)
  41. def test_comment_update(self):
  42. comment = baker.make(
  43. "issues.Comment",
  44. issue=self.issue,
  45. user=self.user,
  46. _fill_optional=["text"],
  47. )
  48. url = reverse(
  49. "issue-comments-detail",
  50. kwargs={"issue_pk": self.issue.pk, "pk": comment.pk},
  51. )
  52. data = {"data": {"text": "Test"}}
  53. res = self.client.put(url, data, format="json")
  54. self.assertEqual(res.data["data"]["text"], "Test")
  55. def test_comment_delete(self):
  56. comment = baker.make(
  57. "issues.Comment",
  58. issue=self.issue,
  59. user=self.user,
  60. _fill_optional=["text"],
  61. )
  62. url = reverse(
  63. "issue-comments-detail",
  64. kwargs={"issue_pk": self.issue.pk, "pk": comment.pk},
  65. )
  66. self.client.delete(url)
  67. res = self.client.get(self.url)
  68. self.assertEqual(len(res.data), 0)