Skip to content

Commit

Permalink
Merge pull request #159 from dinesh-aot/test_cases
Browse files Browse the repository at this point in the history
case file test cases
  • Loading branch information
nitheesh-aot authored Dec 4, 2024
2 parents a83b7f5 + 03d4098 commit bbd64f8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 52 deletions.
41 changes: 18 additions & 23 deletions compliance-api/src/compliance_api/services/case_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,29 @@ def change_case_file_status(cls, case_file_id, status_data):
if not case_file:
raise ResourceNotFoundError("Case file not found.")
status_enum = CaseFileStatusEnum(status_data.get("status"))
if (
case_file.case_file_status == CaseFileStatusEnum.OPEN
and status_enum == CaseFileStatusEnum.OPEN
):
raise UnprocessableEntityError("The case file is already in Open status.")
if (
case_file.case_file_status == CaseFileStatusEnum.CLOSED
and status_enum == CaseFileStatusEnum.CLOSED
):
raise UnprocessableEntityError("The case file is already in Open status.")
if status_enum == case_file.case_file_status:
raise UnprocessableEntityError(
f"The case file is already in {status_enum.value} status."
)
CaseFileModel.change_status(case_file_id, status_enum)


def _set_project_parameters(case_file):
"""Set project parameters."""
project_id = case_file.project_id
if project_id:
project = TrackService.get_project_by_id(project_id)
setattr(case_file, "authorization", project.get("ea_certificate", None))
setattr(case_file, "type", project.get("type").get("name"))
setattr(case_file, "sub_type", project.get("sub_type").get("name"))
setattr(case_file, "regulated_party", project.get("proponent").get("name"))
if not project_id:
project = UnapprovedProjectModel.get_by_case_file_id(case_file.id)
setattr(case_file, "authorization", project.authorization)
setattr(case_file, "type", project.type)
setattr(case_file, "sub_type", project.sub_type)
setattr(case_file, "regulated_party", project.regulated_party)
if case_file:
project_id = case_file.project_id
if project_id:
project = TrackService.get_project_by_id(project_id)
setattr(case_file, "authorization", project.get("ea_certificate", None))
setattr(case_file, "type", project.get("type").get("name"))
setattr(case_file, "sub_type", project.get("sub_type").get("name"))
setattr(case_file, "regulated_party", project.get("proponent").get("name"))
if not project_id:
project = UnapprovedProjectModel.get_by_case_file_id(case_file.id)
setattr(case_file, "authorization", project.authorization)
setattr(case_file, "type", project.type)
setattr(case_file, "sub_type", project.sub_type)
setattr(case_file, "regulated_party", project.regulated_party)
return case_file


Expand Down
87 changes: 58 additions & 29 deletions compliance-api/tests/integration/api/test_case_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,9 @@ def test_get_case_file_by_id(client, auth_header, mocker):
)
mock_get_user_by_guid.return_value = {
"ea_certificate": fake.word(),
"type": {
"name": fake.word()
},
"sub_type": {
"name": fake.word()
},
"proponent": {
"name": fake.word()
}
"type": {"name": fake.word()},
"sub_type": {"name": fake.word()},
"proponent": {"name": fake.word()},
}
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["project_id"] = 2
Expand Down Expand Up @@ -210,15 +204,9 @@ def test_get_case_file_by_number(client, auth_header_super_user, mocker):
)
mock_get_user_by_guid.return_value = {
"ea_certificate": fake.word(),
"type": {
"name": fake.word()
},
"sub_type": {
"name": fake.word()
},
"proponent": {
"name": fake.word()
}
"type": {"name": fake.word()},
"sub_type": {"name": fake.word()},
"proponent": {"name": fake.word()},
}
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
Expand All @@ -245,15 +233,9 @@ def test_case_file_update(client, auth_header_super_user, created_staff, mocker)
)
mock_get_user_by_guid.return_value = {
"ea_certificate": fake.word(),
"type": {
"name": fake.word()
},
"sub_type": {
"name": fake.word()
},
"proponent": {
"name": fake.word()
}
"type": {"name": fake.word()},
"sub_type": {"name": fake.word()},
"proponent": {"name": fake.word()},
}
# creating case file without officers or primary officer
case_file_data = copy.copy(CasefileScenario.default_value.value)
Expand Down Expand Up @@ -308,7 +290,9 @@ 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, auth_header_super_user, created_staff
):
"""Update as Viewer."""
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
Expand All @@ -323,7 +307,9 @@ def test_case_file_update_viewer_fails(client, auth_header, auth_header_super_us
assert result.status_code == HTTPStatus.FORBIDDEN


def test_case_file_update_with_primary(client, jwt, created_staff, auth_header_super_user):
def test_case_file_update_with_primary(
client, jwt, created_staff, auth_header_super_user
):
"""Update as primary."""
case_file_data = copy.copy(CasefileScenario.default_value.value)
case_file_data["case_file_number"] = fake.word()
Expand All @@ -341,3 +327,46 @@ def test_case_file_update_with_primary(client, jwt, created_staff, auth_header_s
url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('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):
"""Update as 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,
)

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")
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)
assert result.status_code == HTTPStatus.NO_CONTENT
result = client.patch(url, data=json.dumps({"status": "CLOSED"}), headers=headers)
assert result.status_code == HTTPStatus.UNPROCESSABLE_ENTITY


def test_case_file_delete(client, jwt, created_staff, auth_header_super_user):
"""Update as 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,
)

url = urljoin(API_BASE_URL, f"case-files/{created_result.json.get('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')}")
result = client.get(url, headers=auth_header_super_user)
assert result.status_code == HTTPStatus.NOT_FOUND

0 comments on commit bbd64f8

Please sign in to comment.