mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-16 07:19:57 +00:00
feat: add reload command and api endpoints
This commit is contained in:
27
tests/ahriman/application/handlers/test_handler_reload.py
Normal file
27
tests/ahriman/application/handlers/test_handler_reload.py
Normal file
@ -0,0 +1,27 @@
|
||||
import argparse
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.application.handlers.reload import Reload
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.repository import Repository
|
||||
|
||||
|
||||
def test_run(args: argparse.Namespace, configuration: Configuration, repository: Repository,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command
|
||||
"""
|
||||
mocker.patch("ahriman.core.repository.Repository.load", return_value=repository)
|
||||
run_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.configuration_reload")
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
Reload.run(args, repository_id, configuration, report=False)
|
||||
run_mock.assert_called_once_with()
|
||||
|
||||
|
||||
def test_disallow_multi_architecture_run() -> None:
|
||||
"""
|
||||
must not allow multi architecture run
|
||||
"""
|
||||
assert not Reload.ALLOW_MULTI_ARCHITECTURE_RUN
|
@ -1563,6 +1563,35 @@ def test_subparsers_web_option_repository(parser: argparse.ArgumentParser) -> No
|
||||
assert args.repository == ""
|
||||
|
||||
|
||||
def test_subparsers_web_reload(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
web-reload command must imply architecture, lock, quiet, report, repository and unsafe
|
||||
"""
|
||||
args = parser.parse_args(["web-reload"])
|
||||
assert args.architecture == ""
|
||||
assert args.lock is None
|
||||
assert args.quiet
|
||||
assert not args.report
|
||||
assert args.repository == ""
|
||||
assert args.unsafe
|
||||
|
||||
|
||||
def test_subparsers_web_reload_option_architecture(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
web-reload command must correctly parse architecture list
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "web-reload"])
|
||||
assert args.architecture == ""
|
||||
|
||||
|
||||
def test_subparsers_web_reload_option_repository(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
web-reload command must correctly parse repository list
|
||||
"""
|
||||
args = parser.parse_args(["-r", "repo", "web-reload"])
|
||||
assert args.repository == ""
|
||||
|
||||
|
||||
def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
application must be run
|
||||
|
@ -97,6 +97,13 @@ def test_load_web_client_from_legacy_unix_socket(configuration: Configuration, d
|
||||
assert isinstance(Client.load(repository_id, configuration, database, report=True), WebClient)
|
||||
|
||||
|
||||
def test_configuration_reload(client: Client) -> None:
|
||||
"""
|
||||
must do nothing on configuration reload
|
||||
"""
|
||||
client.configuration_reload()
|
||||
|
||||
|
||||
def test_event_add(client: Client) -> None:
|
||||
"""
|
||||
must raise not implemented on event insertion
|
||||
|
@ -107,6 +107,55 @@ def test_status_url(web_client: WebClient) -> None:
|
||||
assert web_client._status_url().endswith("/api/v1/status")
|
||||
|
||||
|
||||
def test_configuration_reload(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reload configuration
|
||||
"""
|
||||
requests_mock = mocker.patch("ahriman.core.status.web_client.WebClient.make_request")
|
||||
web_client.configuration_reload()
|
||||
requests_mock.assert_called_once_with("POST", pytest.helpers.anyvar(str, True))
|
||||
|
||||
|
||||
def test_configuration_reload_failed(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress any exception happened during configuration reload
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
web_client.configuration_reload()
|
||||
|
||||
|
||||
def test_configuration_reload_failed_http_error(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress HTTP exception happened during configuration reload
|
||||
"""
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
web_client.configuration_reload()
|
||||
|
||||
|
||||
def test_configuration_reload_failed_suppress(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress any exception happened during configuration reload and don't log
|
||||
"""
|
||||
web_client.suppress_errors = True
|
||||
mocker.patch("requests.Session.request", side_effect=Exception())
|
||||
logging_mock = mocker.patch("logging.exception")
|
||||
|
||||
web_client.configuration_reload()
|
||||
logging_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_configuration_reload_failed_http_error_suppress(web_client: WebClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress HTTP exception happened during configuration reload and don't log
|
||||
"""
|
||||
web_client.suppress_errors = True
|
||||
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
|
||||
logging_mock = mocker.patch("logging.exception")
|
||||
|
||||
web_client.configuration_reload()
|
||||
logging_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_event_add(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create event
|
||||
|
1
tests/ahriman/web/schemas/test_configuration_schema.py
Normal file
1
tests/ahriman/web/schemas/test_configuration_schema.py
Normal file
@ -0,0 +1 @@
|
||||
# schema testing goes in view class tests
|
@ -0,0 +1,54 @@
|
||||
import pytest
|
||||
|
||||
from aiohttp.test_utils import TestClient
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.formatters.configuration_printer import ConfigurationPrinter
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.views.v1.service.config import ConfigView
|
||||
|
||||
|
||||
async def test_get_permission() -> None:
|
||||
"""
|
||||
must return correct permission for the request
|
||||
"""
|
||||
for method in ("GET",):
|
||||
request = pytest.helpers.request("", "", method)
|
||||
assert await ConfigView.get_permission(request) == UserAccess.Full
|
||||
for method in ("POST",):
|
||||
request = pytest.helpers.request("", "", method)
|
||||
assert await ConfigView.get_permission(request) == UserAccess.Full
|
||||
|
||||
|
||||
def test_routes() -> None:
|
||||
"""
|
||||
must return correct routes
|
||||
"""
|
||||
assert ConfigView.ROUTES == ["/api/v1/service/config"]
|
||||
|
||||
|
||||
async def test_get(client: TestClient) -> None:
|
||||
"""
|
||||
must get web configuration
|
||||
"""
|
||||
response_schema = pytest.helpers.schema_response(ConfigView.get)
|
||||
|
||||
response = await client.get("/api/v1/service/config")
|
||||
assert response.status == 200
|
||||
json = await response.json()
|
||||
assert json # check that it is not empty
|
||||
assert not response_schema.validate(json)
|
||||
|
||||
# check that there are no keys which have to be hidden
|
||||
assert not any(value["key"] in ConfigurationPrinter.HIDE_KEYS for value in json)
|
||||
|
||||
|
||||
async def test_post(client: TestClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package changes
|
||||
"""
|
||||
reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload")
|
||||
|
||||
response = await client.post("/api/v1/service/config")
|
||||
assert response.status == 204
|
||||
reload_mock.assert_called_once_with()
|
Reference in New Issue
Block a user