test_push_event.py 6.8 KB

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