toddlers/tests/utils/test_pagure.py
Michal Konečný fc7eb34205 Add method to obtain default branch to pagure module
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
2022-03-14 17:33:26 +01:00

950 lines
29 KiB
Python

"""
Unit tests for `toddlers.utils.pagure`.
"""
import pytest
from unittest.mock import call, Mock, patch
from toddlers.exceptions import PagureError
import toddlers.utils.pagure as pagure
class TestPagureSetPagure:
"""
Test class for `toddlers.pagure.set_pagure` function.
"""
def test_set_pagure_no_pagure_url(self):
"""
Test initialization of pagure module.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
pagure_obj = pagure.set_pagure()
assert pagure_obj._pagure_url == config.get("pagure_url")
assert pagure_obj._pagure_api_key == config.get("pagure_api_key")
assert pagure_obj._requests_session
def test_set_pagure_no_pagure_url(self):
"""
Test initialization of pagure module without required config value.
"""
with pytest.raises(
ValueError, match=r"No pagure_url found in the configuration file"
):
pagure.set_pagure({})
def test_set_pagure_no_pagure_api_key(self):
"""
Test initialization of pagure module without required config value.
"""
with pytest.raises(
ValueError, match=r"No pagure_api_key found in the configuration file"
):
config = {
"pagure_url": "https://pagure.io"
}
pagure.set_pagure(config)
class TestPagureGetAuthHeader:
"""
Test class for `toddlers.pagure.Pagure.get_auth_header` function.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
def test_get_auth_header(self):
"""
Assert that correct header is returned.
"""
exp_header = {
'Authorization': 'token Very secret key',
'Accept': 'application/json',
'Content-Type': 'application/json'
}
header = self.pagure.get_auth_header()
assert exp_header == header
class TestPagureCloseIssue:
"""
Test class for `toddlers.pagure.Pagure.close_issue` function.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_close_issue(self):
"""
Assert that issue on pagure is closed correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
issue_id = 100
namespace = "test"
message = "message"
reason = "Closed"
with patch("toddlers.utils.pagure.Pagure.add_comment_to_issue") as comment_mock:
comment_mock.return_value = True
result = self.pagure.close_issue(issue_id, namespace, message, reason)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/test/issue/100/status",
data={"status": reason},
headers=self.pagure.get_auth_header()
)
def test_close_issue_not_ok(self):
"""
Assert that failing to close issue on pagure is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
issue_id = 100
namespace = "test"
message = "message"
reason = "Closed"
expected_error = "Couldn't close issue 'https://pagure.io/test/issue/100'"
with pytest.raises(PagureError, match=expected_error):
with patch("toddlers.utils.pagure.Pagure.add_comment_to_issue") as comment_mock:
comment_mock.return_value = True
result = self.pagure.close_issue(issue_id, namespace, message, reason)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/test/issue/100/status",
data={"status": reason},
headers=self.pagure.get_auth_header()
)
class TestPagureAddCommentToIssue:
"""
Test class for `toddlers.pagure.Pagure.add_comment_to_issue` function.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_add_comment_to_issue(self):
"""
Assert that comment on pagure issue is posted correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
issue_id = 100
namespace = "test"
message = "message"
result = self.pagure.add_comment_to_issue(issue_id, namespace, message)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/test/issue/100/comment",
data={"comment": message},
headers=self.pagure.get_auth_header()
)
def test_add_comment_to_issue_not_ok(self):
"""
Assert that failing to comment on issue on pagure is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
issue_id = 100
namespace = "test"
message = "message"
expected_error = "Couldn't comment on issue 'https://pagure.io/test/issue/100'"
with pytest.raises(PagureError, match=expected_error):
result = self.pagure.add_comment_to_issue(issue_id, namespace, message)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/test/issue/100/comment",
data={"comment": message},
headers=self.pagure.get_auth_header()
)
class TestPagureUserExists:
"""
Test class for `toddlers.pagure.Pagure.user_exists` function.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_user_exists(self):
"""
Assert that everything is handled correctly if user exists.
"""
username = "Khaine"
response_mock = Mock()
response_mock.status_code = 200
data = {"users": [username]}
response_mock.json.return_value = data
self.pagure._requests_session.get.return_value = response_mock
result = self.pagure.user_exists(username)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/users?pattern={0}".format(username),
)
assert result is True
def test_user_exists_no_user(self):
"""
Assert that everything is handled correctly if user doesn't exists.
"""
username = "Khaine"
response_mock = Mock()
response_mock.status_code = 200
data = {"users": []}
response_mock.json.return_value = data
self.pagure._requests_session.get.return_value = response_mock
result = self.pagure.user_exists(username)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/users?pattern={0}".format(username),
)
assert result is False
def test_user_exists_not_ok(self):
"""
Assert that failing to get user is handled correctly.
"""
username = "Khaine"
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
expected_error = "Couldn't get user '{0}'".format(username)
with pytest.raises(PagureError, match=expected_error):
result = self.pagure.user_exists(username)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/users?pattern={0}".format(username),
)
class TestPagureNewProject:
"""
Test class for `toddlers.pagure.Pagure.new_project` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_new_project(self):
"""
Assert that new project on pagure is created correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = "https://example.com"
self.pagure.new_project(namespace, repo, description, upstream_url, default_branch)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True
},
headers=self.pagure.get_auth_header()
)
def test_new_project_initial_commit(self):
"""
Assert that new project on pagure is created correctly with initial commit.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = "https://example.com"
self.pagure.new_project(
namespace, repo, description, upstream_url, default_branch, initial_commit=True
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True,
'create_readme': True
},
headers=self.pagure.get_auth_header()
)
def test_new_project_alias(self):
"""
Assert that new project on pagure is created correctly with alias.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "rpms"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = ""
self.pagure.new_project(
namespace, repo, description, upstream_url, default_branch, alias=True
)
self.pagure._requests_session.post.assert_has_calls([
call(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True
},
headers=self.pagure.get_auth_header()
),
call(
"https://pagure.io/api/0/{0}/{1}/git/alias/new".format(namespace, repo),
data={
'alias_from': 'main',
'alias_to': 'rawhide',
},
headers=self.pagure.get_auth_header()
),
])
def test_new_project_alias_wrong_namespace(self):
"""
Assert that new project on pagure is created correctly without alias when
in wrong namespace.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = "https://example.com"
self.pagure.new_project(
namespace, repo, description, upstream_url, default_branch, alias=True
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True
},
headers=self.pagure.get_auth_header()
)
def test_new_project_failure(self):
"""
Assert that failing to create project on pagure is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = "https://example.com"
expected_error = "Couldn't create project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.new_project(
namespace, repo, description, upstream_url, default_branch
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True
},
headers=self.pagure.get_auth_header()
)
def test_new_project_alias_failure(self):
"""
Assert that failing to create alias on pagure is handled correctly.
"""
response_mock_ok = Mock()
response_mock_ok.status_code = 200
response_mock_fail = Mock()
response_mock_fail.status_code = 500
self.pagure._requests_session.post.side_effect = [response_mock_ok, response_mock_fail]
namespace = "rpms"
repo = "repo"
default_branch = "default_branch"
description = "description"
upstream_url = "https://example.com"
expected_error = "Couldn't create alias for project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.new_project(
namespace, repo, description, upstream_url, default_branch, alias=True
)
self.pagure._requests_session.post.assert_has_calls([
call(
"https://pagure.io/api/0/new",
data={
'namespace': namespace,
'name': repo,
'default_branch': default_branch,
'description': description,
'url': upstream_url,
'wait': True
},
headers=self.pagure.get_auth_header()
),
call(
"https://pagure.io/api/0/{0}/{1}/git/alias/new".format(namespace, repo),
data={
'alias_from': 'main',
'alias_to': 'rawhide',
},
headers=self.pagure.get_auth_header()
),
])
class TestPagureNewBranch:
"""
Test class for `toddlers.pagure.Pagure.new_branch` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_new_branch_from_commit(self):
"""
Assert that new branch from commit is created correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
branch = "branch"
from_commit = "commit"
self.pagure.new_branch(namespace, repo, branch, from_commit=from_commit)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branch".format(namespace, repo),
data={
'branch': branch,
'from_commit': from_commit,
},
headers=self.pagure.get_auth_header()
)
def test_new_branch_from_branch(self):
"""
Assert that new branch from existing branch is created correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
branch = "branch"
from_branch = "existing_branch"
self.pagure.new_branch(namespace, repo, branch, from_branch=from_branch)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branch".format(namespace, repo),
data={
'branch': branch,
'from_branch': from_branch,
},
headers=self.pagure.get_auth_header()
)
def test_new_branch_missing_required_params(self):
"""
Assert that method fails if required parameters are not provided.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
branch = "branch"
expected_error = ("You must specify either `from_commit` or "
"`from_branch`")
with pytest.raises(RuntimeError, match=expected_error):
self.pagure.new_branch(
namespace, repo, branch
)
def test_new_branch_missing_required_params(self):
"""
Assert that method fails if conflicting parameters are provided.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
branch = "branch"
from_branch = "existing_branch"
from_commit = "commit"
expected_error = ("`from_commit` and `from_branch` were both "
"specified. Only use one.")
with pytest.raises(RuntimeError, match=expected_error):
self.pagure.new_branch(
namespace, repo, branch, from_branch=from_branch, from_commit=from_commit
)
def test_new_branch_failure(self):
"""
Assert that failing to create branch on pagure is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.side_effect = response_mock
namespace = "namespace"
repo = "repo"
branch = "branch"
from_branch = "existing_branch"
expected_error = "Couldn't create branch in project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.new_branch(
namespace, repo, branch, from_branch=from_branch
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branch".format(namespace, repo),
data={
'branch': branch,
'from_branch': from_branch,
},
headers=self.pagure.get_auth_header()
)
class TestPagureSetMonitoringStatus:
"""
Test class for `toddlers.pagure.Pagure.set_monitoring_status` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_set_monitoring_status(self):
"""
Assert that setting monitoring status is done correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
monitoring_level = "no_monitoring"
self.pagure.set_monitoring_status(namespace, repo, monitoring_level)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/_dg/anitya/{0}/{1}".format(namespace, repo),
data={
'anitya_status': monitoring_level
},
headers=self.pagure.get_auth_header()
)
def test_set_monitoring_status_failure(self):
"""
Assert that failing to set monitoring level is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.side_effect = response_mock
namespace = "namespace"
repo = "repo"
monitoring_level = "no_monitoring"
expected_error = "Couldn't set monitoring on project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.set_monitoring_status(
namespace, repo, monitoring_level
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/_dg/anitya/{0}/{1}".format(namespace, repo),
data={
'anitya_status': monitoring_level
},
headers=self.pagure.get_auth_header()
)
class TestPagureChangeProjectMainAdmin:
"""
Test class for `toddlers.pagure.Pagure.change_project_main_admin` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_change_project_main_admin(self):
"""
Assert that changing project main admin is processed correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
self.pagure._requests_session.post.return_value = response_mock
namespace = "namespace"
repo = "repo"
main_admin = "zlopez"
self.pagure.change_project_main_admin(namespace, repo, main_admin)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/{0}/{1}".format(namespace, repo),
data={
'main_admin': main_admin
},
headers=self.pagure.get_auth_header()
)
def test_change_project_main_admin_failure(self):
"""
Assert that failing to set main admin is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.post.side_effect = response_mock
namespace = "namespace"
repo = "repo"
main_admin = "zlopez"
expected_error = "Couldn't set new admin on project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.change_project_main_admin(
namespace, repo, main_admin
)
self.pagure._requests_session.post.assert_called_with(
"https://pagure.io/api/0/{0}/{1}".format(namespace, repo),
data={
'main_admin': main_admin
},
headers=self.pagure.get_auth_header()
)
class TestPagureGetProjectContributors:
"""
Test class for `toddlers.pagure.Pagure.get_project_contributors` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_get_project_contributors(self):
"""
Assert that getting contributors works correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
username = "zlopez"
data = {"username": username}
response_mock.json.return_value = data
self.pagure._requests_session.get.return_value = response_mock
namespace = "namespace"
repo = "repo"
result = self.pagure.get_project_contributors(namespace, repo)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/contributors".format(namespace, repo),
headers=self.pagure.get_auth_header()
)
assert result == data
def test_get_project_contributors_failure(self):
"""
Assert that failing to get contributors is handling correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.get.side_effect = response_mock
namespace = "namespace"
repo = "repo"
expected_error = "Couldn't get contributors for project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.get_project_contributors(
namespace, repo
)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/contributors".format(namespace, repo),
headers=self.pagure.get_auth_header()
)
class TestPagureGetDefaultBranch:
"""
Test class for `toddlers.pagure.Pagure.get_default_branch` method.
"""
def setup(self):
"""
Setup method for the test class.
"""
config = {
"pagure_url": "https://pagure.io",
"pagure_api_key": "Very secret key"
}
self.pagure = pagure.set_pagure(config)
self.pagure._requests_session = Mock()
def test_get_default_branch(self):
"""
Assert that getting default branch works correctly.
"""
response_mock = Mock()
response_mock.status_code = 200
branch = "branch"
data = {"default": branch}
response_mock.json.return_value = data
self.pagure._requests_session.get.return_value = response_mock
namespace = "namespace"
repo = "repo"
result = self.pagure.get_default_branch(namespace, repo)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
headers=self.pagure.get_auth_header()
)
assert result == branch
def test_get_default_branch_not_ser(self):
"""
Assert that getting default branch returns None if not set.
"""
response_mock = Mock()
response_mock.status_code = 200
data = {}
response_mock.json.return_value = data
self.pagure._requests_session.get.return_value = response_mock
namespace = "namespace"
repo = "repo"
result = self.pagure.get_default_branch(namespace, repo)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
headers=self.pagure.get_auth_header()
)
assert result is None
def test_get_default_branch_failure(self):
"""
Assert that failing to get default branch is handled correctly.
"""
response_mock = Mock()
response_mock.status_code = 500
self.pagure._requests_session.get.side_effect = response_mock
namespace = "namespace"
repo = "repo"
expected_error = "Couldn't get default branch for project '{0}/{1}'".format(namespace, repo)
with pytest.raises(PagureError, match=expected_error):
self.pagure.get_default_branch(
namespace, repo
)
self.pagure._requests_session.get.assert_called_with(
"https://pagure.io/api/0/{0}/{1}/git/branches".format(namespace, repo),
headers=self.pagure.get_auth_header()
)