test_group.py 16 KB

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