test_discover_homepage_query.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from django.urls import reverse
  2. from sentry.api.serializers import serialize
  3. from sentry.discover.models import DiscoverSavedQuery, DiscoverSavedQueryTypes
  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_id=self.user.id,
  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_id=self.user.id,
  36. name="Test query",
  37. query=self.query,
  38. dataset=DiscoverSavedQueryTypes.DISCOVER,
  39. is_homepage=True,
  40. )
  41. with self.feature(FEATURES):
  42. response = self.client.put(
  43. self.url,
  44. {
  45. "name": "A new homepage query update",
  46. "projects": self.project_ids,
  47. "fields": ["field1", "field2"],
  48. "queryDataset": DiscoverSavedQueryTypes.get_type_name(
  49. DiscoverSavedQueryTypes.TRANSACTION_LIKE
  50. ),
  51. },
  52. )
  53. assert response.status_code == 200, response.content
  54. saved_query.refresh_from_db()
  55. assert response.data == serialize(saved_query)
  56. assert saved_query.query["fields"] == ["field1", "field2"]
  57. assert saved_query.dataset == DiscoverSavedQueryTypes.TRANSACTION_LIKE
  58. assert set(saved_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  59. def test_put_creates_new_discover_saved_query_if_none_exists(self):
  60. homepage_query_payload = {
  61. "version": 2,
  62. "name": "New Homepage Query",
  63. "projects": self.project_ids,
  64. "environment": ["alpha"],
  65. "fields": ["environment", "platform.name"],
  66. "orderby": "-timestamp",
  67. "range": None,
  68. }
  69. with self.feature(FEATURES):
  70. response = self.client.put(self.url, data=homepage_query_payload)
  71. assert response.status_code == 201, response.content
  72. new_query = DiscoverSavedQuery.objects.get(
  73. created_by_id=self.user.id, organization=self.org, is_homepage=True
  74. )
  75. assert response.data == serialize(new_query)
  76. assert new_query.query["fields"] == homepage_query_payload["fields"]
  77. assert new_query.query["environment"] == homepage_query_payload["environment"]
  78. assert new_query.dataset == DiscoverSavedQueryTypes.get_id_for_type_name("discover")
  79. assert set(new_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  80. def test_put_responds_with_saved_empty_name_field(self):
  81. homepage_query_payload = {
  82. "version": 2,
  83. "name": "New Homepage Query",
  84. "projects": self.project_ids,
  85. "environment": ["alpha"],
  86. "fields": ["environment", "platform.name"],
  87. "orderby": "-timestamp",
  88. "range": None,
  89. }
  90. with self.feature(FEATURES):
  91. response = self.client.put(self.url, data=homepage_query_payload)
  92. assert response.status_code == 201, response.content
  93. new_query = DiscoverSavedQuery.objects.get(
  94. created_by_id=self.user.id, organization=self.org, is_homepage=True
  95. )
  96. assert new_query.name == ""
  97. assert response.data["name"] == ""
  98. def test_put_with_no_name(self):
  99. homepage_query_payload = {
  100. "version": 2,
  101. "name": "",
  102. "projects": self.project_ids,
  103. "environment": ["alpha"],
  104. "fields": ["environment", "platform.name"],
  105. "orderby": "-timestamp",
  106. "range": None,
  107. }
  108. with self.feature(FEATURES):
  109. response = self.client.put(self.url, data=homepage_query_payload)
  110. assert response.status_code == 201, response.content
  111. new_query = DiscoverSavedQuery.objects.get(
  112. created_by_id=self.user.id, organization=self.org, is_homepage=True
  113. )
  114. assert new_query.name == ""
  115. assert response.data["name"] == ""
  116. def test_post_not_allowed(self):
  117. homepage_query_payload = {
  118. "version": 2,
  119. "name": "New Homepage Query",
  120. "projects": ["-1"],
  121. "environment": ["alpha"],
  122. "fields": ["environment", "platform.name"],
  123. "orderby": "-timestamp",
  124. "range": None,
  125. }
  126. with self.feature(FEATURES):
  127. response = self.client.post(self.url, data=homepage_query_payload)
  128. assert response.status_code == 405, response.content
  129. def test_delete_resets_saved_query(self):
  130. DiscoverSavedQuery.objects.create(
  131. organization=self.org,
  132. created_by_id=self.user.id,
  133. name="Test query",
  134. query=self.query,
  135. is_homepage=True,
  136. )
  137. with self.feature(FEATURES):
  138. response = self.client.delete(self.url)
  139. assert response.status_code == 204
  140. assert not DiscoverSavedQuery.objects.filter(
  141. created_by_id=self.user.id, organization=self.org, is_homepage=True
  142. ).exists()