test_webhooks.py 4.8 KB

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