test_push_event.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import contextlib
  2. from datetime import datetime, timezone
  3. from uuid import uuid4
  4. import responses
  5. from fixtures.github import API_GITHUB_COM_USERS_BAXTERTHEHACKER
  6. from sentry.models.commit import Commit
  7. from sentry.models.commitauthor import CommitAuthor
  8. from sentry.models.options.organization_option import OrganizationOption
  9. from sentry.models.repository import Repository
  10. from sentry.testutils.cases import APITestCase
  11. from sentry_plugins.github.testutils import PUSH_EVENT_EXAMPLE
  12. @contextlib.contextmanager
  13. def mock_baxter_response():
  14. with responses.RequestsMock() as mck:
  15. mck.add(
  16. "GET",
  17. "https://api.github.com/users/baxterthehacker",
  18. body=API_GITHUB_COM_USERS_BAXTERTHEHACKER,
  19. )
  20. yield
  21. class PushEventWebhookTest(APITestCase):
  22. @mock_baxter_response()
  23. def test_simple(self):
  24. project = self.project # force creation
  25. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  26. secret = "b3002c3e321d4b7880360d397db2ccfd"
  27. OrganizationOption.objects.set_value(
  28. organization=project.organization, key="github:webhook_secret", value=secret
  29. )
  30. Repository.objects.create(
  31. organization_id=project.organization.id,
  32. external_id="35129377",
  33. provider="github",
  34. name="baxterthehacker/public-repo",
  35. )
  36. response = self.client.post(
  37. path=url,
  38. data=PUSH_EVENT_EXAMPLE,
  39. content_type="application/json",
  40. HTTP_X_GITHUB_EVENT="push",
  41. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  42. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  43. )
  44. assert response.status_code == 204
  45. commit_list = list(
  46. Commit.objects.filter(organization_id=project.organization_id)
  47. .select_related("author")
  48. .order_by("-date_added")
  49. )
  50. assert len(commit_list) == 2
  51. commit = commit_list[0]
  52. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  53. assert commit.message == "Update README.md (àgain)"
  54. assert commit.author is not None
  55. assert commit.author.name == "bàxterthehacker"
  56. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  57. assert commit.author.external_id is None
  58. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  59. commit = commit_list[1]
  60. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  61. assert commit.message == "Update README.md"
  62. assert commit.author is not None
  63. assert commit.author.name == "bàxterthehacker"
  64. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  65. assert commit.author.external_id is None
  66. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  67. @mock_baxter_response()
  68. def test_user_email(self):
  69. project = self.project # force creation
  70. user = self.create_user(email="alberto@sentry.io")
  71. self.create_usersocialauth(provider="github", user=user, uid="6752317")
  72. self.create_member(organization=project.organization, user=user, role="member")
  73. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  74. secret = "b3002c3e321d4b7880360d397db2ccfd"
  75. OrganizationOption.objects.set_value(
  76. organization=project.organization, key="github:webhook_secret", value=secret
  77. )
  78. Repository.objects.create(
  79. organization_id=project.organization.id,
  80. external_id="35129377",
  81. provider="github",
  82. name="baxterthehacker/public-repo",
  83. )
  84. response = self.client.post(
  85. path=url,
  86. data=PUSH_EVENT_EXAMPLE,
  87. content_type="application/json",
  88. HTTP_X_GITHUB_EVENT="push",
  89. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  90. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  91. )
  92. assert response.status_code == 204
  93. commit_list = list(
  94. Commit.objects.filter(organization_id=project.organization_id)
  95. .select_related("author")
  96. .order_by("-date_added")
  97. )
  98. assert len(commit_list) == 2
  99. commit = commit_list[0]
  100. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  101. assert commit.message == "Update README.md (àgain)"
  102. assert commit.author is not None
  103. assert commit.author.name == "bàxterthehacker"
  104. assert commit.author.email == "alberto@sentry.io"
  105. assert commit.author.external_id == "github:baxterthehacker"
  106. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  107. commit = commit_list[1]
  108. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  109. assert commit.message == "Update README.md"
  110. assert commit.author is not None
  111. assert commit.author.name == "bàxterthehacker"
  112. assert commit.author.email == "alberto@sentry.io"
  113. assert commit.author.external_id == "github:baxterthehacker"
  114. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  115. @responses.activate
  116. def test_anonymous_lookup(self):
  117. project = self.project # force creation
  118. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  119. secret = "b3002c3e321d4b7880360d397db2ccfd"
  120. OrganizationOption.objects.set_value(
  121. organization=project.organization, key="github:webhook_secret", value=secret
  122. )
  123. Repository.objects.create(
  124. organization_id=project.organization.id,
  125. external_id="35129377",
  126. provider="github",
  127. name="baxterthehacker/public-repo",
  128. )
  129. CommitAuthor.objects.create(
  130. external_id="github:baxterthehacker",
  131. organization_id=project.organization_id,
  132. email="baxterthehacker@example.com",
  133. name="bàxterthehacker",
  134. )
  135. response = self.client.post(
  136. path=url,
  137. data=PUSH_EVENT_EXAMPLE,
  138. content_type="application/json",
  139. HTTP_X_GITHUB_EVENT="push",
  140. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  141. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  142. )
  143. assert response.status_code == 204
  144. commit_list = list(
  145. Commit.objects.filter(organization_id=project.organization_id)
  146. .select_related("author")
  147. .order_by("-date_added")
  148. )
  149. # should be skipping the #skipsentry commit
  150. assert len(commit_list) == 2
  151. commit = commit_list[0]
  152. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  153. assert commit.message == "Update README.md (àgain)"
  154. assert commit.author is not None
  155. assert commit.author.name == "bàxterthehacker"
  156. assert commit.author.email == "baxterthehacker@example.com"
  157. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  158. commit = commit_list[1]
  159. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  160. assert commit.message == "Update README.md"
  161. assert commit.author is not None
  162. assert commit.author.name == "bàxterthehacker"
  163. assert commit.author.email == "baxterthehacker@example.com"
  164. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)