test_plugin.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from __future__ import annotations
  2. from datetime import timedelta
  3. from typing import Any
  4. from unittest.mock import Mock, patch
  5. import pytest
  6. from django.utils import timezone
  7. from sentry.exceptions import HookValidationError
  8. from sentry.models.commit import Commit
  9. from sentry.models.deploy import Deploy
  10. from sentry.models.environment import Environment
  11. from sentry.models.options.project_option import ProjectOption
  12. from sentry.models.release import Release
  13. from sentry.models.releasecommit import ReleaseCommit
  14. from sentry.models.releaseheadcommit import ReleaseHeadCommit
  15. from sentry.models.repository import Repository
  16. from sentry.testutils.cases import TestCase
  17. from sentry.utils import json
  18. from sentry_plugins.heroku.plugin import HerokuReleaseHook
  19. class SetRefsTest(TestCase):
  20. """
  21. tests that when finish_release is called on a release hook,
  22. we try to get the previous commits based on the version ref
  23. and that we create `ReleaseHeadCommit`s for the version
  24. """
  25. @patch("sentry.tasks.commits.fetch_commits")
  26. def test_minimal(self, mock_fetch_commits):
  27. project = self.create_project()
  28. version = "bbee5b51f84611e4b14834363b8514c2"
  29. data_list = [
  30. {
  31. "id": "c7155651831549cf8a5e47889fce17eb",
  32. "message": "foo",
  33. "author_email": "jane@example.com",
  34. },
  35. {
  36. "id": "62de626b7c7cfb8e77efb4273b1a3df4123e6216",
  37. "message": "hello",
  38. "author_name": "Jess",
  39. },
  40. {
  41. "id": "58de626b7c7cfb8e77efb4273b1a3df4123e6345",
  42. "message": "bar",
  43. "author_name": "Joe^^",
  44. },
  45. {
  46. "id": "bbee5b51f84611e4b14834363b8514c2",
  47. "message": "blah",
  48. "author_email": "katie@example.com",
  49. },
  50. ]
  51. user = self.create_user(email="stebe@sentry.io")
  52. repo = Repository.objects.create(
  53. organization_id=project.organization_id, name=project.name, provider="dummy"
  54. )
  55. ProjectOption.objects.set_value(key="heroku:repository", project=project, value=repo.name)
  56. for data in data_list:
  57. Commit.objects.create(
  58. key=data["id"], organization_id=self.project.organization_id, repository_id=repo.id
  59. )
  60. old_release = Release.objects.create(
  61. version="a" * 40,
  62. organization_id=project.organization_id,
  63. date_added=timezone.now() - timedelta(minutes=30),
  64. )
  65. old_release.add_project(project)
  66. ReleaseCommit.objects.create(
  67. organization_id=project.organization_id,
  68. project_id=project.id,
  69. release=old_release,
  70. commit=Commit.objects.get(key="c7155651831549cf8a5e47889fce17eb"),
  71. order=0,
  72. )
  73. ReleaseHeadCommit.objects.create(
  74. organization_id=project.organization_id,
  75. repository_id=repo.id,
  76. release=old_release,
  77. commit=Commit.objects.get(key="c7155651831549cf8a5e47889fce17eb"),
  78. )
  79. release_heads = ReleaseHeadCommit.objects.filter(
  80. organization_id=project.organization_id,
  81. repository_id=repo.id,
  82. commit=Commit.objects.get(key="bbee5b51f84611e4b14834363b8514c2"),
  83. )
  84. assert len(release_heads) == 0
  85. hook = HerokuReleaseHook(project)
  86. hook.finish_release(version=version, owner_id=user.id)
  87. release = Release.objects.get(projects=project, version=version)
  88. new_release_heads = ReleaseHeadCommit.objects.filter(
  89. organization_id=project.organization_id,
  90. repository_id=repo.id,
  91. release=release,
  92. commit=Commit.objects.get(key="bbee5b51f84611e4b14834363b8514c2"),
  93. )
  94. assert len(new_release_heads) == 1
  95. assert release.version == "bbee5b51f84611e4b14834363b8514c2"
  96. deploy = Deploy.objects.filter(
  97. organization_id=project.organization_id,
  98. release=release,
  99. environment_id=Environment.objects.get(
  100. organization_id=project.organization_id, name="production"
  101. ).id,
  102. )
  103. assert len(deploy) == 1
  104. mock_fetch_commits.apply_async.assert_called_with(
  105. kwargs={
  106. "release_id": release.id,
  107. "user_id": user.id,
  108. "refs": [{"commit": "bbee5b51f84611e4b14834363b8514c2", "repository": repo.name}],
  109. "prev_release_id": old_release.id,
  110. }
  111. )
  112. class HookHandleTest(TestCase):
  113. @pytest.fixture(autouse=True)
  114. def patch_is_valid_signature(self):
  115. with patch.object(HerokuReleaseHook, "is_valid_signature"):
  116. yield
  117. @patch.object(HerokuReleaseHook, "set_refs")
  118. def test_user_success(self, set_refs_mock):
  119. user = self.create_user()
  120. organization = self.create_organization(owner=user)
  121. project = self.create_project(organization=organization)
  122. hook = HerokuReleaseHook(project)
  123. req = Mock()
  124. body: dict[str, Any] = {
  125. "data": {
  126. "user": {"email": user.email},
  127. "slug": {"commit": "abcd123"},
  128. "app": {"name": "example"},
  129. },
  130. "action": "update",
  131. }
  132. req.body = bytes(json.dumps(body), "utf-8")
  133. hook.handle(req)
  134. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  135. assert set_refs_mock.call_count == 1
  136. @patch.object(HerokuReleaseHook, "set_refs")
  137. def test_only_run_on_update(self, set_refs_mock):
  138. user = self.create_user()
  139. organization = self.create_organization(owner=user)
  140. project = self.create_project(organization=organization)
  141. hook = HerokuReleaseHook(project)
  142. req = Mock()
  143. body: dict[str, Any] = {
  144. "data": {
  145. "user": {"email": user.email},
  146. "slug": {"commit": "abcd123"},
  147. "app": {"name": "example"},
  148. },
  149. "action": "create",
  150. }
  151. req.body = bytes(json.dumps(body), "utf-8")
  152. hook.handle(req)
  153. assert not Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  154. assert set_refs_mock.call_count == 0
  155. @patch.object(HerokuReleaseHook, "set_refs")
  156. def test_actor_email_success(self, set_refs_mock):
  157. user = self.create_user()
  158. organization = self.create_organization(owner=user)
  159. project = self.create_project(organization=organization)
  160. hook = HerokuReleaseHook(project)
  161. req = Mock()
  162. body: dict[str, Any] = {
  163. "data": {
  164. "actor": {"email": user.email},
  165. "slug": {"commit": "abcd123"},
  166. "app": {"name": "example"},
  167. },
  168. "action": "update",
  169. }
  170. req.body = bytes(json.dumps(body), "utf-8")
  171. hook.handle(req)
  172. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  173. assert set_refs_mock.call_count == 1
  174. def test_email_mismatch(self):
  175. user = self.create_user()
  176. organization = self.create_organization(owner=user)
  177. project = self.create_project(organization=organization)
  178. hook = HerokuReleaseHook(project)
  179. req = Mock()
  180. body: dict[str, Any] = {
  181. "data": {
  182. "user": {"email": "wrong@example.com"},
  183. "slug": {"commit": "v999"},
  184. "app": {"name": "example"},
  185. },
  186. "action": "update",
  187. }
  188. req.body = bytes(json.dumps(body), "utf-8")
  189. hook.handle(req)
  190. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  191. def test_bad_version(self):
  192. project = self.create_project()
  193. user = self.create_user()
  194. hook = HerokuReleaseHook(project)
  195. req = Mock()
  196. body = {
  197. "data": {
  198. "actor": {"email": user.email},
  199. "slug": {"commit": ""},
  200. "app": {"name": "example"},
  201. },
  202. "action": "update",
  203. }
  204. req.body = bytes(json.dumps(body), "utf-8")
  205. with pytest.raises(HookValidationError):
  206. hook.handle(req)