generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate docs related functions into own module
- Loading branch information
Showing
4 changed files
with
178 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Module to manage the incident document used to track the details.""" | ||
|
||
from integrations.google_workspace import google_docs | ||
|
||
|
||
def update_incident_document_status(document_id, new_status="Closed"): | ||
"""Update the status of the incident document. | ||
Args: | ||
document_id (str): The ID of the document to update. | ||
new_status (str, optional): The new status to set. Defaults to "Closed". | ||
Returns: | ||
bool: True if the status was updated, False otherwise. | ||
""" | ||
# List of possible statuses to be replaced | ||
possible_statuses = [ | ||
"In Progress", | ||
"Open", | ||
"Ready to be Reviewed", | ||
"Reviewed", | ||
"Closed", | ||
] | ||
|
||
if new_status not in possible_statuses: | ||
raise ValueError(f"Invalid status: {new_status}") | ||
|
||
# Replace all possible statuses with the new status | ||
changes = [ | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": f"Status: {status}", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
} | ||
for status in possible_statuses | ||
if status != new_status | ||
] | ||
replies = google_docs.batch_update(document_id, changes)["replies"] | ||
return any( | ||
reply.get("replaceAllText", {}).get("occurrencesChanged", 0) > 0 | ||
for reply in replies | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from modules.incident import incident_document | ||
|
||
|
||
@patch("modules.incident.incident_document.google_docs") | ||
def test_update_incident_document_status_changes_occurred(google_docs_mock): | ||
document_id = "test_document_id" | ||
new_status = "In Progress" | ||
google_docs_mock.batch_update.return_value = { | ||
"replies": [ | ||
{"replaceAllText": {"occurrencesChanged": 1}}, | ||
{"replaceAllText": {}}, | ||
{"replaceAllText": {}}, | ||
{"replaceAllText": {}}, | ||
] | ||
} | ||
|
||
response = incident_document.update_incident_document_status( | ||
document_id, new_status | ||
) | ||
assert response is True | ||
|
||
expected_changes = [ | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Open", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": { | ||
"text": "Status: Ready to be Reviewed", | ||
"matchCase": "false", | ||
}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Reviewed", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Closed", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
] | ||
|
||
google_docs_mock.batch_update.assert_called_once_with(document_id, expected_changes) | ||
|
||
|
||
@patch("modules.incident.incident_document.google_docs") | ||
def test_update_incident_document_status_no_changes_occurred(google_docs_mock): | ||
document_id = "test_document_id" | ||
new_status = "In Progress" | ||
google_docs_mock.batch_update.return_value = { | ||
"replies": [ | ||
{"replaceAllText": {}}, | ||
{"replaceAllText": {}}, | ||
{"replaceAllText": {}}, | ||
{"replaceAllText": {}}, | ||
] | ||
} | ||
|
||
response = incident_document.update_incident_document_status( | ||
document_id, new_status | ||
) | ||
assert response is False | ||
|
||
expected_changes = [ | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Open", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": { | ||
"text": "Status: Ready to be Reviewed", | ||
"matchCase": "false", | ||
}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Reviewed", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": "Status: Closed", "matchCase": "false"}, | ||
"replaceText": f"Status: {new_status}", | ||
} | ||
}, | ||
] | ||
|
||
google_docs_mock.batch_update.assert_called_once_with(document_id, expected_changes) | ||
|
||
|
||
def test_update_incident_document_status_invalid_status(): | ||
document_id = "test_document_id" | ||
invalid_status = "Invalid Status" | ||
|
||
with pytest.raises(ValueError, match=f"Invalid status: {invalid_status}"): | ||
incident_document.update_incident_document_status(document_id, invalid_status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters