Keep compatibility with webhooks
parent
ddaa146dde
commit
e26452a628
|
@ -114,10 +114,19 @@ class WebhookMixin(object):
|
|||
# in `WebhookView`
|
||||
if self.integration is not None:
|
||||
return self.integration
|
||||
integration, _ = Integration.objects.get_or_create(
|
||||
project=self.project,
|
||||
integration_type=self.integration_type,
|
||||
)
|
||||
try:
|
||||
integration = Integration.objects.get(
|
||||
project=self.project,
|
||||
integration_type=self.integration_type,
|
||||
)
|
||||
except Integration.DoesNotExist:
|
||||
integration = Integration.objects.create(
|
||||
project=self.project,
|
||||
integration_type=self.integration_type,
|
||||
# If we didn't create the integration,
|
||||
# we didn't set a secret.
|
||||
secret='',
|
||||
)
|
||||
return integration
|
||||
|
||||
def get_response_push(self, project, branches):
|
||||
|
@ -198,6 +207,8 @@ class GitHubWebhookView(WebhookMixin, APIView):
|
|||
self.project.slug
|
||||
)
|
||||
return True
|
||||
if not signature:
|
||||
return False
|
||||
msg = self.request.raw_body
|
||||
digest = hmac.new(
|
||||
secret.encode(),
|
||||
|
@ -270,6 +281,8 @@ class GitLabWebhookView(WebhookMixin, APIView):
|
|||
self.project.slug
|
||||
)
|
||||
return True
|
||||
if not token:
|
||||
return False
|
||||
result = token == secret
|
||||
return result
|
||||
|
||||
|
|
|
@ -989,13 +989,20 @@ class IntegrationsTests(TestCase):
|
|||
|
||||
def test_github_invalid_payload(self, trigger_build):
|
||||
client = APIClient()
|
||||
integration = Integration.objects.create(
|
||||
project=self.project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
)
|
||||
wrong_signature = '1234'
|
||||
self.assertTrue(integration.secret)
|
||||
self.assertNotEqual(integration.secret, wrong_signature)
|
||||
headers = {
|
||||
GITHUB_EVENT_HEADER: GITHUB_PUSH,
|
||||
GITHUB_SIGNATURE_HEADER: '1234',
|
||||
GITHUB_SIGNATURE_HEADER: wrong_signature,
|
||||
}
|
||||
resp = client.post(
|
||||
'/api/v2/webhook/github/{}/'.format(self.project.slug),
|
||||
{'foo': 'bar'},
|
||||
self.github_payload,
|
||||
format='json',
|
||||
**headers
|
||||
)
|
||||
|
@ -1033,9 +1040,14 @@ class IntegrationsTests(TestCase):
|
|||
GITHUB_EVENT_HEADER: GITHUB_PUSH,
|
||||
GITHUB_SIGNATURE_HEADER: '',
|
||||
}
|
||||
integration = Integration.objects.create(
|
||||
project=self.project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
)
|
||||
self.assertTrue(integration.secret)
|
||||
resp = client.post(
|
||||
'/api/v2/webhook/github/{}/'.format(self.project.slug),
|
||||
{'foo': 'bar'},
|
||||
self.github_payload,
|
||||
format='json',
|
||||
**headers
|
||||
)
|
||||
|
@ -1224,12 +1236,19 @@ class IntegrationsTests(TestCase):
|
|||
|
||||
def test_gitlab_invalid_payload(self, trigger_build):
|
||||
client = APIClient()
|
||||
wrong_secret = '1234'
|
||||
integration = Integration.objects.create(
|
||||
project=self.project,
|
||||
integration_type=Integration.GITLAB_WEBHOOK,
|
||||
)
|
||||
self.assertTrue(integration.secret)
|
||||
self.assertNotEqual(integration.secret, wrong_secret)
|
||||
headers = {
|
||||
GITLAB_TOKEN_HEADER: '1234',
|
||||
GITLAB_TOKEN_HEADER: wrong_secret,
|
||||
}
|
||||
resp = client.post(
|
||||
'/api/v2/webhook/gitlab/{}/'.format(self.project.slug),
|
||||
{'object_kind': 'pull_request'},
|
||||
self.gitlab_payload,
|
||||
format='json',
|
||||
**headers
|
||||
)
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import (
|
||||
absolute_import,
|
||||
division,
|
||||
print_function,
|
||||
unicode_literals,
|
||||
)
|
||||
|
||||
from builtins import range
|
||||
import django_dynamic_fixture as fixture
|
||||
from django.test import TestCase, RequestFactory
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from builtins import range
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from rest_framework.response import Response
|
||||
|
||||
from readthedocs.integrations.models import (
|
||||
HttpExchange, Integration, GitHubWebhook
|
||||
GitHubWebhook,
|
||||
HttpExchange,
|
||||
Integration,
|
||||
)
|
||||
from readthedocs.projects.models import Project
|
||||
|
||||
|
@ -26,9 +30,13 @@ class HttpExchangeTests(TestCase):
|
|||
client = APIClient()
|
||||
client.login(username='super', password='test')
|
||||
project = fixture.get(Project, main_language_project=None)
|
||||
integration = fixture.get(Integration, project=project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
provider_data='')
|
||||
integration = fixture.get(
|
||||
Integration,
|
||||
project=project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
provider_data='',
|
||||
secret='',
|
||||
)
|
||||
resp = client.post(
|
||||
'/api/v2/webhook/github/{0}/'.format(project.slug),
|
||||
{'ref': 'exchange_json'},
|
||||
|
@ -59,9 +67,13 @@ class HttpExchangeTests(TestCase):
|
|||
client = APIClient()
|
||||
client.login(username='super', password='test')
|
||||
project = fixture.get(Project, main_language_project=None)
|
||||
integration = fixture.get(Integration, project=project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
provider_data='')
|
||||
integration = fixture.get(
|
||||
Integration,
|
||||
project=project,
|
||||
integration_type=Integration.GITHUB_WEBHOOK,
|
||||
provider_data='',
|
||||
secret='',
|
||||
)
|
||||
resp = client.post(
|
||||
'/api/v2/webhook/github/{0}/'.format(project.slug),
|
||||
'payload=%7B%22ref%22%3A+%22exchange_form%22%7D',
|
||||
|
|
Loading…
Reference in New Issue