Skip to content

Commit

Permalink
feat: #1538 store audit records tests (#1605)
Browse files Browse the repository at this point in the history
Co-authored-by: Craig Yu <craig.yu93@gmail.com>
  • Loading branch information
ianliuwk1019 and craigyu authored Oct 2, 2024
1 parent a57dc45 commit 9542953
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def to_change_performer_user_details(
@staticmethod
def to_enduser_privliege_granted_details(
enduser_privliege_list: List[FamUserRoleAssignmentCreateRes]
) -> PrivilegeDetailsSchema:
) -> PrivilegeDetailsSchema | None:
if (len(enduser_privliege_list) == 0):
return

Expand Down Expand Up @@ -152,7 +152,7 @@ def to_enduser_privliege_revoked_details(
error_msg = (
"Revoke user permission encountered problem."
+ f"Unknown forest client number {forest_client_number} for "
+ "scoped permission {revoked_permission_role.role_name}."
+ f"scoped permission {revoked_permission_role.role_name}."
)
LOGGER.debug(error_msg)
raise HTTPException(
Expand Down
78 changes: 65 additions & 13 deletions server/backend/testspg/conftest.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
import logging
import os
import sys
from typing import List
from typing import List, Optional

import jwt
import pytest
import starlette
import testcontainers.compose
from Crypto.PublicKey import RSA
from fastapi.testclient import TestClient
from mock import patch
from mock_alchemy.mocking import UnifiedAlchemyMagicMock
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

import api.app.database as database
import api.app.jwt_validation as jwt_validation
import testspg.jwt_utils as jwt_utils
from api.app.constants import COGNITO_USERNAME_KEY, ERROR_CODE_TERMS_CONDITIONS_REQUIRED
from api.app.crud import crud_utils
from api.app.constants import (COGNITO_USERNAME_KEY,
ERROR_CODE_TERMS_CONDITIONS_REQUIRED, UserType)
from api.app.crud import crud_user, crud_utils
from api.app.main import apiPrefix, app
from api.app.models.model import FamUser
from api.app.routers.router_guards import (
enforce_bceid_terms_conditions_guard,
get_current_requester,
get_verified_target_user,
)
enforce_bceid_terms_conditions_guard, get_current_requester,
get_verified_target_user)
from api.app.schemas import RequesterSchema, TargetUserSchema
from testspg.constants import (
ACCESS_GRANT_FOM_DEV_CR_IDIR,
FOM_DEV_ADMIN_ROLE,
FOM_TEST_ADMIN_ROLE,
)
from api.app.schemas.fam_user import FamUserSchema
from testspg.constants import (ACCESS_GRANT_FOM_DEV_CR_IDIR,
FOM_DEV_ADMIN_ROLE, FOM_TEST_ADMIN_ROLE,
TEST_CREATOR)

LOGGER = logging.getLogger(__name__)
# the folder contains test docker-compose.yml, ours in the root directory
Expand Down Expand Up @@ -266,3 +265,56 @@ def auth_headers(test_rsa_key):
token = jwt_utils.create_jwt_token(test_rsa_key)
headers = jwt_utils.headers(token)
return headers


@pytest.fixture(scope="function")
def setup_new_user(db_pg_session: Session):
"""
New user setup for testing.
The fixture returns a function to be called with new user created based on
user_type, user_name and optionally if need to add cognito_user_id.
"""

def _setup_new_user(
user_type: UserType, user_name, user_guid, cognito_user_id: Optional[str] = None
) -> FamUser:
new_user_create = FamUserSchema(
**{
"user_type_code": user_type,
"user_name": user_name,
"user_guid": user_guid,
"create_user": TEST_CREATOR,
"first_name": "Fist",
"last_name": "Last",
"email": "test@test.com"
}
)

fam_user = crud_user.create_user(new_user_create, db_pg_session)
if cognito_user_id is not None:
# SqlAlchemy is a bit strange, need to use `.query()` to do the
# update() and query() again in order to get correct updated entity
# from session.
db_pg_session.query(FamUser).filter(
FamUser.user_id == fam_user.user_id
).update({FamUser.cognito_user_id: cognito_user_id})

fam_user = (
db_pg_session.query(FamUser)
.filter(FamUser.user_id == fam_user.user_id)
.one()
)

return fam_user

return _setup_new_user


@pytest.fixture(scope="function", autouse=True)
def mock_forest_client_integration_service():
# Mocked dependency class object
with patch(
"api.app.integration.forest_client_integration.ForestClientIntegrationService",
autospec=True,
) as m:
yield m.return_value # Very important to get instance of mocked class.
15 changes: 15 additions & 0 deletions server/backend/testspg/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
TEST_USER_ID = 1
TEST_USER_NAME_IDIR = "TEST_USER"
TEST_USER_GUID_IDIR = "MOCKEDGUID5D4ACA9FA901EE2C91CB3B" # this is a faked user guid
TEST_USER_FIREST_NAME = "FIRST NAME"
TEST_USER_LAST_NAME = "LAST NAME"
TEST_USER_EMAIL = "EMAIL"
TEST_REQUESTER = {
"cognito_user_id": "test-idir_e72a12c916afakedffae7@idir",
"user_name": TEST_USER_NAME_IDIR,
"user_guid": TEST_USER_GUID_IDIR,
"user_id": TEST_USER_ID,
"first_name": TEST_USER_FIREST_NAME,
"last_name": TEST_USER_LAST_NAME,
"email": TEST_USER_EMAIL,
}

TEST_NOT_EXIST_USER_TYPE = "NS"

Expand Down Expand Up @@ -77,6 +89,9 @@
FC_NUMBER_EXISTS_RECEIVERSHIP = "00169575"
FC_NUMBER_EXISTS_SUSPENDED = "00003643"

MOCK_FIND_CLIENT_00001011_RETURN = [{
'clientNumber': '00001011', 'clientName': 'AKIECA EXPLORERS LTD.', 'clientStatusCode': 'ACT', 'clientTypeCode': 'C'
}]

# --------------------- Testing Admin role level at token -------------- #
FOM_DEV_ADMIN_ROLE = "FOM_DEV_ADMIN"
Expand Down
Loading

0 comments on commit 9542953

Please sign in to comment.