test_push_event.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.testutils.silo import region_silo_test
  12. from sentry_plugins.github.testutils import PUSH_EVENT_EXAMPLE
  13. @contextlib.contextmanager
  14. def mock_baxter_response():
  15. with responses.RequestsMock() as mck:
  16. mck.add(
  17. "GET",
  18. "https://api.github.com/users/baxterthehacker",
  19. body=API_GITHUB_COM_USERS_BAXTERTHEHACKER,
  20. )
  21. yield
  22. @region_silo_test
  23. class PushEventWebhookTest(APITestCase):
  24. @mock_baxter_response()
  25. def test_simple(self):
  26. project = self.project # force creation
  27. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  28. secret = "b3002c3e321d4b7880360d397db2ccfd"
  29. OrganizationOption.objects.set_value(
  30. organization=project.organization, key="github:webhook_secret", value=secret
  31. )
  32. Repository.objects.create(
  33. organization_id=project.organization.id,
  34. external_id="35129377",
  35. provider="github",
  36. name="baxterthehacker/public-repo",
  37. )
  38. response = self.client.post(
  39. path=url,
  40. data=PUSH_EVENT_EXAMPLE,
  41. content_type="application/json",
  42. HTTP_X_GITHUB_EVENT="push",
  43. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  44. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  45. )
  46. assert response.status_code == 204
  47. commit_list = list(
  48. Commit.objects.filter(organization_id=project.organization_id)
  49. .select_related("author")
  50. .order_by("-date_added")
  51. )
  52. assert len(commit_list) == 2
  53. commit = commit_list[0]
  54. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  55. assert commit.message == "Update README.md (àgain)"
  56. assert commit.author.name == "bàxterthehacker"
  57. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  58. assert commit.author.external_id is None
  59. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  60. commit = commit_list[1]
  61. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  62. assert commit.message == "Update README.md"
  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.name == "bàxterthehacker"
  103. assert commit.author.email == "alberto@sentry.io"
  104. assert commit.author.external_id == "github:baxterthehacker"
  105. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  106. commit = commit_list[1]
  107. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  108. assert commit.message == "Update README.md"
  109. assert commit.author.name == "bàxterthehacker"
  110. assert commit.author.email == "alberto@sentry.io"
  111. assert commit.author.external_id == "github:baxterthehacker"
  112. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  113. @responses.activate
  114. def test_anonymous_lookup(self):
  115. project = self.project # force creation
  116. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  117. secret = "b3002c3e321d4b7880360d397db2ccfd"
  118. OrganizationOption.objects.set_value(
  119. organization=project.organization, key="github:webhook_secret", value=secret
  120. )
  121. Repository.objects.create(
  122. organization_id=project.organization.id,
  123. external_id="35129377",
  124. provider="github",
  125. name="baxterthehacker/public-repo",
  126. )
  127. CommitAuthor.objects.create(
  128. external_id="github:baxterthehacker",
  129. organization_id=project.organization_id,
  130. email="baxterthehacker@example.com",
  131. name="bàxterthehacker",
  132. )
  133. response = self.client.post(
  134. path=url,
  135. data=PUSH_EVENT_EXAMPLE,
  136. content_type="application/json",
  137. HTTP_X_GITHUB_EVENT="push",
  138. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  139. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  140. )
  141. assert response.status_code == 204
  142. commit_list = list(
  143. Commit.objects.filter(organization_id=project.organization_id)
  144. .select_related("author")
  145. .order_by("-date_added")
  146. )
  147. # should be skipping the #skipsentry commit
  148. assert len(commit_list) == 2
  149. commit = commit_list[0]
  150. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  151. assert commit.message == "Update README.md (àgain)"
  152. assert commit.author.name == "bàxterthehacker"
  153. assert commit.author.email == "baxterthehacker@example.com"
  154. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  155. commit = commit_list[1]
  156. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  157. assert commit.message == "Update README.md"
  158. assert commit.author.name == "bàxterthehacker"
  159. assert commit.author.email == "baxterthehacker@example.com"
  160. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)