test_push_event.py 6.9 KB

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