test_push_event.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.name == "bàxterthehacker"
  55. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  56. assert commit.author.external_id is None
  57. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  58. commit = commit_list[1]
  59. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  60. assert commit.message == "Update README.md"
  61. assert commit.author.name == "bàxterthehacker"
  62. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  63. assert commit.author.external_id is None
  64. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  65. @mock_baxter_response()
  66. def test_user_email(self):
  67. project = self.project # force creation
  68. user = self.create_user(email="alberto@sentry.io")
  69. self.create_usersocialauth(provider="github", user=user, uid="6752317")
  70. self.create_member(organization=project.organization, user=user, role="member")
  71. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  72. secret = "b3002c3e321d4b7880360d397db2ccfd"
  73. OrganizationOption.objects.set_value(
  74. organization=project.organization, key="github:webhook_secret", value=secret
  75. )
  76. Repository.objects.create(
  77. organization_id=project.organization.id,
  78. external_id="35129377",
  79. provider="github",
  80. name="baxterthehacker/public-repo",
  81. )
  82. response = self.client.post(
  83. path=url,
  84. data=PUSH_EVENT_EXAMPLE,
  85. content_type="application/json",
  86. HTTP_X_GITHUB_EVENT="push",
  87. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  88. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  89. )
  90. assert response.status_code == 204
  91. commit_list = list(
  92. Commit.objects.filter(organization_id=project.organization_id)
  93. .select_related("author")
  94. .order_by("-date_added")
  95. )
  96. assert len(commit_list) == 2
  97. commit = commit_list[0]
  98. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  99. assert commit.message == "Update README.md (àgain)"
  100. assert commit.author.name == "bàxterthehacker"
  101. assert commit.author.email == "alberto@sentry.io"
  102. assert commit.author.external_id == "github:baxterthehacker"
  103. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  104. commit = commit_list[1]
  105. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  106. assert commit.message == "Update README.md"
  107. assert commit.author.name == "bàxterthehacker"
  108. assert commit.author.email == "alberto@sentry.io"
  109. assert commit.author.external_id == "github:baxterthehacker"
  110. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  111. @responses.activate
  112. def test_anonymous_lookup(self):
  113. project = self.project # force creation
  114. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  115. secret = "b3002c3e321d4b7880360d397db2ccfd"
  116. OrganizationOption.objects.set_value(
  117. organization=project.organization, key="github:webhook_secret", value=secret
  118. )
  119. Repository.objects.create(
  120. organization_id=project.organization.id,
  121. external_id="35129377",
  122. provider="github",
  123. name="baxterthehacker/public-repo",
  124. )
  125. CommitAuthor.objects.create(
  126. external_id="github:baxterthehacker",
  127. organization_id=project.organization_id,
  128. email="baxterthehacker@example.com",
  129. name="bàxterthehacker",
  130. )
  131. response = self.client.post(
  132. path=url,
  133. data=PUSH_EVENT_EXAMPLE,
  134. content_type="application/json",
  135. HTTP_X_GITHUB_EVENT="push",
  136. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  137. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  138. )
  139. assert response.status_code == 204
  140. commit_list = list(
  141. Commit.objects.filter(organization_id=project.organization_id)
  142. .select_related("author")
  143. .order_by("-date_added")
  144. )
  145. # should be skipping the #skipsentry commit
  146. assert len(commit_list) == 2
  147. commit = commit_list[0]
  148. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  149. assert commit.message == "Update README.md (àgain)"
  150. assert commit.author.name == "bàxterthehacker"
  151. assert commit.author.email == "baxterthehacker@example.com"
  152. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  153. commit = commit_list[1]
  154. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  155. assert commit.message == "Update README.md"
  156. assert commit.author.name == "bàxterthehacker"
  157. assert commit.author.email == "baxterthehacker@example.com"
  158. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)