test_discover_saved_query_detail.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import pytest
  2. from django.urls import NoReverseMatch, reverse
  3. from sentry.discover.models import DiscoverSavedQuery, DiscoverSavedQueryProject
  4. from sentry.testutils import APITestCase, SnubaTestCase
  5. class DiscoverSavedQueryDetailTest(APITestCase, SnubaTestCase):
  6. feature_name = "organizations:discover"
  7. def setUp(self):
  8. super().setUp()
  9. self.login_as(user=self.user)
  10. self.org = self.create_organization(owner=self.user)
  11. self.org_without_access = self.create_organization()
  12. self.project_ids = [
  13. self.create_project(organization=self.org).id,
  14. self.create_project(organization=self.org).id,
  15. ]
  16. query = {"fields": ["test"], "conditions": [], "limit": 10}
  17. model = DiscoverSavedQuery.objects.create(
  18. organization=self.org, created_by=self.user, name="Test query", query=query
  19. )
  20. model.set_projects(self.project_ids)
  21. self.query_id = model.id
  22. invalid = DiscoverSavedQuery.objects.create(
  23. organization=self.org_without_access, name="Query without access", query=query
  24. )
  25. invalid.set_projects(self.project_ids)
  26. self.query_id_without_access = invalid.id
  27. def test_invalid_id(self):
  28. with pytest.raises(NoReverseMatch):
  29. reverse("sentry-api-0-discover-saved-query-detail", args=[self.org.slug, "not-an-id"])
  30. def test_get(self):
  31. with self.feature(self.feature_name):
  32. url = reverse(
  33. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, self.query_id]
  34. )
  35. response = self.client.get(url)
  36. assert response.status_code == 200, response.content
  37. assert response.data["id"] == str(self.query_id)
  38. assert set(response.data["projects"]) == set(self.project_ids)
  39. assert response.data["fields"] == ["test"]
  40. assert response.data["conditions"] == []
  41. assert response.data["limit"] == 10
  42. def test_get_discover_query_flag(self):
  43. with self.feature("organizations:discover-query"):
  44. url = reverse(
  45. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, self.query_id]
  46. )
  47. response = self.client.get(url)
  48. assert response.status_code == 200, response.content
  49. assert response.data["id"] == str(self.query_id)
  50. assert set(response.data["projects"]) == set(self.project_ids)
  51. assert response.data["fields"] == ["test"]
  52. assert response.data["conditions"] == []
  53. assert response.data["limit"] == 10
  54. def test_get_version(self):
  55. query = {"fields": ["event_id"], "query": "event.type:error", "limit": 10, "version": 2}
  56. model = DiscoverSavedQuery.objects.create(
  57. organization=self.org, created_by=self.user, name="v2 query", query=query
  58. )
  59. model.set_projects(self.project_ids)
  60. with self.feature(self.feature_name):
  61. url = reverse(
  62. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, model.id]
  63. )
  64. response = self.client.get(url)
  65. assert response.status_code == 200, response.content
  66. assert response.data["id"] == str(model.id)
  67. assert set(response.data["projects"]) == set(self.project_ids)
  68. assert response.data["fields"] == ["event_id"]
  69. assert response.data["query"] == "event.type:error"
  70. assert response.data["limit"] == 10
  71. assert response.data["version"] == 2
  72. def test_get_org_without_access(self):
  73. with self.feature(self.feature_name):
  74. url = reverse(
  75. "sentry-api-0-discover-saved-query-detail",
  76. args=[self.org_without_access.slug, self.query_id],
  77. )
  78. response = self.client.get(url)
  79. assert response.status_code == 403, response.content
  80. def test_put(self):
  81. with self.feature(self.feature_name):
  82. url = reverse(
  83. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, self.query_id]
  84. )
  85. response = self.client.put(
  86. url,
  87. {
  88. "name": "New query",
  89. "projects": self.project_ids,
  90. "fields": [],
  91. "range": "24h",
  92. "limit": 20,
  93. "conditions": [],
  94. "aggregations": [],
  95. "orderby": "-time",
  96. },
  97. )
  98. assert response.status_code == 200, response.content
  99. assert response.data["id"] == str(self.query_id)
  100. assert set(response.data["projects"]) == set(self.project_ids)
  101. assert response.data["fields"] == []
  102. assert response.data["conditions"] == []
  103. assert response.data["limit"] == 20
  104. def test_put_query_without_access(self):
  105. with self.feature(self.feature_name):
  106. url = reverse(
  107. "sentry-api-0-discover-saved-query-detail",
  108. args=[self.org.slug, self.query_id_without_access],
  109. )
  110. response = self.client.put(
  111. url, {"name": "New query", "projects": self.project_ids, "range": "24h"}
  112. )
  113. assert response.status_code == 404
  114. def test_put_query_with_team(self):
  115. team = self.create_team(organization=self.org, members=[self.user])
  116. project = self.create_project(organization=self.org, teams=[team])
  117. query = DiscoverSavedQuery.objects.create(
  118. organization=self.org,
  119. created_by=self.user,
  120. name="Test query",
  121. query={"fields": ["test"], "conditions": [], "limit": 10},
  122. )
  123. query.set_projects([project.id])
  124. with self.feature(self.feature_name):
  125. url = reverse(
  126. "sentry-api-0-discover-saved-query-detail",
  127. args=[self.org.slug, query.id],
  128. )
  129. response = self.client.put(url, {"name": "New query", "projects": [], "range": "24h"})
  130. assert response.status_code == 200
  131. def test_put_query_without_team(self):
  132. team = self.create_team(organization=self.org, members=[])
  133. project = self.create_project(organization=self.org, teams=[team])
  134. query = DiscoverSavedQuery.objects.create(
  135. organization=self.org,
  136. created_by=self.user,
  137. name="Test query",
  138. query={"fields": ["test"], "conditions": [], "limit": 10},
  139. )
  140. query.set_projects([project.id])
  141. with self.feature(self.feature_name):
  142. url = reverse(
  143. "sentry-api-0-discover-saved-query-detail",
  144. args=[self.org.slug, query.id],
  145. )
  146. response = self.client.put(url, {"name": "New query", "projects": [], "range": "24h"})
  147. assert response.status_code == 400
  148. assert "No Projects found, join a Team" == response.data["detail"]
  149. def test_put_org_without_access(self):
  150. with self.feature(self.feature_name):
  151. url = reverse(
  152. "sentry-api-0-discover-saved-query-detail",
  153. args=[self.org_without_access.slug, self.query_id],
  154. )
  155. response = self.client.put(
  156. url, {"name": "New query", "projects": self.project_ids, "range": "24h"}
  157. )
  158. assert response.status_code == 403, response.content
  159. def test_delete(self):
  160. with self.feature(self.feature_name):
  161. url = reverse(
  162. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, self.query_id]
  163. )
  164. response = self.client.delete(url)
  165. assert response.status_code == 204
  166. assert self.client.get(url).status_code == 404
  167. def test_delete_removes_projects(self):
  168. with self.feature(self.feature_name):
  169. url = reverse(
  170. "sentry-api-0-discover-saved-query-detail", args=[self.org.slug, self.query_id]
  171. )
  172. self.client.delete(url)
  173. projects = list(
  174. DiscoverSavedQueryProject.objects.filter(discover_saved_query=self.query_id)
  175. )
  176. assert projects == []
  177. def test_delete_query_without_access(self):
  178. with self.feature(self.feature_name):
  179. url = reverse(
  180. "sentry-api-0-discover-saved-query-detail",
  181. args=[self.org.slug, self.query_id_without_access],
  182. )
  183. response = self.client.delete(url)
  184. assert response.status_code == 404
  185. def test_delete_org_without_access(self):
  186. with self.feature(self.feature_name):
  187. url = reverse(
  188. "sentry-api-0-discover-saved-query-detail",
  189. args=[self.org_without_access.slug, self.query_id],
  190. )
  191. response = self.client.delete(url)
  192. assert response.status_code == 403, response.content
  193. class OrganizationDiscoverQueryVisitTest(APITestCase, SnubaTestCase):
  194. def setUp(self):
  195. super().setUp()
  196. self.login_as(user=self.user)
  197. self.org = self.create_organization(owner=self.user)
  198. self.org_without_access = self.create_organization()
  199. self.project_ids = [
  200. self.create_project(organization=self.org).id,
  201. self.create_project(organization=self.org).id,
  202. ]
  203. q = {"fields": ["test"], "conditions": [], "limit": 10}
  204. self.query = DiscoverSavedQuery.objects.create(
  205. organization=self.org, created_by=self.user, name="Test query", query=q
  206. )
  207. self.query.set_projects(self.project_ids)
  208. def url(self, query_id):
  209. return reverse(
  210. "sentry-api-0-discover-saved-query-visit",
  211. kwargs={"organization_slug": self.org.slug, "query_id": query_id},
  212. )
  213. def test_visit_query(self):
  214. last_visited = self.query.last_visited
  215. assert self.query.visits == 1
  216. with self.feature("organizations:discover-query"):
  217. response = self.client.post(self.url(self.query.id))
  218. assert response.status_code == 204
  219. query = DiscoverSavedQuery.objects.get(id=self.query.id)
  220. assert query.visits == 2
  221. assert query.last_visited > last_visited
  222. def test_visit_query_no_access(self):
  223. last_visited = self.query.last_visited
  224. assert self.query.visits == 1
  225. with self.feature({"organizations:discover-query": False}):
  226. response = self.client.post(self.url(self.query.id))
  227. assert response.status_code == 404
  228. query = DiscoverSavedQuery.objects.get(id=self.query.id)
  229. assert query.visits == 1
  230. assert query.last_visited == last_visited