test_issues_api.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from django.contrib.postgres.search import SearchVector
  2. from django.db.models import Value
  3. from django.test import TestCase
  4. from django.urls import reverse
  5. from django.utils import timezone
  6. from model_bakery import baker
  7. from glitchtip.test_utils.test_case import APIPermissionTestCase, GlitchTipTestCaseMixin
  8. class IssueEventAPITestCase(GlitchTipTestCaseMixin, TestCase):
  9. def setUp(self):
  10. super().create_logged_in_user()
  11. self.list_url = reverse(
  12. "api:list_issues", kwargs={"organization_slug": self.organization.slug}
  13. )
  14. def test_retrieve(self):
  15. issue = baker.make("issue_events.Issue", project=self.project, short_id=1)
  16. event = baker.make("issue_events.IssueEvent", issue=issue)
  17. baker.make(
  18. "issue_events.UserReport",
  19. project=self.project,
  20. issue=issue,
  21. event_id=event.pk.hex,
  22. _quantity=1,
  23. )
  24. baker.make("issue_events.Comment", issue=issue, _quantity=3)
  25. url = reverse(
  26. "api:get_issue",
  27. kwargs={"issue_id": issue.id},
  28. )
  29. res = self.client.get(url)
  30. data = res.json()
  31. self.assertEqual(
  32. data.get("shortId"), f"{self.project.slug.upper()}-{issue.short_id}"
  33. )
  34. self.assertEqual(data.get("count"), str(issue.count))
  35. self.assertEqual(data.get("userReportCount"), 1)
  36. self.assertEqual(data.get("numComments"), 3)
  37. def test_list(self):
  38. res = self.client.get(self.list_url)
  39. self.assertEqual(res.status_code, 200)
  40. not_my_issue = baker.make("issue_events.Issue")
  41. issue = baker.make("issue_events.Issue", project=self.project, short_id=1)
  42. baker.make("issue_events.IssueEvent", issue=issue)
  43. res = self.client.get(self.list_url)
  44. self.assertContains(res, issue.title)
  45. self.assertNotContains(res, not_my_issue.title)
  46. self.assertEqual(len(res.json()), 1)
  47. def test_filter_by_date(self):
  48. """
  49. A user should be able to filter by start and end datetimes.
  50. In the future, this should filter events, not first_seen.
  51. """
  52. issue1 = baker.make(
  53. "issue_events.Issue",
  54. first_seen=timezone.make_aware(timezone.datetime(1999, 1, 1)),
  55. project=self.project,
  56. )
  57. issue2 = baker.make(
  58. "issue_events.Issue",
  59. first_seen=timezone.make_aware(timezone.datetime(2010, 1, 1)),
  60. project=self.project,
  61. )
  62. issue3 = baker.make(
  63. "issue_events.Issue",
  64. first_seen=timezone.make_aware(timezone.datetime(2020, 1, 1)),
  65. project=self.project,
  66. )
  67. res = self.client.get(
  68. self.list_url
  69. + "?start=2000-01-01T05:00:00.000Z&end=2019-01-01T05:00:00.000Z"
  70. )
  71. self.assertContains(res, issue2.title)
  72. self.assertNotContains(res, issue1.title)
  73. self.assertNotContains(res, issue3.title)
  74. def test_sort(self):
  75. issue1 = baker.make("issue_events.Issue", project=self.project)
  76. issue2 = baker.make("issue_events.Issue", project=self.project, count=2)
  77. issue3 = baker.make("issue_events.Issue", project=self.project)
  78. res = self.client.get(self.list_url)
  79. self.assertEqual(res.json()[0]["id"], str(issue3.id))
  80. res = self.client.get(self.list_url + "?sort=-count")
  81. self.assertEqual(res.json()[0]["id"], str(issue2.id))
  82. res = self.client.get(self.list_url + "?sort=priority")
  83. self.assertEqual(res.json()[0]["id"], str(issue1.id))
  84. res = self.client.get(self.list_url + "?sort=-priority")
  85. self.assertEqual(res.json()[0]["id"], str(issue2.id))
  86. def test_search(self):
  87. issue = baker.make(
  88. "issue_events.Issue",
  89. project=self.project,
  90. search_vector=SearchVector(Value("apple sauce")),
  91. )
  92. other_issue = baker.make("issue_events.Issue", project=self.project)
  93. res = self.client.get(self.list_url + "?query=is:unresolved apple+sauce")
  94. self.assertContains(res, issue.title)
  95. self.assertNotContains(res, other_issue.title)
  96. self.assertNotContains(res, "matchingEventId")
  97. self.assertNotIn("X-Sentry-Direct-Hit", res.headers)
  98. class IssueEventAPIPermissionTestCase(APIPermissionTestCase):
  99. def setUp(self):
  100. self.create_user_org()
  101. self.set_client_credentials(self.auth_token.token)
  102. self.team = baker.make("teams.Team", organization=self.organization)
  103. self.team.members.add(self.org_user)
  104. self.project = baker.make("projects.Project", organization=self.organization)
  105. self.project.team_set.add(self.team)
  106. self.issue = baker.make("issues.Issue", project=self.project)
  107. self.list_url = reverse(
  108. "api:list_issues", kwargs={"organization_slug": self.organization.slug}
  109. )
  110. def test_list(self):
  111. self.assertGetReqStatusCode(self.list_url, 403)
  112. self.auth_token.add_permission("event:read")
  113. self.assertGetReqStatusCode(self.list_url, 200)