better reload

This commit is contained in:
Evgenii Alekseev 2021-09-17 19:15:53 +03:00
parent ba483173af
commit 57f45fdc89
5 changed files with 27 additions and 7 deletions

View File

@ -152,7 +152,7 @@ class Setup(Handler):
:param architecture: repository architecture :param architecture: repository architecture
""" """
command = Setup.build_command(prefix, architecture) command = Setup.build_command(prefix, architecture)
Setup.SUDOERS_PATH.write_text(f"ahriman ALL=(ALL) NOPASSWD: {command} *\n") Setup.SUDOERS_PATH.write_text(f"ahriman ALL=(ALL) NOPASSWD: {command} *\n", encoding="utf8")
Setup.SUDOERS_PATH.chmod(0o400) # security! Setup.SUDOERS_PATH.chmod(0o400) # security!
@staticmethod @staticmethod

View File

@ -52,7 +52,7 @@ class User(Handler):
User.clear_user(auth_configuration, user) User.clear_user(auth_configuration, user)
if not args.remove: if not args.remove:
User.create_configuration(auth_configuration, user, salt, args.as_service) User.create_configuration(auth_configuration, user, salt, args.as_service)
User.write_configuration(configuration) User.write_configuration(auth_configuration)
if not args.no_reload: if not args.no_reload:
client = Application(architecture, configuration, no_report=False).repository.reporter client = Application(architecture, configuration, no_report=False).repository.reporter

View File

@ -72,7 +72,7 @@ class WebClient(Client):
""" """
:return: full url for web service to reload authentication module :return: full url for web service to reload authentication module
""" """
return f"{self.address}/status-api/v1/reload-auth" return f"{self.address}/service-api/v1/reload-auth"
@property @property
def _status_url(self) -> str: def _status_url(self) -> str:

View File

@ -35,6 +35,14 @@ class ReloadAuthView(BaseView):
:return: 204 on success :return: 204 on success
""" """
self.configuration.reload() self.configuration.reload()
self.request.app["validator"] = Auth.load(self.configuration)
try:
import aiohttp_security # type: ignore
self.request.app[aiohttp_security.api.AUTZ_KEY].validator =\
self.request.app["validator"] =\
Auth.load(self.configuration)
except (ImportError, KeyError):
self.request.app.logger.warning("could not update authentication module validator", exc_info=True)
raise
return HTTPNoContent() return HTTPNoContent()

View File

@ -2,14 +2,26 @@ from aiohttp.test_utils import TestClient
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
async def test_post(client: TestClient, mocker: MockerFixture) -> None: async def test_post(client_with_auth: TestClient, mocker: MockerFixture) -> None:
""" """
must call post request correctly must call post request correctly
""" """
mocker.patch("aiohttp_security.check_permission", return_value=True)
reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload") reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload")
load_mock = mocker.patch("ahriman.core.auth.auth.Auth.load") load_mock = mocker.patch("ahriman.core.auth.auth.Auth.load")
response = await client.post("/service-api/v1/reload-auth") response = await client_with_auth.post("/service-api/v1/reload-auth")
assert response.ok assert response.ok
reload_mock.assert_called_once() reload_mock.assert_called_once()
load_mock.assert_called_with(client.app["configuration"]) load_mock.assert_called_with(client_with_auth.app["configuration"])
async def test_post_no_auth(client: TestClient, mocker: MockerFixture) -> None:
"""
must call return 500 if no authorization module loaded
"""
reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload")
response = await client.post("/service-api/v1/reload-auth")
assert response.status == 500
reload_mock.assert_called_once()