def test_active_session(self): access_token = get_uuid4_str() path_info = '/sessions/{}/'.format(access_token) api_call = SuccessfulAPICall( path_info, 'HEAD', response=MockResponse(None), ) with _make_connection(api_call) as connection: is_active = is_session_active(connection, access_token) ok_(is_active)
def test_inactive_session(self): access_token = get_uuid4_str() path_info = '/sessions/{}/'.format(access_token) api_call = UnsuccessfulAPICall( path_info, 'HEAD', exception=NotFoundError(), ) with _make_connection(api_call) as connection: is_active = is_session_active(connection, access_token) assert_false(is_active)
def test_invalid_token(self): access_token = get_uuid4_str() path_info = '/sessions/{}/'.format(access_token) api_call = UnsuccessfulAPICall( path_info, 'POST', exception=NotFoundError(), ) with assert_raises(AccessTokenError): with _make_connection(api_call) as connection: claim_access_token(connection, access_token)
def _assert_credentials_set_in_request(key_class, expected_key_name): authentication_key_value = get_uuid4_str() authentication_key = key_class(authentication_key_value) connection = \ _MockPortalConnection(authentication_key=authentication_key) connection.send_get_request(_STUB_URL_PATH) expected_credentials = {expected_key_name: [authentication_key_value]} prepared_request = connection.prepared_requests[0] query_string_args = \ _get_query_string_args_from_url(prepared_request.url) assert_dict_contains_subset(expected_credentials, query_string_args)
def test_valid_token(self): expected_user_id = 1 access_token = get_uuid4_str() path_info = '/sessions/{}/'.format(access_token) api_call = SuccessfulAPICall( path_info, 'POST', response=MockResponse(expected_user_id), ) with _make_connection(api_call) as connection: user_id = claim_access_token(connection, access_token) eq_(expected_user_id, user_id)
def test_change_source(self): change_source = get_uuid4_str() connection = _MockPortalConnection(change_source=change_source) connection.send_get_request(_STUB_URL_PATH) prepared_request = connection.prepared_requests[0] query_string_args = \ _get_query_string_args_from_url(prepared_request.url) expected_change_source_args = {'auditId': [change_source]} assert_dict_contains_subset( expected_change_source_args, query_string_args, )
def test_unsuccessful_api_call(self): exception = \ AuthenticationError('Must authenticate', get_uuid4_str()) expected_api_call = UnsuccessfulAPICall( _STUB_URL_PATH, 'GET', exception=exception, ) connection = \ self._make_connection_for_expected_api_call(expected_api_call) with assert_raises(type(exception)) as context_manager: connection.send_get_request(_STUB_URL_PATH) eq_(exception, context_manager.exception) self._assert_sole_api_call_equals(expected_api_call, connection)
def test_unsuccessful_api_call(self): exception = \ HubspotAuthenticationError('Must authenticate', get_uuid4_str()) expected_api_call = UnsuccessfulAPICall( _STUB_URL_PATH, 'GET', exception=exception, ) connection = \ self._make_connection_for_expected_api_call(expected_api_call) with assert_raises(type(exception)) as context_manager: connection.send_get_request(_STUB_URL_PATH) eq_(exception, context_manager.exception) self._assert_sole_api_call_equals(expected_api_call, connection)
def test_client_error_response(self): request_id = get_uuid4_str() error_message = 'Json node is missing child property' body_deserialization = { 'status': 'error', 'message': error_message, 'requestId': request_id, } response_data_maker = _ResponseMaker(400, body_deserialization) connection = _MockPortalConnection(response_data_maker) with assert_raises(HubspotClientError) as context_manager: connection.send_get_request(_STUB_URL_PATH) exception = context_manager.exception eq_(request_id, exception.request_id) eq_(error_message, str(exception))
def test_unauthorized_response(self): request_id = get_uuid4_str() error_message = 'Invalid credentials' response_data_maker = _ResponseMaker( 401, { 'status': 'error', 'message': error_message, 'requestId': request_id, }, ) connection = _MockPortalConnection( response_data_maker, authentication_key=_STUB_AUTHENTICATION_KEY, ) with assert_raises(HubspotAuthenticationError) as context_manager: connection.send_get_request(_STUB_URL_PATH) exception = context_manager.exception eq_(request_id, exception.request_id) eq_(error_message, str(exception))
from requests.models import Response as RequestsResponse from hubspot.connection import APIKey from hubspot.connection import OAuthKey from hubspot.connection import PortalConnection from hubspot.connection.exc import HubspotAuthenticationError from hubspot.connection.exc import HubspotClientError from hubspot.connection.exc import HubspotServerError from hubspot.connection.exc import HubspotUnsupportedResponseError from tests.utils import get_uuid4_str _STUB_URL_PATH = '/foo' _STUB_AUTHENTICATION_KEY = APIKey(get_uuid4_str()) class TestPortalConnection(object): def test_get_request(self): self._check_request_sender('GET', 'send_get_request', False) def test_post_request(self): self._check_request_sender('POST', 'send_post_request', True) def test_put_request(self): self._check_request_sender('PUT', 'send_put_request', True) def test_delete_request(self): self._check_request_sender('DELETE', 'send_delete_request', False)
from requests.adapters import HTTPAdapter as RequestsHTTPAdapter from requests.models import Response as RequestsResponse from hubspot.connection import APIKey from hubspot.connection import OAuthKey from hubspot.connection import PortalConnection from hubspot.connection.exc import HubspotAuthenticationError from hubspot.connection.exc import HubspotClientError from hubspot.connection.exc import HubspotServerError from hubspot.connection.exc import HubspotUnsupportedResponseError from tests.utils import get_uuid4_str _STUB_URL_PATH = '/foo' _STUB_AUTHENTICATION_KEY = APIKey(get_uuid4_str()) class TestPortalConnection(object): def test_get_request(self): self._check_request_sender('GET', 'send_get_request', False) def test_post_request(self): self._check_request_sender('POST', 'send_post_request', True) def test_put_request(self): self._check_request_sender('PUT', 'send_put_request', True) def test_delete_request(self): self._check_request_sender('DELETE', 'send_delete_request', False)