test_discover_homepage_query.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from django.urls import reverse
  2. from sentry.api.serializers import serialize
  3. from sentry.discover.models import DiscoverSavedQuery
  4. from tests.snuba.api.endpoints.test_discover_saved_queries import DiscoverSavedQueryBase
  5. FEATURES = ("organizations:discover-query",)
  6. class DiscoverHomepageQueryTest(DiscoverSavedQueryBase):
  7. def setUp(self):
  8. super().setUp()
  9. self.url = reverse("sentry-api-0-discover-homepage-query", args=[self.org.slug])
  10. self.query = {"fields": ["test"], "conditions": [], "limit": 10}
  11. self.project_ids = [
  12. self.create_project(organization=self.org).id,
  13. self.create_project(organization=self.org).id,
  14. ]
  15. def test_returns_no_response_if_no_homepage_query_for_user(self):
  16. with self.feature(FEATURES):
  17. response = self.client.get(self.url)
  18. assert response.status_code == 204, response.content
  19. assert response.data is None
  20. def test_returns_serialized_saved_query_if_homepage_is_set(self):
  21. saved_query = DiscoverSavedQuery.objects.create(
  22. organization=self.org,
  23. created_by=self.user,
  24. name="Test query",
  25. query=self.query,
  26. is_homepage=True,
  27. )
  28. with self.feature(FEATURES):
  29. response = self.client.get(self.url)
  30. assert response.status_code == 200, response.content
  31. assert response.data == serialize(saved_query)
  32. def test_put_updates_existing_homepage_query_to_reflect_new_data(self):
  33. saved_query = DiscoverSavedQuery.objects.create(
  34. organization=self.org,
  35. created_by=self.user,
  36. name="Test query",
  37. query=self.query,
  38. is_homepage=True,
  39. )
  40. with self.feature(FEATURES):
  41. response = self.client.put(
  42. self.url,
  43. {
  44. "name": "A new homepage query update",
  45. "projects": self.project_ids,
  46. "fields": ["field1", "field2"],
  47. },
  48. )
  49. assert response.status_code == 200, response.content
  50. saved_query.refresh_from_db()
  51. assert response.data == serialize(saved_query)
  52. assert saved_query.query["fields"] == ["field1", "field2"]
  53. assert set(saved_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  54. def test_put_creates_new_discover_saved_query_if_none_exists(self):
  55. homepage_query_payload = {
  56. "version": 2,
  57. "name": "New Homepage Query",
  58. "projects": self.project_ids,
  59. "environment": ["alpha"],
  60. "fields": ["environment", "platform.name"],
  61. "orderby": "-timestamp",
  62. "range": None,
  63. }
  64. with self.feature(FEATURES):
  65. response = self.client.put(self.url, data=homepage_query_payload)
  66. assert response.status_code == 201, response.content
  67. new_query = DiscoverSavedQuery.objects.get(
  68. created_by=self.user, organization=self.org, is_homepage=True
  69. )
  70. assert response.data == serialize(new_query)
  71. assert new_query.query["fields"] == homepage_query_payload["fields"]
  72. assert new_query.query["environment"] == homepage_query_payload["environment"]
  73. assert set(new_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  74. def test_put_responds_with_saved_empty_name_field(self):
  75. homepage_query_payload = {
  76. "version": 2,
  77. "name": "New Homepage Query",
  78. "projects": self.project_ids,
  79. "environment": ["alpha"],
  80. "fields": ["environment", "platform.name"],
  81. "orderby": "-timestamp",
  82. "range": None,
  83. }
  84. with self.feature(FEATURES):
  85. response = self.client.put(self.url, data=homepage_query_payload)
  86. assert response.status_code == 201, response.content
  87. new_query = DiscoverSavedQuery.objects.get(
  88. created_by=self.user, organization=self.org, is_homepage=True
  89. )
  90. assert new_query.name == ""
  91. assert response.data["name"] == ""
  92. def test_put_with_no_name(self):
  93. homepage_query_payload = {
  94. "version": 2,
  95. "name": "",
  96. "projects": self.project_ids,
  97. "environment": ["alpha"],
  98. "fields": ["environment", "platform.name"],
  99. "orderby": "-timestamp",
  100. "range": None,
  101. }
  102. with self.feature(FEATURES):
  103. response = self.client.put(self.url, data=homepage_query_payload)
  104. assert response.status_code == 201, response.content
  105. new_query = DiscoverSavedQuery.objects.get(
  106. created_by=self.user, organization=self.org, is_homepage=True
  107. )
  108. assert new_query.name == ""
  109. assert response.data["name"] == ""
  110. def test_post_not_allowed(self):
  111. homepage_query_payload = {
  112. "version": 2,
  113. "name": "New Homepage Query",
  114. "projects": ["-1"],
  115. "environment": ["alpha"],
  116. "fields": ["environment", "platform.name"],
  117. "orderby": "-timestamp",
  118. "range": None,
  119. }
  120. with self.feature(FEATURES):
  121. response = self.client.post(self.url, data=homepage_query_payload)
  122. assert response.status_code == 405, response.content
  123. def test_delete_resets_saved_query(self):
  124. DiscoverSavedQuery.objects.create(
  125. organization=self.org,
  126. created_by=self.user,
  127. name="Test query",
  128. query=self.query,
  129. is_homepage=True,
  130. )
  131. with self.feature(FEATURES):
  132. response = self.client.delete(self.url)
  133. assert response.status_code == 204
  134. assert not DiscoverSavedQuery.objects.filter(
  135. created_by=self.user, organization=self.org, is_homepage=True
  136. ).exists()