Exemple #1
0
    def get_sources(self, **params):
        """Lists IMAP sources assigned for an account.

        GET method for sources resource.

        Documentation: http://context.io/docs/2.0/accounts/sources#get

        Optional Arguments:
            status: string - Only return sources whose status is of a specific
                value. Possible statuses are: INVALID_CREDENTIALS,
                CONNECTION_IMPOSSIBLE, NO_ACCESS_TO_ALL_MAIL, OK,
                TEMP_DISABLED, and DISABLED
            status_ok: integer - Set to 0 to get sources that are not working
                correctly. Set to 1 to get those that are.

        Returns:
            A list of Source objects
        """
        all_args = ['status', 'status_ok']
        params = helpers.sanitize_params(params, all_args)

        return [
            Source(self, obj)
            for obj in self._request_uri("sources", params=params)
        ]
    def __init__(self, parent, definition):
        """Constructor.

        Required Arguments:
            parent: Account object - parent is an Account object.
            definition: a dictionary of parameters. The 'gmail_thread_id' parameter
                is required to make method calls.
        """
        super(Thread, self).__init__(parent, 'threads/{gmail_thread_id}',
                                     definition)

        # This is going to be gross - prepare yourself

        if "messages" in definition:
            from contextio.lib.resources.message import Message
            self.messages = [
                Message(self.parent, message)
                for message in definition['messages']
            ]

        if 'sources' in definition:
            from contextio.lib.resources.source import Source
            self.sources = [
                Source(self.parent, source) for source in definition['sources']
            ]
Exemple #3
0
    def post_source(self, **params):
        """Add a mailbox to a given account.

        Documentation: http://context.io/docs/2.0/accounts/sources#post

        Required Arguments:
            email: string - The primary email address used to receive emails
                in this account
            server: string - Name of IP of the IMAP server, eg. imap.gmail.com
            username: string - The username used to authenticate an IMAP
                connection. On some servers, this is the same thing as
                the primary email address.
            use_ssl: integer - Set to 1 if you want SSL encryption to
                be used when opening connections to the IMAP server. Any
                other value will be considered as "do not use SSL"
            port: integer - Port number to connect to on the server. Keep in
                mind that most IMAP servers will have one port for standard
                connection and another one for encrypted connection (see
                use-ssl parameter above)
            type: string - Currently, the only supported type is IMAP

        Optional Arguments:
            origin_ip: string - IP address of the end user requesting the account
                to be created
            expunge_on_deleted_flag: integer - By default, we don't filter out messages
                flagged as deleted. Set this parameter to 1 to turn on this filtering.
            sync_all_folders: integer - By default, we filter out some folders like
                'Deleted Items' and 'Drafts'. Set this parameter to 1 to turn off this
                filtering and show every single folder.
            sync_folders: string - By default, we filter out some folders like
                'Deleted Items' and 'Drafts'. Set this parameter to
                'All,Trash' to show the 'Deleted Items' folder.
            sync_flags:  integer By default, we don't synchronize IMAP flags.
                Set this parameter to 1 to turn on IMAP flag syncing for the 'seen' and
                'flagged' flags.
            raw_file_list: integer - By default, we filter out files like
                signature images or those winmail.dat files form the files
                list. Set this parameter to 1 to turn off this filtering and
                show every single file attachments.
            password: string - Password for authentication on the IMAP server.
                Ignored if any of the provider_* parameters are set below.
            provider_refresh_token: An OAuth2 refresh token obtained from the
                IMAP account provider to be used to authenticate on this email
                account.
            provider_consumer_key: string - The OAuth consumer key used to
                obtain the the token and token secret above for that account.
                That consumer key and secret must be configured in your
                Context.IO account.
            callback_url: string (url) - If specified, we'll make a POST
                request to this URL when the initial sync is completed.
            status_callback_url: string (url) - If specified, we'll make a POST
                request to this URL if the connection status of the source changes.

        Returns:
            A mostly empty Source object or False if something failed.
        """
        # set some default values
        if 'use_ssl' not in params:
            params['use_ssl'] = 1
        if 'port' not in params:
            params['port'] = 993
        if 'type' not in params:
            params['type'] = 'IMAP'

        req_args = ['email', 'server', 'username', 'port', 'type', 'use_ssl']
        all_args = [
            'email', 'server', 'username', 'port', 'type', 'use_ssl',
            'origin_ip', 'expunge_on_deleted_flag', 'sync_all_folders',
            'sync_folders', 'sync_flags', 'raw_file_list', 'password',
            'provider_refresh_token', 'provider_consumer_key', 'callback_url',
            'status_callback_url'
        ]

        data = super(Account, self).post(uri="sources",
                                         return_bool=False,
                                         params=params,
                                         all_args=all_args,
                                         required_args=req_args)

        if data.get("success") is False:
            return False

        return Source(self, {'label': data['label']})
Exemple #4
0
 def setUp(self):
     self.source = Source(Mock(spec=[]), {"label": "foobar"})
Exemple #5
0
class TestSource(unittest.TestCase):
    def setUp(self):
        self.source = Source(Mock(spec=[]), {"label": "foobar"})

    def test_constructor_creates_message_object_with_all_attributes_in_keys_list(self):
        self.assertTrue(hasattr(self.source, "username"))
        self.assertTrue(hasattr(self.source, "status"))
        self.assertTrue(hasattr(self.source, "type"))
        self.assertTrue(hasattr(self.source, "label"))
        self.assertTrue(hasattr(self.source, "use_ssl"))
        self.assertTrue(hasattr(self.source, "resource_url"))
        self.assertTrue(hasattr(self.source, "server"))
        self.assertTrue(hasattr(self.source, "port"))

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_delete_connect_token_calls_request_uri_with_correct_args(self, mock_request):
        self.source.delete_connect_token("fake_token_id")

        mock_request.assert_called_with("connect_tokens/fake_token_id", method="DELETE")

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_connect_token_returns_a_ConnectToken(self, mock_request):
        mock_request.return_value = {"token": "fake_token_id"}
        response = self.source.get_connect_token("fake_token_id")

        mock_request.is_called_with("connect_tokens/fake_token_id")
        self.assertIsInstance(response, ConnectToken)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_connect_tokens_returns_a_list_of_ConnectTokens(self, mock_request):
        mock_request.return_value = [{"token": "fake_token_id"}, {"token": "fake_token_id_2"}]
        response = self.source.get_connect_tokens()

        mock_request.is_called_with("connect_tokens/fake_token_id")
        self.assertEqual(2, len(response))
        self.assertIsInstance(response[0], ConnectToken)
        self.assertIsInstance(response[1], ConnectToken)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_post_calls_request_uri_with_correct_args(self, mock_request):
        mock_request.return_value = {"success": True}
        response = self.source.post_connect_token(callback_url="http://some.url")

        mock_request.is_called_with(
            "connect_tokens", method="POST", params={"callback_url": "http://some.url"})
        self.assertEqual({"success": True}, response)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_folder_returns_a_list_of_Folders(self, mock_request):
        mock_request.return_value = [{"name": "folder_name"}, {"name": "folder_name_2"}]
        response = self.source.get_folders()

        mock_request.is_called_with("folders")
        self.assertEqual(2, len(response))
        self.assertIsInstance(response[0], Folder)
        self.assertIsInstance(response[1], Folder)


    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_sync_returns_a_dictionary(self, mock_request):
        mock_request.return_value = {"foo": "bar"}
        response = self.source.get_sync()

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

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_post_sync_calls_request_uri_with_correct_args(self, mock_request):
        mock_request.return_value = {"foo": "bar"}
        response = self.source.post_sync()

        mock_request.assert_called_with("sync", method="POST")
        self.assertEqual({"foo": "bar"}, response)