def test_get_page_when_bad_request_raises(self, mocker, mock_connection): mock_connection.get.side_effect = create_mock_error( Py42BadRequestError, mocker, "BAD REQUEST") service = UserService(mock_connection) with pytest.raises(Py42BadRequestError): service.get_page(1, org_uid="TestOrgUid")
def test_delete_when_unknown_error_raises_error(self, mocker, mock_connection): case_file_event_service = CasesFileEventsService(mock_connection) mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, UNKNOWN_ERROR) with pytest.raises(Py42BadRequestError): case_file_event_service.add(_TEST_CASE_NUMBER, event_id=u"x")
def test_create_user_when_service_returns_cannot_find_user_bad_request_raises_unable_to_create_error( self, mocker, mock_detection_list_user_service, mock_departing_employee_service, mock_high_risk_employee_service, ): mock_err_response_content = """ { "pop-bulletin": { "type$": "com.code42.casemanagement.CaseMessages.InvalidUser", "text$": "Could not find user: testusername", "details": [], "user": "******" } }""" mock_detection_list_user_service.get.side_effect = create_mock_error( Py42BadRequestError, mocker, mock_err_response_content) client = DetectionListsClient( mock_detection_list_user_service, mock_departing_employee_service, mock_high_risk_employee_service, ) with pytest.raises(Py42UnableToCreateProfileError) as err: client.create_user("testusername") assert ( "Detection-list profiles are now created automatically on the server. " "Unable to find a detection-list profile for 'testusername'. It is " "possibly still being created if you just recently created the Code42 " "user." in str(err.value)) assert err.value.username == "testusername"
def test_add_on_a_closed_case_raises_error(self, mocker, mock_connection): mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, UPDATE_ERROR_RESPONSE) case_file_event_service = CasesFileEventsService(mock_connection) with pytest.raises(Py42UpdateClosedCaseError) as err: case_file_event_service.add(_TEST_CASE_NUMBER, event_id=u"x") assert err.value.args[0] == u"Cannot update a closed case."
def test_search_when_bad_request_raised_with_token_but_has_not_invalid_token_text_raises_bad_request( self, mocker, connection): connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "DIFFERENT_ERROR") query = _create_test_query() query.page_token = "test_page_token" service = FileEventService(connection) with pytest.raises(Py42BadRequestError): service.search(query)
def test_add_when_same_event_is_added_multiple_times_raises_error( self, mocker, mock_connection): case_file_event_service = CasesFileEventsService(mock_connection) mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, ADDED_SAME_EVENT_AGAIN_ERROR) with pytest.raises(Py42CaseAlreadyHasEventError) as err: case_file_event_service.add(_TEST_CASE_NUMBER, event_id=u"x") assert err.value.args[0] == u"Event is already associated to the case."
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")
def mock_user_client_error_on_adding_cloud_aliases(self, mocker, mock_connection, user_context, py42_response): user_client = UserService(mock_connection) mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, CLOUD_ALIAS_LIMIT_EXCEEDED_ERROR_MESSAGE) return user_client
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."
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."
def test_get_page_when_org_not_found_raises_expected_error( self, mocker, mock_connection): text = '[{"name":"SYSTEM","description":"Organization was not found"}]' mock_connection.get.side_effect = create_mock_error( Py42BadRequestError, mocker, text) service = UserService(mock_connection) with pytest.raises(Py42OrgNotFoundError) as err: service.get_page(1, org_uid="TestOrgUid") assert str(err.value ) == "The organization with UID 'TestOrgUid' was not found."
def test_deactivate_when_user_in_legal_hold_raises_active_legal_hold_error( self, mocker, mock_connection): mock_connection.put.side_effect = create_mock_error( Py42BadRequestError, mocker, "ACTIVE_LEGAL_HOLD") client = UserService(mock_connection) with pytest.raises(Py42ActiveLegalHoldError) as err: client.deactivate(1234) expected = ( "Cannot deactivate the user with ID 1234 as the user is involved in " "a legal hold matter.") assert str(err.value) == expected
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."
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_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." )
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." )
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_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." )
def test_create_user_when_service_returns_bad_request_raises( self, mocker, mock_detection_list_user_service, mock_departing_employee_service, mock_high_risk_employee_service, ): mock_detection_list_user_service.get.side_effect = create_mock_error( Py42BadRequestError, mocker, "") client = DetectionListsClient( mock_detection_list_user_service, mock_departing_employee_service, mock_high_risk_employee_service, ) with pytest.raises(Py42BadRequestError): client.create_user("testusername")
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 mock_user_client_raises_exception(self, mocker, mock_connection, user_context, py42_response): user_client = UserService(mock_connection) mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "") return user_client
def invalid_password_error_response(self, mocker): return create_mock_error(Py42InternalServerError, mocker, MOCK_INVALID_PASSWORD_TEXT)
def invalid_email_error_response(self, mocker): return create_mock_error(Py42InternalServerError, mocker, MOCK_INVALID_EMAIL_TEXT)
def username_must_be_email_error_response(self, mocker): return create_mock_error(Py42InternalServerError, mocker, MOCK_USERNAME_MUST_BE_EMAIL_TEXT)
def internal_server_error(self, mocker): return create_mock_error(Py42InternalServerError, mocker, "")
def mock_restore_connection_with_bad_request(mocker, mock_connection): mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "CREATE_FAILED" ) return mock_connection
def mock_get_by_id_fails(self, mocker, mock_connection): mock_connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "") return mock_connection
def invalid_username_error_response(self, mocker): return create_mock_error(Py42InternalServerError, mocker, MOCK_INVALID_USERNAME_TEXT)
def user_duplicate_error_response(self, mocker): return create_mock_error(Py42InternalServerError, mocker, MOCK_USER_DUPLICATE_ERROR_TEXT)
def mock_invalid_page_token_connection(mocker, connection): connection.post.side_effect = create_mock_error( Py42BadRequestError, mocker, "INVALID_PAGE_TOKEN" ) return connection