test_deploy.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from unittest import mock
  2. import responses
  3. from django.utils import timezone
  4. from sentry.models.activity import Activity
  5. from sentry.models.deploy import Deploy
  6. from sentry.models.release import Release
  7. from sentry.notifications.notifications.activity.release import ReleaseActivityNotification
  8. from sentry.testutils.cases import SlackActivityNotificationTest
  9. from sentry.testutils.helpers.features import with_feature
  10. from sentry.testutils.helpers.slack import get_attachment, send_notification
  11. from sentry.testutils.silo import region_silo_test
  12. from sentry.types.activity import ActivityType
  13. @region_silo_test(stable=True)
  14. class SlackDeployNotificationTest(SlackActivityNotificationTest):
  15. @responses.activate
  16. @mock.patch("sentry.notifications.notify.notify", side_effect=send_notification)
  17. def test_deploy(self, mock_func):
  18. """
  19. Test that a Slack message is sent with the expected payload when a deploy happens.
  20. """
  21. release = Release.objects.create(
  22. version="meow" * 10,
  23. organization_id=self.project.organization_id,
  24. date_released=timezone.now(),
  25. )
  26. # The projects can appear out of order.
  27. projects = (self.project, self.create_project(name="battlesnake"))
  28. SLUGS_TO_PROJECT = {project.slug: project for project in projects}
  29. for project in projects:
  30. release.add_project(project)
  31. deploy = Deploy.objects.create(
  32. release=release,
  33. organization_id=self.organization.id,
  34. environment_id=self.environment.id,
  35. )
  36. notification = ReleaseActivityNotification(
  37. Activity(
  38. project=self.project,
  39. user_id=self.user.id,
  40. type=ActivityType.RELEASE.value,
  41. data={"version": release.version, "deploy_id": deploy.id},
  42. )
  43. )
  44. with self.tasks():
  45. notification.send()
  46. attachment, text = get_attachment()
  47. assert (
  48. text
  49. == f"Release {release.version} was deployed to {self.environment.name} for these projects"
  50. )
  51. first_project = None
  52. notification_uuid = self.get_notification_uuid(attachment["actions"][0]["url"])
  53. for i in range(len(projects)):
  54. project = SLUGS_TO_PROJECT[attachment["actions"][i]["text"]]
  55. if not first_project:
  56. first_project = project
  57. assert (
  58. attachment["actions"][i]["url"]
  59. == f"http://testserver/organizations/{self.organization.slug}/releases/"
  60. f"{release.version}/?project={project.id}&unselectedSeries=Healthy&referrer=release_activity&notification_uuid={notification_uuid}"
  61. )
  62. assert first_project is not None
  63. assert (
  64. attachment["footer"]
  65. == f"{first_project.slug} | <http://testserver/settings/account/notifications/"
  66. f"deploy/?referrer=release_activity-slack-user&notification_uuid={notification_uuid}|Notification Settings>"
  67. )
  68. @responses.activate
  69. @mock.patch("sentry.notifications.notify.notify", side_effect=send_notification)
  70. @with_feature("organizations:notification-settings-v2")
  71. def test_deploy_v2(self, mock_func):
  72. """
  73. Test that a Slack message is sent with the expected payload when a deploy happens.
  74. """
  75. release = Release.objects.create(
  76. version="meow" * 10,
  77. organization_id=self.project.organization_id,
  78. date_released=timezone.now(),
  79. )
  80. # The projects can appear out of order.
  81. projects = (self.project, self.create_project(name="battlesnake"))
  82. SLUGS_TO_PROJECT = {project.slug: project for project in projects}
  83. for project in projects:
  84. release.add_project(project)
  85. deploy = Deploy.objects.create(
  86. release=release,
  87. organization_id=self.organization.id,
  88. environment_id=self.environment.id,
  89. )
  90. notification = ReleaseActivityNotification(
  91. Activity(
  92. project=self.project,
  93. user_id=self.user.id,
  94. type=ActivityType.RELEASE.value,
  95. data={"version": release.version, "deploy_id": deploy.id},
  96. )
  97. )
  98. with self.tasks():
  99. notification.send()
  100. attachment, text = get_attachment()
  101. assert (
  102. text
  103. == f"Release {release.version} was deployed to {self.environment.name} for these projects"
  104. )
  105. first_project = None
  106. notification_uuid = self.get_notification_uuid(attachment["actions"][0]["url"])
  107. for i in range(len(projects)):
  108. project = SLUGS_TO_PROJECT[attachment["actions"][i]["text"]]
  109. if not first_project:
  110. first_project = project
  111. assert (
  112. attachment["actions"][i]["url"]
  113. == f"http://testserver/organizations/{self.organization.slug}/releases/"
  114. f"{release.version}/?project={project.id}&unselectedSeries=Healthy&referrer=release_activity&notification_uuid={notification_uuid}"
  115. )
  116. assert first_project is not None
  117. assert (
  118. attachment["footer"]
  119. == f"{first_project.slug} | <http://testserver/settings/account/notifications/"
  120. f"deploy/?referrer=release_activity-slack-user&notification_uuid={notification_uuid}|Notification Settings>"
  121. )