Skip to content

Commit

Permalink
refactored text in docstrings, function names, and exceptions #88
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuarnaccia5 committed Jun 24, 2024
1 parent 70fa839 commit 1948249
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
11 changes: 5 additions & 6 deletions ldap_jwt_auth/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
31 changes: 15 additions & 16 deletions ldap_jwt_auth/core/maintenance.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions ldap_jwt_auth/routers/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
22 changes: 11 additions & 11 deletions test/unit/maintenance/test_maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -41,17 +41,17 @@ 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():
with patch("builtins.open", mock_open()) as mocked_open:
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"


Expand All @@ -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"
Expand All @@ -87,15 +87,15 @@ 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():
with patch("builtins.open", mock_open()) as mocked_open:
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"

0 comments on commit 1948249

Please sign in to comment.