test_discover_homepage_query.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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", "organizations:discover-query-builder-as-landing-page")
  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. def test_returns_no_response_if_no_homepage_query_for_user(self):
  12. with self.feature(FEATURES):
  13. response = self.client.get(self.url)
  14. assert response.status_code == 204, response.content
  15. assert response.data is None
  16. def test_returns_serialized_saved_query_if_homepage_is_set(self):
  17. saved_query = DiscoverSavedQuery.objects.create(
  18. organization=self.org,
  19. created_by=self.user,
  20. name="Test query",
  21. query=self.query,
  22. is_homepage=True,
  23. )
  24. with self.feature(FEATURES):
  25. response = self.client.get(self.url)
  26. assert response.status_code == 200, response.content
  27. assert response.data == serialize(saved_query)
  28. def test_put_updates_existing_homepage_query_to_reflect_new_data(self):
  29. saved_query = DiscoverSavedQuery.objects.create(
  30. organization=self.org,
  31. created_by=self.user,
  32. name="Test query",
  33. query=self.query,
  34. is_homepage=True,
  35. )
  36. with self.feature(FEATURES):
  37. response = self.client.put(
  38. self.url,
  39. {
  40. "name": "A new homepage query update",
  41. "projects": ["-1"],
  42. "fields": ["field1", "field2"],
  43. },
  44. )
  45. assert response.status_code == 204, response.content
  46. saved_query.refresh_from_db()
  47. assert saved_query.name == "A new homepage query update"
  48. assert saved_query.query["fields"] == ["field1", "field2"]
  49. def test_put_creates_new_discover_saved_query_if_none_exists(self):
  50. homepage_query_payload = {
  51. "version": 2,
  52. "name": "New Homepage Query",
  53. "projects": ["-1"],
  54. "environment": ["alpha"],
  55. "fields": ["environment", "platform.name"],
  56. "orderby": "-timestamp",
  57. "range": None,
  58. }
  59. with self.feature(FEATURES):
  60. response = self.client.put(self.url, data=homepage_query_payload)
  61. assert response.status_code == 201, response.content
  62. new_query = DiscoverSavedQuery.objects.get(
  63. created_by=self.user, organization=self.org, is_homepage=True
  64. )
  65. assert new_query.name == homepage_query_payload["name"]
  66. assert new_query.query["fields"] == homepage_query_payload["fields"]
  67. assert new_query.query["environment"] == homepage_query_payload["environment"]
  68. def test_post_not_allowed(self):
  69. homepage_query_payload = {
  70. "version": 2,
  71. "name": "New Homepage Query",
  72. "projects": ["-1"],
  73. "environment": ["alpha"],
  74. "fields": ["environment", "platform.name"],
  75. "orderby": "-timestamp",
  76. "range": None,
  77. }
  78. with self.feature(FEATURES):
  79. response = self.client.post(self.url, data=homepage_query_payload)
  80. assert response.status_code == 405, response.content
  81. def test_delete_resets_saved_query(self):
  82. DiscoverSavedQuery.objects.create(
  83. organization=self.org,
  84. created_by=self.user,
  85. name="Test query",
  86. query=self.query,
  87. is_homepage=True,
  88. )
  89. with self.feature(FEATURES):
  90. response = self.client.delete(self.url)
  91. assert response.status_code == 204
  92. assert not DiscoverSavedQuery.objects.filter(
  93. created_by=self.user, organization=self.org, is_homepage=True
  94. ).exists()