test_group.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import mock
  4. import six
  5. from datetime import timedelta
  6. from django.utils import timezone
  7. from mock import patch
  8. from sentry.api.serializers import serialize
  9. from sentry.api.serializers.models.group import GroupSerializerSnuba, StreamGroupSerializerSnuba, snuba_tsdb
  10. from sentry.models import (
  11. Environment, GroupEnvironment, GroupLink, GroupResolution, GroupSnooze, GroupStatus,
  12. GroupSubscription, UserOption, UserOptionValue
  13. )
  14. from sentry.testutils import APITestCase, SnubaTestCase
  15. class GroupSerializerSnubaTest(APITestCase, SnubaTestCase):
  16. def setUp(self):
  17. super(GroupSerializerSnubaTest, self).setUp()
  18. self.min_ago = timezone.now() - timedelta(minutes=1)
  19. self.day_ago = timezone.now() - timedelta(days=1)
  20. self.week_ago = timezone.now() - timedelta(days=7)
  21. def test_is_ignored_with_expired_snooze(self):
  22. now = timezone.now().replace(microsecond=0)
  23. user = self.create_user()
  24. group = self.create_group(
  25. status=GroupStatus.IGNORED,
  26. )
  27. GroupSnooze.objects.create(
  28. group=group,
  29. until=now - timedelta(minutes=1),
  30. )
  31. result = serialize(group, user, serializer=GroupSerializerSnuba())
  32. assert result['status'] == 'unresolved'
  33. assert result['statusDetails'] == {}
  34. def test_is_ignored_with_valid_snooze(self):
  35. now = timezone.now().replace(microsecond=0)
  36. user = self.create_user()
  37. group = self.create_group(
  38. status=GroupStatus.IGNORED,
  39. )
  40. snooze = GroupSnooze.objects.create(
  41. group=group,
  42. until=now + timedelta(minutes=1),
  43. )
  44. result = serialize(group, user, serializer=GroupSerializerSnuba())
  45. assert result['status'] == 'ignored'
  46. assert result['statusDetails']['ignoreCount'] == snooze.count
  47. assert result['statusDetails']['ignoreWindow'] == snooze.window
  48. assert result['statusDetails']['ignoreUserCount'] == snooze.user_count
  49. assert result['statusDetails']['ignoreUserWindow'] == snooze.user_window
  50. assert result['statusDetails']['ignoreUntil'] == snooze.until
  51. assert result['statusDetails']['actor'] is None
  52. def test_is_ignored_with_valid_snooze_and_actor(self):
  53. now = timezone.now().replace(microsecond=0)
  54. user = self.create_user()
  55. group = self.create_group(
  56. status=GroupStatus.IGNORED,
  57. )
  58. GroupSnooze.objects.create(
  59. group=group,
  60. until=now + timedelta(minutes=1),
  61. actor_id=user.id,
  62. )
  63. result = serialize(group, user, serializer=GroupSerializerSnuba())
  64. assert result['status'] == 'ignored'
  65. assert result['statusDetails']['actor']['id'] == six.text_type(user.id)
  66. def test_resolved_in_next_release(self):
  67. release = self.create_release(project=self.project, version='a')
  68. user = self.create_user()
  69. group = self.create_group(
  70. status=GroupStatus.RESOLVED,
  71. )
  72. GroupResolution.objects.create(
  73. group=group,
  74. release=release,
  75. type=GroupResolution.Type.in_next_release,
  76. )
  77. result = serialize(group, user, serializer=GroupSerializerSnuba())
  78. assert result['status'] == 'resolved'
  79. assert result['statusDetails'] == {'inNextRelease': True, 'actor': None}
  80. def test_resolved_in_release(self):
  81. release = self.create_release(project=self.project, version='a')
  82. user = self.create_user()
  83. group = self.create_group(
  84. status=GroupStatus.RESOLVED,
  85. )
  86. GroupResolution.objects.create(
  87. group=group,
  88. release=release,
  89. type=GroupResolution.Type.in_release,
  90. )
  91. result = serialize(group, user, serializer=GroupSerializerSnuba())
  92. assert result['status'] == 'resolved'
  93. assert result['statusDetails'] == {'inRelease': 'a', 'actor': None}
  94. def test_resolved_with_actor(self):
  95. release = self.create_release(project=self.project, version='a')
  96. user = self.create_user()
  97. group = self.create_group(
  98. status=GroupStatus.RESOLVED,
  99. )
  100. GroupResolution.objects.create(
  101. group=group,
  102. release=release,
  103. type=GroupResolution.Type.in_release,
  104. actor_id=user.id,
  105. )
  106. result = serialize(group, user, serializer=GroupSerializerSnuba())
  107. assert result['status'] == 'resolved'
  108. assert result['statusDetails']['actor']['id'] == six.text_type(user.id)
  109. def test_resolved_in_commit(self):
  110. repo = self.create_repo(project=self.project)
  111. commit = self.create_commit(repo=repo)
  112. user = self.create_user()
  113. group = self.create_group(
  114. status=GroupStatus.RESOLVED,
  115. )
  116. GroupLink.objects.create(
  117. group_id=group.id,
  118. project_id=group.project_id,
  119. linked_id=commit.id,
  120. linked_type=GroupLink.LinkedType.commit,
  121. relationship=GroupLink.Relationship.resolves,
  122. )
  123. result = serialize(group, user, serializer=GroupSerializerSnuba())
  124. assert result['status'] == 'resolved'
  125. assert result['statusDetails']['inCommit']['id'] == commit.key
  126. @patch('sentry.models.Group.is_over_resolve_age')
  127. def test_auto_resolved(self, mock_is_over_resolve_age):
  128. mock_is_over_resolve_age.return_value = True
  129. user = self.create_user()
  130. group = self.create_group(
  131. status=GroupStatus.UNRESOLVED,
  132. )
  133. result = serialize(group, user, serializer=GroupSerializerSnuba())
  134. assert result['status'] == 'resolved'
  135. assert result['statusDetails'] == {'autoResolved': True}
  136. def test_subscribed(self):
  137. user = self.create_user()
  138. group = self.create_group()
  139. GroupSubscription.objects.create(
  140. user=user,
  141. group=group,
  142. project=group.project,
  143. is_active=True,
  144. )
  145. result = serialize(group, user, serializer=GroupSerializerSnuba())
  146. assert result['isSubscribed']
  147. assert result['subscriptionDetails'] == {
  148. 'reason': 'unknown',
  149. }
  150. def test_explicit_unsubscribed(self):
  151. user = self.create_user()
  152. group = self.create_group()
  153. GroupSubscription.objects.create(
  154. user=user,
  155. group=group,
  156. project=group.project,
  157. is_active=False,
  158. )
  159. result = serialize(group, user, serializer=GroupSerializerSnuba())
  160. assert not result['isSubscribed']
  161. assert not result['subscriptionDetails']
  162. def test_implicit_subscribed(self):
  163. user = self.create_user()
  164. group = self.create_group()
  165. combinations = (
  166. # ((default, project), (subscribed, details))
  167. ((None, None), (True, None)),
  168. ((UserOptionValue.all_conversations, None), (True, None)),
  169. ((UserOptionValue.all_conversations, UserOptionValue.all_conversations), (True, None)),
  170. ((UserOptionValue.all_conversations, UserOptionValue.participating_only), (False, None)),
  171. ((UserOptionValue.all_conversations, UserOptionValue.no_conversations),
  172. (False, {'disabled': True})),
  173. ((UserOptionValue.participating_only, None), (False, None)),
  174. ((UserOptionValue.participating_only, UserOptionValue.all_conversations), (True, None)),
  175. ((UserOptionValue.participating_only, UserOptionValue.participating_only), (False, None)),
  176. ((UserOptionValue.participating_only, UserOptionValue.no_conversations),
  177. (False, {'disabled': True})),
  178. ((UserOptionValue.no_conversations, None), (False, {'disabled': True})),
  179. ((UserOptionValue.no_conversations, UserOptionValue.all_conversations), (True, None)),
  180. ((UserOptionValue.no_conversations, UserOptionValue.participating_only), (False, None)),
  181. ((UserOptionValue.no_conversations, UserOptionValue.no_conversations),
  182. (False, {'disabled': True})),
  183. )
  184. def maybe_set_value(project, value):
  185. if value is not None:
  186. UserOption.objects.set_value(
  187. user=user,
  188. project=project,
  189. key='workflow:notifications',
  190. value=value,
  191. )
  192. else:
  193. UserOption.objects.unset_value(
  194. user=user,
  195. project=project,
  196. key='workflow:notifications',
  197. )
  198. for options, (is_subscribed, subscription_details) in combinations:
  199. default_value, project_value = options
  200. UserOption.objects.clear_local_cache()
  201. maybe_set_value(None, default_value)
  202. maybe_set_value(group.project, project_value)
  203. result = serialize(group, user, serializer=GroupSerializerSnuba())
  204. assert result['isSubscribed'] is is_subscribed
  205. assert result.get('subscriptionDetails') == subscription_details
  206. def test_global_no_conversations_overrides_group_subscription(self):
  207. user = self.create_user()
  208. group = self.create_group()
  209. GroupSubscription.objects.create(
  210. user=user,
  211. group=group,
  212. project=group.project,
  213. is_active=True,
  214. )
  215. UserOption.objects.set_value(
  216. user=user,
  217. project=None,
  218. key='workflow:notifications',
  219. value=UserOptionValue.no_conversations,
  220. )
  221. result = serialize(group, user, serializer=GroupSerializerSnuba())
  222. assert not result['isSubscribed']
  223. assert result['subscriptionDetails'] == {
  224. 'disabled': True,
  225. }
  226. def test_project_no_conversations_overrides_group_subscription(self):
  227. user = self.create_user()
  228. group = self.create_group()
  229. GroupSubscription.objects.create(
  230. user=user,
  231. group=group,
  232. project=group.project,
  233. is_active=True,
  234. )
  235. UserOption.objects.set_value(
  236. user=user,
  237. project=group.project,
  238. key='workflow:notifications',
  239. value=UserOptionValue.no_conversations,
  240. )
  241. result = serialize(group, user, serializer=GroupSerializerSnuba())
  242. assert not result['isSubscribed']
  243. assert result['subscriptionDetails'] == {
  244. 'disabled': True,
  245. }
  246. def test_no_user_unsubscribed(self):
  247. group = self.create_group()
  248. result = serialize(group, serializer=GroupSerializerSnuba())
  249. assert not result['isSubscribed']
  250. def test_seen_stats(self):
  251. group = self.create_group(first_seen=self.week_ago, times_seen=5)
  252. # should use group columns when no environments arg passed
  253. result = serialize(group, serializer=GroupSerializerSnuba())
  254. assert result['count'] == '5'
  255. assert result['lastSeen'] == group.last_seen
  256. assert result['firstSeen'] == group.first_seen
  257. environment = self.create_environment(project=group.project)
  258. environment2 = self.create_environment(project=group.project)
  259. self.create_event(
  260. 'a' * 32, group=group, datetime=self.day_ago, tags={'environment': environment.name}
  261. )
  262. self.create_event(
  263. 'b' * 32, group=group, datetime=self.min_ago, tags={'environment': environment.name}
  264. )
  265. self.create_event(
  266. 'c' * 32, group=group, datetime=self.min_ago, tags={'environment': environment2.name}
  267. )
  268. # update this to something different to make sure it's being used
  269. group_env = GroupEnvironment.objects.get(group_id=group.id, environment_id=environment.id)
  270. group_env.first_seen = self.day_ago - timedelta(days=3)
  271. group_env.save()
  272. group_env2 = GroupEnvironment.objects.get(group_id=group.id, environment_id=environment2.id)
  273. result = serialize(
  274. group, serializer=GroupSerializerSnuba(
  275. environment_ids=[environment.id, environment2.id])
  276. )
  277. assert result['count'] == '3'
  278. # result is rounded down to nearest second
  279. assert result['lastSeen'] == self.min_ago - timedelta(microseconds=self.min_ago.microsecond)
  280. assert result['firstSeen'] == group_env.first_seen
  281. assert group_env2.first_seen > group_env.first_seen
  282. class StreamGroupSerializerTestCase(APITestCase, SnubaTestCase):
  283. def test_environment(self):
  284. group = self.group
  285. environment = Environment.get_or_create(group.project, 'production')
  286. with mock.patch(
  287. 'sentry.api.serializers.models.group.snuba_tsdb.get_range',
  288. side_effect=snuba_tsdb.get_range) as get_range:
  289. serialize(
  290. [group],
  291. serializer=StreamGroupSerializerSnuba(
  292. environment_ids=[environment.id],
  293. stats_period='14d',
  294. ),
  295. )
  296. assert get_range.call_count == 1
  297. for args, kwargs in get_range.call_args_list:
  298. assert kwargs['environment_ids'] == [environment.id]
  299. with mock.patch(
  300. 'sentry.api.serializers.models.group.snuba_tsdb.get_range',
  301. side_effect=snuba_tsdb.get_range) as get_range:
  302. serialize(
  303. [group],
  304. serializer=StreamGroupSerializerSnuba(
  305. environment_ids=None,
  306. stats_period='14d',
  307. )
  308. )
  309. assert get_range.call_count == 1
  310. for args, kwargs in get_range.call_args_list:
  311. assert kwargs['environment_ids'] is None