# -*- 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 . # # 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 rhodecode.model.repo import RepoModel from rhodecode.tests import TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN from rhodecode.api.tests.utils import ( build_data, api_call, assert_error, assert_ok, crash) from rhodecode.tests.fixture import Fixture fixture = Fixture() @pytest.mark.usefixtures("testuser_api", "app") class TestApiUpdateRepo(object): @pytest.mark.parametrize("changing_attr, updates", [ ('owner', {'owner': TEST_USER_REGULAR_LOGIN}), ('description', {'description': 'new description'}), ('active', {'active': True}), ('active', {'active': False}), ('clone_uri', {'clone_uri': 'http://foo.com/repo'}), ('clone_uri', {'clone_uri': None}), ('landing_rev', {'landing_rev': 'branch:master'}), ('enable_statistics', {'enable_statistics': True}), ('enable_locking', {'enable_locking': True}), ('enable_downloads', {'enable_downloads': True}), ('name', {'name': 'new_repo_name'}), ('repo_group', {'group': 'test_group_for_update'}), ]) def test_api_update_repo(self, changing_attr, updates, backend): repo_name = 'api_update_me' repo = fixture.create_repo(repo_name, repo_type=backend.alias) if changing_attr == 'repo_group': fixture.create_repo_group(updates['group']) id_, params = build_data( self.apikey, 'update_repo', repoid=repo_name, **updates) response = api_call(self.app, params) if changing_attr == 'name': repo_name = updates['name'] if changing_attr == 'repo_group': repo_name = '/'.join([updates['group'], repo_name]) try: expected = { 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name), 'repository': repo.get_api_data(include_secrets=True) } assert_ok(id_, expected, given=response.body) finally: fixture.destroy_repo(repo_name) if changing_attr == 'repo_group': fixture.destroy_repo_group(updates['group']) def test_api_update_repo_fork_of_field(self, backend): master_repo = backend.create_repo() repo = backend.create_repo() updates = { 'fork_of': master_repo.repo_name } id_, params = build_data( self.apikey, 'update_repo', repoid=repo.repo_name, **updates) response = api_call(self.app, params) expected = { 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo.repo_name), 'repository': repo.get_api_data(include_secrets=True) } assert_ok(id_, expected, given=response.body) result = response.json['result']['repository'] assert result['fork_of'] == master_repo.repo_name def test_api_update_repo_fork_of_not_found(self, backend): master_repo_name = 'fake-parent-repo' repo = backend.create_repo() updates = { 'fork_of': master_repo_name } id_, params = build_data( self.apikey, 'update_repo', repoid=repo.repo_name, **updates) response = api_call(self.app, params) expected = 'repository `{}` does not exist'.format(master_repo_name) assert_error(id_, expected, given=response.body) def test_api_update_repo_with_repo_group_not_existing(self): repo_name = 'admin_owned' fixture.create_repo(repo_name) updates = {'group': 'test_group_for_update'} id_, params = build_data( self.apikey, 'update_repo', repoid=repo_name, **updates) response = api_call(self.app, params) try: expected = 'repository group `%s` does not exist' % ( updates['group'],) assert_error(id_, expected, given=response.body) finally: fixture.destroy_repo(repo_name) def test_api_update_repo_regular_user_not_allowed(self): repo_name = 'admin_owned' fixture.create_repo(repo_name) updates = {'active': False} id_, params = build_data( self.apikey_regular, 'update_repo', repoid=repo_name, **updates) response = api_call(self.app, params) try: expected = 'repository `%s` does not exist' % (repo_name,) assert_error(id_, expected, given=response.body) finally: fixture.destroy_repo(repo_name) @mock.patch.object(RepoModel, 'update', crash) def test_api_update_repo_exception_occurred(self, backend): repo_name = 'api_update_me' fixture.create_repo(repo_name, repo_type=backend.alias) id_, params = build_data( self.apikey, 'update_repo', repoid=repo_name, owner=TEST_USER_ADMIN_LOGIN,) response = api_call(self.app, params) try: expected = 'failed to update repo `%s`' % (repo_name,) assert_error(id_, expected, given=response.body) finally: fixture.destroy_repo(repo_name)