From 194824993ccf9737629d7d145370989a6f92279d Mon Sep 17 00:00:00 2001 From: MatteoGuarnaccia5 Date: Mon, 24 Jun 2024 09:03:23 +0000 Subject: [PATCH] refactored text in docstrings, function names, and exceptions #88 --- ldap_jwt_auth/core/exceptions.py | 11 ++++---- ldap_jwt_auth/core/maintenance.py | 31 +++++++++++------------ ldap_jwt_auth/routers/maintenance.py | 14 +++++----- test/unit/maintenance/test_maintenance.py | 22 ++++++++-------- 4 files changed, 38 insertions(+), 40 deletions(-) diff --git a/ldap_jwt_auth/core/exceptions.py b/ldap_jwt_auth/core/exceptions.py index f05f2eb..f81741f 100644 --- a/ldap_jwt_auth/core/exceptions.py +++ b/ldap_jwt_auth/core/exceptions.py @@ -45,14 +45,13 @@ class UsernameMismatchError(Exception): """ -class InvalidMaintenanceFileFormat(Exception): +class InvalidMaintenanceFileError(Exception): """ - Exception raised when the maintenance state files do not have the correct format - or value types. + Exception raised when the maintenance state files do not have the correct format or value types. """ -class MissingMaintenanceFile(Exception): + +class MissingMaintenanceFileError(Exception): """ - Exception raised when the maintenance state file is missing or it's data cannot - be read. + Exception raised when the maintenance state file is missing or its data cannot be read. """ diff --git a/ldap_jwt_auth/core/maintenance.py b/ldap_jwt_auth/core/maintenance.py index 38b8f5e..5f86bb4 100644 --- a/ldap_jwt_auth/core/maintenance.py +++ b/ldap_jwt_auth/core/maintenance.py @@ -1,49 +1,48 @@ """ -Module for handling maintenance requests +Module for handling maintenance mode """ import json from pydantic import ValidationError -from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileFormat, MissingMaintenanceFile +from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError from ldap_jwt_auth.core.schemas import MaintenanceState, ScheduledMaintenanceState class Maintenance: """ - Class for managing maintenance requests. + Class for managing maintenance and scheduled maintenance states. """ - def get_maintenance(self) -> MaintenanceState: + def get_maintenance_state(self) -> MaintenanceState: """ - Return a schema for maintenance state of ims + Return the maintenance state of the system :return: Maintenance state schema :raises InvalidFileFormat: If the maintenance state file is incorrectly formatted + :raises MissingMaintenanceFileError: If the maintenance state file can not be found or read """ try: with open("maintenance/maintenance.json", "r", encoding='utf-8') as file: data = json.load(file) - maintenance: MaintenanceState = MaintenanceState(**data) - return maintenance + return MaintenanceState(**data) except IOError as exc: - print(exc) - raise MissingMaintenanceFile("Unable to find maintenance file") from exc + raise MissingMaintenanceFileError("Unable to find maintenance file") from exc except ValidationError as exc: - raise InvalidMaintenanceFileFormat("Maintenance file format is incorrect") from exc + raise InvalidMaintenanceFileError("Maintenance file format is incorrect") from exc - def get_scheduled_maintenance(self) -> ScheduledMaintenanceState: + def get_scheduled_maintenance_state(self) -> ScheduledMaintenanceState: """ - Return a schema for scheduled maintenance state + Return the scheduled maintenance state of the system :return: Scheduled maintenance state schema :raises InvalidFileFormat: If the scheduled maintenance state file is incorrectly formatted + :raises MissingMaintenanceFileError: If the scheduled maintenance state file can not be found or read """ try: with open("maintenance/scheduled_maintenance.json", "r", encoding='utf-8') as file: data = json.load(file) - maintenance: MaintenanceState = ScheduledMaintenanceState(**data) - return maintenance + return ScheduledMaintenanceState(**data) except IOError as exc: - raise MissingMaintenanceFile("Unable to find scheduled maintenance file") from exc + raise MissingMaintenanceFileError("Unable to find scheduled maintenance file") from exc except ValidationError as exc: - raise InvalidMaintenanceFileFormat("Scheduled maintenance file format is incorrect") from exc + raise InvalidMaintenanceFileError("Scheduled maintenance file format is incorrect") from exc diff --git a/ldap_jwt_auth/routers/maintenance.py b/ldap_jwt_auth/routers/maintenance.py index ce5f80f..2601369 100644 --- a/ldap_jwt_auth/routers/maintenance.py +++ b/ldap_jwt_auth/routers/maintenance.py @@ -6,7 +6,7 @@ from typing import Annotated from fastapi import APIRouter, Depends, HTTPException, status -from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileFormat, MissingMaintenanceFile +from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError from ldap_jwt_auth.core.maintenance import Maintenance from ldap_jwt_auth.core.schemas import MaintenanceState, ScheduledMaintenanceState @@ -23,12 +23,12 @@ def get_maintenance_state(maintenance: Annotated[Maintenance, Depends(Maintenanc logger.info("Getting maintenance state") try: - return maintenance.get_maintenance() + return maintenance.get_maintenance_state() - except MissingMaintenanceFile as exc: + except MissingMaintenanceFileError as exc: message = "Unable to find maintenance file" raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc - except InvalidMaintenanceFileFormat as exc: + except InvalidMaintenanceFileError as exc: message = "Maintenance file format is incorrect" raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc @@ -44,11 +44,11 @@ def get_scheduled_maintenance_state( # pylint: disable=missing-function-docstring logger.info("Getting scheduled maintenance state") try: - return maintenance.get_scheduled_maintenance() + return maintenance.get_scheduled_maintenance_state() - except MissingMaintenanceFile as exc: + except MissingMaintenanceFileError as exc: message = "Unable to find scheduled maintenance file" raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc - except InvalidMaintenanceFileFormat as exc: + except InvalidMaintenanceFileError as exc: message = "Scheduled maintenance file format is incorrect" raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc diff --git a/test/unit/maintenance/test_maintenance.py b/test/unit/maintenance/test_maintenance.py index 8cd9e3e..59b591d 100644 --- a/test/unit/maintenance/test_maintenance.py +++ b/test/unit/maintenance/test_maintenance.py @@ -5,7 +5,7 @@ import json import pytest -from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileFormat, MissingMaintenanceFile +from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError from ldap_jwt_auth.core.maintenance import Maintenance from unittest.mock import mock_open, patch @@ -22,7 +22,7 @@ def test_get_maintenance_state(): patch("json.load", return_value=mock_maintenance_data), ): maintenance = Maintenance() - response = maintenance.get_maintenance() + response = maintenance.get_maintenance_state() assert response.show == True assert response.message == "This is a test message" @@ -41,8 +41,8 @@ def test_get_maintenance_state_invalid_file(): ): maintenance = Maintenance() - with pytest.raises(InvalidMaintenanceFileFormat) as exc: - maintenance.get_maintenance() + with pytest.raises(InvalidMaintenanceFileError) as exc: + maintenance.get_maintenance_state() assert str(exc.value) == "Maintenance file format is incorrect" def test_get_maintenance_state_missing_file(): @@ -50,8 +50,8 @@ def test_get_maintenance_state_missing_file(): mocked_open.side_effect = IOError maintenance = Maintenance() - with pytest.raises(MissingMaintenanceFile) as exc: - maintenance.get_maintenance() + with pytest.raises(MissingMaintenanceFileError) as exc: + maintenance.get_maintenance_state() assert str(exc.value) == "Unable to find maintenance file" @@ -67,7 +67,7 @@ def test_get_scheduled_maintenance_state(): patch("json.load", return_value=mock_scheduled_maintenance_data), ): maintenance = Maintenance() - response = maintenance.get_scheduled_maintenance() + response = maintenance.get_scheduled_maintenance_state() assert response.show is True assert response.message == "This is a test message" @@ -87,8 +87,8 @@ def test_get_scheduled_maintenance_state_invalid_file(): ): maintenance = Maintenance() - with pytest.raises(InvalidMaintenanceFileFormat) as exc: - maintenance.get_scheduled_maintenance() + with pytest.raises(InvalidMaintenanceFileError) as exc: + maintenance.get_scheduled_maintenance_state() assert str(exc.value) == "Scheduled maintenance file format is incorrect" def test_get_scheduled_maintenance_state_missing_file(): @@ -96,6 +96,6 @@ def test_get_scheduled_maintenance_state_missing_file(): mocked_open.side_effect = IOError maintenance = Maintenance() - with pytest.raises(MissingMaintenanceFile) as exc: - maintenance.get_scheduled_maintenance() + with pytest.raises(MissingMaintenanceFileError) as exc: + maintenance.get_scheduled_maintenance_state() assert str(exc.value) == "Unable to find scheduled maintenance file"