Ejemplo n.º 1
0
    def test_get_all_called_with_expected_url_and_all_optional_params(
        self, mock_connection, mock_case_response
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.get.side_effect = [
            mock_case_response,
            mock_case_response,
        ]
        for _ in cases_service.get_all(
            name="test-case",
            subject="test",
            assignee="user-uid",
            updated_at="2010-04-30T001",
            created_at="2010-01-03T002",
            status="open",
        ):
            continue

        expected_params = {
            "name": "test-case",
            "subject": "test",
            "assignee": "user-uid",
            "createdAt": "2010-01-03T002",
            "updatedAt": "2010-04-30T001",
            "status": "open",
            "pgNum": 1,
            "pgSize": 500,
            "srtDir": "asc",
            "srtKey": "number",
        }
        mock_connection.get.assert_called_once_with(_BASE_URI, params=expected_params)
Ejemplo n.º 2
0
    def test_get_all_called_with_expected_url_and_default_params(
        self, mock_connection, mock_case_response, mock_case_empty_response
    ):
        cases_service = CasesService(mock_connection)
        items = [
            mock_case_response,
        ]

        mock_connection.get.side_effect = items
        for _ in cases_service.get_all():
            pass

        expected_params = {
            "name": None,
            "subject": None,
            "assignee": None,
            "createdAt": None,
            "updatedAt": None,
            "status": None,
            "pgNum": 1,
            "pgSize": 500,
            "srtDir": "asc",
            "srtKey": "number",
        }
        mock_connection.get.assert_called_once_with(_BASE_URI, params=expected_params)
Ejemplo n.º 3
0
 def test_export_called_with_expected_url_and_params(self, mock_connection):
     cases_service = CasesService(mock_connection)
     cases_service.export_summary(_TEST_CASE_NUMBER)
     assert (
         mock_connection.get.call_args[0][0]
         == f"/api/v1/case/{_TEST_CASE_NUMBER}/export"
     )
Ejemplo n.º 4
0
 def test_create_when_fails_with_unknown_error_raises_custom_exception(
         self, mock_connection, mock_unknown_error):
     cases_service = CasesService(mock_connection)
     mock_connection.post.side_effect = Py42BadRequestError(
         mock_unknown_error)
     with pytest.raises(Py42BadRequestError) as e:
         cases_service.create("Case")
     assert e.value.response.status_code == 400
Ejemplo n.º 5
0
 def test_create_when_fails_with_unknown_error_raises_exception(
     self, mocker, mock_connection
 ):
     cases_service = CasesService(mock_connection)
     mock_connection.post.side_effect = create_mock_error(
         Py42BadRequestError, mocker, UNKNOWN_ERROR_MSG
     )
     with pytest.raises(Py42BadRequestError):
         cases_service.create("Case")
Ejemplo n.º 6
0
    def test_create_when_fails_with_description_too_long_error_raises_custom_exception(
            self, mock_connection, mock_description_too_long_response):
        cases_service = CasesService(mock_connection)
        mock_connection.post.side_effect = Py42BadRequestError(
            mock_description_too_long_response)
        with pytest.raises(Py42DescriptionLimitExceededError) as e:
            cases_service.create("test", description=u"supposedly too long")

        assert (e.value.args[0] ==
                u"Description limit exceeded, max 250 characters allowed.")
Ejemplo n.º 7
0
    def test_update_failure_raised_appropriate_custom_exception(
            self, mock_connection, mock_get_response,
            mock_update_failed_response):
        cases_service = CasesService(mock_connection)
        mock_connection.get.return_value = mock_get_response
        mock_connection.put.side_effect = Py42BadRequestError(
            mock_update_failed_response)
        with pytest.raises(Py42UpdateClosedCaseError) as e:
            cases_service.update(_TEST_CASE_NUMBER, findings=u"x")

        assert e.value.args[0] == u"Cannot update a closed case."
Ejemplo n.º 8
0
    def test_create_when_fails_with_invalid_subject_raises_custom_exception(
        self, mocker, mock_connection
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.post.side_effect = create_mock_error(
            Py42BadRequestError, mocker, _get_invalid_user_text("subject")
        )
        with pytest.raises(Py42InvalidCaseUserError) as err:
            cases_service.create("test", subject="Not a person")

        assert err.value.args[0] == "The provided subject is not a valid user."
Ejemplo n.º 9
0
    def test_update_when_fails_with_invalid_assignee_raises_custom_exception(
        self, mocker, mock_connection
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.put.side_effect = create_mock_error(
            Py42BadRequestError, mocker, _get_invalid_user_text("assignee")
        )
        with pytest.raises(Py42InvalidCaseUserError) as err:
            cases_service.update(_TEST_CASE_NUMBER, assignee="Not a person")

        assert err.value.args[0] == "The provided assignee is not a valid user."
Ejemplo n.º 10
0
    def test_create_when_fails_with_name_exists_error_raises_custom_exception(
            self, mock_connection, mock_name_exists_response):
        cases_service = CasesService(mock_connection)
        mock_connection.post.side_effect = Py42BadRequestError(
            mock_name_exists_response)
        with pytest.raises(Py42CaseNameExistsError) as e:
            cases_service.create("Duplicate")

        assert (
            e.value.args[0] ==
            u"Case name 'Duplicate' already exists, please set another name")
Ejemplo n.º 11
0
    def test_update_when_case_is_closed_raises_custom_exception(
        self, mocker, mock_connection, mock_get_response
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.get.return_value = mock_get_response
        mock_connection.put.side_effect = create_mock_error(
            Py42BadRequestError, mocker, UPDATE_ERROR_RESPONSE
        )
        with pytest.raises(Py42UpdateClosedCaseError) as err:
            cases_service.update(_TEST_CASE_NUMBER, findings="x")

        assert err.value.args[0] == "Cannot update a closed case."
Ejemplo n.º 12
0
 def test_create_called_with_expected_url_and_params(self, mock_connection):
     cases_service = CasesService(mock_connection)
     cases_service.create("name", "subject", "user uid", "description", "findings")
     assert mock_connection.post.call_args[0][0] == "/api/v1/case"
     data = {
         "name": "name",
         "subject": "subject",
         "assignee": "user uid",
         "description": "description",
         "findings": "findings",
     }
     mock_connection.post.assert_called_once_with(_BASE_URI, json=data)
Ejemplo n.º 13
0
    def test_get_all_called_expected_number_of_times(
        self, mock_connection, mock_case_response, mock_case_empty_response
    ):
        cases_service = CasesService(mock_connection)
        py42.settings.items_per_page = 1
        items = [mock_case_response, mock_case_empty_response]

        mock_connection.get.side_effect = items
        for _ in cases_service.get_all():
            pass

        assert mock_connection.get.call_count == 2
        py42.settings.items_per_page = 500
Ejemplo n.º 14
0
    def test_create_when_fails_with_description_too_long_error_raises_custom_exception(
        self, mocker, mock_connection
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.post.side_effect = create_mock_error(
            Py42BadRequestError, mocker, DESCRIPTION_TOO_LONG_ERROR_MSG
        )
        with pytest.raises(Py42DescriptionLimitExceededError) as err:
            cases_service.create("test", description="supposedly too long")

        assert (
            err.value.args[0]
            == "Description limit exceeded, max 250 characters allowed."
        )
Ejemplo n.º 15
0
    def test_create_when_fails_with_name_exists_error_raises_custom_exception(
        self, mocker, mock_connection
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.post.side_effect = create_mock_error(
            Py42BadRequestError, mocker, NAME_EXISTS_ERROR_MSG
        )
        with pytest.raises(Py42CaseNameExistsError) as err:
            cases_service.create("Duplicate")

        assert (
            err.value.args[0]
            == "Case name 'Duplicate' already exists, please set another name."
        )
Ejemplo n.º 16
0
 def test_update_called_with_expected_url_and_params(
         self, mock_connection, mock_get_response):
     cases_service = CasesService(mock_connection)
     mock_connection.get.return_value = mock_get_response
     cases_service.update(_TEST_CASE_NUMBER, findings=u"x")
     data = {
         "name": "string",
         "subject": "string",
         "assignee": "string",
         "description": None,
         "status": "OPEN",
         "findings": u"x",
     }
     mock_connection.put.assert_called_once_with(
         u"/api/v1/case/{}".format(_TEST_CASE_NUMBER), json=data)
Ejemplo n.º 17
0
    def test_update_when_fails_with_description_too_long_error_raises_custom_exception(
        self, mocker, mock_connection, mock_get_response
    ):
        cases_service = CasesService(mock_connection)
        mock_connection.get.return_value = mock_get_response
        mock_connection.put.side_effect = create_mock_error(
            Py42BadRequestError, mocker, DESCRIPTION_TOO_LONG_ERROR_MSG
        )
        with pytest.raises(Py42DescriptionLimitExceededError) as err:
            cases_service.update(_TEST_CASE_NUMBER, description=u"supposedly too long")

        assert (
            err.value.args[0]
            == u"Description limit exceeded, max 250 characters allowed."
        )
Ejemplo n.º 18
0
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"employeecasemanagementV2-API_URL"
    kv_prefix = u"simple-key-value-store"
    audit_logs_key = u"AUDIT-LOG_API-URL"
    cases_key = u"CASES_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)

    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),
        cases=CasesService(cases_conn),
        casesfileevents=CasesFileEventsService(cases_conn),
    )

    return services, user_ctx
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
 def test_get_called_with_expected_url_and_params(self, mock_connection):
     cases_service = CasesService(mock_connection)
     cases_service.get(_TEST_CASE_NUMBER)
     assert mock_connection.get.call_args[0][0] == u"/api/v1/case/{}".format(
         _TEST_CASE_NUMBER
     )