コード例 #1
0
ファイル: test_authn.py プロジェクト: 2degrees/twapi-authn
    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)
コード例 #2
0
ファイル: test_authn.py プロジェクト: 2degrees/twapi-authn
    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)
コード例 #3
0
ファイル: test_authn.py プロジェクト: 2degrees/twapi-authn
    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)
コード例 #4
0
    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)
コード例 #5
0
    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)
コード例 #6
0
ファイル: test_authn.py プロジェクト: 2degrees/twapi-authn
    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)
コード例 #7
0
    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,
            )
コード例 #8
0
    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,
        )
コード例 #9
0
    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)
コード例 #10
0
    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)
コード例 #11
0
    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))
コード例 #12
0
    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))
コード例 #13
0
    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))
コード例 #14
0
    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))
コード例 #15
0
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)
コード例 #16
0
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)