test_webhooks.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from datetime import datetime
  2. from django.utils import timezone
  3. from sentry.models import Commit, CommitAuthor, Repository
  4. from sentry.testutils import APITestCase
  5. from sentry.testutils.silo import region_silo_test
  6. from sentry_plugins.bitbucket.testutils import PUSH_EVENT_EXAMPLE
  7. BAD_IP = "109.111.111.10"
  8. BITBUCKET_IP_IN_RANGE = "104.192.143.10"
  9. BITBUCKET_IP = "34.198.178.64"
  10. class WebhookTest(APITestCase):
  11. def test_get(self):
  12. project = self.project # force creation
  13. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  14. response = self.client.get(url)
  15. assert response.status_code == 405
  16. def test_unregistered_event(self):
  17. project = self.project # force creation
  18. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  19. response = self.client.post(
  20. path=url,
  21. data=PUSH_EVENT_EXAMPLE,
  22. content_type="application/json",
  23. HTTP_X_EVENT_KEY="UnregisteredEvent",
  24. REMOTE_ADDR=BITBUCKET_IP,
  25. )
  26. assert response.status_code == 204
  27. response = self.client.post(
  28. path=url,
  29. data=PUSH_EVENT_EXAMPLE,
  30. content_type="application/json",
  31. HTTP_X_EVENT_KEY="UnregisteredEvent",
  32. REMOTE_ADDR=BITBUCKET_IP_IN_RANGE,
  33. )
  34. assert response.status_code == 204
  35. def test_invalid_signature_ip(self):
  36. project = self.project # force creation
  37. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  38. response = self.client.post(
  39. path=url,
  40. data=PUSH_EVENT_EXAMPLE,
  41. content_type="application/json",
  42. HTTP_X_EVENT_KEY="repo:push",
  43. REMOTE_ADDR=BAD_IP,
  44. )
  45. assert response.status_code == 401
  46. @region_silo_test
  47. class PushEventWebhookTest(APITestCase):
  48. def test_simple(self):
  49. project = self.project # force creation
  50. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  51. Repository.objects.create(
  52. organization_id=project.organization.id,
  53. external_id="{c78dfb25-7882-4550-97b1-4e0d38f32859}",
  54. provider="bitbucket",
  55. name="maxbittker/newsdiffs",
  56. )
  57. response = self.client.post(
  58. path=url,
  59. data=PUSH_EVENT_EXAMPLE,
  60. content_type="application/json",
  61. HTTP_X_EVENT_KEY="repo:push",
  62. REMOTE_ADDR=BITBUCKET_IP,
  63. )
  64. assert response.status_code == 204
  65. commit_list = list(
  66. Commit.objects.filter(organization_id=project.organization_id)
  67. .select_related("author")
  68. .order_by("-date_added")
  69. )
  70. assert len(commit_list) == 1
  71. commit = commit_list[0]
  72. assert commit.key == "e0e377d186e4f0e937bdb487a23384fe002df649"
  73. assert commit.message == "README.md edited online with Bitbucket"
  74. assert commit.author.name == "Max Bittker"
  75. assert commit.author.email == "max@getsentry.com"
  76. assert commit.author.external_id is None
  77. assert commit.date_added == datetime(2017, 5, 24, 1, 5, 47, tzinfo=timezone.utc)
  78. def test_anonymous_lookup(self):
  79. project = self.project # force creation
  80. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  81. Repository.objects.create(
  82. organization_id=project.organization.id,
  83. external_id="{c78dfb25-7882-4550-97b1-4e0d38f32859}",
  84. provider="bitbucket",
  85. name="maxbittker/newsdiffs",
  86. )
  87. CommitAuthor.objects.create(
  88. external_id="bitbucket:baxterthehacker",
  89. organization_id=project.organization_id,
  90. email="baxterthehacker@example.com",
  91. name="bàxterthehacker",
  92. )
  93. response = self.client.post(
  94. path=url,
  95. data=PUSH_EVENT_EXAMPLE,
  96. content_type="application/json",
  97. HTTP_X_EVENT_KEY="repo:push",
  98. REMOTE_ADDR=BITBUCKET_IP,
  99. )
  100. assert response.status_code == 204
  101. commit_list = list(
  102. Commit.objects.filter(organization_id=project.organization_id)
  103. .select_related("author")
  104. .order_by("-date_added")
  105. )
  106. # should be skipping the #skipsentry commit
  107. assert len(commit_list) == 1
  108. commit = commit_list[0]
  109. assert commit.key == "e0e377d186e4f0e937bdb487a23384fe002df649"
  110. assert commit.message == "README.md edited online with Bitbucket"
  111. assert commit.author.name == "Max Bittker"
  112. assert commit.author.email == "max@getsentry.com"
  113. assert commit.author.external_id is None
  114. assert commit.date_added == datetime(2017, 5, 24, 1, 5, 47, tzinfo=timezone.utc)