From b1a585c63848b4fd44dc48ca9345102a3c7131b6 Mon Sep 17 00:00:00 2001 From: dinesh Date: Fri, 13 Dec 2024 14:17:00 -0800 Subject: [PATCH 1/2] case file integration tests --- .../tests/integration/api/test_case_file.py | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/compliance-api/tests/integration/api/test_case_file.py b/compliance-api/tests/integration/api/test_case_file.py index 4629cc3..3ccda35 100644 --- a/compliance-api/tests/integration/api/test_case_file.py +++ b/compliance-api/tests/integration/api/test_case_file.py @@ -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( @@ -40,8 +40,10 @@ def mock_auth_service(mocker): "compliance_api.services.authorize_service.auth_service.AuthService.update_user_group" ) mock_update_user_group.return_value = {} + contains_role = mocker.patch("compliance_api.auth.jwt.contains_role") + contains_role.return_value = True - yield mock_get_user_by_guid, mock_update_user_group + yield mock_get_user_by_guid, mock_update_user_group, contains_role @pytest.fixture @@ -61,6 +63,15 @@ def test_get_case_file_initiation_options(client, auth_header): assert len(result.json) == 2 assert result.status_code == HTTPStatus.OK +def test_get_case_file_by_id(client, auth_header, created_staff): + """Get case file by id.""" + case_file_data = CasefileScenario.default_value.value + case_file_data["primary_officer_id"] = created_staff.id + created_case_file = CaseFileService.create(case_file_data) + + url = urljoin(API_BASE_URL, f"case-files/{created_case_file.id}") + result = client.get(url, headers=auth_header) + assert result.status_code == HTTPStatus.OK def test_create_case_file_without_file_number( client, auth_header_super_user, created_staff @@ -167,17 +178,20 @@ 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): From 46349608a2d55cd03b150656248e07aa2b77f857 Mon Sep 17 00:00:00 2001 From: dinesh Date: Tue, 31 Dec 2024 10:22:16 -0800 Subject: [PATCH 2/2] test cases for complaint --- .../tests/integration/api/test_case_file.py | 118 ++++++------------ .../tests/integration/api/test_complaint.py | 62 +++++++++ 2 files changed, 100 insertions(+), 80 deletions(-) diff --git a/compliance-api/tests/integration/api/test_case_file.py b/compliance-api/tests/integration/api/test_case_file.py index 3ccda35..85145a0 100644 --- a/compliance-api/tests/integration/api/test_case_file.py +++ b/compliance-api/tests/integration/api/test_case_file.py @@ -40,10 +40,8 @@ def mock_auth_service(mocker): "compliance_api.services.authorize_service.auth_service.AuthService.update_user_group" ) mock_update_user_group.return_value = {} - contains_role = mocker.patch("compliance_api.auth.jwt.contains_role") - contains_role.return_value = True - yield mock_get_user_by_guid, mock_update_user_group, contains_role + yield mock_get_user_by_guid, mock_update_user_group @pytest.fixture @@ -63,15 +61,6 @@ def test_get_case_file_initiation_options(client, auth_header): assert len(result.json) == 2 assert result.status_code == HTTPStatus.OK -def test_get_case_file_by_id(client, auth_header, created_staff): - """Get case file by id.""" - case_file_data = CasefileScenario.default_value.value - case_file_data["primary_officer_id"] = created_staff.id - created_case_file = CaseFileService.create(case_file_data) - - url = urljoin(API_BASE_URL, f"case-files/{created_case_file.id}") - result = client.get(url, headers=auth_header) - assert result.status_code == HTTPStatus.OK def test_create_case_file_without_file_number( client, auth_header_super_user, created_staff @@ -190,7 +179,9 @@ def test_get_case_files(client, auth_header, mocker): result = client.get(url, headers=auth_header) assert result.status_code == HTTPStatus.OK - filtered_case_file = next((case for case in result.json if case["id"] == created_case.id), None) + filtered_case_file = next( + (case for case in result.json if case["id"] == created_case.id), None + ) assert filtered_case_file is not None @@ -218,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 @@ -228,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 @@ -254,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']}", @@ -286,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']}", @@ -309,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 ) @@ -323,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 ) @@ -334,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 @@ -358,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) @@ -446,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), diff --git a/compliance-api/tests/integration/api/test_complaint.py b/compliance-api/tests/integration/api/test_complaint.py index 4244755..efa951e 100644 --- a/compliance-api/tests/integration/api/test_complaint.py +++ b/compliance-api/tests/integration/api/test_complaint.py @@ -1,9 +1,55 @@ """test suit for complaint.""" + from http import HTTPStatus from urllib.parse import urljoin +import pytest +from faker import Faker + +from compliance_api.services import CaseFileService, ComplaintService +from tests.utilities.factory_scenario import CasefileScenario, ComplaintScenario, StaffScenario + API_BASE_URL = "/api/" +fake = Faker() + + +@pytest.fixture +def mock_track_service(mocker): + """Fixture to mock TrackService methods.""" + mock_get_project_by_id = mocker.patch( + "compliance_api.services.epic_track_service.track_service.TrackService.get_project_by_id" + ) + mock_get_project_by_id.return_value = { + "abbreviation": fake.word(), + "ea_certificate": "", + "type": {"name": ""}, + "sub_type": {"name": ""}, + "proponent": {"name": ""}, + } + + yield mock_get_project_by_id + + +@pytest.fixture +def created_staff(mocker): + """Create staff.""" + user_data = StaffScenario.default_data.value + auth_user_guid = fake.word() + user_data["auth_user_guid"] = auth_user_guid + new_user = StaffScenario.create(user_data) + return new_user + + +@pytest.fixture +def created_case_file(mocker, created_staff, mock_track_service): + """Create case file.""" + contains_role = mocker.patch("compliance_api.auth.jwt.contains_role") + contains_role.return_value = True + case_file_data = CasefileScenario.default_value.value + case_file_data["primary_officer_id"] = created_staff.id + created_case_file = CaseFileService.create(case_file_data) + return created_case_file def test_get_complaint_sources(client, auth_header): @@ -13,3 +59,19 @@ def test_get_complaint_sources(client, auth_header): result = client.get(url, headers=auth_header) assert len(result.json) == 4 assert result.status_code == HTTPStatus.OK + + +def test_get_case_files_without_case_file_id_passed( + client, auth_header, mocker, mock_track_service, created_staff, created_case_file +): + """Get case files.""" + contains_role = mocker.patch("compliance_api.auth.jwt.contains_role") + contains_role.return_value = True + complaint_data = ComplaintScenario.complaint_default.value + complaint_data["lead_officer_id"] = created_staff.id + complaint_data["case_file_id"] = created_case_file.id + ComplaintService.create(complaint_data) + + result = client.get(urljoin(API_BASE_URL, "complaints"), headers=auth_header) + + assert result.status_code == HTTPStatus.OK