示例#1
0
    def test_constructor_maps_True_to_print_for_debug_value(self):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   debug=True)

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("print", self.contextio.debug)
示例#2
0
    def test_request_uri_returns_response_text_if_ValueError_raised(
            self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.status_code = 200
        mock_request.json.side_effect = ValueError()
        mock_request.text = "This is some text"

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")
        self.assertEqual("This is some text", response)
示例#3
0
    def test_request_uri_returns_response_content_if_UnicodeDecodeError_raised(
            self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.json.side_effect = UnicodeDecodeError("", "", 42, 43, "")
        mock_request.status_code = 200
        mock_request.content = bytes("foo bar")

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")

        self.assertEqual("foo bar", response)
示例#4
0
    def test_constructor_creates_object_with_custom_config(self):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   url_base="http://fake.url",
                                   debug="print",
                                   version="lite")

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("http://fake.url", self.contextio.url_base)
        self.assertEqual("lite", self.contextio.version)
        self.assertEqual("print", self.contextio.debug)
示例#5
0
 def test_ContextIOFactory_returns_v2_0_instance_by_default(self):
     context_io = ContextIO(consumer_key=CONTEXTIO_KEY,
                            consumer_secret=CONTEXTIO_SECRET)
     self.assertIsInstance(context_io, V2_0)
     params = {'id': CONTEXTIO_EMAIL_ID}
     account = contextio.Account(context_io, params)
     self.assertTrue(account.get())
示例#6
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        self.connect_token = ConnectToken(self.account,
                                          {"token": "fake_token"})
        self.uri = "https://api.context.io/2.0/accounts/fake_id/sources/foobar/folders/fake_folder_name/"
示例#7
0
 def setUp(self):
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
     self.email_address = EmailAddress(self.contextio, {
         "email": "*****@*****.**",
         "validated": 1234567890,
         "primary": 1
     })
示例#8
0
    def test_constructor_maps_True_to_print_for_debug_value(self):
        self.contextio = ContextIO(
            consumer_key="foo",
            consumer_secret="bar",
            debug=True
        )

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("print", self.contextio.debug)
示例#9
0
    def test_request_uri_returns_response_text_if_ValueError_raised(self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.status_code = 200
        mock_request.json.side_effect = ValueError()
        mock_request.text = "This is some text"

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")
        self.assertEqual("This is some text", response)
示例#10
0
    def test_request_uri_returns_response_content_if_UnicodeDecodeError_raised(self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.json.side_effect = UnicodeDecodeError("", "", 42, 43, "")
        mock_request.status_code = 200
        mock_request.content = bytes("foo bar")

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")

        self.assertEqual("foo bar", response)
示例#11
0
    def test_debug_logs_message_when_debug_equals_log(self,
                                                      mock_logging_debug):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   debug="log")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404
        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(mock_response.request.url,
                                mock_response.request.method,
                                mock_response.status_code,
                                mock_response.request.__dict__,
                                mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_logging_debug.assert_called_with(message)
示例#12
0
    def test_request_defaults_to_GET_method_and_include_user_agent_header(
            self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_session.return_value.request = mock.Mock()
        mock_request = mock_session.return_value.request

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

        mock_request.assert_called_with(
            "GET",
            "https://api.context.io/2.0/catpants",
            data="",
            header_auth=True,
            headers={'user-agent': 'contextio/2.0/python-lib-v1.0.0'},
            params={})
示例#13
0
    def test_request_includes_body_if_method_is_POST(self, mock_session,
                                                     mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_request = mock_session.return_value.request
        mock_request.return_value.status_code = 200

        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   version="some_version")

        self.contextio._request_uri("catpants",
                                    method="POST",
                                    body=json.dumps({"foo": "bar"}))

        mock_request.assert_called_with(
            "POST",
            "https://api.context.io/some_version/catpants",
            data={"body": '{"foo": "bar"}'},
            header_auth=True,
            headers={'user-agent': 'contextio/some_version/python-lib-v1.0.0'})
示例#14
0
    def test_constructor_creates_object_with_custom_config(self):
        self.contextio = ContextIO(
            consumer_key="foo",
            consumer_secret="bar",
            url_base="http://fake.url",
            debug="print",
            version="lite"
        )

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("http://fake.url", self.contextio.url_base)
        self.assertEqual("lite", self.contextio.version)
        self.assertEqual("print", self.contextio.debug)
示例#15
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        contact_definition = {
            "email": "*****@*****.**",
            "count": 1,
            "sent_count": 0,
            "received_count": 1,
            "sent_from_account_count": 0,
            "thumbnail": "https://some.url",
            "last_sent": None,
            "last_received": 1455649370
        }

        self.contact = Contact(self.account, contact_definition)
示例#16
0
    def test_debug_logs_message_when_debug_equals_log(self, mock_logging_debug):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar", debug="log")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404
        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(
                mock_response.request.url, mock_response.request.method, mock_response.status_code,
                mock_response.request.__dict__, mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_logging_debug.assert_called_with(message)
示例#17
0
    def test_request_includes_body_if_method_is_POST(self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_request = mock_session.return_value.request
        mock_request.return_value.status_code = 200

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar", version="some_version")

        self.contextio._request_uri("catpants", method="POST", body=json.dumps({"foo": "bar"}))

        mock_request.assert_called_with(
            "POST", "https://api.context.io/some_version/catpants",
            data={"body": '{"foo": "bar"}'},
            header_auth=True,
            headers={'user-agent': 'contextio/some_version/python-lib-v1.0.0'}
        )
示例#18
0
    def test_request_defaults_to_GET_method_and_include_user_agent_header(self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_session.return_value.request = mock.Mock()
        mock_request = mock_session.return_value.request

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

        mock_request.assert_called_with(
            "GET", "https://api.context.io/2.0/catpants",
            data="",
            header_auth=True,
            headers={'user-agent': 'contextio/2.0/python-lib-v1.0.0'},
            params={}
        )
示例#19
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        self.uri = "https://api.context.io/2.0/accounts/fake_id/"
示例#20
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        self.file = File(self.account, {"file_id": "fake_file_id"})
示例#21
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        self.message = Message(self.account, {"message_id": "fake_message_id"})
示例#22
0
class TestContextIO(unittest.TestCase):
    def setUp(self):
        # some of the following tests mock the OAuth session created in the constructor
        # in for these tests we must redeclare self.contextio
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

    def test_constructor_creates_object_with_default_config(self):
        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("https://api.context.io", self.contextio.url_base)
        self.assertEqual("2.0", self.contextio.version)
        self.assertEqual(None, self.contextio.debug)
        self.assertIsInstance(self.contextio.session, OAuth1Session)

    def test_constructor_creates_object_with_custom_config(self):
        self.contextio = ContextIO(
            consumer_key="foo",
            consumer_secret="bar",
            url_base="http://fake.url",
            debug="print",
            version="lite"
        )

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("http://fake.url", self.contextio.url_base)
        self.assertEqual("lite", self.contextio.version)
        self.assertEqual("print", self.contextio.debug)

    def test_constructor_maps_True_to_print_for_debug_value(self):
        self.contextio = ContextIO(
            consumer_key="foo",
            consumer_secret="bar",
            debug=True
        )

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("print", self.contextio.debug)

    @mock.patch("contextio.contextio.six.print_")
    def test_debug_prints_message_when_debug_equals_print(self, mock_six_print):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar", debug="print")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404

        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(
                mock_response.request.url, mock_response.request.method, mock_response.status_code,
                mock_response.request.__dict__, mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_six_print.assert_called_with(message)

    @mock.patch("contextio.contextio.logging.debug")
    def test_debug_logs_message_when_debug_equals_log(self, mock_logging_debug):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar", debug="log")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404
        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(
                mock_response.request.url, mock_response.request.method, mock_response.status_code,
                mock_response.request.__dict__, mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_logging_debug.assert_called_with(message)

    @mock.patch("contextio.contextio.pkg_resources")
    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_defaults_to_GET_method_and_include_user_agent_header(self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_session.return_value.request = mock.Mock()
        mock_request = mock_session.return_value.request

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

        mock_request.assert_called_with(
            "GET", "https://api.context.io/2.0/catpants",
            data="",
            header_auth=True,
            headers={'user-agent': 'contextio/2.0/python-lib-v1.0.0'},
            params={}
        )

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_request_uri_raises_HTTPError_if_status_not_between_200_and_300(self, mock_request):
        mock_request.side_effect = HTTPError

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_uri_returns_response_content_if_UnicodeDecodeError_raised(self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.json.side_effect = UnicodeDecodeError("", "", 42, 43, "")
        mock_request.status_code = 200
        mock_request.content = bytes("foo bar")

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")

        self.assertEqual("foo bar", response)

    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_uri_returns_response_text_if_ValueError_raised(self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.status_code = 200
        mock_request.json.side_effect = ValueError()
        mock_request.text = "This is some text"

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")
        self.assertEqual("This is some text", response)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_request_uri_returns_json(self, mock_request):
        mock_request.return_value = ({"foo": "bar"})

        response = self.contextio._request_uri("catpants")

        self.assertEqual({"foo": "bar"}, response)

    @mock.patch("contextio.contextio.pkg_resources")
    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_includes_body_if_method_is_POST(self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_request = mock_session.return_value.request
        mock_request.return_value.status_code = 200

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar", version="some_version")

        self.contextio._request_uri("catpants", method="POST", body=json.dumps({"foo": "bar"}))

        mock_request.assert_called_with(
            "POST", "https://api.context.io/some_version/catpants",
            data={"body": '{"foo": "bar"}'},
            header_auth=True,
            headers={'user-agent': 'contextio/some_version/python-lib-v1.0.0'}
        )

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_accounts_returns_list_of_Account_resources(self, mock_request):
        mock_request.return_value = {"id": "some_id"}
        accounts = self.contextio.get_accounts()

        self.assertEqual(1, len(accounts))
        self.assertIsInstance(accounts[0], Account)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_account_returns_Account_object(self, mock_request):
        mock_request.return_value = {"id": "some_id"}

        account = self.contextio.post_account(email="*****@*****.**")

        self.assertIsInstance(account, Account)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_connect_tokens_returns_list_of_ConnectToken_resources_without_token(self, mock_request):
        mock_request.return_value = [{"token": "fake_token", "account": {"id": "account_id"}}]

        connect_tokens = self.contextio.get_connect_tokens()

        self.assertEqual(1, len(connect_tokens))
        self.assertIsInstance(connect_tokens[0], ConnectToken)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_connect_tokens_returns_dictionary_if_token_is_provided(self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        connect_token = self.contextio.get_connect_tokens(token="fake_token")

        self.assertEqual({"token": "fake_token"}, connect_token)


    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_connect_token_requires_callback_url(self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        with self.assertRaises(errors.ArgumentError):
            self.contextio.post_connect_token()

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_connect_token_returns_dictionary(self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        connect_token = self.contextio.post_connect_token(callback_url="http://some.callback.url")

        self.assertEqual({"token": "fake_token"}, connect_token)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_discovery_returns_discovery_object(self, mock_request):
        mock_request.return_value = {"email": "*****@*****.**"}


        discovery = self.contextio.get_discovery(email="*****@*****.**")

        self.assertIsInstance(discovery, Discovery)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_discovery_requires_email_argument(self, mock_request):
        with self.assertRaises(errors.ArgumentError):
            self.contextio.get_discovery()

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_oauth_providers_returns_list_of_OauthProvider_objects(self, mock_request):
        mock_request.return_value = [{"provider_consumer_key": "1234"}]

        oauth_providers = self.contextio.get_oauth_providers()

        self.assertEqual(1, len(oauth_providers))
        self.assertIsInstance(oauth_providers[0], OauthProvider)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_oauth_provider_returns_dictionary(self, mock_request):
        mock_request.return_value = {"provider_consumer_key": "123"}

        oauth_provider = self.contextio.post_oauth_provider(type="VALIDOAUTHTYPE", provider_consumer_key="1234", provider_consumer_secret="1234")

        self.assertEqual({"provider_consumer_key": "123"}, oauth_provider)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_oauth_provider_requires_type_provider_key_and_provider_secret(self, mock_request):
        mock_request.return_value = {"provider_consumer_key": "123"}

        with self.assertRaises(errors.ArgumentError):
            self.contextio.post_oauth_provider()
示例#23
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})

        self.webhook = WebHook(self.account, {"webhook_id": "fake_webhook_id"})
示例#24
0
 def setUp(self):
     # some of the following tests mock the OAuth session created in the constructor
     # in for these tests we must redeclare self.contextio
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
示例#25
0
 def setUp(self):
     # some of the following tests mock the OAuth session created in the constructor
     # in for these tests we must redeclare self.contextio
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
示例#26
0
class TestContextIO(unittest.TestCase):
    def setUp(self):
        # some of the following tests mock the OAuth session created in the constructor
        # in for these tests we must redeclare self.contextio
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

    def test_constructor_creates_object_with_default_config(self):
        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("https://api.context.io", self.contextio.url_base)
        self.assertEqual("2.0", self.contextio.version)
        self.assertEqual(None, self.contextio.debug)
        self.assertIsInstance(self.contextio.session, OAuth1Session)

    def test_constructor_creates_object_with_custom_config(self):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   url_base="http://fake.url",
                                   debug="print",
                                   version="lite")

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("http://fake.url", self.contextio.url_base)
        self.assertEqual("lite", self.contextio.version)
        self.assertEqual("print", self.contextio.debug)

    def test_constructor_maps_True_to_print_for_debug_value(self):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   debug=True)

        self.assertEqual("foo", self.contextio.consumer_key)
        self.assertEqual("bar", self.contextio.consumer_secret)
        self.assertEqual("print", self.contextio.debug)

    @mock.patch("contextio.contextio.six.print_")
    def test_debug_prints_message_when_debug_equals_print(
            self, mock_six_print):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   debug="print")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404

        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(mock_response.request.url,
                                mock_response.request.method,
                                mock_response.status_code,
                                mock_response.request.__dict__,
                                mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_six_print.assert_called_with(message)

    @mock.patch("contextio.contextio.logging.debug")
    def test_debug_logs_message_when_debug_equals_log(self,
                                                      mock_logging_debug):
        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   debug="log")
        mock_response = mock.Mock()
        mock_response.request.url = "fake_url"
        mock_response.request.method = "GET"
        mock_response.status_code = 404
        message = (
            "--------------------------------------------------\n"
            "URL:    {0}\nMETHOD: {1}\nSTATUS: 2\n\nREQUEST\n{3}\n\nRESPON"
            "SE\n{4}\n").format(mock_response.request.url,
                                mock_response.request.method,
                                mock_response.status_code,
                                mock_response.request.__dict__,
                                mock_response.__dict__)

        self.contextio._debug(mock_response)

        mock_logging_debug.assert_called_with(message)

    @mock.patch("contextio.contextio.pkg_resources")
    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_defaults_to_GET_method_and_include_user_agent_header(
            self, mock_session, mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_session.return_value.request = mock.Mock()
        mock_request = mock_session.return_value.request

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

        mock_request.assert_called_with(
            "GET",
            "https://api.context.io/2.0/catpants",
            data="",
            header_auth=True,
            headers={'user-agent': 'contextio/2.0/python-lib-v1.0.0'},
            params={})

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_request_uri_raises_HTTPError_if_status_not_between_200_and_300(
            self, mock_request):
        mock_request.side_effect = HTTPError

        with self.assertRaises(HTTPError):
            self.contextio._request_uri("catpants")

    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_uri_returns_response_content_if_UnicodeDecodeError_raised(
            self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.json.side_effect = UnicodeDecodeError("", "", 42, 43, "")
        mock_request.status_code = 200
        mock_request.content = bytes("foo bar")

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")

        self.assertEqual("foo bar", response)

    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_uri_returns_response_text_if_ValueError_raised(
            self, mock_session):
        mock_request = mock_session.return_value.request.return_value
        mock_request.status_code = 200
        mock_request.json.side_effect = ValueError()
        mock_request.text = "This is some text"

        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        response = self.contextio._request_uri("catpants")
        self.assertEqual("This is some text", response)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_request_uri_returns_json(self, mock_request):
        mock_request.return_value = ({"foo": "bar"})

        response = self.contextio._request_uri("catpants")

        self.assertEqual({"foo": "bar"}, response)

    @mock.patch("contextio.contextio.pkg_resources")
    @mock.patch("contextio.contextio.OAuth1Session")
    def test_request_includes_body_if_method_is_POST(self, mock_session,
                                                     mock_pkg_resources):
        mock_package = mock.Mock()
        mock_package.version = "v1.0.0"
        mock_pkg_resources.require.return_value = [mock_package]

        mock_request = mock_session.return_value.request
        mock_request.return_value.status_code = 200

        self.contextio = ContextIO(consumer_key="foo",
                                   consumer_secret="bar",
                                   version="some_version")

        self.contextio._request_uri("catpants",
                                    method="POST",
                                    body=json.dumps({"foo": "bar"}))

        mock_request.assert_called_with(
            "POST",
            "https://api.context.io/some_version/catpants",
            data={"body": '{"foo": "bar"}'},
            header_auth=True,
            headers={'user-agent': 'contextio/some_version/python-lib-v1.0.0'})

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_accounts_returns_list_of_Account_resources(
            self, mock_request):
        mock_request.return_value = {"id": "some_id"}
        accounts = self.contextio.get_accounts()

        self.assertEqual(1, len(accounts))
        self.assertIsInstance(accounts[0], Account)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_account_returns_Account_object(self, mock_request):
        mock_request.return_value = {"id": "some_id"}

        account = self.contextio.post_account(email="*****@*****.**")

        self.assertIsInstance(account, Account)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_connect_tokens_returns_list_of_ConnectToken_resources_without_token(
            self, mock_request):
        mock_request.return_value = [{
            "token": "fake_token",
            "account": {
                "id": "account_id"
            }
        }]

        connect_tokens = self.contextio.get_connect_tokens()

        self.assertEqual(1, len(connect_tokens))
        self.assertIsInstance(connect_tokens[0], ConnectToken)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_connect_tokens_returns_dictionary_if_token_is_provided(
            self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        connect_token = self.contextio.get_connect_tokens(token="fake_token")

        self.assertEqual({"token": "fake_token"}, connect_token)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_connect_token_requires_callback_url(self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        with self.assertRaises(errors.ArgumentError):
            self.contextio.post_connect_token()

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_connect_token_returns_dictionary(self, mock_request):
        mock_request.return_value = {"token": "fake_token"}

        connect_token = self.contextio.post_connect_token(
            callback_url="http://some.callback.url")

        self.assertEqual({"token": "fake_token"}, connect_token)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_discovery_returns_discovery_object(self, mock_request):
        mock_request.return_value = {"email": "*****@*****.**"}

        discovery = self.contextio.get_discovery(email="*****@*****.**")

        self.assertIsInstance(discovery, Discovery)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_discovery_requires_email_argument(self, mock_request):
        with self.assertRaises(errors.ArgumentError):
            self.contextio.get_discovery()

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_get_oauth_providers_returns_list_of_OauthProvider_objects(
            self, mock_request):
        mock_request.return_value = [{"provider_consumer_key": "1234"}]

        oauth_providers = self.contextio.get_oauth_providers()

        self.assertEqual(1, len(oauth_providers))
        self.assertIsInstance(oauth_providers[0], OauthProvider)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_oauth_provider_returns_dictionary(self, mock_request):
        mock_request.return_value = {"provider_consumer_key": "123"}

        oauth_provider = self.contextio.post_oauth_provider(
            type="VALIDOAUTHTYPE",
            provider_consumer_key="1234",
            provider_consumer_secret="1234")

        self.assertEqual({"provider_consumer_key": "123"}, oauth_provider)

    @mock.patch("contextio.contextio.ContextIO._request_uri")
    def test_post_oauth_provider_requires_type_provider_key_and_provider_secret(
            self, mock_request):
        mock_request.return_value = {"provider_consumer_key": "123"}

        with self.assertRaises(errors.ArgumentError):
            self.contextio.post_oauth_provider()
示例#27
0
    def setUp(self):
        self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
        self.account = Account(self.contextio, {"id": "fake_id"})
        self.source = Source(self.account, {"label": "foobar"})

        self.folder = Folder(self.source, {"name": "fake_folder_name"})
 def setUp(self):
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
     self.oauth_provider = OauthProvider(
         self.contextio, {"provider_consumer_key": "foobar"})
示例#29
0
 def setUp(self):
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
     self.account = Account(self.contextio, {"id": "fake_id"})
     self.thread = Thread(self.account, {"gmail_thread_id": "foobar"})
示例#30
0
 def test_ContextIOFactory_returns_Lite_instance(self):
     context_io = ContextIO(consumer_key="foo",
                            consumer_secret="bar",
                            api_version="lite")
     self.assertIsInstance(context_io, Lite)
示例#31
0
 def setUp(self):
     self.contextio = ContextIO(consumer_key="foo", consumer_secret="bar")
     self.discovery = Discovery(self.contextio, {})
示例#32
0
    def test_ContextIOFactory_returns_v2_0_instance_by_default(self):
        contextio = ContextIO(consumer_key="foo", consumer_secret="bar")

        self.assertIsInstance(contextio, V2_0)