test_user_reports_api.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.test import TestCase
  2. from django.urls import reverse
  3. from model_bakery import baker
  4. from glitchtip.test_utils.test_case import APIPermissionTestCase, GlitchTipTestCaseMixin
  5. def get_issue_event_url(issue_id: int, event_id: str) -> str:
  6. return reverse(
  7. "api:get_issue_event", kwargs={"issue_id": issue_id, "event_id": event_id}
  8. )
  9. def list_user_reports_url(issue_id: int) -> str:
  10. return reverse("api:list_user_reports", kwargs={"issue_id": issue_id})
  11. class IssuesUserReportTestCase(GlitchTipTestCaseMixin, TestCase):
  12. def setUp(self):
  13. super().create_logged_in_user()
  14. self.event = baker.make("issue_events.IssueEvent", issue__project=self.project)
  15. self.user_report = baker.make(
  16. "issue_events.UserReport",
  17. project=self.project,
  18. issue=self.event.issue,
  19. event_id=self.event.pk.hex,
  20. )
  21. def test_events_user_report(self):
  22. url = get_issue_event_url(self.event.issue_id, self.event.pk.hex)
  23. res = self.client.get(url)
  24. self.assertContains(res, self.user_report.email)
  25. self.assertContains(res, self.user_report.name)
  26. self.assertContains(res, self.user_report.comments)
  27. self.assertEqual(res.json()["userReport"]["eventID"], self.event.pk.hex)
  28. def test_issues_user_report_list(self):
  29. event2 = baker.make("issue_events.IssueEvent", issue__project=self.project)
  30. user_report2 = baker.make(
  31. "issue_events.UserReport",
  32. project=self.project,
  33. issue=event2.issue,
  34. event_id=event2.pk.hex,
  35. )
  36. url = list_user_reports_url(self.event.issue.id)
  37. res = self.client.get(url)
  38. self.assertContains(res, self.user_report.email)
  39. self.assertNotContains(res, user_report2.email)
  40. class UserReportAPIPermissionTests(APIPermissionTestCase):
  41. def setUp(self):
  42. self.create_org_team_project()
  43. self.set_client_credentials(self.auth_token.token)
  44. self.issue = baker.make("issue_events.Issue", project=self.project)
  45. self.user_report = baker.make(
  46. "issue_events.UserReport", project=self.project, issue=self.issue
  47. )
  48. self.list_url = list_user_reports_url(self.issue.id)
  49. def test_list(self):
  50. self.assertGetReqStatusCode(self.list_url, 403)
  51. self.auth_token.add_permission("event:read")
  52. self.assertGetReqStatusCode(self.list_url, 200)