Skip to content

Commit

Permalink
fix(1139): refactor forest client validator as util service, refs: #1139
Browse files Browse the repository at this point in the history
  • Loading branch information
MCatherine1994 committed Feb 8, 2024
1 parent 490510d commit bb78831
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
LOGGER = logging.getLogger(__name__)


class ForestClientService():
class ForestClientIntegrationService():
"""
The class is used for making requests to get information from Forest Client API.
Api is located at BC API Service Portal: https://api.gov.bc.ca/devportal/api-directory/3179.
Expand Down Expand Up @@ -44,7 +44,7 @@ def find_by_client_number(self, p_client_number: str):
in next version.
"""
url = f"{self.api_clients_url}/findByClientNumber/{p_client_number}"
LOGGER.debug(f"ForestClientService find_by_client_number() - url: {url}")
LOGGER.debug(f"ForestClientIntegrationService find_by_client_number() - url: {url}")

try:
r = self.session.get(url, timeout=self.TIMEOUT)
Expand Down
2 changes: 1 addition & 1 deletion server/admin_management/api/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class AdminUserAccessResponse(BaseModel):


# ------------------------------------- Forest Client Validator ---------------------------------------- #
class ForestClientValidatorResponse(BaseModel):
class ForestClientValidationResponse(BaseModel):
clientNumber: str
clientName: str
clientStatusCode: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
from api.app import constants as famConstants
from api.app import schemas

from api.app.integration.forest_client_integration import ForestClientIntegrationService
from api.app.repositories.access_control_privilege_repository import (
AccessControlPrivilegeRepository,
)
from api.app.services.role_service import RoleService
from api.app.services.user_service import UserService
from api.app.services.validator_service import ForestClientValidator
from api.app.services.validator.forest_client_validator import (
forest_client_number_exists,
forest_client_active,
get_forest_client_status,
)
from api.app.utils import utils
from sqlalchemy.orm import Session

Expand Down Expand Up @@ -79,30 +84,24 @@ def create_access_control_privilege_many(
error_msg = "Invalid access control privilege request, missing forest client number."
utils.raise_http_exception(HTTPStatus.BAD_REQUEST, error_msg)

forest_client_validator = ForestClientValidator()
forest_client_integration_service = ForestClientIntegrationService()
for forest_client_number in request.forest_client_numbers:
# validate the forest client number
forest_client_validator_return = (
forest_client_validator.find_forest_client_number(
forest_client_number
)
forest_client_integration_service.find_by_client_number(forest_client_number)
)
if not forest_client_validator.forest_client_number_exists(
forest_client_validator_return
):
if not forest_client_number_exists(forest_client_validator_return):
error_msg = (
"Invalid access control privilege request. "
+ f"Forest client number {forest_client_number} does not exist."
)
utils.raise_http_exception(HTTPStatus.BAD_REQUEST, error_msg)

if not forest_client_validator.forest_client_active(
forest_client_validator_return
):
if not forest_client_active(forest_client_validator_return):
error_msg = (
"Invalid access control privilege request. "
+ f"Forest client number {forest_client_number} is not in active status: "
+ f"{forest_client_validator.get_forest_client(forest_client_validator_return)[famConstants.FOREST_CLIENT_STATUS['KEY']]}."
+ f"{get_forest_client_status(forest_client_validator_return)}."
)
utils.raise_http_exception(HTTPStatus.BAD_REQUEST, error_msg)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging
from typing import List, Union

from api.app.constants import FOREST_CLIENT_STATUS
from api.app.schemas import ForestClientValidationResponse


LOGGER = logging.getLogger(__name__)


def forest_client_number_exists(
forest_client_find_result: List[ForestClientValidationResponse],
) -> bool:
# Exact client number search - should only contain 1 result.
return len(forest_client_find_result) == 1


def forest_client_active(
forest_client_find_result: List[ForestClientValidationResponse],
) -> bool:
return (
(
forest_client_find_result[0][FOREST_CLIENT_STATUS["KEY"]]
== FOREST_CLIENT_STATUS["CODE_ACTIVE"]
)
if forest_client_number_exists(forest_client_find_result)
else False
)


def get_forest_client_status(
forest_client_find_result: List[ForestClientValidationResponse],
) -> Union[str, None]:
return (
forest_client_find_result[0][FOREST_CLIENT_STATUS["KEY"]]
if forest_client_number_exists(forest_client_find_result)
else None
)
55 changes: 0 additions & 55 deletions server/admin_management/api/app/services/validator_service.py

This file was deleted.

7 changes: 4 additions & 3 deletions server/admin_management/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from api.app.services.access_control_privilege_service import (
AccessControlPrivilegeService,
)
from api.app.services.validator_service import ForestClientValidator
from api.app.integration.forest_client_integration import ForestClientIntegrationService


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -203,5 +203,6 @@ def access_control_privilege_service(db_pg_session: Session):


@pytest.fixture(scope="function")
def forest_client_validator():
return ForestClientValidator()
def forest_client_integration_service():
return ForestClientIntegrationService()

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import logging

import pytest
from api.app.integration.forest_client_integration import ForestClientService
from api.app.integration.forest_client_integration import ForestClientIntegrationService

LOGGER = logging.getLogger(__name__)


class TestForestClientServiceClass(object):
class TestForestClientIntegrationServiceClass(object):
"""
Testing ForestClientService class with real remote API calls (TEST environment).
Testing ForestClientIntegrationService class with real remote API calls (TEST environment).
Initially Forest Client API returns also "acronyms" field but it disappears
some day. Since this field is not important at the moment, so test does not
includ3e it.
"""
fc_api: ForestClientService
fc_api: ForestClientIntegrationService
example_expected_valid_result = {
'clientNumber': '00000002',
'clientName': 'PENDING S & R BILLING',
Expand All @@ -22,7 +22,7 @@ class TestForestClientServiceClass(object):
}

def setup_class(self):
self.fc_api = ForestClientService()
self.fc_api = ForestClientIntegrationService()

def test_verify_init(self):
# Quick Verifying for init not empty
Expand Down
109 changes: 109 additions & 0 deletions server/admin_management/tests/services/test_forest_client_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import logging

from api.app.constants import FOREST_CLIENT_STATUS
from api.app.integration.forest_client_integration import ForestClientIntegrationService
from api.app.services.validator.forest_client_validator import (
forest_client_number_exists,
forest_client_active,
get_forest_client_status,
)
from tests.constants import (
TEST_FOREST_CLIENT_NUMBER,
TEST_NON_EXIST_FOREST_CLIENT_NUMBER,
TEST_INACTIVE_FOREST_CLIENT_NUMBER,
)

LOGGER = logging.getLogger(__name__)


def test_forest_client_number_exists(
forest_client_integration_service: ForestClientIntegrationService,
):
# find active forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_FOREST_CLIENT_NUMBER
)
)
# test forest_client_number_exists return true
assert forest_client_number_exists(forest_client_validator_return) is True

# find inactive forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_INACTIVE_FOREST_CLIENT_NUMBER
)
)
# test forest_client_number_exists return true
assert forest_client_number_exists(forest_client_validator_return) is True

# find non exist forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_NON_EXIST_FOREST_CLIENT_NUMBER
)
)
# test forest_client_number_exists return false
assert forest_client_number_exists(forest_client_validator_return) is False


def test_forest_client_active(forest_client_integration_service: ForestClientIntegrationService):
# find active forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_FOREST_CLIENT_NUMBER
)
)
# test forest_client_active return true
assert forest_client_active(forest_client_validator_return) is True

# find inactive forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_INACTIVE_FOREST_CLIENT_NUMBER
)
)
# test forest_client_active return false
assert forest_client_active(forest_client_validator_return) is False

# find non exist forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_NON_EXIST_FOREST_CLIENT_NUMBER
)
)
# test forest_client_active return false
assert forest_client_active(forest_client_validator_return) is False


def test_get_forest_client_status(
forest_client_integration_service: ForestClientIntegrationService,
):
# find active forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_FOREST_CLIENT_NUMBER
)
)
# test get_forest_client_status return forest client information
assert get_forest_client_status(
forest_client_validator_return
) == FOREST_CLIENT_STATUS.get("CODE_ACTIVE")

# find inactive forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_INACTIVE_FOREST_CLIENT_NUMBER
)
)
# test get_forest_client_status return forest client information
assert get_forest_client_status(forest_client_validator_return) == "DAC"

# find non exist forest client number
forest_client_validator_return = (
forest_client_integration_service.find_by_client_number(
TEST_NON_EXIST_FOREST_CLIENT_NUMBER
)
)
# test get_forest_client_status return None
assert get_forest_client_status(forest_client_validator_return) is None
Loading

0 comments on commit bb78831

Please sign in to comment.