test_discover_homepage_query.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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_id=self.user.id, 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 new_query.dataset == DiscoverSavedQueryTypes.get_id_for_type_name("discover")
  74. assert set(new_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  75. def test_put_responds_with_saved_empty_name_field(self):
  76. homepage_query_payload = {
  77. "version": 2,
  78. "name": "New Homepage Query",
  79. "projects": self.project_ids,
  80. "environment": ["alpha"],
  81. "fields": ["environment", "platform.name"],
  82. "orderby": "-timestamp",
  83. "range": None,
  84. }
  85. with self.feature(FEATURES):
  86. response = self.client.put(self.url, data=homepage_query_payload)
  87. assert response.status_code == 201, response.content
  88. new_query = DiscoverSavedQuery.objects.get(
  89. created_by_id=self.user.id, organization=self.org, is_homepage=True
  90. )
  91. assert new_query.name == ""
  92. assert response.data["name"] == ""
  93. def test_put_with_no_name(self):
  94. homepage_query_payload = {
  95. "version": 2,
  96. "name": "",
  97. "projects": self.project_ids,
  98. "environment": ["alpha"],
  99. "fields": ["environment", "platform.name"],
  100. "orderby": "-timestamp",
  101. "range": None,
  102. }
  103. with self.feature(FEATURES):
  104. response = self.client.put(self.url, data=homepage_query_payload)
  105. assert response.status_code == 201, response.content
  106. new_query = DiscoverSavedQuery.objects.get(
  107. created_by_id=self.user.id, organization=self.org, is_homepage=True
  108. )
  109. assert new_query.name == ""
  110. assert response.data["name"] == ""
  111. def test_post_not_allowed(self):
  112. homepage_query_payload = {
  113. "version": 2,
  114. "name": "New Homepage Query",
  115. "projects": ["-1"],
  116. "environment": ["alpha"],
  117. "fields": ["environment", "platform.name"],
  118. "orderby": "-timestamp",
  119. "range": None,
  120. }
  121. with self.feature(FEATURES):
  122. response = self.client.post(self.url, data=homepage_query_payload)
  123. assert response.status_code == 405, response.content
  124. def test_delete_resets_saved_query(self):
  125. DiscoverSavedQuery.objects.create(
  126. organization=self.org,
  127. created_by_id=self.user.id,
  128. name="Test query",
  129. query=self.query,
  130. is_homepage=True,
  131. )
  132. with self.feature(FEATURES):
  133. response = self.client.delete(self.url)
  134. assert response.status_code == 204
  135. assert not DiscoverSavedQuery.objects.filter(
  136. created_by_id=self.user.id, organization=self.org, is_homepage=True
  137. ).exists()