core: run ruff format

This commit is contained in:
RhodeCode Admin 2025-01-13 17:47:37 +01:00
parent 7130effb58
commit 166db21812
821 changed files with 82905 additions and 84121 deletions

View file

@ -22,85 +22,63 @@ import pytest
from rhodecode.model.repo import RepoModel
from rhodecode.model.scm import ScmModel
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, jsonify)
from rhodecode.api.tests.utils import build_data, api_call, assert_error, assert_ok, crash, jsonify
from rhodecode.tests.fixtures.rc_fixture import Fixture
from rhodecode.tests.fixtures.fixture_utils import plain_http_host_only_stub
fixture = Fixture()
UPDATE_REPO_NAME = 'api_update_me'
UPDATE_REPO_NAME = "api_update_me"
class SAME_AS_UPDATES(object):
""" Constant used for tests below """
"""Constant used for tests below"""
@pytest.mark.usefixtures("testuser_api", "app")
class TestApiUpdateRepo(object):
@pytest.mark.parametrize("updates, expected", [
({'owner': TEST_USER_REGULAR_LOGIN},
SAME_AS_UPDATES),
({'description': 'new description'},
SAME_AS_UPDATES),
({'clone_uri': 'http://foo.com/repo'},
SAME_AS_UPDATES),
({'clone_uri': None},
{'clone_uri': ''}),
({'clone_uri': ''},
{'clone_uri': ''}),
({'clone_uri': 'http://example.com/repo_pull'},
{'clone_uri': 'http://example.com/repo_pull'}),
({'push_uri': ''},
{'push_uri': ''}),
({'push_uri': 'http://example.com/repo_push'},
{'push_uri': 'http://example.com/repo_push'}),
({'landing_rev': None}, # auto-updated based on type of repo
{'landing_rev': [None, None]}),
({'enable_statistics': True},
SAME_AS_UPDATES),
({'enable_locking': True},
SAME_AS_UPDATES),
({'enable_downloads': True},
SAME_AS_UPDATES),
({'repo_name': 'new_repo_name'},
{
'repo_name': 'new_repo_name',
'url': 'http://{}/new_repo_name'.format(plain_http_host_only_stub())
}),
({'repo_name': 'test_group_for_update/{}'.format(UPDATE_REPO_NAME),
'_group': 'test_group_for_update'},
{
'repo_name': 'test_group_for_update/{}'.format(UPDATE_REPO_NAME),
'url': 'http://{}/test_group_for_update/{}'.format(
plain_http_host_only_stub(), UPDATE_REPO_NAME)
}),
])
@pytest.mark.parametrize(
"updates, expected",
[
({"owner": TEST_USER_REGULAR_LOGIN}, SAME_AS_UPDATES),
({"description": "new description"}, SAME_AS_UPDATES),
({"clone_uri": "http://foo.com/repo"}, SAME_AS_UPDATES),
({"clone_uri": None}, {"clone_uri": ""}),
({"clone_uri": ""}, {"clone_uri": ""}),
({"clone_uri": "http://example.com/repo_pull"}, {"clone_uri": "http://example.com/repo_pull"}),
({"push_uri": ""}, {"push_uri": ""}),
({"push_uri": "http://example.com/repo_push"}, {"push_uri": "http://example.com/repo_push"}),
(
{"landing_rev": None}, # auto-updated based on type of repo
{"landing_rev": [None, None]},
),
({"enable_statistics": True}, SAME_AS_UPDATES),
({"enable_locking": True}, SAME_AS_UPDATES),
({"enable_downloads": True}, SAME_AS_UPDATES),
(
{"repo_name": "new_repo_name"},
{"repo_name": "new_repo_name", "url": "http://{}/new_repo_name".format(plain_http_host_only_stub())},
),
(
{"repo_name": "test_group_for_update/{}".format(UPDATE_REPO_NAME), "_group": "test_group_for_update"},
{
"repo_name": "test_group_for_update/{}".format(UPDATE_REPO_NAME),
"url": "http://{}/test_group_for_update/{}".format(plain_http_host_only_stub(), UPDATE_REPO_NAME),
},
),
],
)
def test_api_update_repo(self, updates, expected, backend):
repo_name = UPDATE_REPO_NAME
repo = fixture.create_repo(repo_name, repo_type=backend.alias)
if updates.get('_group'):
fixture.create_repo_group(updates['_group'])
if updates.get("_group"):
fixture.create_repo_group(updates["_group"])
if 'landing_rev' in updates:
if "landing_rev" in updates:
default_landing_ref, _lbl = ScmModel.backend_landing_ref(backend.alias)
_type, _name = default_landing_ref.split(':')
updates['landing_rev'] = default_landing_ref
expected['landing_rev'] = [_type, _name]
_type, _name = default_landing_ref.split(":")
updates["landing_rev"] = default_landing_ref
expected["landing_rev"] = [_type, _name]
expected_api_data = repo.get_api_data(include_secrets=True)
if expected is SAME_AS_UPDATES:
@ -108,101 +86,90 @@ class TestApiUpdateRepo(object):
else:
expected_api_data.update(expected)
id_, params = build_data(
self.apikey, 'update_repo', repoid=repo_name, **updates)
id_, params = build_data(self.apikey, "update_repo", repoid=repo_name, **updates)
with mock.patch('rhodecode.model.validation_schema.validators.url_validator'):
with mock.patch("rhodecode.model.validation_schema.validators.url_validator"):
response = api_call(self.app, params)
if updates.get('repo_name'):
repo_name = updates['repo_name']
if updates.get("repo_name"):
repo_name = updates["repo_name"]
try:
expected = {
'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name),
'repository': jsonify(expected_api_data)
"msg": "updated repo ID:%s %s" % (repo.repo_id, repo_name),
"repository": jsonify(expected_api_data),
}
assert_ok(id_, expected, given=response.body)
finally:
fixture.destroy_repo(repo_name)
if updates.get('_group'):
fixture.destroy_repo_group(updates['_group'])
if updates.get("_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,
'fork_of_id': master_repo.repo_id
}
updates = {"fork_of": master_repo.repo_name, "fork_of_id": master_repo.repo_id}
expected_api_data = repo.get_api_data(include_secrets=True)
expected_api_data.update(updates)
id_, params = build_data(
self.apikey, 'update_repo', repoid=repo.repo_name, **updates)
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': jsonify(expected_api_data)
"msg": "updated repo ID:%s %s" % (repo.repo_id, repo.repo_name),
"repository": jsonify(expected_api_data),
}
assert_ok(id_, expected, given=response.body)
result = response.json['result']['repository']
assert result['fork_of'] == master_repo.repo_name
assert result['fork_of_id'] == master_repo.repo_id
result = response.json["result"]["repository"]
assert result["fork_of"] == master_repo.repo_name
assert result["fork_of_id"] == master_repo.repo_id
def test_api_update_repo_fork_of_not_found(self, backend):
master_repo_name = 'fake-parent-repo'
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)
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 = {
'repo_fork_of': 'Fork with id `{}` does not exists'.format(
master_repo_name)}
expected = {"repo_fork_of": "Fork with id `{}` does not exists".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'
fake_repo_group = 'test_group_for_update'
repo_name = "admin_owned"
fake_repo_group = "test_group_for_update"
fixture.create_repo(repo_name)
updates = {'repo_name': '{}/{}'.format(fake_repo_group, repo_name)}
id_, params = build_data(
self.apikey, 'update_repo', repoid=repo_name, **updates)
updates = {"repo_name": "{}/{}".format(fake_repo_group, repo_name)}
id_, params = build_data(self.apikey, "update_repo", repoid=repo_name, **updates)
response = api_call(self.app, params)
try:
expected = {
'repo_group': 'Repository group `{}` does not exist'.format(fake_repo_group)
}
expected = {"repo_group": "Repository group `{}` does not exist".format(fake_repo_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'
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)
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,)
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)
@mock.patch.object(RepoModel, "update", crash)
def test_api_update_repo_exception_occurred(self, backend):
repo_name = UPDATE_REPO_NAME
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,)
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,)
expected = "failed to update repo `%s`" % (repo_name,)
assert_error(id_, expected, given=response.body)
finally:
fixture.destroy_repo(repo_name)