From bddd1f0370f2fad81cc51d2e27109664eca40fd2 Mon Sep 17 00:00:00 2001 From: Avery Lee Date: Fri, 17 May 2024 12:01:52 -0700 Subject: [PATCH 1/4] Adds test cases for the case views. --- src/dispatch/case/flows.py | 6 +- src/dispatch/case/models.py | 4 + tests/case/test_case_views.py | 145 ++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 tests/case/test_case_views.py diff --git a/src/dispatch/case/flows.py b/src/dispatch/case/flows.py index 05e6f6151270..983f1225903b 100644 --- a/src/dispatch/case/flows.py +++ b/src/dispatch/case/flows.py @@ -321,7 +321,7 @@ def case_update_flow( """Runs the case update flow.""" # we get the case case = get(db_session=db_session, case_id=case_id) - + print("in update flow") # we run the case assign role flow for the reporter case_assign_role_flow( case_id=case.id, @@ -351,6 +351,7 @@ def case_update_flow( ticket_flows.update_case_ticket(case=case, db_session=db_session) if case.status in [CaseStatus.escalated, CaseStatus.closed] and case.case_document: + print("updating the document bc either escalated or closed") # we update the document document_flows.update_document( document=case.case_document, project_id=case.project.id, db_session=db_session @@ -420,6 +421,7 @@ def case_escalated_status_flow( ): """Runs the case escalated transition flow.""" # we set the escalated_at time + print("we escalated") case.escalated_at = datetime.utcnow() db_session.add(case) db_session.commit() @@ -494,7 +496,7 @@ def reactivate_case_participants(case: Case, db_session: Session): ) -def case_active_status_flow(case: CaseStatus, db_session: Session) -> None: +def case_active_status_flow(case: Case, db_session: Session) -> None: """Runs the case active flow.""" # we un-archive the conversation if case.dedicated_channel: diff --git a/src/dispatch/case/models.py b/src/dispatch/case/models.py index 0c2b73c224b2..18dea4e731a3 100644 --- a/src/dispatch/case/models.py +++ b/src/dispatch/case/models.py @@ -174,10 +174,14 @@ def participant_observer(self, participants): @property def has_channel(self) -> bool: + if not self.conversation: + return False return True if not self.conversation.thread_id else False @property def has_thread(self) -> bool: + if not self.conversation: + return False return True if self.conversation.thread_id else False diff --git a/tests/case/test_case_views.py b/tests/case/test_case_views.py new file mode 100644 index 000000000000..1cbd696a9140 --- /dev/null +++ b/tests/case/test_case_views.py @@ -0,0 +1,145 @@ +def test_update_case_triage(session, case, user): + """Tests the update of a case to triage status.""" + from fastapi import FastAPI, BackgroundTasks + from fastapi.testclient import TestClient + from dispatch.case import service as case_service + from dispatch.case.enums import CaseStatus + from dispatch.case.models import CaseUpdate, CaseRead + from dispatch.case.views import update_case, router + + app = FastAPI() + app.include_router(router, prefix=f"/{case.project.organization.slug}/cases", tags=["cases"]) + client = TestClient(app) + + @app.get("/{case_id}", response_model=CaseRead) + async def views_update_case(background_tasks: BackgroundTasks): + case_in = CaseUpdate.from_orm(case) + case_in.status = CaseStatus.triage + return update_case( + db_session=session, + current_case=case, + organization=case.project.organization, + case_id=case.id, + case_in=case_in, + current_user=user, + background_tasks=background_tasks, + ) + + client.get(f"/{case.id}") + t_case = case_service.get(db_session=session, case_id=case.id) + assert t_case.status == CaseStatus.triage + + +def test_update_case_closed(session, case, user): + """Tests the update of a case to closed status.""" + from fastapi import FastAPI, BackgroundTasks + from fastapi.testclient import TestClient + from dispatch.case import service as case_service + from dispatch.case.enums import CaseStatus + from dispatch.case.models import CaseUpdate, CaseRead + from dispatch.case.views import update_case, router + + app = FastAPI() + app.include_router(router, prefix=f"/{case.project.organization.slug}/cases", tags=["cases"]) + client = TestClient(app) + + @app.get("/{case_id}", response_model=CaseRead) + async def views_update_case(background_tasks: BackgroundTasks): + case_in = CaseUpdate.from_orm(case) + case_in.status = CaseStatus.closed + return update_case( + db_session=session, + current_case=case, + organization=case.project.organization, + case_id=case.id, + case_in=case_in, + current_user=user, + background_tasks=background_tasks, + ) + + client.get(f"/{case.id}") + t_case = case_service.get(db_session=session, case_id=case.id) + assert t_case.status == CaseStatus.closed + + +def test_update_case_escalated(session, case, user): + """Tests the update of a case to escalated status. + + There is a known bug where cases can be escalated in the UI without creating an incident.""" + from fastapi import FastAPI, BackgroundTasks + from fastapi.testclient import TestClient + from dispatch.case.views import update_case, router + from dispatch.case.enums import CaseStatus + from dispatch.case.models import CaseUpdate, CaseRead + from dispatch.case import service as case_service + + app = FastAPI() + app.include_router(router, prefix=f"/{case.project.organization.slug}/cases", tags=["cases"]) + client = TestClient(app) + + @app.get("/{case_id}", response_model=CaseRead) + async def views_update_case(background_tasks: BackgroundTasks): + case_in = CaseUpdate.from_orm(case) + case_in.status = CaseStatus.escalated + return update_case( + db_session=session, + current_case=case, + organization=case.project.organization, + case_id=case.id, + case_in=case_in, + current_user=user, + background_tasks=background_tasks, + ) + + client.get(f"/{case.id}") + t_case = case_service.get(db_session=session, case_id=case.id) + assert t_case.status == CaseStatus.escalated + + +def test_case_escalated_create_incident(session, case, user, incident): + from fastapi import FastAPI, BackgroundTasks + from fastapi.testclient import TestClient + from dispatch.case.views import router, escalate_case + from dispatch.case.enums import CaseStatus + from dispatch.case import service as case_service + from dispatch.incident.enums import IncidentStatus + from dispatch.incident.models import IncidentCreate, IncidentRead + + # Initial setup. + case.case_type.project = case.project + case.case_priority.project = case.project + case.case_severity.project = case.project + + incident.project = case.project + incident.incident_type.project = case.project + incident.incident_priority.project = case.project + incident.incident_severity.project = case.project + + app = FastAPI() + app.include_router(router, prefix=f"/{case.project.organization.slug}/cases", tags=["cases"]) + + @app.get("/{case_id}/escalate", response_model=IncidentRead) + async def views_escalate_case(background_tasks: BackgroundTasks): + + incident_in = IncidentCreate.from_orm(incident) + incident_in.status = IncidentStatus.active + incident_in.title = case.title + + incident_out = escalate_case( + db_session=session, + current_case=case, + organization=case.project.organization, + incident_in=incident_in, + current_user=user, + background_tasks=background_tasks, + ) + + return incident_out + + client = TestClient(app) + client.get(f"/{case.id}/escalate").json() + + case_t = case_service.get(db_session=session, case_id=case.id) + assert case_t.status == CaseStatus.escalated + assert len(case_t.incidents) + assert case_t.incidents[0].title == case.title From 23560d77415321a7ae8bc7cc22ad755cb1f703b2 Mon Sep 17 00:00:00 2001 From: Avery Lee Date: Fri, 17 May 2024 12:04:30 -0700 Subject: [PATCH 2/4] Remove debugging print statements. --- src/dispatch/case/flows.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dispatch/case/flows.py b/src/dispatch/case/flows.py index 983f1225903b..22c511159bc1 100644 --- a/src/dispatch/case/flows.py +++ b/src/dispatch/case/flows.py @@ -321,7 +321,7 @@ def case_update_flow( """Runs the case update flow.""" # we get the case case = get(db_session=db_session, case_id=case_id) - print("in update flow") + # we run the case assign role flow for the reporter case_assign_role_flow( case_id=case.id, @@ -351,7 +351,6 @@ def case_update_flow( ticket_flows.update_case_ticket(case=case, db_session=db_session) if case.status in [CaseStatus.escalated, CaseStatus.closed] and case.case_document: - print("updating the document bc either escalated or closed") # we update the document document_flows.update_document( document=case.case_document, project_id=case.project.id, db_session=db_session @@ -421,7 +420,6 @@ def case_escalated_status_flow( ): """Runs the case escalated transition flow.""" # we set the escalated_at time - print("we escalated") case.escalated_at = datetime.utcnow() db_session.add(case) db_session.commit() From 25f401603cb13e1775c2d2fafd657279c6556a6a Mon Sep 17 00:00:00 2001 From: Avery Lee Date: Fri, 17 May 2024 12:11:41 -0700 Subject: [PATCH 3/4] Adds docstring to test. --- tests/case/test_case_views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/case/test_case_views.py b/tests/case/test_case_views.py index 1cbd696a9140..c80b113d90d3 100644 --- a/tests/case/test_case_views.py +++ b/tests/case/test_case_views.py @@ -97,6 +97,7 @@ async def views_update_case(background_tasks: BackgroundTasks): def test_case_escalated_create_incident(session, case, user, incident): + """Tests the escalation of a case to an incident.""" from fastapi import FastAPI, BackgroundTasks from fastapi.testclient import TestClient from dispatch.case.views import router, escalate_case From 69688886da2f04f0b8b9ec5aa547e14d89a5e118 Mon Sep 17 00:00:00 2001 From: Avery Lee Date: Fri, 17 May 2024 12:25:38 -0700 Subject: [PATCH 4/4] Remove unused json call. --- tests/case/test_case_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/case/test_case_views.py b/tests/case/test_case_views.py index c80b113d90d3..69e1aac183a2 100644 --- a/tests/case/test_case_views.py +++ b/tests/case/test_case_views.py @@ -138,7 +138,7 @@ async def views_escalate_case(background_tasks: BackgroundTasks): return incident_out client = TestClient(app) - client.get(f"/{case.id}/escalate").json() + client.get(f"/{case.id}/escalate") case_t = case_service.get(db_session=session, case_id=case.id) assert case_t.status == CaseStatus.escalated