def test_remove_paralegal_from_case(add_user_to_case, remove_user_from_case): user = UserFactory() issue = IssueFactory(is_case_sent=True, paralegal=user, lawyer=UserFactory()) remove_user_from_case.assert_not_called() issue.paralegal = UserFactory() issue.save() remove_user_from_case.assert_called_once_with(user, issue)
def test_get_next_fileref__with_prior_filerefs__over_9999(): IssueFactory(topic=CaseTopic.REPAIRS, fileref="R9999") IssueFactory(topic=CaseTopic.REPAIRS, fileref="R0001") IssueFactory(topic=CaseTopic.BONDS, fileref="B0004") IssueFactory(topic=CaseTopic.BONDS, fileref="B0056") issue = IssueFactory(topic=CaseTopic.REPAIRS) assert issue.fileref == "R10000"
def test_slack_not_dispatched_when_already_sent(mock_async): """ Ensure Slack message not sent twice. """ mock_async.assert_not_called() issue = IssueFactory(is_case_sent=True) issue.save() # Ensure only email task was dispatched mock_async.assert_has_calls([mock.call(send_issue_slack, str(issue.pk))])
def test_all_tasks_dispatched_when_complete(mock_async): """ Ensure all tasks are triggered when a new issue is first completed. """ mock_async.assert_not_called() issue = IssueFactory(is_alert_sent=False, is_case_sent=False) issue.save() # Ensure tasks were dispatched mock_async.assert_has_calls([mock.call(send_issue_slack, str(issue.pk))], )
def test_build_email_address(settings): settings.EMAIL_DOMAIN = "fake.anikalegal.com" issue_pk = "0e62ccc2-b9ee-4a07-979a-da8a9d450404" issue = IssueFactory(id=issue_pk) issue_addr = build_clerk_address(issue) expected = "Anika Legal <*****@*****.**>" assert issue_addr == expected
def test_add_user_to_case(mock_api): """Check service function gives user write permissions to existing case folder.""" user = UserFactory() issue = IssueFactory() add_user_to_case(user, issue) mock_api.folder.create_permissions.assert_called_once_with( f"cases/{issue.id}", "write", [user.email])
def test_set_up_new_case(mock_api): """Check service function creates new case folder and places it inside parent folder.""" issue = IssueFactory() mock_api.folder.get_child_if_exists.side_effect = [None, {"id": "abcefg"}] set_up_new_case(issue) mock_api.folder.copy.assert_called_once_with(TEMPLATE_PATHS[issue.topic], str(issue.id), settings.CASES_FOLDER_ID)
def test_remove_user_from_case_A(mock_api): """Check service function when there are no permissions on the case folder.""" user = UserFactory() issue = IssueFactory() mock_api.folder.list_permissions.return_value = None remove_user_from_case(user, issue) mock_api.folder.list_permissions.assert_called_once_with( f"cases/{issue.id}") mock_api.folder.delete_permission.assert_not_called()
def test_file_upload_create(client): """ User can upload a file which is associated with an issue. """ issue = IssueFactory() list_url = reverse("upload-list") assert FileUpload.objects.count() == 0 f = get_dummy_file("doc.pdf") resp = client.post(list_url, {"file": f, "issue": str(issue.id)}) assert resp.data["id"] assert resp.data["issue"] == issue.id assert FileUpload.objects.count() == 1
def test_get_case_folder_info_A(mock_api): """Check service function when the case folder doesn't exist.""" issue = IssueFactory() mock_api.folder.get_children.return_value = [] mock_api.folder.get.return_value = None documents, url = get_case_folder_info(issue) mock_api.folder.get_children.assert_called_once_with(f"cases/{issue.id}") mock_api.folder.get.assert_called_once_with(f"cases/{issue.id}") assert documents == [] assert url == None
def test_slack_issue_message(monkeypatch): """ Ensure we call Slack without anything exploding """ mock_send_msg = mock.Mock() monkeypatch.setattr(slack, "send_slack_message", mock_send_msg) issue = IssueFactory() assert not issue.is_alert_sent mock_send_msg.assert_not_called() send_issue_slack(issue.pk) assert mock_send_msg.call_count == 1 issue = Issue.objects.get(pk=issue.id) assert issue.is_alert_sent
def test_remove_paralegal_from_group( core_add_user_to_case, core_remove_user_from_case, accounts_tear_down_coordinator, accounts_set_up_coordinator, accounts_remove_user_from_case, ): user = UserFactory() group = Group.objects.get(name=CaseGroups.PARALEGAL) user.groups.add(group) issue = IssueFactory(is_case_sent=True, paralegal=user) accounts_remove_user_from_case.assert_not_called() user.groups.remove(group) accounts_remove_user_from_case.assert_called_once_with(user, issue)
def test_remove_user_from_case_B(mock_api): """Check service function when there are permissions on the case folder that don't belong to our user.""" user = UserFactory(email="*****@*****.**") issue = IssueFactory() mock_api.folder.list_permissions.return_value = [("666", { "user": { "email": "*****@*****.**" } })] remove_user_from_case(user, issue) mock_api.folder.list_permissions.assert_called_once_with( f"cases/{issue.id}") mock_api.folder.delete_permission.assert_not_called()
def test_get_case_folder_info_B(mock_api): """Check service function when there is a proper case folder.""" issue = IssueFactory() mock_api.folder.get_children.return_value = [{ "name": "War and Peace", "webUrl": "https://en.wikipedia.org/wiki/War_and_Peace", }] mock_api.folder.get.return_value = { "webUrl": "https://en.wikipedia.org/wiki/Leo_Tolstoy" } documents, url = get_case_folder_info(issue) mock_api.folder.get_children.assert_called_once_with(f"cases/{issue.id}") mock_api.folder.get.assert_called_once_with(f"cases/{issue.id}") assert documents[0][0] == "War and Peace" assert url == "https://en.wikipedia.org/wiki/Leo_Tolstoy"
def test_ingest_email__with_success(settings, test_data): expected_parsed, received_data = ( test_data["expected_parsed"], test_data["received_data"], ) settings.EMAIL_DOMAIN = "fake.anikalegal.com" issue_pk = "0e62ccc2-b9ee-4a07-979a-da8a9d450404" IssueFactory(id=issue_pk) email = EmailFactory(state=EmailState.RECEIVED, received_data=received_data, issue=None) receive_email_task(email.pk) email.refresh_from_db() assert email.state == EmailState.INGESTED assert str(email.issue_id) == issue_pk assert email.from_address == expected_parsed["from_address"] assert email.to_address == expected_parsed["to_address"] assert email.cc_addresses == expected_parsed["cc_addresses"] assert email.subject == expected_parsed["subject"] assert email.text == expected_parsed["text"]
def test_email_thread_aggregation(): issue = IssueFactory() emails = [ EmailFactory(issue=issue, state=state, subject=subject, created_at=created_at) for state, subject, created_at in THREADED_EMAILS ] threads = _get_email_threads(issue) # Thread 2 - r00956-case-closure assert threads[2].subject == "R00956 Case Closure" assert threads[2].slug == "r00956-case-closure" assert threads[2].emails == list(reversed(emails[0:8])) # Thread 1 - legal-advice assert threads[1].subject == "Legal Advice" assert threads[1].slug == "legal-advice" assert threads[1].emails == list(reversed(emails[8:12])) # Thread 0 - a-quick-question assert threads[0].subject == "A quick question" assert threads[0].slug == "a-quick-question" assert threads[0].emails == list(reversed(emails[12:13]))
def test_get_next_fileref__first_one(): issue = IssueFactory(topic=CaseTopic.REPAIRS) assert issue.fileref == "R0001"
def test_issue_actionstep(mock_api, mock_send_slack_message): # Create the mock Issue client = ClientFactory( first_name="Keith", last_name="Leonardo", email="*****@*****.**", phone_number="0412348793", ) TenancyFactory(client=client) answers = {"FAVOURITE_ANIMAL": "Cow", "BEST_TRICK": "I can do a backflip."} issue = IssueFactory(answers=answers, client=client) # Set mock return values for all API calls made in _send_issue_actionstep() participant = { "id": 11, "displayName": "Leonardo, Keith", "firstName": "Keith", "lastName": "Leonardo", "phone1Number": None, # Business "phone2Number": None, # Mobile "phone3Number": None, # Home "email": "*****@*****.**", } filenote = { "id": 1234, "enteredTimestamp": "2020-07-02T17:35:36+12:00", "text": "blah blah blah", "enteredBy": "Leonardo, Keith", "source": "User", "noteTimestamp": "2020-07-02T17:35:22+12:00", "links": { "action": "65", "participant": "11" }, } action = { "id": 65, "name": "Fakey McFakeFake", "reference": "R0123", "priority": 0, "status": "Closed", "statusTimestamp": "2020-07-09T19:34:10+12:00", "isBillableOverride": None, "createdTimestamp": "2020-07-02", "modifiedTimestamp": "2020-07-11T07:30:51+12:00", "isDeleted": "F", "deletedBy": None, "deletedTimestamp": None, "isFavorite": "F", "overrideBillingStatus": None, "lastAccessTimestamp": "2020-07-30T16:41:33+12:00", "links": { "assignedTo": "11", "actionType": "28", "primaryParticipants": ["159"], "relatedActions": None, }, } fileschema = { "id": 2531, "name": "test", "modifiedTimestamp": "2020-07-30T16:47:48+12:00", "status": "uploaded", "keywords": None, "summary": None, "checkedOutTimestamp": None, "fileType": None, "checkedOutTo": None, "checkedOutEtaTimestamp": None, "fileSize": 17, "extension": ".txt", "sharepointUrl": "https://www.example.com/test.txt", "fileName": "65_20200730164748_test.txt", "isDeleted": "F", "file": "DL::Actions::65::2531", "links": { "action": "65", "checkedOutBy": None, "folder": None, "createdBy": "11", }, } file_upload_status = {"id": "testid", "status": "Uploaded"} mock_api.return_value.participants.get_by_email.return_value = participant mock_api.return_value.participants.get_or_create.return_value = [ participant, True ] # For testing both if a issue has an action or not mock_api.return_value.filenotes.list_by_text_match.side_effect = [[ filenote ], []] mock_api.return_value.actions.get_next_ref.return_value = action[ "reference"] mock_api.return_value.actions.get.return_value = action mock_api.return_value.actions.create.return_value = action mock_api.return_value.files.upload.return_value = file_upload_status # Test when issue has action _send_issue_actionstep(issue.pk) res_issue = Issue.objects.get(pk=issue.id) assert mock_api.return_value.actions.create.call_count == 0 assert mock_api.return_value.files.upload.call_count == 1 assert res_issue.is_case_sent mock_api.reset_mock() # Test when issue has no action _send_issue_actionstep(issue.pk) res_issue = Issue.objects.get(pk=issue.id) assert mock_api.return_value.actions.create.call_count == 1 assert mock_api.return_value.files.upload.call_count == 1 assert res_issue.is_case_sent