diff --git a/hapi/contact_lists.py b/hapi/contact_lists.py index c25a9b9..f436292 100644 --- a/hapi/contact_lists.py +++ b/hapi/contact_lists.py @@ -18,10 +18,25 @@ def __init__(self, *args, **kwargs): def _get_path(self, subpath): return 'contacts/v%s/%s' % (self.options.get('version') or CONTACT_LISTS_API_VERSION, subpath) - def get_contact_lists(self, **options): + def get_a_contact_lists(self, **options): + """ Returns all of the contact lists """ return self._call('lists', method='GET', **options) - def add_contact_to_a_list(self, list_id, data=None, **options): + def add_contact_to_a_list(self, list_id, vids, data=None, **options): + """ Adds a list of contact vids to the specified list. """ data = data or {} + data['vids'] = vids return self._call('lists/{list_id}/add'.format(list_id=list_id), data=data, method='POST', **options) + + def create_a_contact_list(self, list_name, portal_id, dynamic=True, data=None, **options): + """ Creates a contact list with given list_name on the given portal_id. """ + data = data or {} + data['name'] = list_name + data['portal_id'] = portal_id + data['dynamic'] = dynamic + return self._call('lists', data=data, method='POST', **options) + + def delete_a_contact_list(self, list_id, **options): + """ Deletes the contact list by list_id. """ + return self._call('lists/{list_id}'.format(list_id=list_id), method='DELETE', **options) diff --git a/hapi/contacts.py b/hapi/contacts.py index e87b3d1..1bd1954 100644 --- a/hapi/contacts.py +++ b/hapi/contacts.py @@ -19,15 +19,22 @@ def _get_path(self, subpath): return 'contacts/v%s/%s' % (self.options.get('version') or CONTACTS_API_VERSION, subpath) def create_or_update_a_contact(self, email, data=None, **options): + """ Creates or Updates a client with the supplied data. """ data = data or {} return self._call('contact/createOrUpdate/email/{email}'.format(email=email), data=data, method='POST', **options) def get_contact_by_email(self, email, **options): + """ Gets contact specified by email address. """ return self._call('contact/email/{email}/profile'.format(email=email), method='GET', **options) def update_a_contact(self, contact_id, data=None, **options): + """ Updates the contact by contact_id with the given data. """ data = data or {} return self._call('contact/vid/{contact_id}/profile'.format(contact_id=contact_id), data=data, method='POST', **options) + + def delete_a_contact(self, contact_id, **options): + """ Deletes a contact by contact_id. """ + return self._call('contact/vid/{contact_id}'.format(contact_id=contact_id), method='DELETE', **options) diff --git a/hapi/test/test_contact_lists.py b/hapi/test/test_contact_lists.py new file mode 100644 index 0000000..6801ab6 --- /dev/null +++ b/hapi/test/test_contact_lists.py @@ -0,0 +1,72 @@ +import unittest2 +import random + +from nose.plugins.attrib import attr + +import helper +from hapi.contact_lists import ContactListsClient +from hapi.contacts import ContactsClient +from test_contacts import ContactsClientTestCase + + +class ConstactListsClientTestCase(unittest2.TestCase): + + """ Unit tests for the HubSpot Contact Lists API Python client. + + This file contains some unittest tests for the Contact Lists API. + + Questions, comments: http://developers.hubspot.com/docs/methods/lists/create_list + """ + + test_portal_id = 62515 + + def setUp(self): + self.client = ContactListsClient(**helper.get_options()) + self.contacts_client = ContactsClient(**helper.get_options()) + self.lists = [] + self.contacts =[] + + def tearDown(self): + """ Clean up all the created objects. """ + if self.contacts: + [self.contacts_client.delete_a_contact(contact) for contact in self.contacts] + if self.lists: + [self.client.delete_a_contact_list(list) for list in self.lists] + + @attr('api') + def test_get_a_contact_lists(self): + """ Test that the get contact lists endpoint is valid. """ + response = self.client.get_a_contact_lists() + self.assertTrue(len(response) > 0) + + @attr('api') + def test_add_contact_to_a_list(self): + """ Test that the add contact to a list endpoint is valid. """ + email = ContactsClientTestCase.test_contact_json['properties'][0]['value'] + contact = self.contacts_client.create_or_update_a_contact(email, data=ContactsClientTestCase.test_contact_json)['vid'] + self.contacts.append(contact) + contact_list = self.client.create_a_contact_list(list_name='test_add_contact_to_a_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + self.lists.append(contact_list['listId']) + + response = self.client.add_contact_to_a_list(contact_list['listId'], [contact]) + self.assertTrue(len(response) > 0) + + def test_create_a_contact_list(self): + """ Test that the create contact list endpoint is valid. """ + response = self.client.create_a_contact_list(list_name='test_create_a_contact_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + self.assertTrue(len(response) > 0) + + self.lists.append(response['listId']) + + def test_delete_a_contact_list(self): + """ Test that the delete contact list endpoint is valid. """ + contact_list = self.client.create_a_contact_list(list_name='test_delete_a_contact_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + + response = self.client.delete_a_contact_list(contact_list['listId']) + \ No newline at end of file diff --git a/hapi/test/test_contacts.py b/hapi/test/test_contacts.py new file mode 100644 index 0000000..bfd4a82 --- /dev/null +++ b/hapi/test/test_contacts.py @@ -0,0 +1,117 @@ +import unittest2 + +from faker import Faker +from nose.plugins.attrib import attr + +import helper +from hapi.contacts import ContactsClient + +fake = Faker() + +class ContactsClientTestCase(unittest2.TestCase): + + """ Unit tests for the HubSpot Contacts API Python client. + + This file contains some unittest tests for the Contacts API. + + Questions, comments: http://developers.hubspot.com/docs/methods/contacts/contacts-overview + """ + + test_contact_json = { + "properties": [ + { + "property": "email", + "value": fake.email() + }, + { + "property": "firstname", + "value": fake.first_name() + }, + { + "property": "lastname", + "value": fake.last_name() + }, + { + "property": "website", + "value": fake.url() + }, + { + "property": "company", + "value": fake.company() + }, + { + "property": "phone", + "value": fake.phone_number() + }, + { + "property": "address", + "value": fake.street_address() + }, + { + "property": "city", + "value": fake.city() + }, + { + "property": "state", + "value": fake.state() + }, + { + "property": "zip", + "value": fake.zipcode() + } + ] + } + + def setUp(self): + self.client = ContactsClient(**helper.get_options()) + self.contacts = [] + + def tearDown(self): + """ Cleans up the created objects. """ + if self.contacts: + [self.client.delete_a_contact(contact) for contact in self.contacts] + + @attr('api') + def test_create_or_update_a_contact(self): + """ Test the create or update a contact endpoint is valid. """ + email = self.test_contact_json['properties'][0]['value'] + + response = self.client.create_or_update_a_contact(email, data=self.test_contact_json) + self.assertTrue(len(response) > 0) + + self.contacts.append(response['vid']) + + @attr('api') + def test_get_contact_by_email(self): + """ Test that the get contact by email address endoint is valid. """ + email = self.test_contact_json['properties'][0]['value'] + contact = self.client.create_or_update_a_contact(email, data=self.test_contact_json)['vid'] + + response = self.client.get_contact_by_email(email) + self.assertTrue(len(response) > 0) + + self.contacts.append(contact) + + @attr('api') + def test_update_a_contact(self): + """ Test that the update contact endpoint is valid and that changes persist. """ + email = self.test_contact_json['properties'][0]['value'] + contact = self.client.create_or_update_a_contact(email, data=self.test_contact_json)['vid'] + new_contact_json = self.test_contact_json.copy() + new_contact_json['properties'][4]['value'] = new_contact_json['properties'][4]['value'] + ' UPDATED' + + response = self.client.update_a_contact(contact, data=self.test_contact_json) + contact_response = self.client.get_contact_by_email(email) + + self.assertEqual(contact_response['properties']['company']['value'], new_contact_json['properties'][4]['value']) + + self.contacts.append(contact) + + @attr('api') + def test_delete_a_contact(self): + """ Test that the delete contact endpoint is valid. """ + email = self.test_contact_json['properties'][0]['value'] + contact = self.client.create_or_update_a_contact(email, data=self.test_contact_json)['vid'] + + response = self.client.delete_a_contact(contact) + self.assertTrue(len(response) > 0) diff --git a/requirements.pip b/requirements.pip index 065097c..490130a 100644 --- a/requirements.pip +++ b/requirements.pip @@ -3,3 +3,4 @@ nose==1.1.2 unittest2==0.5.1 simplejson==2.2.1 +fake-factory==0.5.2