mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
use http client class for all http requests
This commit is contained in:
@ -67,80 +67,73 @@ def test_remote_web_url(aur_package_ahriman: AURPackage) -> None:
|
||||
assert web_url.startswith(AUR.DEFAULT_AUR_URL)
|
||||
|
||||
|
||||
def test_make_request(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
def test_aur_request(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must perform request to AUR
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.json.return_value = json.loads(_get_response(resource_path_root))
|
||||
request_mock = mocker.patch("requests.get", return_value=response_mock)
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=response_mock)
|
||||
|
||||
assert aur.make_request("info", "ahriman") == [aur_package_ahriman]
|
||||
assert aur.aur_request("info", "ahriman") == [aur_package_ahriman]
|
||||
request_mock.assert_called_once_with(
|
||||
"https://aur.archlinux.org/rpc",
|
||||
params={"v": "5", "type": "info", "arg": ["ahriman"]},
|
||||
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
|
||||
timeout=aur.DEFAULT_TIMEOUT)
|
||||
"GET", "https://aur.archlinux.org/rpc",
|
||||
params=[("type", "info"), ("v", "5"), ("arg", "ahriman")])
|
||||
|
||||
|
||||
def test_make_request_multi_arg(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
def test_aur_request_multi_arg(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must perform request to AUR with multiple args
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.json.return_value = json.loads(_get_response(resource_path_root))
|
||||
request_mock = mocker.patch("requests.get", return_value=response_mock)
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=response_mock)
|
||||
|
||||
assert aur.make_request("search", "ahriman", "is", "cool") == [aur_package_ahriman]
|
||||
assert aur.aur_request("search", "ahriman", "is", "cool") == [aur_package_ahriman]
|
||||
request_mock.assert_called_once_with(
|
||||
"https://aur.archlinux.org/rpc",
|
||||
params={"v": "5", "type": "search", "arg[]": ["ahriman", "is", "cool"]},
|
||||
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
|
||||
timeout=aur.DEFAULT_TIMEOUT)
|
||||
"GET", "https://aur.archlinux.org/rpc",
|
||||
params=[("type", "search"), ("v", "5"), ("arg[]", "ahriman"), ("arg[]", "is"), ("arg[]", "cool")])
|
||||
|
||||
|
||||
def test_make_request_with_kwargs(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
def test_aur_request_with_kwargs(aur: AUR, aur_package_ahriman: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must perform request to AUR with named parameters
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.json.return_value = json.loads(_get_response(resource_path_root))
|
||||
request_mock = mocker.patch("requests.get", return_value=response_mock)
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=response_mock)
|
||||
|
||||
assert aur.make_request("search", "ahriman", by="name") == [aur_package_ahriman]
|
||||
assert aur.aur_request("search", "ahriman", by="name") == [aur_package_ahriman]
|
||||
request_mock.assert_called_once_with(
|
||||
"https://aur.archlinux.org/rpc",
|
||||
params={"v": "5", "type": "search", "arg": ["ahriman"], "by": "name"},
|
||||
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
|
||||
timeout=aur.DEFAULT_TIMEOUT)
|
||||
"GET", "https://aur.archlinux.org/rpc",
|
||||
params=[("type", "search"), ("v", "5"), ("arg", "ahriman"), ("by", "name")])
|
||||
|
||||
|
||||
def test_make_request_failed(aur: AUR, mocker: MockerFixture) -> None:
|
||||
def test_aur_request_failed(aur: AUR, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise generic exception
|
||||
"""
|
||||
mocker.patch("requests.get", side_effect=Exception())
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
with pytest.raises(Exception):
|
||||
aur.make_request("info", "ahriman")
|
||||
aur.aur_request("info", "ahriman")
|
||||
|
||||
|
||||
def test_make_request_failed_http_error(aur: AUR, mocker: MockerFixture) -> None:
|
||||
def test_aur_request_failed_http_error(aur: AUR, mocker: MockerFixture) -> None:
|
||||
""" must reraise http exception
|
||||
"""
|
||||
must reraise http exception
|
||||
"""
|
||||
mocker.patch("requests.get", side_effect=requests.exceptions.HTTPError())
|
||||
with pytest.raises(requests.exceptions.HTTPError):
|
||||
aur.make_request("info", "ahriman")
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
with pytest.raises(requests.HTTPError):
|
||||
aur.aur_request("info", "ahriman")
|
||||
|
||||
|
||||
def test_package_info(aur: AUR, aur_package_ahriman: AURPackage, pacman: Pacman, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must make request for info
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=[aur_package_ahriman])
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.aur_request", return_value=[aur_package_ahriman])
|
||||
assert aur.package_info(aur_package_ahriman.name, pacman=pacman) == aur_package_ahriman
|
||||
request_mock.assert_called_once_with("info", aur_package_ahriman.name)
|
||||
|
||||
@ -150,7 +143,7 @@ def test_package_info_not_found(aur: AUR, aur_package_ahriman: AURPackage, pacma
|
||||
"""
|
||||
must raise UnknownPackage exception in case if no package was found
|
||||
"""
|
||||
mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=[])
|
||||
mocker.patch("ahriman.core.alpm.remote.AUR.aur_request", return_value=[])
|
||||
with pytest.raises(UnknownPackageError, match=aur_package_ahriman.name):
|
||||
assert aur.package_info(aur_package_ahriman.name, pacman=pacman)
|
||||
|
||||
@ -159,6 +152,6 @@ def test_package_search(aur: AUR, aur_package_ahriman: AURPackage, pacman: Pacma
|
||||
"""
|
||||
must make request for search
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.make_request", return_value=[aur_package_ahriman])
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.AUR.aur_request", return_value=[aur_package_ahriman])
|
||||
assert aur.package_search(aur_package_ahriman.name, pacman=pacman) == [aur_package_ahriman]
|
||||
request_mock.assert_called_once_with("search", aur_package_ahriman.name, by="name-desc")
|
||||
|
@ -62,39 +62,37 @@ def test_remote_web_url(aur_package_akonadi: AURPackage) -> None:
|
||||
assert web_url.startswith(Official.DEFAULT_ARCHLINUX_URL)
|
||||
|
||||
|
||||
def test_make_request(official: Official, aur_package_akonadi: AURPackage,
|
||||
def test_arch_request(official: Official, aur_package_akonadi: AURPackage,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must perform request to official repositories
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.json.return_value = json.loads(_get_response(resource_path_root))
|
||||
request_mock = mocker.patch("requests.get", return_value=response_mock)
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.Official.make_request", return_value=response_mock)
|
||||
|
||||
assert official.make_request("akonadi", by="q") == [aur_package_akonadi]
|
||||
assert official.arch_request("akonadi", by="q") == [aur_package_akonadi]
|
||||
request_mock.assert_called_once_with(
|
||||
"https://archlinux.org/packages/search/json",
|
||||
params={"q": ("akonadi",), "repo": Official.DEFAULT_SEARCH_REPOSITORIES},
|
||||
headers={"User-Agent": Official.DEFAULT_USER_AGENT},
|
||||
timeout=official.DEFAULT_TIMEOUT)
|
||||
"GET", "https://archlinux.org/packages/search/json",
|
||||
params=[("repo", repository) for repository in Official.DEFAULT_SEARCH_REPOSITORIES] + [("q", "akonadi")])
|
||||
|
||||
|
||||
def test_make_request_failed(official: Official, mocker: MockerFixture) -> None:
|
||||
def test_arch_request_failed(official: Official, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise generic exception
|
||||
"""
|
||||
mocker.patch("requests.get", side_effect=Exception())
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
with pytest.raises(Exception):
|
||||
official.make_request("akonadi", by="q")
|
||||
official.arch_request("akonadi", by="q")
|
||||
|
||||
|
||||
def test_make_request_failed_http_error(official: Official, mocker: MockerFixture) -> None:
|
||||
def test_arch_request_failed_http_error(official: Official, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise http exception
|
||||
"""
|
||||
mocker.patch("requests.get", side_effect=requests.exceptions.HTTPError())
|
||||
with pytest.raises(requests.exceptions.HTTPError):
|
||||
official.make_request("akonadi", by="q")
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
with pytest.raises(requests.HTTPError):
|
||||
official.arch_request("akonadi", by="q")
|
||||
|
||||
|
||||
def test_package_info(official: Official, aur_package_akonadi: AURPackage, pacman: Pacman,
|
||||
@ -102,7 +100,7 @@ def test_package_info(official: Official, aur_package_akonadi: AURPackage, pacma
|
||||
"""
|
||||
must make request for info
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.Official.make_request",
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.Official.arch_request",
|
||||
return_value=[aur_package_akonadi])
|
||||
assert official.package_info(aur_package_akonadi.name, pacman=pacman) == aur_package_akonadi
|
||||
request_mock.assert_called_once_with(aur_package_akonadi.name, by="name")
|
||||
@ -113,7 +111,7 @@ def test_package_info_not_found(official: Official, aur_package_ahriman: AURPack
|
||||
"""
|
||||
must raise UnknownPackage exception in case if no package was found
|
||||
"""
|
||||
mocker.patch("ahriman.core.alpm.remote.Official.make_request", return_value=[])
|
||||
mocker.patch("ahriman.core.alpm.remote.Official.arch_request", return_value=[])
|
||||
with pytest.raises(UnknownPackageError, match=aur_package_ahriman.name):
|
||||
assert official.package_info(aur_package_ahriman.name, pacman=pacman)
|
||||
|
||||
@ -123,7 +121,7 @@ def test_package_search(official: Official, aur_package_akonadi: AURPackage, pac
|
||||
"""
|
||||
must make request for search
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.Official.make_request",
|
||||
request_mock = mocker.patch("ahriman.core.alpm.remote.Official.arch_request",
|
||||
return_value=[aur_package_akonadi])
|
||||
assert official.package_search(aur_package_akonadi.name, pacman=pacman) == [aur_package_akonadi]
|
||||
request_mock.assert_called_once_with(aur_package_akonadi.name, by="q")
|
||||
|
154
tests/ahriman/core/http/test_sync_http_client.py
Normal file
154
tests/ahriman/core/http/test_sync_http_client.py
Normal file
@ -0,0 +1,154 @@
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock, call as MockCall
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.http import SyncHttpClient
|
||||
|
||||
|
||||
def test_init() -> None:
|
||||
"""
|
||||
must init from empty parameters
|
||||
"""
|
||||
assert SyncHttpClient()
|
||||
|
||||
|
||||
def test_init_auth(configuration: Configuration) -> None:
|
||||
"""
|
||||
must init with auth
|
||||
"""
|
||||
configuration.set_option("web", "username", "username")
|
||||
configuration.set_option("web", "password", "password")
|
||||
|
||||
assert SyncHttpClient("web", configuration).auth == ("username", "password")
|
||||
assert SyncHttpClient(configuration=configuration).auth is None
|
||||
|
||||
|
||||
def test_init_auth_empty() -> None:
|
||||
"""
|
||||
must init with empty auth
|
||||
"""
|
||||
assert SyncHttpClient().auth is None
|
||||
|
||||
|
||||
def test_session() -> None:
|
||||
"""
|
||||
must generate valid session
|
||||
"""
|
||||
session = SyncHttpClient().session
|
||||
assert "User-Agent" in session.headers
|
||||
|
||||
|
||||
def test_exception_response_text() -> None:
|
||||
"""
|
||||
must parse HTTP response to string
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.text = "hello"
|
||||
exception = requests.exceptions.HTTPError(response=response_mock)
|
||||
|
||||
assert SyncHttpClient.exception_response_text(exception) == "hello"
|
||||
|
||||
|
||||
def test_exception_response_text_empty() -> None:
|
||||
"""
|
||||
must parse HTTP exception with empty response to empty string
|
||||
"""
|
||||
exception = requests.exceptions.HTTPError(response=None)
|
||||
assert SyncHttpClient.exception_response_text(exception) == ""
|
||||
|
||||
|
||||
def test_make_request(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must make HTTP request
|
||||
"""
|
||||
request_mock = mocker.patch("requests.Session.request")
|
||||
client = SyncHttpClient()
|
||||
|
||||
assert client.make_request("GET", "url1") is not None
|
||||
assert client.make_request("GET", "url2", params=[("param", "value")]) is not None
|
||||
|
||||
assert client.make_request("POST", "url3") is not None
|
||||
assert client.make_request("POST", "url4", json={"param": "value"}) is not None
|
||||
assert client.make_request("POST", "url5", data={"param": "value"}) is not None
|
||||
# we don't want to put full descriptor here
|
||||
assert client.make_request("POST", "url6", files={"file": "tuple"}) is not None
|
||||
|
||||
assert client.make_request("DELETE", "url7") is not None
|
||||
|
||||
assert client.make_request("GET", "url8", headers={"user-agent": "ua"}) is not None
|
||||
|
||||
auth = client.auth = ("username", "password")
|
||||
assert client.make_request("GET", "url9") is not None
|
||||
|
||||
request_mock.assert_has_calls([
|
||||
MockCall("GET", "url1", params=None, data=None, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("GET", "url2", params=[("param", "value")], data=None, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", "url3", params=None, data=None, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", "url4", params=None, data=None, headers=None, files=None, json={"param": "value"},
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", "url5", params=None, data={"param": "value"}, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", "url6", params=None, data=None, headers=None, files={"file": "tuple"}, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("DELETE", "url7", params=None, data=None, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("GET", "url8", params=None, data=None, headers={"user-agent": "ua"}, files=None, json=None,
|
||||
auth=None, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("GET", "url9", params=None, data=None, headers=None, files=None, json=None,
|
||||
auth=auth, timeout=client.timeout),
|
||||
MockCall().raise_for_status(),
|
||||
])
|
||||
|
||||
|
||||
def test_make_request_failed(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must process request errors
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
logging_mock = mocker.patch("logging.Logger.exception")
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SyncHttpClient().make_request("GET", "url")
|
||||
logging_mock.assert_called_once() # we do not check logging arguments
|
||||
|
||||
|
||||
def test_make_request_suppress_errors(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress request errors correctly
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
logging_mock = mocker.patch("logging.Logger.exception")
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SyncHttpClient().make_request("GET", "url", suppress_errors=True)
|
||||
with pytest.raises(Exception):
|
||||
SyncHttpClient(suppress_errors=True).make_request("GET", "url")
|
||||
|
||||
logging_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_make_request_session() -> None:
|
||||
"""
|
||||
must use session from arguments
|
||||
"""
|
||||
session_mock = MagicMock()
|
||||
client = SyncHttpClient()
|
||||
|
||||
client.make_request("GET", "url", session=session_mock)
|
||||
session_mock.request.assert_called_once_with(
|
||||
"GET", "url", params=None, data=None, headers=None, files=None, json=None,
|
||||
auth=None, timeout=client.timeout)
|
@ -40,7 +40,7 @@ def test_is_process_alive_unknown(remote_call: RemoteCall, mocker: MockerFixture
|
||||
response = requests.Response()
|
||||
response.status_code = 404
|
||||
mocker.patch("ahriman.core.status.web_client.WebClient.make_request",
|
||||
side_effect=requests.RequestException(response=response))
|
||||
side_effect=requests.HTTPError(response=response))
|
||||
|
||||
assert not remote_call.is_process_alive("id")
|
||||
|
||||
@ -62,9 +62,9 @@ def test_is_process_alive_http_error(remote_call: RemoteCall, mocker: MockerFixt
|
||||
response = requests.Response()
|
||||
response.status_code = 500
|
||||
mocker.patch("ahriman.core.status.web_client.WebClient.make_request",
|
||||
side_effect=requests.RequestException(response=response))
|
||||
side_effect=requests.HTTPError(response=response))
|
||||
|
||||
with pytest.raises(requests.RequestException):
|
||||
with pytest.raises(requests.HTTPError):
|
||||
remote_call.is_process_alive("id")
|
||||
|
||||
|
||||
|
@ -14,35 +14,35 @@ def test_send(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must send a message
|
||||
"""
|
||||
request_mock = mocker.patch("requests.post")
|
||||
request_mock = mocker.patch("ahriman.core.report.telegram.Telegram.make_request")
|
||||
report = Telegram("x86_64", configuration, "telegram")
|
||||
|
||||
report._send("a text")
|
||||
request_mock.assert_called_once_with(
|
||||
"POST",
|
||||
pytest.helpers.anyvar(str, strict=True),
|
||||
data={"chat_id": pytest.helpers.anyvar(str, strict=True), "text": "a text", "parse_mode": "HTML"},
|
||||
timeout=report.timeout)
|
||||
data={"chat_id": pytest.helpers.anyvar(str, strict=True), "text": "a text", "parse_mode": "HTML"})
|
||||
|
||||
|
||||
def test_send_failed(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise generic exception
|
||||
"""
|
||||
mocker.patch("requests.post", side_effect=Exception())
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
report = Telegram("x86_64", configuration, "telegram")
|
||||
|
||||
with pytest.raises(Exception):
|
||||
report._send("a text")
|
||||
|
||||
|
||||
def test_make_request_failed_http_error(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
def test_send_failed_http_error(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise http exception
|
||||
"""
|
||||
mocker.patch("requests.post", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
report = Telegram("x86_64", configuration, "telegram")
|
||||
|
||||
with pytest.raises(requests.exceptions.HTTPError):
|
||||
with pytest.raises(requests.HTTPError):
|
||||
report._send("a text")
|
||||
|
||||
|
||||
|
@ -87,20 +87,19 @@ def test_key_download(gpg: GPG, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must download the key from public server
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.get")
|
||||
requests_mock = mocker.patch("ahriman.core.sign.gpg.GPG.make_request")
|
||||
gpg.key_download("keyserver.ubuntu.com", "0xE989490C")
|
||||
requests_mock.assert_called_once_with(
|
||||
"https://keyserver.ubuntu.com/pks/lookup",
|
||||
params={"op": "get", "options": "mr", "search": "0xE989490C"},
|
||||
timeout=gpg.DEFAULT_TIMEOUT)
|
||||
"GET", "https://keyserver.ubuntu.com/pks/lookup",
|
||||
params=[("op", "get"), ("options", "mr"), ("search", "0xE989490C")])
|
||||
|
||||
|
||||
def test_key_download_failure(gpg: GPG, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must download the key from public server and log error if any (and raise it again)
|
||||
"""
|
||||
mocker.patch("requests.get", side_effect=requests.exceptions.HTTPError())
|
||||
with pytest.raises(requests.exceptions.HTTPError):
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
with pytest.raises(requests.HTTPError):
|
||||
gpg.key_download("keyserver.ubuntu.com", "0xE989490C")
|
||||
|
||||
|
||||
|
@ -5,7 +5,6 @@ import requests
|
||||
import requests_unixsocket
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import call as MockCall
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.status.web_client import WebClient
|
||||
@ -16,35 +15,6 @@ from ahriman.models.package import Package
|
||||
from ahriman.models.user import User
|
||||
|
||||
|
||||
def test_login_url(web_client: WebClient) -> None:
|
||||
"""
|
||||
must generate login url correctly
|
||||
"""
|
||||
assert web_client._login_url.endswith("/api/v1/login")
|
||||
|
||||
|
||||
def test_status_url(web_client: WebClient) -> None:
|
||||
"""
|
||||
must generate package status url correctly
|
||||
"""
|
||||
assert web_client._status_url.endswith("/api/v1/status")
|
||||
|
||||
|
||||
def test_logs_url(web_client: WebClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate logs url correctly
|
||||
"""
|
||||
assert web_client._logs_url(package_ahriman.base).endswith(f"/api/v1/packages/{package_ahriman.base}/logs")
|
||||
|
||||
|
||||
def test_package_url(web_client: WebClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate package status url correctly
|
||||
"""
|
||||
assert web_client._package_url("").endswith("/api/v1/packages")
|
||||
assert web_client._package_url(package_ahriman.base).endswith(f"/api/v1/packages/{package_ahriman.base}")
|
||||
|
||||
|
||||
def test_parse_address(configuration: Configuration) -> None:
|
||||
"""
|
||||
must extract address correctly
|
||||
@ -87,16 +57,16 @@ def test_login(web_client: WebClient, user: User, mocker: MockerFixture) -> None
|
||||
"""
|
||||
must login user
|
||||
"""
|
||||
web_client.user = user
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
web_client.auth = (user.username, user.password)
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
payload = {
|
||||
"username": user.username,
|
||||
"password": user.password
|
||||
}
|
||||
session = requests.Session()
|
||||
|
||||
web_client._login(requests.Session())
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True),
|
||||
params=None, json=payload, files=None)
|
||||
web_client._login(session)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), json=payload, session=session)
|
||||
|
||||
|
||||
def test_login_failed(web_client: WebClient, user: User, mocker: MockerFixture) -> None:
|
||||
@ -113,7 +83,7 @@ def test_login_failed_http_error(web_client: WebClient, user: User, mocker: Mock
|
||||
must suppress HTTP exception happened during login
|
||||
"""
|
||||
web_client.user = user
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client._login(requests.Session())
|
||||
|
||||
|
||||
@ -126,57 +96,50 @@ def test_login_skip(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
requests_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_make_request(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
def test_login_url(web_client: WebClient) -> None:
|
||||
"""
|
||||
must make HTTP request
|
||||
must generate login url correctly
|
||||
"""
|
||||
request_mock = mocker.patch("requests.Session.request")
|
||||
|
||||
assert web_client.make_request("GET", "/url1") is not None
|
||||
assert web_client.make_request("GET", "/url2", params=[("param", "value")]) is not None
|
||||
|
||||
assert web_client.make_request("POST", "/url3") is not None
|
||||
assert web_client.make_request("POST", "/url4", json={"param": "value"}) is not None
|
||||
# we don't want to put full descriptor here
|
||||
assert web_client.make_request("POST", "/url5", files={"file": "tuple"}) is not None
|
||||
|
||||
assert web_client.make_request("DELETE", "/url6") is not None
|
||||
|
||||
request_mock.assert_has_calls([
|
||||
MockCall("GET", f"{web_client.address}/url1", params=None, json=None, files=None),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("GET", f"{web_client.address}/url2", params=[("param", "value")], json=None, files=None),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", f"{web_client.address}/url3", params=None, json=None, files=None),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", f"{web_client.address}/url4", params=None, json={"param": "value"}, files=None),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("POST", f"{web_client.address}/url5", params=None, json=None, files={"file": "tuple"}),
|
||||
MockCall().raise_for_status(),
|
||||
MockCall("DELETE", f"{web_client.address}/url6", params=None, json=None, files=None),
|
||||
MockCall().raise_for_status(),
|
||||
])
|
||||
assert web_client._login_url().startswith(web_client.address)
|
||||
assert web_client._login_url().endswith("/api/v1/login")
|
||||
|
||||
|
||||
def test_make_request_failed(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
def test_status_url(web_client: WebClient) -> None:
|
||||
"""
|
||||
must make HTTP request
|
||||
must generate package status url correctly
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
with pytest.raises(Exception):
|
||||
web_client.make_request("GET", "url")
|
||||
assert web_client._status_url().startswith(web_client.address)
|
||||
assert web_client._status_url().endswith("/api/v1/status")
|
||||
|
||||
|
||||
def test_logs_url(web_client: WebClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate logs url correctly
|
||||
"""
|
||||
assert web_client._logs_url(package_ahriman.base).startswith(web_client.address)
|
||||
assert web_client._logs_url(package_ahriman.base).endswith(f"/api/v1/packages/{package_ahriman.base}/logs")
|
||||
|
||||
|
||||
def test_package_url(web_client: WebClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate package status url correctly
|
||||
"""
|
||||
assert web_client._package_url("").startswith(web_client.address)
|
||||
assert web_client._package_url("").endswith("/api/v1/packages")
|
||||
|
||||
assert web_client._package_url(package_ahriman.base).startswith(web_client.address)
|
||||
assert web_client._package_url(package_ahriman.base).endswith(f"/api/v1/packages/{package_ahriman.base}")
|
||||
|
||||
|
||||
def test_package_add(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must process package addition
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
payload = pytest.helpers.get_package_status(package_ahriman)
|
||||
|
||||
web_client.package_add(package_ahriman, BuildStatusEnum.Unknown)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True),
|
||||
params=None, json=payload, files=None)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), json=payload)
|
||||
|
||||
|
||||
def test_package_add_failed(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
@ -191,7 +154,7 @@ def test_package_add_failed_http_error(web_client: WebClient, package_ahriman: P
|
||||
"""
|
||||
must suppress HTTP exception happened during addition
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client.package_add(package_ahriman, BuildStatusEnum.Unknown)
|
||||
|
||||
|
||||
@ -213,7 +176,7 @@ def test_package_add_failed_http_error_suppress(web_client: WebClient, package_a
|
||||
must suppress HTTP exception happened during addition and don't log
|
||||
"""
|
||||
web_client.suppress_errors = True
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
logging_mock = mocker.patch("logging.exception")
|
||||
|
||||
web_client.package_add(package_ahriman, BuildStatusEnum.Unknown)
|
||||
@ -229,11 +192,11 @@ def test_package_get_all(web_client: WebClient, package_ahriman: Package, mocker
|
||||
response_obj._content = json.dumps(response).encode("utf8")
|
||||
response_obj.status_code = 200
|
||||
|
||||
requests_mock = mocker.patch("requests.Session.request", return_value=response_obj)
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request",
|
||||
return_value=response_obj)
|
||||
|
||||
result = web_client.package_get(None)
|
||||
requests_mock.assert_called_once_with("GET", f"{web_client.address}{web_client._package_url()}",
|
||||
params=None, json=None, files=None)
|
||||
requests_mock.assert_called_once_with("GET", web_client._package_url())
|
||||
assert len(result) == len(response)
|
||||
assert (package_ahriman, BuildStatusEnum.Unknown) in [(package, status.status) for package, status in result]
|
||||
|
||||
@ -250,7 +213,7 @@ def test_package_get_failed_http_error(web_client: WebClient, mocker: MockerFixt
|
||||
"""
|
||||
must suppress HTTP exception happened during status getting
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
assert web_client.package_get(None) == []
|
||||
|
||||
|
||||
@ -263,12 +226,11 @@ def test_package_get_single(web_client: WebClient, package_ahriman: Package, moc
|
||||
response_obj._content = json.dumps(response).encode("utf8")
|
||||
response_obj.status_code = 200
|
||||
|
||||
requests_mock = mocker.patch("requests.Session.request", return_value=response_obj)
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request",
|
||||
return_value=response_obj)
|
||||
|
||||
result = web_client.package_get(package_ahriman.base)
|
||||
requests_mock.assert_called_once_with("GET",
|
||||
f"{web_client.address}{web_client._package_url(package_ahriman.base)}",
|
||||
params=None, json=None, files=None)
|
||||
requests_mock.assert_called_once_with("GET", web_client._package_url(package_ahriman.base))
|
||||
assert len(result) == len(response)
|
||||
assert (package_ahriman, BuildStatusEnum.Unknown) in [(package, status.status) for package, status in result]
|
||||
|
||||
@ -278,7 +240,7 @@ def test_package_logs(web_client: WebClient, log_record: logging.LogRecord, pack
|
||||
"""
|
||||
must process log record
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
payload = {
|
||||
"created": log_record.created,
|
||||
"message": log_record.getMessage(),
|
||||
@ -286,8 +248,8 @@ def test_package_logs(web_client: WebClient, log_record: logging.LogRecord, pack
|
||||
}
|
||||
|
||||
web_client.package_logs(LogRecordId(package_ahriman.base, package_ahriman.version), log_record)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True),
|
||||
params=None, json=payload, files=None)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), json=payload,
|
||||
suppress_errors=True)
|
||||
|
||||
|
||||
def test_package_logs_failed(web_client: WebClient, log_record: logging.LogRecord, package_ahriman: Package,
|
||||
@ -306,7 +268,7 @@ def test_package_logs_failed_http_error(web_client: WebClient, log_record: loggi
|
||||
"""
|
||||
must pass exception during log post
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
log_record.package_base = package_ahriman.base
|
||||
with pytest.raises(Exception):
|
||||
web_client.package_logs(LogRecordId(package_ahriman.base, package_ahriman.version), log_record)
|
||||
@ -316,11 +278,10 @@ def test_package_remove(web_client: WebClient, package_ahriman: Package, mocker:
|
||||
"""
|
||||
must process package removal
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
|
||||
web_client.package_remove(package_ahriman.base)
|
||||
requests_mock.assert_called_once_with("DELETE", pytest.helpers.anyvar(str, True),
|
||||
params=None, json=None, files=None)
|
||||
requests_mock.assert_called_once_with("DELETE", pytest.helpers.anyvar(str, True))
|
||||
|
||||
|
||||
def test_package_remove_failed(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
@ -336,7 +297,7 @@ def test_package_remove_failed_http_error(web_client: WebClient, package_ahriman
|
||||
"""
|
||||
must suppress HTTP exception happened during removal
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client.package_remove(package_ahriman.base)
|
||||
|
||||
|
||||
@ -344,12 +305,12 @@ def test_package_update(web_client: WebClient, package_ahriman: Package, mocker:
|
||||
"""
|
||||
must process package update
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
|
||||
web_client.package_update(package_ahriman.base, BuildStatusEnum.Unknown)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), params=None, json={
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), json={
|
||||
"status": BuildStatusEnum.Unknown.value
|
||||
}, files=None)
|
||||
})
|
||||
|
||||
|
||||
def test_package_update_failed(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
@ -365,7 +326,7 @@ def test_package_update_failed_http_error(web_client: WebClient, package_ahriman
|
||||
"""
|
||||
must suppress HTTP exception happened during update
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client.package_update(package_ahriman.base, BuildStatusEnum.Unknown)
|
||||
|
||||
|
||||
@ -378,11 +339,11 @@ def test_status_get(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
response_obj._content = json.dumps(status.view()).encode("utf8")
|
||||
response_obj.status_code = 200
|
||||
|
||||
requests_mock = mocker.patch("requests.Session.request", return_value=response_obj)
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request",
|
||||
return_value=response_obj)
|
||||
|
||||
result = web_client.status_get()
|
||||
requests_mock.assert_called_once_with("GET", f"{web_client.address}{web_client._status_url}",
|
||||
params=None, json=None, files=None)
|
||||
requests_mock.assert_called_once_with("GET", web_client._status_url())
|
||||
assert result.architecture == "x86_64"
|
||||
|
||||
|
||||
@ -398,7 +359,7 @@ def test_status_get_failed_http_error(web_client: WebClient, mocker: MockerFixtu
|
||||
"""
|
||||
must suppress HTTP exception happened during web service status getting
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
assert web_client.status_get().architecture is None
|
||||
|
||||
|
||||
@ -406,12 +367,12 @@ def test_status_update(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must process service update
|
||||
"""
|
||||
requests_mock = mocker.patch("requests.Session.request")
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
|
||||
web_client.status_update(BuildStatusEnum.Unknown)
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), params=None, json={
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True), json={
|
||||
"status": BuildStatusEnum.Unknown.value
|
||||
}, files=None)
|
||||
})
|
||||
|
||||
|
||||
def test_status_update_self_failed(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
@ -426,5 +387,5 @@ def test_status_update_failed_http_error(web_client: WebClient, mocker: MockerFi
|
||||
"""
|
||||
must suppress HTTP exception happened during service update
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.exceptions.HTTPError())
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client.status_update(BuildStatusEnum.Unknown)
|
||||
|
@ -10,9 +10,9 @@ from typing import Any
|
||||
from unittest.mock import MagicMock, call as MockCall
|
||||
|
||||
from ahriman.core.exceptions import BuildError, CalledProcessError, OptionError, UnsafeRunError
|
||||
from ahriman.core.util import check_output, check_user, dataclass_view, enum_values, exception_response_text, \
|
||||
extract_user, filter_json, full_version, package_like, parse_version, partition, pretty_datetime, pretty_size, \
|
||||
safe_filename, srcinfo_property, srcinfo_property_list, trim_package, utcnow, walk
|
||||
from ahriman.core.util import check_output, check_user, dataclass_view, enum_values, extract_user, filter_json, \
|
||||
full_version, package_like, parse_version, partition, pretty_datetime, pretty_size, safe_filename, \
|
||||
srcinfo_property, srcinfo_property_list, trim_package, utcnow, walk
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.package_source import PackageSource
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
@ -204,25 +204,6 @@ def test_dataclass_view_without_none(package_ahriman: Package) -> None:
|
||||
assert Package.from_json(result) == package_ahriman
|
||||
|
||||
|
||||
def test_exception_response_text() -> None:
|
||||
"""
|
||||
must parse HTTP response to string
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
response_mock.text = "hello"
|
||||
exception = requests.exceptions.HTTPError(response=response_mock)
|
||||
|
||||
assert exception_response_text(exception) == "hello"
|
||||
|
||||
|
||||
def test_exception_response_text_empty() -> None:
|
||||
"""
|
||||
must parse HTTP exception with empty response to empty string
|
||||
"""
|
||||
exception = requests.exceptions.HTTPError(response=None)
|
||||
assert exception_response_text(exception) == ""
|
||||
|
||||
|
||||
def test_extract_user() -> None:
|
||||
"""
|
||||
must extract user from system environment
|
||||
|
@ -13,7 +13,7 @@ def test_asset_remove(github: Github, github_release: dict[str, Any], mocker: Mo
|
||||
"""
|
||||
must remove asset from the release
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
github.asset_remove(github_release, "asset_name")
|
||||
request_mock.assert_called_once_with("DELETE", "asset_url")
|
||||
|
||||
@ -22,7 +22,7 @@ def test_asset_remove_unknown(github: Github, github_release: dict[str, Any], mo
|
||||
"""
|
||||
must not fail if no asset found
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
github.asset_remove(github_release, "unknown_asset_name")
|
||||
request_mock.assert_not_called()
|
||||
|
||||
@ -32,11 +32,11 @@ def test_asset_upload(github: Github, github_release: dict[str, Any], mocker: Mo
|
||||
must upload asset to the repository
|
||||
"""
|
||||
mocker.patch("pathlib.Path.open")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
remove_mock = mocker.patch("ahriman.core.upload.github.Github.asset_remove")
|
||||
|
||||
github.asset_upload(github_release, Path("/root/new.tar.xz"))
|
||||
request_mock.assert_called_once_with("POST", "upload_url", params={"name": "new.tar.xz"},
|
||||
request_mock.assert_called_once_with("POST", "upload_url", params=[("name", "new.tar.xz")],
|
||||
data=pytest.helpers.anyvar(int),
|
||||
headers={"Content-Type": "application/x-tar"})
|
||||
remove_mock.assert_not_called()
|
||||
@ -47,7 +47,7 @@ def test_asset_upload_with_removal(github: Github, github_release: dict[str, Any
|
||||
must remove existing file before upload
|
||||
"""
|
||||
mocker.patch("pathlib.Path.open")
|
||||
mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
remove_mock = mocker.patch("ahriman.core.upload.github.Github.asset_remove")
|
||||
|
||||
github.asset_upload(github_release, Path("asset_name"))
|
||||
@ -65,10 +65,10 @@ def test_asset_upload_empty_mimetype(github: Github, github_release: dict[str, A
|
||||
mocker.patch("pathlib.Path.open")
|
||||
mocker.patch("ahriman.core.upload.github.Github.asset_remove")
|
||||
mocker.patch("mimetypes.guess_type", return_value=(None, None))
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
|
||||
github.asset_upload(github_release, Path("/root/new.tar.xz"))
|
||||
request_mock.assert_called_once_with("POST", "upload_url", params={"name": "new.tar.xz"},
|
||||
request_mock.assert_called_once_with("POST", "upload_url", params=[("name", "new.tar.xz")],
|
||||
data=pytest.helpers.anyvar(int),
|
||||
headers={"Content-Type": "application/octet-stream"})
|
||||
|
||||
@ -125,7 +125,7 @@ def test_release_create(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create release
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
github.release_create()
|
||||
request_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True),
|
||||
json={"tag_name": github.architecture, "name": github.architecture})
|
||||
@ -135,7 +135,7 @@ def test_release_get(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must get release
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
github.release_get()
|
||||
request_mock.assert_called_once_with("GET", pytest.helpers.anyvar(str, True))
|
||||
|
||||
@ -146,7 +146,8 @@ def test_release_get_empty(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
response = requests.Response()
|
||||
response.status_code = 404
|
||||
mocker.patch("ahriman.core.upload.github.Github._request", side_effect=requests.HTTPError(response=response))
|
||||
mocker.patch("ahriman.core.upload.github.Github.make_request",
|
||||
side_effect=requests.HTTPError(response=response))
|
||||
assert github.release_get() is None
|
||||
|
||||
|
||||
@ -154,7 +155,7 @@ def test_release_get_exception(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must re-raise non HTTPError exception
|
||||
"""
|
||||
mocker.patch("ahriman.core.upload.github.Github._request", side_effect=Exception())
|
||||
mocker.patch("ahriman.core.upload.github.Github.make_request", side_effect=Exception())
|
||||
with pytest.raises(Exception):
|
||||
github.release_get()
|
||||
|
||||
@ -164,7 +165,7 @@ def test_release_get_exception_http_error(github: Github, mocker: MockerFixture)
|
||||
must re-raise HTTPError exception with code differs from 404
|
||||
"""
|
||||
exception = requests.HTTPError(response=requests.Response())
|
||||
mocker.patch("ahriman.core.upload.github.Github._request", side_effect=exception)
|
||||
mocker.patch("ahriman.core.upload.github.Github.make_request", side_effect=exception)
|
||||
with pytest.raises(requests.HTTPError):
|
||||
github.release_get()
|
||||
|
||||
@ -173,7 +174,7 @@ def test_release_update(github: Github, github_release: dict[str, Any], mocker:
|
||||
"""
|
||||
must update release
|
||||
"""
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github._request")
|
||||
request_mock = mocker.patch("ahriman.core.upload.github.Github.make_request")
|
||||
github.release_update(github_release, "body")
|
||||
request_mock.assert_called_once_with("POST", "release_url", json={"body": "body"})
|
||||
|
||||
|
@ -1,11 +1,5 @@
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.core.upload.github import Github
|
||||
from ahriman.core.upload.http_upload import HttpUpload
|
||||
|
||||
|
||||
@ -40,24 +34,3 @@ def test_get_hashes_empty() -> None:
|
||||
must read empty body
|
||||
"""
|
||||
assert HttpUpload.get_hashes("") == {}
|
||||
|
||||
|
||||
def test_request(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must call request method
|
||||
"""
|
||||
response_mock = MagicMock()
|
||||
request_mock = mocker.patch("requests.Session.request", return_value=response_mock)
|
||||
|
||||
github._request("GET", "url", arg="arg")
|
||||
request_mock.assert_called_once_with("GET", "url", auth=github.auth, timeout=github.timeout, arg="arg")
|
||||
response_mock.raise_for_status.assert_called_once_with()
|
||||
|
||||
|
||||
def test_request_exception(github: Github, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must call request method and log HTTPError exception
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
with pytest.raises(requests.HTTPError):
|
||||
github._request("GET", "url", arg="arg")
|
||||
|
@ -24,7 +24,7 @@ def test_package_upload(remote_service: RemoteService, package_ahriman: Package,
|
||||
mocker.patch("pathlib.Path.is_file", return_value=False)
|
||||
file_mock = MagicMock()
|
||||
open_mock = mocker.patch("pathlib.Path.open", return_value=file_mock)
|
||||
upload_mock = mocker.patch("ahriman.core.upload.http_upload.HttpUpload._request")
|
||||
upload_mock = mocker.patch("ahriman.core.upload.http_upload.HttpUpload.make_request")
|
||||
filename = package_ahriman.packages[package_ahriman.base].filename
|
||||
|
||||
remote_service.sync(Path("local"), [package_ahriman])
|
||||
@ -43,7 +43,7 @@ def test_package_upload_with_signature(remote_service: RemoteService, package_ah
|
||||
mocker.patch("pathlib.Path.is_file", return_value=True)
|
||||
file_mock = MagicMock()
|
||||
open_mock = mocker.patch("pathlib.Path.open", return_value=file_mock)
|
||||
upload_mock = mocker.patch("ahriman.core.upload.http_upload.HttpUpload._request")
|
||||
upload_mock = mocker.patch("ahriman.core.upload.http_upload.HttpUpload.make_request")
|
||||
filename = package_ahriman.packages[package_ahriman.base].filename
|
||||
|
||||
remote_service.sync(Path("local"), [package_ahriman])
|
||||
|
@ -4,27 +4,6 @@ from ahriman.models.user import User
|
||||
from ahriman.models.user_access import UserAccess
|
||||
|
||||
|
||||
def test_from_option(user: User) -> None:
|
||||
"""
|
||||
must generate user from options
|
||||
"""
|
||||
user = replace(user, access=UserAccess.Read, packager_id=None, key=None)
|
||||
assert User.from_option(user.username, user.password) == user
|
||||
# default is read access
|
||||
user = replace(user, access=UserAccess.Full)
|
||||
assert User.from_option(user.username, user.password) != user
|
||||
assert User.from_option(user.username, user.password, user.access) == user
|
||||
|
||||
|
||||
def test_from_option_empty() -> None:
|
||||
"""
|
||||
must return nothing if settings are missed
|
||||
"""
|
||||
assert User.from_option(None, "") is None
|
||||
assert User.from_option("", None) is None
|
||||
assert User.from_option(None, None) is None
|
||||
|
||||
|
||||
def test_check_credentials_hash_password(user: User) -> None:
|
||||
"""
|
||||
must generate and validate user password
|
||||
|
Reference in New Issue
Block a user