test_group.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. from datetime import timedelta
  2. from unittest.mock import patch
  3. import pytz
  4. from django.utils import timezone
  5. from sentry.api.serializers import serialize
  6. from sentry.api.serializers.models.group import GroupSerializerSnuba
  7. from sentry.models import (
  8. Group,
  9. GroupEnvironment,
  10. GroupLink,
  11. GroupResolution,
  12. GroupSnooze,
  13. GroupStatus,
  14. GroupSubscription,
  15. NotificationSetting,
  16. UserOption,
  17. )
  18. from sentry.notifications.types import NotificationSettingOptionValues, NotificationSettingTypes
  19. from sentry.testutils import APITestCase, SnubaTestCase
  20. from sentry.testutils.helpers.datetime import before_now, iso_format
  21. from sentry.testutils.perfomance_issues.store_transaction import PerfIssueTransactionTestMixin
  22. from sentry.testutils.silo import region_silo_test
  23. from sentry.types.integrations import ExternalProviders
  24. from sentry.types.issues import GroupType
  25. @region_silo_test
  26. class GroupSerializerSnubaTest(APITestCase, SnubaTestCase):
  27. def setUp(self):
  28. super().setUp()
  29. self.min_ago = before_now(minutes=1)
  30. self.day_ago = before_now(days=1)
  31. self.week_ago = before_now(days=7)
  32. def test_permalink(self):
  33. group = self.create_group()
  34. result = serialize(group, self.user, serializer=GroupSerializerSnuba())
  35. assert "http://" in result["permalink"]
  36. assert f"{group.organization.slug}/issues/{group.id}" in result["permalink"]
  37. def test_permalink_outside_org(self):
  38. outside_user = self.create_user()
  39. group = self.create_group()
  40. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  41. assert result["permalink"] is None
  42. def test_is_ignored_with_expired_snooze(self):
  43. now = timezone.now()
  44. user = self.create_user()
  45. group = self.create_group(status=GroupStatus.IGNORED)
  46. GroupSnooze.objects.create(group=group, until=now - timedelta(minutes=1))
  47. result = serialize(group, user, serializer=GroupSerializerSnuba())
  48. assert result["status"] == "unresolved"
  49. assert result["statusDetails"] == {}
  50. def test_is_ignored_with_valid_snooze(self):
  51. now = timezone.now()
  52. user = self.create_user()
  53. group = self.create_group(status=GroupStatus.IGNORED)
  54. snooze = GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1))
  55. result = serialize(group, user, serializer=GroupSerializerSnuba())
  56. assert result["status"] == "ignored"
  57. assert result["statusDetails"]["ignoreCount"] == snooze.count
  58. assert result["statusDetails"]["ignoreWindow"] == snooze.window
  59. assert result["statusDetails"]["ignoreUserCount"] == snooze.user_count
  60. assert result["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  61. assert result["statusDetails"]["ignoreUntil"] == snooze.until
  62. assert result["statusDetails"]["actor"] is None
  63. def test_is_ignored_with_valid_snooze_and_actor(self):
  64. now = timezone.now()
  65. user = self.create_user()
  66. group = self.create_group(status=GroupStatus.IGNORED)
  67. GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1), actor_id=user.id)
  68. result = serialize(group, user, serializer=GroupSerializerSnuba())
  69. assert result["status"] == "ignored"
  70. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  71. def test_resolved_in_next_release(self):
  72. release = self.create_release(project=self.project, version="a")
  73. user = self.create_user()
  74. group = self.create_group(status=GroupStatus.RESOLVED)
  75. GroupResolution.objects.create(
  76. group=group, release=release, type=GroupResolution.Type.in_next_release
  77. )
  78. result = serialize(group, user, serializer=GroupSerializerSnuba())
  79. assert result["status"] == "resolved"
  80. assert result["statusDetails"] == {"inNextRelease": True, "actor": None}
  81. def test_resolved_in_release(self):
  82. release = self.create_release(project=self.project, version="a")
  83. user = self.create_user()
  84. group = self.create_group(status=GroupStatus.RESOLVED)
  85. GroupResolution.objects.create(
  86. group=group, release=release, type=GroupResolution.Type.in_release
  87. )
  88. result = serialize(group, user, serializer=GroupSerializerSnuba())
  89. assert result["status"] == "resolved"
  90. assert result["statusDetails"] == {"inRelease": "a", "actor": None}
  91. def test_resolved_with_actor(self):
  92. release = self.create_release(project=self.project, version="a")
  93. user = self.create_user()
  94. group = self.create_group(status=GroupStatus.RESOLVED)
  95. GroupResolution.objects.create(
  96. group=group, release=release, type=GroupResolution.Type.in_release, actor_id=user.id
  97. )
  98. result = serialize(group, user, serializer=GroupSerializerSnuba())
  99. assert result["status"] == "resolved"
  100. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  101. def test_resolved_in_commit(self):
  102. repo = self.create_repo(project=self.project)
  103. commit = self.create_commit(repo=repo)
  104. user = self.create_user()
  105. group = self.create_group(status=GroupStatus.RESOLVED)
  106. GroupLink.objects.create(
  107. group_id=group.id,
  108. project_id=group.project_id,
  109. linked_id=commit.id,
  110. linked_type=GroupLink.LinkedType.commit,
  111. relationship=GroupLink.Relationship.resolves,
  112. )
  113. result = serialize(group, user, serializer=GroupSerializerSnuba())
  114. assert result["status"] == "resolved"
  115. assert result["statusDetails"]["inCommit"]["id"] == commit.key
  116. @patch("sentry.models.Group.is_over_resolve_age")
  117. def test_auto_resolved(self, mock_is_over_resolve_age):
  118. mock_is_over_resolve_age.return_value = True
  119. user = self.create_user()
  120. group = self.create_group(status=GroupStatus.UNRESOLVED)
  121. result = serialize(group, user, serializer=GroupSerializerSnuba())
  122. assert result["status"] == "resolved"
  123. assert result["statusDetails"] == {"autoResolved": True}
  124. def test_subscribed(self):
  125. user = self.create_user()
  126. group = self.create_group()
  127. GroupSubscription.objects.create(
  128. user=user, group=group, project=group.project, is_active=True
  129. )
  130. result = serialize(group, user, serializer=GroupSerializerSnuba())
  131. assert result["isSubscribed"]
  132. assert result["subscriptionDetails"] == {"reason": "unknown"}
  133. def test_explicit_unsubscribed(self):
  134. user = self.create_user()
  135. group = self.create_group()
  136. GroupSubscription.objects.create(
  137. user=user, group=group, project=group.project, is_active=False
  138. )
  139. result = serialize(group, user, serializer=GroupSerializerSnuba())
  140. assert not result["isSubscribed"]
  141. assert not result["subscriptionDetails"]
  142. def test_implicit_subscribed(self):
  143. user = self.create_user()
  144. group = self.create_group()
  145. combinations = (
  146. # (default, project, subscribed, has_details)
  147. (
  148. NotificationSettingOptionValues.ALWAYS,
  149. NotificationSettingOptionValues.DEFAULT,
  150. True,
  151. False,
  152. ),
  153. (
  154. NotificationSettingOptionValues.ALWAYS,
  155. NotificationSettingOptionValues.ALWAYS,
  156. True,
  157. False,
  158. ),
  159. (
  160. NotificationSettingOptionValues.ALWAYS,
  161. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  162. False,
  163. False,
  164. ),
  165. (
  166. NotificationSettingOptionValues.ALWAYS,
  167. NotificationSettingOptionValues.NEVER,
  168. False,
  169. True,
  170. ),
  171. (
  172. NotificationSettingOptionValues.DEFAULT,
  173. NotificationSettingOptionValues.DEFAULT,
  174. False,
  175. False,
  176. ),
  177. (
  178. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  179. NotificationSettingOptionValues.DEFAULT,
  180. False,
  181. False,
  182. ),
  183. (
  184. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  185. NotificationSettingOptionValues.ALWAYS,
  186. True,
  187. False,
  188. ),
  189. (
  190. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  191. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  192. False,
  193. False,
  194. ),
  195. (
  196. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  197. NotificationSettingOptionValues.NEVER,
  198. False,
  199. True,
  200. ),
  201. (
  202. NotificationSettingOptionValues.NEVER,
  203. NotificationSettingOptionValues.DEFAULT,
  204. False,
  205. True,
  206. ),
  207. (
  208. NotificationSettingOptionValues.NEVER,
  209. NotificationSettingOptionValues.ALWAYS,
  210. True,
  211. False,
  212. ),
  213. (
  214. NotificationSettingOptionValues.NEVER,
  215. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  216. False,
  217. False,
  218. ),
  219. (
  220. NotificationSettingOptionValues.NEVER,
  221. NotificationSettingOptionValues.NEVER,
  222. False,
  223. True,
  224. ),
  225. )
  226. for default_value, project_value, is_subscribed, has_details in combinations:
  227. UserOption.objects.clear_local_cache()
  228. NotificationSetting.objects.update_settings(
  229. ExternalProviders.EMAIL,
  230. NotificationSettingTypes.WORKFLOW,
  231. default_value,
  232. user=user,
  233. )
  234. NotificationSetting.objects.update_settings(
  235. ExternalProviders.EMAIL,
  236. NotificationSettingTypes.WORKFLOW,
  237. project_value,
  238. user=user,
  239. project=group.project,
  240. )
  241. NotificationSetting.objects.update_settings(
  242. ExternalProviders.SLACK,
  243. NotificationSettingTypes.WORKFLOW,
  244. default_value,
  245. user=user,
  246. )
  247. NotificationSetting.objects.update_settings(
  248. ExternalProviders.SLACK,
  249. NotificationSettingTypes.WORKFLOW,
  250. project_value,
  251. user=user,
  252. project=group.project,
  253. )
  254. result = serialize(group, user, serializer=GroupSerializerSnuba())
  255. subscription_details = result.get("subscriptionDetails")
  256. assert result["isSubscribed"] is is_subscribed
  257. assert (
  258. subscription_details == {"disabled": True}
  259. if has_details
  260. else subscription_details is None
  261. )
  262. def test_global_no_conversations_overrides_group_subscription(self):
  263. user = self.create_user()
  264. group = self.create_group()
  265. GroupSubscription.objects.create(
  266. user=user, group=group, project=group.project, is_active=True
  267. )
  268. for provider in [ExternalProviders.EMAIL, ExternalProviders.SLACK]:
  269. NotificationSetting.objects.update_settings(
  270. provider,
  271. NotificationSettingTypes.WORKFLOW,
  272. NotificationSettingOptionValues.NEVER,
  273. user=user,
  274. )
  275. result = serialize(group, user, serializer=GroupSerializerSnuba())
  276. assert not result["isSubscribed"]
  277. assert result["subscriptionDetails"] == {"disabled": True}
  278. def test_project_no_conversations_overrides_group_subscription(self):
  279. user = self.create_user()
  280. group = self.create_group()
  281. GroupSubscription.objects.create(
  282. user=user, group=group, project=group.project, is_active=True
  283. )
  284. for provider in [ExternalProviders.EMAIL, ExternalProviders.SLACK]:
  285. NotificationSetting.objects.update_settings(
  286. provider,
  287. NotificationSettingTypes.WORKFLOW,
  288. NotificationSettingOptionValues.NEVER,
  289. user=user,
  290. project=group.project,
  291. )
  292. result = serialize(group, user, serializer=GroupSerializerSnuba())
  293. assert not result["isSubscribed"]
  294. assert result["subscriptionDetails"] == {"disabled": True}
  295. def test_no_user_unsubscribed(self):
  296. group = self.create_group()
  297. result = serialize(group, serializer=GroupSerializerSnuba())
  298. assert not result["isSubscribed"]
  299. def test_seen_stats(self):
  300. environment = self.create_environment(project=self.project)
  301. environment2 = self.create_environment(project=self.project)
  302. events = []
  303. for event_id, env, user_id, timestamp in [
  304. ("a" * 32, environment, 1, iso_format(self.min_ago)),
  305. ("b" * 32, environment, 2, iso_format(self.min_ago)),
  306. ("c" * 32, environment2, 3, iso_format(self.week_ago)),
  307. ]:
  308. events.append(
  309. self.store_event(
  310. data={
  311. "event_id": event_id,
  312. "fingerprint": ["put-me-in-group1"],
  313. "timestamp": timestamp,
  314. "environment": env.name,
  315. "user": {"id": user_id},
  316. },
  317. project_id=self.project.id,
  318. )
  319. )
  320. # Assert all events are in the same group
  321. (group_id,) = {e.group.id for e in events}
  322. group = Group.objects.get(id=group_id)
  323. group.times_seen = 3
  324. group.first_seen = self.week_ago - timedelta(days=5)
  325. group.last_seen = self.week_ago
  326. group.save()
  327. # should use group columns when no environments arg passed
  328. result = serialize(group, serializer=GroupSerializerSnuba(environment_ids=[]))
  329. assert result["count"] == "3"
  330. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  331. assert result["firstSeen"] == group.first_seen
  332. # update this to something different to make sure it's being used
  333. group_env = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment.id)
  334. group_env.first_seen = self.day_ago - timedelta(days=3)
  335. group_env.save()
  336. group_env2 = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment2.id)
  337. result = serialize(
  338. group,
  339. serializer=GroupSerializerSnuba(environment_ids=[environment.id, environment2.id]),
  340. )
  341. assert result["count"] == "3"
  342. # result is rounded down to nearest second
  343. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  344. assert iso_format(result["firstSeen"]) == iso_format(group_env.first_seen)
  345. assert iso_format(group_env2.first_seen) > iso_format(group_env.first_seen)
  346. assert result["userCount"] == 3
  347. result = serialize(
  348. group,
  349. serializer=GroupSerializerSnuba(
  350. environment_ids=[environment.id, environment2.id],
  351. start=self.week_ago - timedelta(hours=1),
  352. end=self.week_ago + timedelta(hours=1),
  353. ),
  354. )
  355. assert result["userCount"] == 1
  356. assert iso_format(result["lastSeen"]) == iso_format(self.week_ago)
  357. assert iso_format(result["firstSeen"]) == iso_format(self.week_ago)
  358. assert result["count"] == "1"
  359. def test_get_start_from_seen_stats(self):
  360. for days, expected in [(None, 30), (0, 14), (1000, 90)]:
  361. last_seen = None if days is None else before_now(days=days).replace(tzinfo=pytz.UTC)
  362. start = GroupSerializerSnuba._get_start_from_seen_stats({"": {"last_seen": last_seen}})
  363. assert iso_format(start) == iso_format(before_now(days=expected))
  364. @region_silo_test
  365. class PerformanceGroupSerializerSnubaTest(
  366. APITestCase,
  367. SnubaTestCase,
  368. PerfIssueTransactionTestMixin,
  369. ):
  370. def test_perf_seen_stats(self):
  371. proj = self.create_project()
  372. environment = self.create_environment(project=proj)
  373. first_group_fingerprint = f"{GroupType.PERFORMANCE_SLOW_SPAN.value}-group1"
  374. timestamp = timezone.now() - timedelta(days=5)
  375. times = 5
  376. for _ in range(0, times):
  377. self.store_transaction(
  378. proj.id,
  379. "user1",
  380. [first_group_fingerprint],
  381. environment.name,
  382. timestamp=timestamp + timedelta(minutes=1),
  383. )
  384. event = self.store_transaction(
  385. proj.id,
  386. "user2",
  387. [first_group_fingerprint],
  388. environment.name,
  389. timestamp=timestamp + timedelta(minutes=2),
  390. )
  391. first_group = event.groups[0]
  392. result = serialize(
  393. first_group,
  394. serializer=GroupSerializerSnuba(
  395. environment_ids=[environment.id],
  396. start=timestamp - timedelta(hours=1),
  397. end=timestamp + timedelta(hours=1),
  398. ),
  399. )
  400. assert result["userCount"] == 2
  401. assert iso_format(result["lastSeen"]) == iso_format(timestamp + timedelta(minutes=2))
  402. assert iso_format(result["firstSeen"]) == iso_format(timestamp + timedelta(minutes=1))
  403. assert result["count"] == str(times + 1)