Commit 72ba58a7 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

fix tests

parent ca7c1b56
...@@ -22,5 +22,5 @@ build: ...@@ -22,5 +22,5 @@ build:
script: script:
- docker build -t ${DOCKER_TAG} . - docker build -t ${DOCKER_TAG} .
- docker run --rm -e SECRET_KEY -e GITHUB_WEBHOOK_KEY -e GITLAB_WEBHOOK_KEY -e GITHUB_PIPELINE_TOKEN -e GITLAB_PIPELINE_TOKEN -e PYTHONWARNINGS ${DOCKER_TAG} ./manage.py migrate - docker run --rm -e SECRET_KEY -e GITHUB_WEBHOOK_KEY -e GITLAB_WEBHOOK_KEY -e GITHUB_PIPELINE_TOKEN -e GITLAB_PIPELINE_TOKEN -e PYTHONWARNINGS ${DOCKER_TAG} ./manage.py migrate
- docker run --rm -e SECRET_KEY -e GITHUB_WEBHOOK_KEY -e GITLAB_WEBHOOK_KEY -e GITHUB_PIPELINE_TOKEN -e GITLAB_PIPELINE_TOKEN -e PYTHONWARNINGS ${DOCKER_TAG} ./manage.py test - docker run --rm -e SECRET_KEY -e GITHUB_WEBHOOK_KEY -e GITLAB_WEBHOOK_KEY -e GITHUB_PIPELINE_TOKEN -e GITLAB_PIPELINE_TOKEN -e PYTHONWARNINGS ${DOCKER_TAG} ./manage.py test --parallel 1
- docker push ${DOCKER_TAG} - docker push ${DOCKER_TAG}
import asyncio
from ipaddress import ip_address, ip_network from ipaddress import ip_address, ip_network
from django.conf import settings from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import reverse from django.shortcuts import reverse
from django.utils.decorators import sync_and_async_middleware
from rest_framework import permissions from rest_framework import permissions
...@@ -15,19 +17,30 @@ def ip_laas(request: HttpRequest) -> bool: ...@@ -15,19 +17,30 @@ def ip_laas(request: HttpRequest) -> bool:
return any(forwarded_for in ip_network(net) for net in settings.LAAS_NETWORKS) return any(forwarded_for in ip_network(net) for net in settings.LAAS_NETWORKS)
class LAASPermsMiddleware: def allowed(request: HttpRequest) -> bool:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request: HttpRequest) -> HttpResponse:
"""Allow access to pages protected at a higher application level, """Allow access to pages protected at a higher application level,
or if the user is authenticated, or if the user is authenticated,
or if the request comes from a trusted IP. or if the request comes from a trusted IP.
""" """
allowed = (any(request.path.startswith(f'/{url}/') for url in ALLOWED_URLS) return (any(request.path.startswith(f'/{url}/') for url in ALLOWED_URLS)
or request.user and request.user.is_authenticated or request.user and request.user.is_authenticated
or request.method in permissions.SAFE_METHODS and ip_laas(request)) or request.method in permissions.SAFE_METHODS and ip_laas(request))
if allowed:
return self.get_response(request) @sync_and_async_middleware
def laas_perms_middleware(get_response):
# One-time configuration and initialization goes here.
if asyncio.iscoroutinefunction(get_response):
async def middleware(request) -> HttpResponse:
if allowed(request):
return await get_response(request)
return HttpResponseRedirect(reverse('login')) return HttpResponseRedirect(reverse('login'))
else:
def middleware(request) -> HttpResponse:
if allowed(request):
return get_response(request)
return HttpResponseRedirect(reverse('login'))
return middleware
...@@ -58,7 +58,7 @@ MIDDLEWARE = [ ...@@ -58,7 +58,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
f'{PROJECT}.middleware.LAASPermsMiddleware', f'{PROJECT}.middleware.laas_perms_middleware',
] ]
ROOT_URLCONF = f'{PROJECT}.urls' ROOT_URLCONF = f'{PROJECT}.urls'
......
import hmac import hmac
import re import re
from hashlib import sha1 from hashlib import sha1
import logging
from asyncio import sleep
from asgiref.sync import sync_to_async
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
...@@ -11,6 +14,8 @@ import git ...@@ -11,6 +14,8 @@ import git
from autoslug.utils import slugify from autoslug.utils import slugify
from rainboard.models import Forge, Namespace, Project from rainboard.models import Forge, Namespace, Project
LOGGER = logging.getLogger('dashboard.gh.tests')
def redact_token(func): def redact_token(func):
"""Decorator used to prevent git from leaking tokens in exception messages.""" """Decorator used to prevent git from leaking tokens in exception messages."""
...@@ -54,6 +59,9 @@ class GhTests(TestCase): ...@@ -54,6 +59,9 @@ class GhTests(TestCase):
if branch_name in gl_branches: if branch_name in gl_branches:
cls.gitlab.branches.delete(branch_name) cls.gitlab.branches.delete(branch_name)
logging.basicConfig(level=logging.DEBUG)
LOGGER.info('start gh tests')
def assertSync(self, branch): def assertSync(self, branch):
"""Raise an exception if the branch is not synced between both repos.""" """Raise an exception if the branch is not synced between both repos."""
last_commit_github = self.github.get_branch(branch).commit.sha last_commit_github = self.github.get_branch(branch).commit.sha
...@@ -61,23 +69,23 @@ class GhTests(TestCase): ...@@ -61,23 +69,23 @@ class GhTests(TestCase):
self.assertEqual(last_commit_github, last_commit_gitlab) self.assertEqual(last_commit_github, last_commit_gitlab)
@redact_token @redact_token
def sync(self): async def sync(self):
"""Force both repos to be synced.""" """Force both repos to be synced."""
for branch in ('master', 'devel'): for branch in ('master', 'devel'):
last_commit_github = self.github.get_branch(branch).commit.sha last_commit_github = self.github.get_branch(branch).commit.sha
last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id
if last_commit_github != last_commit_gitlab: if last_commit_github != last_commit_gitlab:
print(f'sync: {branch} is not synced, force pushing commit {last_commit_gitlab} on github') LOGGER.warning(f'sync: {branch} is not synced, force pushing commit {last_commit_gitlab} on github')
git_repo = self.project.git() git_repo = self.project.git()
# Create the remotes if necessary # Create the remotes if necessary
gl_remote_name = f'gitlab/{self.namespace.slug}' gl_remote_name = f'gitlab/{self.namespace.slug}'
gh_remote_name = f'github/{self.namespace.slug}' gh_remote_name = f'github/{self.namespace.slug}'
if gl_remote_name not in git_repo.remotes: if gl_remote_name not in git_repo.remotes:
git_repo.create_remote(gl_remote_name, url=self.project.remote_url_gitlab()) git_repo.create_remote(gl_remote_name, url=await sync_to_async(self.project.remote_url_gitlab)())
if gh_remote_name not in git_repo.remotes: if gh_remote_name not in git_repo.remotes:
git_repo.create_remote(gh_remote_name, url=self.project.remote_url_github()) git_repo.create_remote(gh_remote_name, url=await sync_to_async(self.project.remote_url_github)())
# Force push the latest gitlab commit on github # Force push the latest gitlab commit on github
git_repo.remote(gl_remote_name).fetch() git_repo.remote(gl_remote_name).fetch()
...@@ -86,7 +94,7 @@ class GhTests(TestCase): ...@@ -86,7 +94,7 @@ class GhTests(TestCase):
self.assertSync(branch) self.assertSync(branch)
@redact_token @redact_token
def gh_webhook_event(self, event, last_commit='', branch='master', pr_action='', pr_number='', pr_login=''): async def gh_webhook_event(self, event, last_commit='', branch='master', pr_action='', pr_number='', pr_login=''):
"""Simulate receiving an event from a github webhook.""" """Simulate receiving an event from a github webhook."""
data = { data = {
'repository': { 'repository': {
...@@ -105,7 +113,7 @@ class GhTests(TestCase): ...@@ -105,7 +113,7 @@ class GhTests(TestCase):
'owner': { 'owner': {
'login': pr_login 'login': pr_login
}, },
'clone_url': self.project.remote_url_github() 'clone_url': await sync_to_async(self.project.remote_url_github)()
}, },
'sha': last_commit 'sha': last_commit
} }
...@@ -116,15 +124,17 @@ class GhTests(TestCase): ...@@ -116,15 +124,17 @@ class GhTests(TestCase):
request_body = self.client._encode_data(encoded_data, content_type='application/json') request_body = self.client._encode_data(encoded_data, content_type='application/json')
msg = force_bytes(request_body) msg = force_bytes(request_body)
signature = 'sha1=' + hmac.new(force_bytes(settings.GITHUB_WEBHOOK_KEY), msg, digestmod=sha1).hexdigest() signature = 'sha1=' + hmac.new(force_bytes(settings.GITHUB_WEBHOOK_KEY), msg, digestmod=sha1).hexdigest()
return self.client.post(reverse('webhook'), LOGGER.info('posting a simulated gh webhook event')
LOGGER.debug(f'with: {data}')
return await self.async_client.post(reverse('webhook'),
data, data,
content_type='application/json', content_type='application/json',
HTTP_X_FORWARDED_FOR='140.82.112.1', X_FORWARDED_FOR='140.82.112.1',
HTTP_X_HUB_SIGNATURE=signature, X_HUB_SIGNATURE=signature,
HTTP_X_GITHUB_EVENT=event) X_GITHUB_EVENT=event)
@redact_token @redact_token
def gl_webhook_event(self, event, last_commit='', branch='master', status=''): async def gl_webhook_event(self, event, last_commit='', branch='master', status=''):
"""Simulate receiving an event from a gitlab webhook.""" """Simulate receiving an event from a gitlab webhook."""
data = { data = {
'repository': { 'repository': {
...@@ -143,74 +153,76 @@ class GhTests(TestCase): ...@@ -143,74 +153,76 @@ class GhTests(TestCase):
'id': 1 'id': 1
} }
} }
return self.client.post(reverse('gl-webhook'), return await self.async_client.post(reverse('gl-webhook'),
data, data,
content_type='application/json', content_type='application/json',
HTTP_X_FORWARDED_FOR='140.93.0.1', X_FORWARDED_FOR='140.93.0.1',
HTTP_X_GITLAB_TOKEN=settings.GITLAB_WEBHOOK_KEY, X_GITLAB_TOKEN=settings.GITLAB_WEBHOOK_KEY,
HTTP_X_GITLAB_EVENT=event) X_GITLAB_EVENT=event)
def test_gh_webhook(self): async def test_gh_webhook(self):
"""Test the security of the github webhook.""" """Test the security of the github webhook."""
# Not from github IP # Not from github IP
response = self.client.get(reverse('webhook'), HTTP_X_FORWARDED_FOR='5.5.5.5') response = await self.async_client.get(reverse('webhook'), X_FORWARDED_FOR='5.5.5.5')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
# No signature # No signature
response = self.client.get(reverse('webhook'), HTTP_X_FORWARDED_FOR='140.82.112.1') response = await self.async_client.get(reverse('webhook'), X_FORWARDED_FOR='140.82.112.1')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
# Signature not sha1 # Signature not sha1
response = self.client.get(reverse('webhook'), response = await self.async_client.get(reverse('webhook'),
HTTP_X_FORWARDED_FOR='140.82.112.1', X_FORWARDED_FOR='140.82.112.1',
HTTP_X_HUB_SIGNATURE='sha256=foo') X_HUB_SIGNATURE='sha256=foo')
self.assertEqual(response.status_code, 501) self.assertEqual(response.status_code, 501)
# Wrong signature # Wrong signature
response = self.client.get(reverse('webhook'), response = await self.async_client.get(reverse('webhook'),
HTTP_X_FORWARDED_FOR='140.82.112.1', X_FORWARDED_FOR='140.82.112.1',
HTTP_X_HUB_SIGNATURE='sha1=foo') X_HUB_SIGNATURE='sha1=foo')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
# Ping # Ping
response = self.gh_webhook_event('ping') response = await self.gh_webhook_event('ping')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_gl_webhook(self): async def test_gl_webhook(self):
"""Test the security of the gitlab webhook.""" """Test the security of the gitlab webhook."""
# Not from gitlab IP # Not from gitlab IP
response = self.client.get(reverse('gl-webhook'), HTTP_X_FORWARDED_FOR='5.5.5.5') response = await self.async_client.get(reverse('gl-webhook'), X_FORWARDED_FOR='5.5.5.5')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
# No token # No token
response = self.client.get(reverse('gl-webhook'), HTTP_X_FORWARDED_FOR='140.93.0.1') response = await self.async_client.get(reverse('gl-webhook'), X_FORWARDED_FOR='140.93.0.1')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
# Wrong token # Wrong token
response = self.client.get(reverse('gl-webhook'), HTTP_X_FORWARDED_FOR='140.93.0.1', HTTP_X_GITLAB_TOKEN='foo') response = await self.async_client.get(reverse('gl-webhook'),
X_FORWARDED_FOR='140.93.0.1',
X_GITLAB_TOKEN='foo')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
# Ping # Ping
response = self.client.get(reverse('gl-webhook'), response = await self.async_client.get(reverse('gl-webhook'),
HTTP_X_FORWARDED_FOR='140.93.0.1', X_FORWARDED_FOR='140.93.0.1',
HTTP_X_GITLAB_TOKEN=settings.GITLAB_WEBHOOK_KEY, X_GITLAB_TOKEN=settings.GITLAB_WEBHOOK_KEY,
HTTP_X_GITLAB_EVENT='ping') X_GITLAB_EVENT='ping')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_push_already_synced(self): async def test_push_already_synced(self):
"""Test push when both repos are already synced.""" """Test push when both repos are already synced."""
self.sync() await self.sync()
last_commit = self.github.get_branch('master').commit.sha last_commit = self.github.get_branch('master').commit.sha
response = self.gh_webhook_event('push', last_commit) response = await self.gh_webhook_event('push', last_commit)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'already synced') self.assertEqual(response.content.decode(), 'already synced')
def push_github(self, branch): async def push_github(self, branch):
"""Test sync after pushing to the given branch on github.""" """Test sync after pushing to the given branch on github."""
self.sync() await self.sync()
last_commit = self.github.get_branch(branch).commit.sha last_commit = self.github.get_branch(branch).commit.sha
file = self.github.get_contents('README.md', branch) file = self.github.get_contents('README.md', branch)
...@@ -223,24 +235,24 @@ class GhTests(TestCase): ...@@ -223,24 +235,24 @@ class GhTests(TestCase):
last_commit_github = self.github.get_branch(branch).commit.sha last_commit_github = self.github.get_branch(branch).commit.sha
self.assertNotEqual(last_commit, last_commit_github) self.assertNotEqual(last_commit, last_commit_github)
response = self.gh_webhook_event('push', last_commit_github, branch) response = await self.gh_webhook_event('push', last_commit_github, branch)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id
self.assertEqual(last_commit_github, last_commit_gitlab) self.assertEqual(last_commit_github, last_commit_gitlab)
def test_push_github_master(self): async def test_push_github_master(self):
"""Test sync after pushing to github on master.""" """Test sync after pushing to github on master."""
self.push_github('master') await self.push_github('master')
def test_push_github_devel(self): async def test_push_github_devel(self):
"""Test sync after pushing to github on devel.""" """Test sync after pushing to github on devel."""
self.push_github('devel') await self.push_github('devel')
def push_gitlab(self, branch): async def push_gitlab(self, branch):
"""Test sync after pushing to the given branch on gitlab.""" """Test sync after pushing to the given branch on gitlab."""
self.sync() await self.sync()
last_commit = self.gitlab.commits.list(ref_name=branch)[0].id last_commit = self.gitlab.commits.list(ref_name=branch)[0].id
# Push a new commit to gitlab # Push a new commit to gitlab
...@@ -251,24 +263,24 @@ class GhTests(TestCase): ...@@ -251,24 +263,24 @@ class GhTests(TestCase):
last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id
self.assertNotEqual(last_commit, last_commit_gitlab) self.assertNotEqual(last_commit, last_commit_gitlab)
response = self.gl_webhook_event('Push Hook', last_commit_gitlab, branch) response = await self.gl_webhook_event('Push Hook', last_commit_gitlab, branch)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
last_commit_github = self.github.get_branch(branch).commit.sha last_commit_github = self.github.get_branch(branch).commit.sha
self.assertEqual(last_commit_github, last_commit_gitlab) self.assertEqual(last_commit_github, last_commit_gitlab)
def test_push_gitlab_master(self): async def test_push_gitlab_master(self):
"""Test sync after pushing to gitlab on master.""" """Test sync after pushing to gitlab on master."""
self.push_gitlab('master') await self.push_gitlab('master')
def test_push_gitlab_devel(self): async def test_push_gitlab_devel(self):
"""Test sync after pushing to gitlab on devel.""" """Test sync after pushing to gitlab on devel."""
self.push_gitlab('devel') await self.push_gitlab('devel')
def test_branch_github(self): async def test_branch_github(self):
"""Test sync after creating or deleting a branch on github.""" """Test sync after creating or deleting a branch on github."""
self.sync() await self.sync()
source_branch_name = 'master' source_branch_name = 'master'
target_branch_name = 'test-branch-github' target_branch_name = 'test-branch-github'
last_commit = self.github.get_branch(source_branch_name).commit.sha last_commit = self.github.get_branch(source_branch_name).commit.sha
...@@ -289,7 +301,7 @@ class GhTests(TestCase): ...@@ -289,7 +301,7 @@ class GhTests(TestCase):
target_branch = self.github.get_branch(target_branch_name) target_branch = self.github.get_branch(target_branch_name)
last_commit_github = target_branch.commit.sha last_commit_github = target_branch.commit.sha
response = self.gh_webhook_event('push', last_commit_github, target_branch_name) response = await self.gh_webhook_event('push', last_commit_github, target_branch_name)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
...@@ -300,14 +312,14 @@ class GhTests(TestCase): ...@@ -300,14 +312,14 @@ class GhTests(TestCase):
branch_ref.delete() branch_ref.delete()
self.assertNotIn(target_branch_name, [b.name for b in self.github.get_branches()]) self.assertNotIn(target_branch_name, [b.name for b in self.github.get_branches()])
response = self.gh_webhook_event('push', '0000000000000000000000000000000000000000', target_branch_name) response = await self.gh_webhook_event('push', '0000000000000000000000000000000000000000', target_branch_name)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
self.assertNotIn(target_branch_name, [b.name for b in self.gitlab.branches.list()]) self.assertNotIn(target_branch_name, [b.name for b in self.gitlab.branches.list()])
def test_branch_gitlab(self): async def test_branch_gitlab(self):
"""Test sync after creating or deleting a branch on gitlab.""" """Test sync after creating or deleting a branch on gitlab."""
self.sync() await self.sync()
source_branch_name = 'master' source_branch_name = 'master'
target_branch_name = 'test-branch-gitlab' target_branch_name = 'test-branch-gitlab'
last_commit = self.gitlab.commits.list(ref_name=source_branch_name)[0].id last_commit = self.gitlab.commits.list(ref_name=source_branch_name)[0].id
...@@ -323,7 +335,7 @@ class GhTests(TestCase): ...@@ -323,7 +335,7 @@ class GhTests(TestCase):
file.save(branch=target_branch_name, commit_message='Test new branch on gitlab') file.save(branch=target_branch_name, commit_message='Test new branch on gitlab')
last_commit_gitlab = self.gitlab.commits.list(ref_name=target_branch_name)[0].id last_commit_gitlab = self.gitlab.commits.list(ref_name=target_branch_name)[0].id
response = self.gl_webhook_event('Push Hook', last_commit_gitlab, target_branch_name) response = await self.gl_webhook_event('Push Hook', last_commit_gitlab, target_branch_name)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
...@@ -334,51 +346,77 @@ class GhTests(TestCase): ...@@ -334,51 +346,77 @@ class GhTests(TestCase):
target_branch.delete() target_branch.delete()
self.assertNotIn(target_branch_name, [b.name for b in self.gitlab.branches.list()]) self.assertNotIn(target_branch_name, [b.name for b in self.gitlab.branches.list()])
response = self.gl_webhook_event('Push Hook', '0000000000000000000000000000000000000000', target_branch_name) response = await self.gl_webhook_event('Push Hook', '0000000000000000000000000000000000000000',
target_branch_name)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content.decode(), 'push event detected') self.assertEqual(response.content.decode(), 'push event detected')
self.assertNotIn(target_branch_name, [b.name for b in self.github.get_branches()]) self.assertNotIn(target_branch_name, [b.name for b in self.github.get_branches()])
def test_pipeline(self): async def test_pipeline(self):
"""Test reporting the gitlab pipeline status to github.""" """Test reporting the gitlab pipeline status to github."""
self.sync() await self.sync()
last_commit = self.gitlab.commits.list(ref_name='master')[0].id last_commit = self.gitlab.commits.list(ref_name='master')[0].id
response = self.gl_webhook_event('Pipeline Hook', last_commit, status='success') response = await self.gl_webhook_event('Pipeline Hook', last_commit, status='success')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertIn('success', [status.state for status in self.github.get_branch('master').commit.get_statuses()]) self.assertIn('success', [status.state for status in self.github.get_branch('master').commit.get_statuses()])
def test_pr(self): async def test_pr(self):
"""Test github's pull requests.""" """Test github's pull requests."""
self.sync() LOGGER.debug('test_pr: 0')
await self.sync()
LOGGER.debug('test_pr: 1')
not_accepted_string = "doesn't usually accept pull requests on master" not_accepted_string = "doesn't usually accept pull requests on master"
LOGGER.debug('test_pr: 2')
# Test pr on master # Test pr on master
last_commit = self.github.get_branch("devel").commit.sha last_commit = self.github.get_branch("devel").commit.sha
pr_master = self.project.github().create_pull(title="Test pr on master", body='', head="devel", base="master") LOGGER.debug('test_pr: 3')
response = self.gh_webhook_event('pull_request', github = await sync_to_async(self.project.github)()
LOGGER.debug('test_pr: 4')
pr_master = github.create_pull(title="Test pr on master", body='', head="devel", base="master")
LOGGER.debug('test_pr: 5')
response = await self.gh_webhook_event('pull_request',
last_commit=last_commit, last_commit=last_commit,
pr_action="opened", pr_action="opened",
pr_login="foo", pr_login="foo",
pr_number=pr_master.number) pr_number=pr_master.number)
LOGGER.debug('test_pr: 6')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
LOGGER.debug('test_pr: 7')
self.assertTrue([c.body for c in pr_master.get_issue_comments() if not_accepted_string in c.body]) self.assertTrue([c.body for c in pr_master.get_issue_comments() if not_accepted_string in c.body])
LOGGER.debug('test_pr: 8')
await sleep(60)
LOGGER.debug('test_pr: 9')
self.assertIn(f'pr/{pr_master.number}', [b.name for b in self.gitlab.branches.list()]) self.assertIn(f'pr/{pr_master.number}', [b.name for b in self.gitlab.branches.list()])
LOGGER.debug('test_pr: 10')
# Test pr on devel # Test pr on devel
last_commit = self.github.get_branch("master").commit.sha last_commit = self.github.get_branch("master").commit.sha
pr_devel = self.project.github().create_pull(title="Test pr on devel", body='', head="master", base="devel") LOGGER.debug('test_pr: 11')
response = self.gh_webhook_event('pull_request', pr_devel = github.create_pull(title="Test pr on devel", body='', head="master", base="devel")
LOGGER.debug('test_pr: 12')
response = await self.gh_webhook_event('pull_request',
last_commit=last_commit, last_commit=last_commit,
pr_action="opened", pr_action="opened",
pr_login="foo", pr_login="foo",
pr_number=pr_devel.number) pr_number=pr_devel.number)
LOGGER.debug('test_pr: 13')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)