test_webhooks.py 4.8 KB

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