add ability to reload authentication module

This commit is contained in:
2021-09-17 16:05:38 +03:00
parent 41731ca359
commit 16bb1403a1
15 changed files with 219 additions and 12 deletions

View File

@ -21,6 +21,7 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
args.password = "pa55w0rd"
args.access = UserAccess.Read
args.as_service = False
args.no_reload = False
args.remove = False
return args
@ -30,11 +31,13 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
must run command
"""
args = _default_args(args)
mocker.patch("pathlib.Path.mkdir")
get_auth_configuration_mock = mocker.patch("ahriman.application.handlers.User.get_auth_configuration")
create_configuration_mock = mocker.patch("ahriman.application.handlers.User.create_configuration")
write_configuration_mock = mocker.patch("ahriman.application.handlers.User.write_configuration")
create_user = mocker.patch("ahriman.application.handlers.User.create_user")
get_salt_mock = mocker.patch("ahriman.application.handlers.User.get_salt")
reload_mock = mocker.patch("ahriman.core.status.client.Client.reload_auth")
User.run(args, "x86_64", configuration, True)
get_auth_configuration_mock.assert_called_once()
@ -42,6 +45,7 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
create_user.assert_called_once()
get_salt_mock.assert_called_once()
write_configuration_mock.assert_called_once()
reload_mock.assert_called_once()
def test_run_remove(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
@ -50,14 +54,32 @@ def test_run_remove(args: argparse.Namespace, configuration: Configuration, mock
"""
args = _default_args(args)
args.remove = True
mocker.patch("pathlib.Path.mkdir")
get_auth_configuration_mock = mocker.patch("ahriman.application.handlers.User.get_auth_configuration")
create_configuration_mock = mocker.patch("ahriman.application.handlers.User.create_configuration")
write_configuration_mock = mocker.patch("ahriman.application.handlers.User.write_configuration")
reload_mock = mocker.patch("ahriman.core.status.client.Client.reload_auth")
User.run(args, "x86_64", configuration, True)
get_auth_configuration_mock.assert_called_once()
create_configuration_mock.assert_not_called()
write_configuration_mock.assert_called_once()
reload_mock.assert_called_once()
def test_run_no_reload(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command with no reload
"""
args = _default_args(args)
args.no_reload = True
mocker.patch("ahriman.application.handlers.User.get_auth_configuration")
mocker.patch("ahriman.application.handlers.User.create_configuration")
mocker.patch("ahriman.application.handlers.User.write_configuration")
reload_mock = mocker.patch("ahriman.core.status.client.Client.reload_auth")
User.run(args, "x86_64", configuration, True)
reload_mock.assert_not_called()
def test_clear_user(configuration: Configuration, user: MUser) -> None:

View File

@ -61,6 +61,13 @@ def test_get_self(client: Client) -> None:
assert client.get_self().status == BuildStatusEnum.Unknown
def test_reload_auth(client: Client) -> None:
"""
must process auth reload without errors
"""
client.reload_auth()
def test_remove(client: Client, package_ahriman: Package) -> None:
"""
must process remove without errors

View File

@ -230,6 +230,32 @@ def test_get_self_failed_http_error(web_client: WebClient, mocker: MockerFixture
assert web_client.get_self().status == BuildStatusEnum.Unknown
def test_reload_auth(web_client: WebClient, mocker: MockerFixture) -> None:
"""
must process auth reload
"""
requests_mock = mocker.patch("requests.Session.post")
web_client.reload_auth()
requests_mock.assert_called_with(pytest.helpers.anyvar(str, True))
def test_reload_auth_failed(web_client: WebClient, mocker: MockerFixture) -> None:
"""
must suppress any exception happened during auth reload
"""
mocker.patch("requests.Session.post", side_effect=Exception())
web_client.reload_auth()
def test_reload_auth_failed_http_error(web_client: WebClient, mocker: MockerFixture) -> None:
"""
must suppress any exception happened during removal
"""
mocker.patch("requests.Session.post", side_effect=requests.exceptions.HTTPError())
web_client.reload_auth()
def test_remove(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must process package removal

View File

@ -5,6 +5,7 @@ import pytest
from pytest_mock import MockerFixture
from ahriman.core.configuration import Configuration
from ahriman.core.exceptions import InitializeException
def test_from_path(mocker: MockerFixture) -> None:
@ -115,6 +116,7 @@ def test_getlist_single(configuration: Configuration) -> None:
"""
configuration.set_option("build", "test_list", "a")
assert configuration.getlist("build", "test_list") == ["a"]
assert configuration.getlist("build", "test_list") == ["a"]
def test_load_includes_missing(configuration: Configuration) -> None:
@ -170,6 +172,36 @@ def test_merge_sections_missing(configuration: Configuration) -> None:
assert configuration.get("build", "key") == "value"
def test_reload(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must reload configuration
"""
load_mock = mocker.patch("ahriman.core.configuration.Configuration.load")
merge_mock = mocker.patch("ahriman.core.configuration.Configuration.merge_sections")
configuration.reload()
load_mock.assert_called_once()
merge_mock.assert_called_once()
def test_reload_no_architecture(configuration: Configuration) -> None:
"""
must raise exception on reload if no architecture set
"""
configuration.architecture = None
with pytest.raises(InitializeException):
configuration.reload()
def test_reload_no_path(configuration: Configuration) -> None:
"""
must raise exception on reload if no path set
"""
configuration.path = None
with pytest.raises(InitializeException):
configuration.reload()
def test_set_option(configuration: Configuration) -> None:
"""
must set option correctly

View File

@ -0,0 +1,15 @@
from aiohttp.test_utils import TestClient
from pytest_mock import MockerFixture
async def test_post(client: TestClient, mocker: MockerFixture) -> None:
"""
must call post request correctly
"""
reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload")
load_mock = mocker.patch("ahriman.core.auth.auth.Auth.load")
response = await client.post("/service-api/v1/reload-auth")
assert response.ok
reload_mock.assert_called_once()
load_mock.assert_called_with(client.app["configuration"])

View File

@ -6,19 +6,19 @@ async def test_post(client: TestClient, mocker: MockerFixture) -> None:
"""
must call post request correctly
"""
add_mock = mocker.patch("ahriman.core.spawn.Spawn.packages_remove")
remove_mock = mocker.patch("ahriman.core.spawn.Spawn.packages_remove")
response = await client.post("/service-api/v1/remove", json={"packages": ["ahriman"]})
assert response.ok
add_mock.assert_called_with(["ahriman"])
remove_mock.assert_called_with(["ahriman"])
async def test_post_exception(client: TestClient, mocker: MockerFixture) -> None:
"""
must raise exception on missing packages payload
"""
add_mock = mocker.patch("ahriman.core.spawn.Spawn.packages_remove")
remove_mock = mocker.patch("ahriman.core.spawn.Spawn.packages_remove")
response = await client.post("/service-api/v1/remove")
assert response.status == 400
add_mock.assert_not_called()
remove_mock.assert_not_called()

View File

@ -5,6 +5,13 @@ from multidict import MultiDict
from ahriman.web.views.base import BaseView
def test_configuration(base: BaseView) -> None:
"""
must return configuration
"""
assert base.configuration
def test_service(base: BaseView) -> None:
"""
must return service