Example #1
0
    def get_contacts(self, **params):
        """List contacts in an account.

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

        Optional Arguments:
            search: string - String identifying the name or the email address
                of the contact(s) you are looking for.
            active_before: integer (unix time) - Only include contacts
                included in at least one email dated before a given time. This
                parameter should be a standard unix timestamp
            active_after: integer (unix time) - Only include contacts included
                in at least one email dated after a given time. This parameter
                should be a standard unix timestamp
            sort_by: string - The field by which to sort the returned results.
                Possible values are email, count, received_count and sent_count
            sort_order: string - The sort order of the returned results.
                Possible values are asc and desc
            limit: integer - The maximum number of results to return.
            offset: integer - Start the list at this offset (zero-based).

        Returns:
            A list of Contact objects
        """
        all_args = [
            'search', 'active_before', 'active_after', 'limit', 'offset',
            'sort_by', 'sort_order'
        ]

        params = helpers.sanitize_params(params, all_args)
        contacts = self._request_uri("contacts", params=params)

        return [Contact(self, obj) for obj in contacts.get('matches')]
class TestContact(unittest.TestCase):
    def setUp(self):
        self.contact = Contact(Mock(spec=[]), {"email": "*****@*****.**"})

    def test_constructor_creates_contact_object_with_all_attributes_in_keys_list(self):
        self.assertTrue(hasattr(self.contact, 'emails'))
        self.assertTrue(hasattr(self.contact, 'name'))
        self.assertTrue(hasattr(self.contact, 'thumbnail'))
        self.assertTrue(hasattr(self.contact, 'last_received'))
        self.assertTrue(hasattr(self.contact, 'last_sent'))
        self.assertTrue(hasattr(self.contact, 'count'))
        self.assertTrue(hasattr(self.contact, 'sent_count'))
        self.assertTrue(hasattr(self.contact, 'received_count'))
        self.assertTrue(hasattr(self.contact, 'sent_from_account_count'))
        self.assertTrue(hasattr(self.contact, 'email'))

    def test_constructor_sets_emails_if_undefined_in_definition(self):
        self.assertEqual(["*****@*****.**"], self.contact.emails)

    def test_constructor_creates_emails_list_if_doesnt_exist(self):
        self.assertEqual(["*****@*****.**"], self.contact.emails)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_updates_attributes_and_returns_True(self, mock_request):
        mock_request.return_value = {
            "email": "*****@*****.**",
            "count": 5,
            "sent_count": 200,
            "received_count": 101,
            "sent_from_account_count": 600,
            "thumbnail": "https://some.url",
            "last_sent": 2000000000,
            "last_received": 2000000000
        }

        response = self.contact.get()

        self.assertTrue(response)
        self.assertEqual("*****@*****.**", self.contact.email)
        self.assertEqual(5, self.contact.count)
        self.assertEqual(200, self.contact.sent_count)
        self.assertEqual(101, self.contact.received_count)
        self.assertEqual(600, self.contact.sent_from_account_count)
        self.assertEqual("https://some.url", self.contact.thumbnail)
        self.assertEqual(2000000000, self.contact.last_sent)
        self.assertEqual(2000000000, self.contact.last_received)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_when_email_is_None_it_is_set_from_the_list_of_emails_from_response(self, mock_request):
        mock_request.return_value = {"emails": ["*****@*****.**"]}

        self.contact.get()

        self.assertEqual("*****@*****.**", self.contact.email)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_raises_error_when_email_and_emails_is_empty_in_response(self, mock_request):
        mock_request.return_value = {}

        with self.assertRaises(MissingResourceId):
            self.contact.get()

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_files(self, mock_request):
        mock_request.return_value =[{"file_id": "foobar"}]

        contact_files = self.contact.get_files()

        self.assertEqual(1, len(contact_files))
        self.assertIsInstance(contact_files[0], File)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_messages(self, mock_request):
        mock_request.return_value = [{"message_id": "foobar"}]

        contact_messages = self.contact.get_messages()

        self.assertEqual(1, len(contact_messages))
        self.assertIsInstance(contact_messages[0], Message)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_threads(self, mock_request):
        mock_request.return_value =["https://thread.urls/thread"]

        contact_threads = self.contact.get_threads()

        self.assertEqual(1, len(contact_threads))
        self.assertIsInstance(contact_threads[0], Thread)
Example #3
0
 def setUp(self):
     self.contact = Contact(Mock(spec=[]), {"email": "*****@*****.**"})
 def setUp(self):
     self.contact = Contact(Mock(spec=[]), {"email": "*****@*****.**"})
Example #5
0
class TestContact(unittest.TestCase):
    def setUp(self):
        self.contact = Contact(Mock(spec=[]), {"email": "*****@*****.**"})

    def test_constructor_creates_contact_object_with_all_attributes_in_keys_list(
            self):
        self.assertTrue(hasattr(self.contact, 'emails'))
        self.assertTrue(hasattr(self.contact, 'name'))
        self.assertTrue(hasattr(self.contact, 'thumbnail'))
        self.assertTrue(hasattr(self.contact, 'last_received'))
        self.assertTrue(hasattr(self.contact, 'last_sent'))
        self.assertTrue(hasattr(self.contact, 'count'))
        self.assertTrue(hasattr(self.contact, 'sent_count'))
        self.assertTrue(hasattr(self.contact, 'received_count'))
        self.assertTrue(hasattr(self.contact, 'sent_from_account_count'))
        self.assertTrue(hasattr(self.contact, 'email'))

    def test_constructor_sets_emails_if_undefined_in_definition(self):
        self.assertEqual(["*****@*****.**"], self.contact.emails)

    def test_constructor_creates_emails_list_if_doesnt_exist(self):
        self.assertEqual(["*****@*****.**"], self.contact.emails)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_updates_attributes_and_returns_True(self, mock_request):
        mock_request.return_value = {
            "email": "*****@*****.**",
            "count": 5,
            "sent_count": 200,
            "received_count": 101,
            "sent_from_account_count": 600,
            "thumbnail": "https://some.url",
            "last_sent": 2000000000,
            "last_received": 2000000000
        }

        response = self.contact.get()

        self.assertTrue(response)
        self.assertEqual("*****@*****.**", self.contact.email)
        self.assertEqual(5, self.contact.count)
        self.assertEqual(200, self.contact.sent_count)
        self.assertEqual(101, self.contact.received_count)
        self.assertEqual(600, self.contact.sent_from_account_count)
        self.assertEqual("https://some.url", self.contact.thumbnail)
        self.assertEqual(2000000000, self.contact.last_sent)
        self.assertEqual(2000000000, self.contact.last_received)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_when_email_is_None_it_is_set_from_the_list_of_emails_from_response(
            self, mock_request):
        mock_request.return_value = {"emails": ["*****@*****.**"]}

        self.contact.get()

        self.assertEqual("*****@*****.**", self.contact.email)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_raises_error_when_email_and_emails_is_empty_in_response(
            self, mock_request):
        mock_request.return_value = {}

        with self.assertRaises(MissingResourceId):
            self.contact.get()

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_files(self, mock_request):
        mock_request.return_value = [{"file_id": "foobar"}]

        contact_files = self.contact.get_files()

        self.assertEqual(1, len(contact_files))
        self.assertIsInstance(contact_files[0], File)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_messages(self, mock_request):
        mock_request.return_value = [{"message_id": "foobar"}]

        contact_messages = self.contact.get_messages()

        self.assertEqual(1, len(contact_messages))
        self.assertIsInstance(contact_messages[0], Message)

    @patch("contextio.lib.resources.base_resource.BaseResource._request_uri")
    def test_get_files_returns_list_of_threads(self, mock_request):
        mock_request.return_value = ["https://thread.urls/thread"]

        contact_threads = self.contact.get_threads()

        self.assertEqual(1, len(contact_threads))
        self.assertIsInstance(contact_threads[0], Thread)