test_push_event.py 6.8 KB

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