test_issue_event_api.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import re
  2. from django.test import TestCase
  3. from django.urls import reverse
  4. from model_bakery import baker
  5. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  6. class IssueEventAPITestCase(GlitchTipTestCaseMixin, TestCase):
  7. def setUp(self):
  8. super().create_logged_in_user()
  9. def test_multi_page_list(self):
  10. first_event = baker.make("issue_events.IssueEvent", issue__project=self.project)
  11. baker.make("issue_events.IssueEvent", issue__project=self.project, issue_id=first_event.issue_id, _quantity=50)
  12. last_event = baker.make("issue_events.IssueEvent", issue__project=self.project, issue_id=first_event.issue_id)
  13. url = reverse("api:list_issue_event", args=[first_event.issue_id])
  14. with self.assertNumQueries(2):
  15. res = self.client.get(url)
  16. self.assertEqual(res.headers.get("X-Hits"), "52")
  17. self.assertEqual(res.json()[0]["id"], last_event.pk.hex)
  18. self.assertNotContains(res, first_event.pk.hex)
  19. pattern = r'(?<=\<).+?(?=\>)'
  20. links = re.findall(pattern, res.headers.get("Link"))
  21. res = self.client.get(links[1])
  22. self.assertEqual(res.headers.get("X-Hits"), "52")
  23. self.assertEqual(res.json()[-1]["id"], first_event.pk.hex)
  24. self.assertNotContains(res, last_event.pk.hex)
  25. def test_single_page_list(self):
  26. """
  27. Single page query should not hit DB for count
  28. """
  29. first_event = baker.make("issue_events.IssueEvent", issue__project=self.project)
  30. last_event = baker.make("issue_events.IssueEvent", issue__project=self.project, issue_id=first_event.issue_id)
  31. url = reverse("api:list_issue_event", args=[first_event.issue_id])
  32. with self.assertNumQueries(1):
  33. res = self.client.get(url)
  34. self.assertEqual(res.headers.get("X-Hits"), "2")
  35. self.assertContains(res, last_event.pk.hex)
  36. self.assertContains(res, first_event.pk.hex)
  37. def test_retrieve(self):
  38. event = baker.make("issue_events.IssueEvent", issue__project=self.project)
  39. url = reverse(
  40. "api:get_issue_event",
  41. kwargs={"issue_id": event.issue_id, "event_id": "a" * 32},
  42. )
  43. res = self.client.get(url)
  44. self.assertEqual(res.status_code, 404)
  45. url = reverse(
  46. "api:get_issue_event",
  47. kwargs={"issue_id": event.issue_id, "event_id": event.id},
  48. )
  49. res = self.client.get(url)
  50. self.assertContains(res, event.pk.hex)
  51. url = reverse("api:get_latest_issue_event", kwargs={"issue_id": event.issue_id})
  52. res = self.client.get(url)
  53. self.assertContains(res, event.pk.hex)
  54. def test_authentication(self):
  55. url = reverse("api:list_issue_event", args=[1])
  56. self.client.logout()
  57. res = self.client.get(url)
  58. self.assertEqual(res.status_code, 401)