test_webhooks.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_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. class PushEventWebhookTest(APITestCase):
  46. def test_simple(self):
  47. project = self.project # force creation
  48. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  49. Repository.objects.create(
  50. organization_id=project.organization.id,
  51. external_id="{c78dfb25-7882-4550-97b1-4e0d38f32859}",
  52. provider="bitbucket",
  53. name="maxbittker/newsdiffs",
  54. )
  55. response = self.client.post(
  56. path=url,
  57. data=PUSH_EVENT_EXAMPLE,
  58. content_type="application/json",
  59. HTTP_X_EVENT_KEY="repo:push",
  60. REMOTE_ADDR=BITBUCKET_IP,
  61. )
  62. assert response.status_code == 204
  63. commit_list = list(
  64. Commit.objects.filter(organization_id=project.organization_id)
  65. .select_related("author")
  66. .order_by("-date_added")
  67. )
  68. assert len(commit_list) == 1
  69. commit = commit_list[0]
  70. assert commit.key == "e0e377d186e4f0e937bdb487a23384fe002df649"
  71. assert commit.message == "README.md edited online with Bitbucket"
  72. assert commit.author.name == "Max Bittker"
  73. assert commit.author.email == "max@getsentry.com"
  74. assert commit.author.external_id is None
  75. assert commit.date_added == datetime(2017, 5, 24, 1, 5, 47, tzinfo=timezone.utc)
  76. def test_anonymous_lookup(self):
  77. project = self.project # force creation
  78. url = f"/plugins/bitbucket/organizations/{project.organization.id}/webhook/"
  79. Repository.objects.create(
  80. organization_id=project.organization.id,
  81. external_id="{c78dfb25-7882-4550-97b1-4e0d38f32859}",
  82. provider="bitbucket",
  83. name="maxbittker/newsdiffs",
  84. )
  85. CommitAuthor.objects.create(
  86. external_id="bitbucket:baxterthehacker",
  87. organization_id=project.organization_id,
  88. email="baxterthehacker@example.com",
  89. name="bàxterthehacker",
  90. )
  91. response = self.client.post(
  92. path=url,
  93. data=PUSH_EVENT_EXAMPLE,
  94. content_type="application/json",
  95. HTTP_X_EVENT_KEY="repo:push",
  96. REMOTE_ADDR=BITBUCKET_IP,
  97. )
  98. assert response.status_code == 204
  99. commit_list = list(
  100. Commit.objects.filter(organization_id=project.organization_id)
  101. .select_related("author")
  102. .order_by("-date_added")
  103. )
  104. # should be skipping the #skipsentry commit
  105. assert len(commit_list) == 1
  106. commit = commit_list[0]
  107. assert commit.key == "e0e377d186e4f0e937bdb487a23384fe002df649"
  108. assert commit.message == "README.md edited online with Bitbucket"
  109. assert commit.author.name == "Max Bittker"
  110. assert commit.author.email == "max@getsentry.com"
  111. assert commit.author.external_id is None
  112. assert commit.date_added == datetime(2017, 5, 24, 1, 5, 47, tzinfo=timezone.utc)