##// END OF EJS Templates
vcs: Minimal change to expose the shadow repository...
vcs: Minimal change to expose the shadow repository Based on my original research, this was the "minimal" starting point. It shows that three concepts are needed for the "repo_name": * From the security standpoint we think of the shadow repository having the same ACL as the target repository of the pull request. This is because the pull request itself is considered to be a part of the target repository. Out of this thought, the variable "acl_repo_name" is used whenever we want to check permissions or when we need the database configuration of the repository. An alternative name would have been "db_repo_name", but the usage for ACL checking is the most important one. * From the web interaction perspective, we need the URL which was originally used to get to the repository. This is because based on this base URL commands can be identified. Especially for Git this is important, so that the commands are correctly recognized. Since the URL is in the focus, this is called "url_repo_name". * Finally we have to deal with the repository on the file system. This is what the VCS layer deal with normally, so this name is called "vcs_repo_name". The original repository interaction is a special case where all three names are the same. When interacting with a pull request, these three names are typically all different. This change is minimal in a sense that it just makes the interaction with a shadow repository barely work, without checking any special constraints yet. This was the starting point for further work on this topic.

File last commit:

r793:fc8d2069 default
r887:175782be default
Show More
test_integrations.py
264 lines | 10.2 KiB | text/x-python | PythonLexer
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2016 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import mock
import pytest
from webob.exc import HTTPNotFound
import rhodecode
from rhodecode.model.db import Integration
from rhodecode.model.meta import Session
from rhodecode.tests import assert_session_flash, url, TEST_USER_ADMIN_LOGIN
from rhodecode.tests.utils import AssertResponse
from rhodecode.integrations import integration_type_registry
from rhodecode.config.routing import ADMIN_PREFIX
@pytest.mark.usefixtures('app', 'autologin_user')
class TestIntegrationsView(object):
pass
class TestGlobalIntegrationsView(TestIntegrationsView):
def test_index_no_integrations(self, app):
url = ADMIN_PREFIX + '/integrations'
response = app.get(url)
assert response.status_code == 200
assert 'exist yet' in response.body
def test_index_with_integrations(self, app, global_integration_stub):
url = ADMIN_PREFIX + '/integrations'
response = app.get(url)
assert response.status_code == 200
assert 'exist yet' not in response.body
assert global_integration_stub.name in response.body
def test_new_integration_page(self, app):
url = ADMIN_PREFIX + '/integrations/new'
response = app.get(url)
assert response.status_code == 200
for integration_key in integration_type_registry:
nurl = (ADMIN_PREFIX + '/integrations/{integration}/new').format(
integration=integration_key)
assert nurl in response.body
@pytest.mark.parametrize(
'IntegrationType', integration_type_registry.values())
def test_get_create_integration_page(self, app, IntegrationType):
url = ADMIN_PREFIX + '/integrations/{integration_key}/new'.format(
integration_key=IntegrationType.key)
response = app.get(url)
assert response.status_code == 200
assert IntegrationType.display_name in response.body
def test_post_integration_page(self, app, StubIntegrationType, csrf_token,
test_repo_group, backend_random):
url = ADMIN_PREFIX + '/integrations/{integration_key}/new'.format(
integration_key=StubIntegrationType.key)
_post_integration_test_helper(app, url, csrf_token, admin_view=True,
repo=backend_random.repo, repo_group=test_repo_group)
class TestRepoGroupIntegrationsView(TestIntegrationsView):
def test_index_no_integrations(self, app, test_repo_group):
url = '/{repo_group_name}/settings/integrations'.format(
repo_group_name=test_repo_group.group_name)
response = app.get(url)
assert response.status_code == 200
assert 'exist yet' in response.body
def test_index_with_integrations(self, app, test_repo_group,
repogroup_integration_stub):
url = '/{repo_group_name}/settings/integrations'.format(
repo_group_name=test_repo_group.group_name)
stub_name = repogroup_integration_stub.name
response = app.get(url)
assert response.status_code == 200
assert 'exist yet' not in response.body
assert stub_name in response.body
def test_new_integration_page(self, app, test_repo_group):
repo_group_name = test_repo_group.group_name
url = '/{repo_group_name}/settings/integrations/new'.format(
repo_group_name=test_repo_group.group_name)
response = app.get(url)
assert response.status_code == 200
for integration_key in integration_type_registry:
nurl = ('/{repo_group_name}/settings/integrations'
'/{integration}/new').format(
repo_group_name=repo_group_name,
integration=integration_key)
assert nurl in response.body
@pytest.mark.parametrize(
'IntegrationType', integration_type_registry.values())
def test_get_create_integration_page(self, app, test_repo_group,
IntegrationType):
repo_group_name = test_repo_group.group_name
url = ('/{repo_group_name}/settings/integrations/{integration_key}/new'
).format(repo_group_name=repo_group_name,
integration_key=IntegrationType.key)
response = app.get(url)
assert response.status_code == 200
assert IntegrationType.display_name in response.body
def test_post_integration_page(self, app, test_repo_group, backend_random,
StubIntegrationType, csrf_token):
repo_group_name = test_repo_group.group_name
url = ('/{repo_group_name}/settings/integrations/{integration_key}/new'
).format(repo_group_name=repo_group_name,
integration_key=StubIntegrationType.key)
_post_integration_test_helper(app, url, csrf_token, admin_view=False,
repo=backend_random.repo, repo_group=test_repo_group)
class TestRepoIntegrationsView(TestIntegrationsView):
def test_index_no_integrations(self, app, backend_random):
url = '/{repo_name}/settings/integrations'.format(
repo_name=backend_random.repo.repo_name)
response = app.get(url)
assert response.status_code == 200
assert 'exist yet' in response.body
def test_index_with_integrations(self, app, repo_integration_stub):
url = '/{repo_name}/settings/integrations'.format(
repo_name=repo_integration_stub.repo.repo_name)
stub_name = repo_integration_stub.name
response = app.get(url)
assert response.status_code == 200
assert stub_name in response.body
assert 'exist yet' not in response.body
def test_new_integration_page(self, app, backend_random):
repo_name = backend_random.repo.repo_name
url = '/{repo_name}/settings/integrations/new'.format(
repo_name=repo_name)
response = app.get(url)
assert response.status_code == 200
for integration_key in integration_type_registry:
nurl = ('/{repo_name}/settings/integrations'
'/{integration}/new').format(
repo_name=repo_name,
integration=integration_key)
assert nurl in response.body
@pytest.mark.parametrize(
'IntegrationType', integration_type_registry.values())
def test_get_create_integration_page(self, app, backend_random,
IntegrationType):
repo_name = backend_random.repo.repo_name
url = '/{repo_name}/settings/integrations/{integration_key}/new'.format(
repo_name=repo_name, integration_key=IntegrationType.key)
response = app.get(url)
assert response.status_code == 200
assert IntegrationType.display_name in response.body
def test_post_integration_page(self, app, backend_random, test_repo_group,
StubIntegrationType, csrf_token):
repo_name = backend_random.repo.repo_name
url = '/{repo_name}/settings/integrations/{integration_key}/new'.format(
repo_name=repo_name, integration_key=StubIntegrationType.key)
_post_integration_test_helper(app, url, csrf_token, admin_view=False,
repo=backend_random.repo, repo_group=test_repo_group)
def _post_integration_test_helper(app, url, csrf_token, repo, repo_group,
admin_view):
"""
Posts form data to create integration at the url given then deletes it and
checks if the redirect url is correct.
"""
app.post(url, params={}, status=403) # missing csrf check
response = app.post(url, params={'csrf_token': csrf_token})
assert response.status_code == 200
assert 'Errors exist' in response.body
scopes_destinations = [
('global',
ADMIN_PREFIX + '/integrations'),
('root-repos',
ADMIN_PREFIX + '/integrations'),
('repo:%s' % repo.repo_name,
'/%s/settings/integrations' % repo.repo_name),
('repogroup:%s' % repo_group.group_name,
'/%s/settings/integrations' % repo_group.group_name),
('repogroup-recursive:%s' % repo_group.group_name,
'/%s/settings/integrations' % repo_group.group_name),
]
for scope, destination in scopes_destinations:
if admin_view:
destination = ADMIN_PREFIX + '/integrations'
form_data = [
('csrf_token', csrf_token),
('__start__', 'options:mapping'),
('name', 'test integration'),
('scope', scope),
('enabled', 'true'),
('__end__', 'options:mapping'),
('__start__', 'settings:mapping'),
('test_int_field', '34'),
('test_string_field', ''), # empty value on purpose as it's required
('__end__', 'settings:mapping'),
]
errors_response = app.post(url, form_data)
assert 'Errors exist' in errors_response.body
form_data[-2] = ('test_string_field', 'data!')
assert Session().query(Integration).count() == 0
created_response = app.post(url, form_data)
assert Session().query(Integration).count() == 1
delete_response = app.post(
created_response.location,
params={'csrf_token': csrf_token, 'delete': 'delete'})
assert Session().query(Integration).count() == 0
assert delete_response.location.endswith(destination)