test_push_event.py 6.7 KB

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