test_push_event.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_plugins.github.testutils import PUSH_EVENT_EXAMPLE
  7. class PushEventWebhookTest(APITestCase):
  8. def test_simple(self):
  9. project = self.project # force creation
  10. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  11. secret = "b3002c3e321d4b7880360d397db2ccfd"
  12. OrganizationOption.objects.set_value(
  13. organization=project.organization, key="github:webhook_secret", value=secret
  14. )
  15. Repository.objects.create(
  16. organization_id=project.organization.id,
  17. external_id="35129377",
  18. provider="github",
  19. name="baxterthehacker/public-repo",
  20. )
  21. response = self.client.post(
  22. path=url,
  23. data=PUSH_EVENT_EXAMPLE,
  24. content_type="application/json",
  25. HTTP_X_GITHUB_EVENT="push",
  26. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  27. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  28. )
  29. assert response.status_code == 204
  30. commit_list = list(
  31. Commit.objects.filter(organization_id=project.organization_id)
  32. .select_related("author")
  33. .order_by("-date_added")
  34. )
  35. assert len(commit_list) == 2
  36. commit = commit_list[0]
  37. assert commit.key == "133d60480286590a610a0eb7352ff6e02b9674c4"
  38. assert commit.message == "Update README.md (àgain)"
  39. assert commit.author.name == "bàxterthehacker"
  40. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  41. assert commit.author.external_id is None
  42. assert commit.date_added == datetime(2015, 5, 5, 23, 45, 15, tzinfo=timezone.utc)
  43. commit = commit_list[1]
  44. assert commit.key == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
  45. assert commit.message == "Update README.md"
  46. assert commit.author.name == "bàxterthehacker"
  47. assert commit.author.email == "baxterthehacker@users.noreply.github.com"
  48. assert commit.author.external_id is None
  49. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
  50. def test_anonymous_lookup(self):
  51. project = self.project # force creation
  52. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  53. secret = "b3002c3e321d4b7880360d397db2ccfd"
  54. OrganizationOption.objects.set_value(
  55. organization=project.organization, key="github:webhook_secret", value=secret
  56. )
  57. Repository.objects.create(
  58. organization_id=project.organization.id,
  59. external_id="35129377",
  60. provider="github",
  61. name="baxterthehacker/public-repo",
  62. )
  63. CommitAuthor.objects.create(
  64. external_id="github:baxterthehacker",
  65. organization_id=project.organization_id,
  66. email="baxterthehacker@example.com",
  67. name="bàxterthehacker",
  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. # should be skipping the #skipsentry commit
  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 == "baxterthehacker@example.com"
  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 == "baxterthehacker@example.com"
  96. assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)