def test_get_matter_by_uid_calls_get_with_uri_and_params( self, mock_connection, successful_response): mock_connection.get.return_value = successful_response service = LegalHoldService(mock_connection) service.get_matter_by_uid("LEGAL_HOLD_UID") uri = "{}/{}".format(LEGAL_HOLD_URI, "LEGAL_HOLD_UID") mock_connection.get.assert_called_once_with(uri)
def test_add_to_matter_calls_post_with_expected_url_and_params( self, mock_connection): service = LegalHoldService(mock_connection) service.add_to_matter("user", "legal") expected_data = {"legalHoldUid": "legal", "userUid": "user"} mock_connection.post.assert_called_once_with( "/api/LegalHoldMembership", json=expected_data)
def _init_services(main_connection, main_auth): alert_rules_key = u"FedObserver-API_URL" alerts_key = u"AlertService-API_URL" file_events_key = u"FORENSIC_SEARCH-API_URL" preservation_data_key = u"PRESERVATION-DATA-SERVICE_API-URL" employee_case_mgmt_key = u"employeecasemanagement-API_URL" kv_prefix = u"simple-key-value-store" audit_logs_key = u"AUDIT-LOG_API-URL" kv_connection = Connection.from_microservice_prefix( main_connection, kv_prefix) kv_service = KeyValueStoreService(kv_connection) alert_rules_conn = Connection.from_microservice_key(kv_service, alert_rules_key, auth=main_auth) alerts_conn = Connection.from_microservice_key(kv_service, alerts_key, auth=main_auth) file_events_conn = Connection.from_microservice_key(kv_service, file_events_key, auth=main_auth) pds_conn = Connection.from_microservice_key(kv_service, preservation_data_key, auth=main_auth) ecm_conn = Connection.from_microservice_key(kv_service, employee_case_mgmt_key, auth=main_auth) audit_logs_conn = Connection.from_microservice_key(kv_service, audit_logs_key, auth=main_auth) user_svc = UserService(main_connection) administration_svc = AdministrationService(main_connection) file_event_svc = FileEventService(file_events_conn) user_ctx = UserContext(administration_svc) user_profile_svc = DetectionListUserService(ecm_conn, user_ctx, user_svc) services = Services( administration=administration_svc, archive=ArchiveService(main_connection), devices=DeviceService(main_connection), legalhold=LegalHoldService(main_connection), orgs=OrgService(main_connection), securitydata=SecurityDataService(main_connection), users=UserService(main_connection), alertrules=AlertRulesService(alert_rules_conn, user_ctx, user_profile_svc), alerts=AlertService(alerts_conn, user_ctx), fileevents=file_event_svc, savedsearch=SavedSearchService(file_events_conn, file_event_svc), preservationdata=PreservationDataService(pds_conn), departingemployee=DepartingEmployeeService(ecm_conn, user_ctx, user_profile_svc), highriskemployee=HighRiskEmployeeService(ecm_conn, user_ctx, user_profile_svc), userprofile=user_profile_svc, auditlogs=AuditLogsService(audit_logs_conn), ) return services, user_ctx
def test_get_custodians_page_raises_error_when_required_option_missing( self, mocker, mock_connection): def side_effect(*args, **kwargs): base_err = mocker.MagicMock(spec=HTTPError) base_err.response = mocker.MagicMock(spec=Response) base_err.response.text = "At least one criteria must be specified; holdMembershipUid, holdUid, userUid, or userSearch" raise Py42BadRequestError(base_err) mock_connection.get.side_effect = side_effect service = LegalHoldService(mock_connection) with pytest.raises(Py42LegalHoldCriteriaMissingError): service.get_custodians_page(1)
def test_get_matter_by_uid_when_forbidden_raises_legal_hold_permission_denied_error( self, mocker, mock_connection, successful_response ): mock_connection.get.side_effect = create_mock_error( Py42ForbiddenError, mocker, "" ) service = LegalHoldService(mock_connection) with pytest.raises(Py42LegalHoldNotFoundOrPermissionDeniedError) as err: service.get_matter_by_uid("matter") expected = "Matter with UID 'matter' can not be found. Your account may not have permission to view the matter." assert expected in str(err.value) assert err.value.uid == "matter"
def test_get_events_page_calls_get_with_expected_url_and_params( self, mock_connection): service = LegalHoldService(mock_connection) service.get_events_page("legalhold", None, None, 20, 200) mock_connection.get.assert_called_once_with( "/api/LegalHoldEventReport", params={ "legalHoldUid": "legalhold", "minEventDate": None, "maxEventDate": None, "pgNum": 20, "pgSize": 200, }, )
def test_get_matter_by_uid_when_forbidden_raises_legal_hold_permission_denied_error( self, mocker, mock_connection, successful_response): def side_effect(*args, **kwargs): base_err = mocker.MagicMock(spec=HTTPError) base_err.response = mocker.MagicMock(spec=Response) raise Py42ForbiddenError(base_err) mock_connection.get.side_effect = side_effect service = LegalHoldService(mock_connection) with pytest.raises( Py42LegalHoldNotFoundOrPermissionDeniedError) as err: service.get_matter_by_uid("matter") expected = "Matter with ID=matter can not be found. Your account may not have permission to view the matter." assert str(err.value) == expected
def test_get_matters_page_calls_get_with_expected_url_and_params( self, mock_connection): service = LegalHoldService(mock_connection) service.get_matters_page(10, "creator", True, "name", "ref", 100) mock_connection.get.assert_called_once_with( "/api/LegalHold", params={ "creatorUserUid": "creator", "activeState": "ACTIVE", "name": "name", "holdExtRef": "ref", "pgNum": 10, "pgSize": 100, }, )
def test_add_to_matter_when_post_raises_bad_request_error_indicating_user_already_added_raises_user_already_added( self, mocker, mock_connection ): mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "USER_ALREADY_IN_HOLD" ) mock_connection.get.return_value = {"name": "NAME"} service = LegalHoldService(mock_connection) with pytest.raises(Py42UserAlreadyAddedError) as err: service.add_to_matter("user", "legal") expected = ( "User with ID user is already on the legal hold matter id=legal, name=NAME." ) assert str(err.value) == expected
def test_get_all_matters_calls_get_expected_number_of_times( self, mock_connection, mock_get_all_matters_response, mock_get_all_matters_empty_response, ): py42.settings.items_per_page = 1 service = LegalHoldService(mock_connection) mock_connection.get.side_effect = [ mock_get_all_matters_response, mock_get_all_matters_response, mock_get_all_matters_empty_response, ] for _ in service.get_all_matters(): pass py42.settings.items_per_page = 500 assert mock_connection.get.call_count == 3
def test_get_custodians_page_calls_get_with_expected_url_and_params( self, mock_connection): service = LegalHoldService(mock_connection) service.get_custodians_page(20, "membership", "legalhold", "user ID", "username", True, 200) mock_connection.get.assert_called_once_with( "/api/LegalHoldMembership", params={ "legalHoldMembershipUid": "membership", "legalHoldUid": "legalhold", "userUid": "user ID", "user": "******", "activeState": "ACTIVE", "pgNum": 20, "pgSize": 200, }, )
def test_add_to_matter_when_post_raises_bad_request_error_indicating_user_already_added_raises_user_already_added( self, mocker, mock_connection): def side_effect(*args, **kwargs): base_err = mocker.MagicMock(spec=HTTPError) base_err.response = mocker.MagicMock(spec=Response) base_err.response.text = "USER_ALREADY_IN_HOLD" raise Py42BadRequestError(base_err) mock_connection.post.side_effect = side_effect mock_connection.get.return_value = {"name": "NAME"} service = LegalHoldService(mock_connection) with pytest.raises(Py42UserAlreadyAddedError) as err: service.add_to_matter("user", "legal") expected = ( "User with ID user is already on the legal hold matter id=legal, name=NAME." ) assert str(err.value) == expected
def test_get_custodians_page_raises_error_when_required_option_missing( self, mocker, mock_connection ): text = ( "At least one criteria must be specified; holdMembershipUid, holdUid, " "userUid, or userSearch" ) mock_connection.get.side_effect = create_mock_error( Py42BadRequestError, mocker, text ) service = LegalHoldService(mock_connection) with pytest.raises(Py42LegalHoldCriteriaMissingError) as err: service.get_custodians_page(1) assert ( str(err.value) == "At least one criteria must be specified; " "legal_hold_membership_uid, legal_hold_uid, user_uid, or user." )
def _init_services(main_connection, main_auth): # services are imported within function to prevent circular imports when a service # imports anything from py42.sdk.queries from py42.services import Services from py42.services._keyvaluestore import KeyValueStoreService from py42.services.administration import AdministrationService from py42.services.alertrules import AlertRulesService from py42.services.alerts import AlertService from py42.services.archive import ArchiveService from py42.services.auditlogs import AuditLogsService from py42.services.cases import CasesService from py42.services.casesfileevents import CasesFileEventsService from py42.services.detectionlists.departing_employee import DepartingEmployeeService from py42.services.detectionlists.high_risk_employee import HighRiskEmployeeService from py42.services.detectionlists.user_profile import DetectionListUserService from py42.services.devices import DeviceService from py42.services.fileevent import FileEventService from py42.services.legalhold import LegalHoldService from py42.services.orgs import OrgService from py42.services.preservationdata import PreservationDataService from py42.services.savedsearch import SavedSearchService from py42.services.trustedactivities import TrustedActivitiesService from py42.services.users import UserService alert_rules_key = "FedObserver-API_URL" alerts_key = "AlertService-API_URL" file_events_key = "FORENSIC_SEARCH-API_URL" preservation_data_key = "PRESERVATION-DATA-SERVICE_API-URL" employee_case_mgmt_key = "employeecasemanagementV2-API_URL" kv_prefix = "simple-key-value-store" audit_logs_key = "AUDIT-LOG_API-URL" cases_key = "CASES_API-URL" trusted_activities_key = "TRUSTED-DOMAINS_API-URL" kv_connection = Connection.from_microservice_prefix( main_connection, kv_prefix) kv_service = KeyValueStoreService(kv_connection) alert_rules_conn = Connection.from_microservice_key(kv_service, alert_rules_key, auth=main_auth) alerts_conn = Connection.from_microservice_key(kv_service, alerts_key, auth=main_auth) file_events_conn = Connection.from_microservice_key(kv_service, file_events_key, auth=main_auth) pds_conn = Connection.from_microservice_key(kv_service, preservation_data_key, auth=main_auth) ecm_conn = Connection.from_microservice_key(kv_service, employee_case_mgmt_key, auth=main_auth) audit_logs_conn = Connection.from_microservice_key(kv_service, audit_logs_key, auth=main_auth) user_svc = UserService(main_connection) administration_svc = AdministrationService(main_connection) file_event_svc = FileEventService(file_events_conn) user_ctx = UserContext(administration_svc) user_profile_svc = DetectionListUserService(ecm_conn, user_ctx, user_svc) cases_conn = Connection.from_microservice_key(kv_service, cases_key, auth=main_auth) trusted_activities_conn = Connection.from_microservice_key( kv_service, trusted_activities_key, auth=main_auth) services = Services( administration=administration_svc, archive=ArchiveService(main_connection), devices=DeviceService(main_connection), legalhold=LegalHoldService(main_connection), orgs=OrgService(main_connection), users=UserService(main_connection), alertrules=AlertRulesService(alert_rules_conn, user_ctx, user_profile_svc), alerts=AlertService(alerts_conn, user_ctx), fileevents=file_event_svc, savedsearch=SavedSearchService(file_events_conn, file_event_svc), preservationdata=PreservationDataService(pds_conn), departingemployee=DepartingEmployeeService(ecm_conn, user_ctx, user_profile_svc), highriskemployee=HighRiskEmployeeService(ecm_conn, user_ctx, user_profile_svc), userprofile=user_profile_svc, auditlogs=AuditLogsService(audit_logs_conn), cases=CasesService(cases_conn), casesfileevents=CasesFileEventsService(cases_conn), trustedactivities=TrustedActivitiesService(trusted_activities_conn), ) return services, user_ctx