test_push_event.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @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_anonymous_lookup(self):
  53. project = self.project # force creation
  54. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  55. secret = "b3002c3e321d4b7880360d397db2ccfd"
  56. OrganizationOption.objects.set_value(
  57. organization=project.organization, key="github:webhook_secret", value=secret
  58. )
  59. Repository.objects.create(
  60. organization_id=project.organization.id,
  61. external_id="35129377",
  62. provider="github",
  63. name="baxterthehacker/public-repo",
  64. )
  65. CommitAuthor.objects.create(
  66. external_id="github:baxterthehacker",
  67. organization_id=project.organization_id,
  68. email="baxterthehacker@example.com",
  69. name="bàxterthehacker",
  70. )
  71. response = self.client.post(
  72. path=url,
  73. data=PUSH_EVENT_EXAMPLE,
  74. content_type="application/json",
  75. HTTP_X_GITHUB_EVENT="push",
  76. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  77. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  78. )
  79. assert response.status_code == 204
  80. commit_list = list(
  81. Commit.objects.filter(organization_id=project.organization_id)
  82. .select_related("author")
  83. .order_by("-date_added")
  84. )
  85. # should be skipping the #skipsentry commit
  86. assert len(commit_list) == 2
  87. commit = commit_list[0]
  88. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  89. assert commit.message == "Update README.md (àgain)"
  90. assert commit.author.name == "bàxterthehacker"
  91. assert commit.author.email == "baxterthehacker@example.com"
  92. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  93. commit = commit_list[1]
  94. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  95. assert commit.message == "Update README.md"
  96. assert commit.author.name == "bàxterthehacker"
  97. assert commit.author.email == "baxterthehacker@example.com"
  98. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)