test_webhooks.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_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. 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)