test_webhooks.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from uuid import uuid4
  2. from sentry.models.options.organization_option import OrganizationOption
  3. from sentry.testutils.cases import APITestCase
  4. from sentry_plugins.github.testutils import PUSH_EVENT_EXAMPLE
  5. class WebhookTest(APITestCase):
  6. def test_get(self):
  7. project = self.project # force creation
  8. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  9. response = self.client.get(url)
  10. assert response.status_code == 405
  11. def test_unregistered_event(self):
  12. project = self.project # force creation
  13. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  14. secret = "b3002c3e321d4b7880360d397db2ccfd"
  15. OrganizationOption.objects.set_value(
  16. organization=project.organization, key="github:webhook_secret", value=secret
  17. )
  18. response = self.client.post(
  19. path=url,
  20. data=PUSH_EVENT_EXAMPLE,
  21. content_type="application/json",
  22. HTTP_X_GITHUB_EVENT="UnregisteredEvent",
  23. HTTP_X_HUB_SIGNATURE="sha1=98196e70369945ffa6b248cf70f7dc5e46dff241",
  24. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  25. )
  26. assert response.status_code == 204
  27. def test_invalid_signature_event(self):
  28. project = self.project # force creation
  29. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  30. secret = "2d7565c3537847b789d6995dca8d9f84"
  31. OrganizationOption.objects.set_value(
  32. organization=project.organization, key="github:webhook_secret", value=secret
  33. )
  34. response = self.client.post(
  35. path=url,
  36. data=PUSH_EVENT_EXAMPLE,
  37. content_type="application/json",
  38. HTTP_X_GITHUB_EVENT="push",
  39. HTTP_X_HUB_SIGNATURE="sha1=33521abeaaf9a57c2abf486e0ccd54d23cf36fec",
  40. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  41. )
  42. assert response.status_code == 401