Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/dinesh-aot/EPIC.compliance
Browse files Browse the repository at this point in the history
… into COMP-298
  • Loading branch information
dinesh-aot committed Jan 3, 2025
2 parents 7727260 + 901daf3 commit a4442bc
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""inspection_requirement patches
"""inspection_requirement_patches
Revision ID: 791cc75df1c4
Revision ID: 6c3b6755e2dc
Revises: a684c47ac3e8
Create Date: 2025-01-02 15:02:27.066126
Create Date: 2025-01-02 18:17:21.124175
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '791cc75df1c4'
revision = '6c3b6755e2dc'
down_revision = 'a684c47ac3e8'
branch_labels = None
depends_on = None
Expand All @@ -34,21 +34,25 @@ def upgrade():
existing_type=sa.INTEGER(),
nullable=True,
existing_comment='Compliance finding of the requirement')
batch_op.create_index(batch_op.f('ix_inspection_requirements_inspection_id'), ['inspection_id'], unique=False)

with op.batch_alter_table('inspection_requirements_version', schema=None) as batch_op:
batch_op.add_column(sa.Column('sort_order', sa.Integer(), autoincrement=False, nullable=True, comment='The order of requirements'))
batch_op.add_column(sa.Column('sort_order_mod', sa.Boolean(), server_default=sa.text('false'), nullable=False))
batch_op.create_index(batch_op.f('ix_inspection_requirements_version_inspection_id'), ['inspection_id'], unique=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('inspection_requirements_version', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_inspection_requirements_version_inspection_id'))
batch_op.drop_column('sort_order_mod')
batch_op.drop_column('sort_order')

with op.batch_alter_table('inspection_requirements', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_inspection_requirements_inspection_id'))
batch_op.alter_column('compliance_finding_id',
existing_type=sa.INTEGER(),
nullable=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class InspectionReqDetailDocument(BaseModelVersioned):
"InspectionReqSourceDetail",
back_populates="documents",
lazy="select",
uselist=False
)
document_type = relationship(
"DocumentType", foreign_keys=[document_type_id], lazy="select"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ class InspectionReqSourceDetail(BaseModelVersioned):
"InspectionRequirement",
back_populates="requirement_source_details",
lazy="select",
uselist=False
)
requirement_source = relationship(
"RequirementSource", foreign_keys=[requirement_source_id], lazy="joined"
)
documents = relationship(
"InspectionReqDetailDocument",
back_populates="requirement_source_detail",
lazy="joined",
lazy="select",
)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from ..base_model import BaseModelVersioned
from ..base_model import BaseModelVersioned, db


class InspectionRequirement(BaseModelVersioned):
Expand All @@ -20,6 +20,7 @@ class InspectionRequirement(BaseModelVersioned):
Integer,
ForeignKey("inspections.id", name="inspection_requirements_inspection_id_fkey"),
nullable=False,
index=True,
comment="The unique identifier of the inspection",
)
summary = Column(String, nullable=False, comment="The summary of the requirement")
Expand Down Expand Up @@ -61,7 +62,7 @@ class InspectionRequirement(BaseModelVersioned):
requirement_source_details = relationship(
"InspectionReqSourceDetail",
back_populates="inspection_requirement",
lazy="joined",
lazy="select"
)

@classmethod
Expand All @@ -74,3 +75,12 @@ def create_requirement(cls, requirement_obj, session=None):
else:
requirement.save()
return requirement

@classmethod
def get_by_inspection_id(cls, inspection_id):
"""Get requirements by inspection id."""
return db.session.query(InspectionRequirement).filter(
InspectionRequirement.inspection_id == inspection_id,
InspectionRequirement.is_deleted.is_(False),
InspectionRequirement.is_active.is_(True)
).all()
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
class InspectionRequirements(Resource):
"""InspectionRequirements."""

@staticmethod
@ApiHelper.swagger_decorators(API, endpoint_description="Get all requirements by inspection")
@auth.require
@API.response(code=200, description="Success", model=[inspection_requirement_list_model])
def get(inspection_id):
"""Get requirements by inspection id."""
requirements = InspectionRequirementService.get_all(inspection_id)
return InspectionRequirementSchema(many=True).dump(requirements), HTTPStatus.OK

@staticmethod
@ApiHelper.swagger_decorators(API, endpoint_description="Create an inspection")
@API.expect(inspection_requirement_create_model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ class Meta(AutoSchemaBase.Meta): # pylint: disable=too-few-public-methods
model = InspectionReqSourceDetail
include_fk = True


documents = fields.List(fields.Nested(InspectionReqDetailDocSchema))
documents = fields.List(fields.Nested(InspectionReqDetailDocSchema))


class InspectionRequirementSchema(AutoSchemaBase): # pylint: disable=too-many-ancestors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
class InspectionRequirementService:
"""InspectionRequirementService."""

@classmethod
def get_all(cls, inspection_id):
"""Get all requirements by inspection id."""
return InspectionRequirementModel.get_by_inspection_id(inspection_id)

@classmethod
def create(cls, inspection_id, requirement_data):
"""Create inspection requirement."""
Expand Down
114 changes: 43 additions & 71 deletions compliance-api/tests/integration/api/test_case_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
fake = Faker()


@pytest.fixture
@pytest.fixture(scope="session")
def mock_auth_service(mocker):
"""Fixture to mock AuthService methods."""
mock_get_user_by_guid = mocker.patch(
Expand Down Expand Up @@ -167,17 +167,22 @@ def test_get_case_files_by_project_id(client, auth_header):
assert len(result.json) == 2


def test_get_case_files(client, auth_header):
def test_get_case_files(client, auth_header, mocker):
"""Get all case files."""
contains_role = mocker.patch("compliance_api.auth.jwt.contains_role")
contains_role.return_value = True
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["project_id"] = 2
case_file_data["case_file_number"] = fake.word()
CaseFileModel.create_case_file(case_file_data)
created_case = CaseFileService.create(case_file_data)
url = urljoin(API_BASE_URL, "case-files")
result = client.get(url, headers=auth_header)

assert result.status_code == HTTPStatus.OK
assert len(result.json) == 4
filtered_case_file = next(
(case for case in result.json if case["id"] == created_case.id), None
)
assert filtered_case_file is not None


def test_get_case_file_by_id(client, auth_header, mocker):
Expand All @@ -204,8 +209,10 @@ def test_get_case_file_by_id(client, auth_header, mocker):
assert result.json["case_file_status"] == CaseFileStatusEnum.OPEN.value


def test_get_case_file_officers(client, auth_header_super_user):
def test_get_case_file_officers(client, auth_header_super_user, mocker):
"""Create case file with file number."""
contains_role = mocker.patch("compliance_api.auth.jwt.contains_role")
contains_role.return_value = True
user_data = StaffScenario.default_data.value
auth_user_guid = fake.word()
user_data["auth_user_guid"] = auth_user_guid
Expand All @@ -214,13 +221,8 @@ def test_get_case_file_officers(client, auth_header_super_user):
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = new_user.id
case_file_data["officer_ids"] = [new_user.id]
result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
print(result.json)
url = urljoin(API_BASE_URL, f"case-files/{result.json.get('id')}/officers")
result = CaseFileService.create(case_file_data)
url = urljoin(API_BASE_URL, f"case-files/{result.id}/officers")
result = client.get(url, headers=auth_header_super_user)
assert result.status_code == HTTPStatus.OK
assert len(result.json) == 1
Expand All @@ -240,11 +242,7 @@ def test_get_case_file_by_number(client, auth_header_super_user, mocker):
}
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
result = CaseFileModel.create_case_file(case_file_data)
url = urljoin(
API_BASE_URL,
f"case-files/case-file-numbers/{case_file_data['case_file_number']}",
Expand Down Expand Up @@ -272,11 +270,7 @@ def test_case_file_update(client, auth_header_super_user, created_staff, mocker)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
case_file_data["project_description"] = "sample description"
created_result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
created_result = CaseFileModel.create_case_file(case_file_data)
url = urljoin(
API_BASE_URL,
f"case-files/case-file-numbers/{case_file_data['case_file_number']}",
Expand All @@ -295,7 +289,7 @@ def test_case_file_update(client, auth_header_super_user, created_staff, mocker)
case_file_data["primary_officer_id"] = new_user.id
case_file_data["officer_ids"] = [new_user.id]
case_file_data["project_description"] = "changed description"
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.patch(
url, data=json.dumps(case_file_data), headers=auth_header_super_user
)
Expand All @@ -309,7 +303,7 @@ def test_case_file_update(client, auth_header_super_user, created_staff, mocker)
# update the payload by making the officer list empty
case_file_data["primary_officer_id"] = new_user.id
case_file_data["officer_ids"] = []
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.patch(
url, data=json.dumps(case_file_data), headers=auth_header_super_user
)
Expand All @@ -320,19 +314,13 @@ def test_case_file_update(client, auth_header_super_user, created_staff, mocker)
assert len(officers) == 0


def test_case_file_update_viewer_fails(
client, auth_header, auth_header_super_user, created_staff
):
def test_case_file_update_viewer_fails(client, auth_header, created_staff):
"""Update as Viewer."""
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
created_result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
created_result = CaseFileModel.create_case_file(case_file_data)
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.patch(url, data=json.dumps(case_file_data), headers=auth_header)
assert result.status_code == HTTPStatus.FORBIDDEN

Expand All @@ -344,38 +332,31 @@ def test_case_file_update_with_primary(
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
created_result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)

created_result = CaseFileModel.create_case_file(case_file_data)
header = TokenJWTClaims.default.value
header["preferred_username"] = created_staff.auth_user_guid
headers = factory_auth_header(jwt=jwt, claims=header)

url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.patch(url, data=json.dumps(case_file_data), headers=headers)
assert result.status_code == HTTPStatus.OK


def test_case_file_close(client, jwt, created_staff, auth_header_super_user):
def test_case_file_close(client, jwt, created_staff, mocker):
"""Update as primary."""
contains_role = mocker.patch("compliance_api.auth.jwt.contains_role")
contains_role.return_value = True
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
created_result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
case_file_id = created_result.json.get("id")
case_file_number = created_result.json.get("case_file_number")
created_result = CaseFileService.create(case_file_data)
case_file_id = created_result.id
case_file_number = created_result.case_file_number
header = TokenJWTClaims.default.value
header["preferred_username"] = created_staff.auth_user_guid
headers = factory_auth_header(jwt=jwt, claims=header)
print(created_result)
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}/status")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}/status")
result = client.patch(url, data=json.dumps({"status": "OPEN"}), headers=headers)
assert result.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
result = client.patch(url, data=json.dumps({"status": "CLOSED"}), headers=headers)
Expand Down Expand Up @@ -432,48 +413,39 @@ def test_case_file_close(client, jwt, created_staff, auth_header_super_user):
assert cr_key is not None


def test_case_file_delete(client, jwt, created_staff, auth_header_super_user):
def test_case_file_delete(client, jwt, created_staff, mocker, auth_header_super_user):
"""Update as primary."""
contains_role = mocker.patch("compliance_api.auth.jwt.contains_role")
contains_role.return_value = True
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
created_result = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
created_result = CaseFileService.create(case_file_data)

url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.delete(url, headers=auth_header_super_user)
assert result.status_code == HTTPStatus.NO_CONTENT
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('id')}")
url = urljoin(API_BASE_URL, f"case-files/{created_result.id}")
result = client.get(url, headers=auth_header_super_user)
assert result.status_code == HTTPStatus.NOT_FOUND


def test_case_file_linking(client, jwt, created_staff, auth_header_super_user):
def test_case_file_linking(client, jwt, created_staff, auth_header_super_user, mocker):
"""Link case file."""
contains_role = mocker.patch("compliance_api.auth.jwt.contains_role")
contains_role.return_value = True
# Create source case file
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
source_case_file = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
source_case_file = CaseFileService.create(case_file_data)
# Create target case file
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
case_file_data["primary_officer_id"] = created_staff.id
target_case_file = client.post(
urljoin(API_BASE_URL, "case-files"),
data=json.dumps(case_file_data),
headers=auth_header_super_user,
)
print(target_case_file.json.get("id"))
url = urljoin(API_BASE_URL, f"case-files/{source_case_file.json.get('id')}/links")
post_data = {"link_case_file_id": target_case_file.json.get("id")}
target_case_file = CaseFileService.create(case_file_data)
url = urljoin(API_BASE_URL, f"case-files/{source_case_file.id}/links")
post_data = {"link_case_file_id": target_case_file.id}
result = client.post(
url,
data=json.dumps(post_data),
Expand Down
Loading

0 comments on commit a4442bc

Please sign in to comment.