def test_post_customer_creation_first_method(self, mock_post): expected_body = { 'base': { 'contacts': { 'email': '*****@*****.**' } }, 'extra': 'extra', 'extended': { 'prova': 'prova' }, 'tags': { 'auto': ['auto'], 'manual': ['manual'] } } mock_post.return_value = json.loads( FakeHTTPResponse(resp_path='tests/util/fake_post_response').text) c = Customer( node=self.node, base=Properties(contacts=Properties(email='*****@*****.**'))) c.extra = 'extra' c.extended.prova = 'prova' c.tags.auto = ['auto'] c.tags.manual = ['manual'] c.post() mock_post.assert_called_with(body=expected_body, force_update=False)
def test_add_customer_tags(self, mock_get): c = Customer(node=self.node, base=Properties(contacts=Properties(email='email'))) c.extended.prova = 'prova' c.tags.auto = ['auto'] c.tags.manual = ['manual'] self.node.add_customer(**c.to_dict()) body = { 'nodeId': self.node.node_id, 'base': { 'contacts': { 'email': 'email' } }, 'extended': { 'prova': 'prova' }, 'tags': { 'auto': ['auto'], 'manual': ['manual'] } } mock_get.assert_called_with(self.base_url, headers=self.headers_expected, json=body)
def test_update_customer_not_full(self, mock_patch): c = Customer(node=self.node, id='01', base=Properties(contacts=Properties(email='email'))) c.extra = 'extra' self.node.update_customer(c.id, **c.get_mutation_tracker()) body = {'extra': 'extra'} mock_patch.assert_called_with(self.base_url + '/01', headers=self.headers_expected, json=body)
def test_customer_patch_new_prop(self): c = Customer(node=self.node, default_attributes={ 'prop': { 'prop1': 'value1' }, 'prop2': 'value2' }, prop3=Properties(prop4='value4')) c.prop5 = Properties(prop6='value5') assert c.mute == {'prop5': {'prop6': 'value5'}}, c.mute
def setUp(cls, mock_get_customers): w = Workspace(workspace_id="123", token="456") cls.node = w.get_node("123") cls.customers = cls.node.get_customers() cls.headers_expected = {'Authorization': 'Bearer 456', 'Content-Type': 'application/json'} cls.base_url_customer = 'https://api.contactlab.it/hub/v1/workspaces/123/customers' cls.customer = Customer(id='01', node=cls.node) cls.job = Job(customer=cls.customer, **{'id': '01'})
def test_post_like_create_base(self, mock_post): c = Customer(node=self.node, default_attributes={}, id='01') j = Like(customer=c, id='id', category='category', name='name', createdTime='1994-02-11T14:05M') j.post() mock_post.assert_called_with(self.base_url_customer +'/' + c.id + '/likes', headers=self.headers_expected, json=j.attributes) assert c.base.likes[0].attributes == j.attributes, (c.base.likes[0].attributes, j.attributes)
def test_put_no_timezone(self, mock_put): c = Customer(node=self.node, id='01') c.base.timezone = None c.put() params_expected = { 'id': '01', 'base': { 'contacts': {}, 'timezone': 'Europe/Rome' }, 'extended': {}, 'tags': { 'manual': [], 'auto': [] } } mock_put.assert_called_with(self.base_url_customer + '/01', headers=self.headers_expected, json=params_expected)
def test_post_job_create_base(self, mock_post): c = Customer(node=self.node, default_attributes={}, id='01') s = Subscription(customer=c, id='01', name='name', type='type', kind='SERVICE', subscribed=True, startDate='1994-10-06', endDate='1994-10-10', subscriberId='subscriberId', preferences=[{'key': 'key', 'value': 'value'}]) s.post() mock_post.assert_called_with(self.base_url_customer + '/' + c.id + '/subscriptions', headers=self.headers_expected, json=s.attributes) assert c.base.subscriptions[0].attributes == s.attributes, (c.base.subscriptions[0].attributes, s.attributes)
def delete_customer(self, id, **attributes): """ Delete the specified Customer from contacthub. For deleting an existing customer object, you should:: node.delete_customer(**c.to_dict()) :param id: a the id of the customer to delete :param attributes: the attributes of the customer to delete :return: an object representing the deleted customer """ return Customer(node=self, **self.customer_api_manager.delete(_id=id))
def test_post_job_create_base(self, mock_post): c = Customer(node=self.node, default_attributes={}, id='01') j = Job(customer=c, id='01', companyIndustry='companyIndustry', startDate=u'1994-10-06', endDate=u'1994-10-06', companyName='companyName', jobTitle='jobTitle', isCurrent=True) j.post() mock_post.assert_called_with(self.base_url_customer + '/' + c.id + '/jobs', headers=self.headers_expected, json=j.attributes) assert c.base.jobs[0].attributes == j.attributes, (c.base.jobs[0].attributes, j.attributes)
def test_post_education_create_base(self, mock_post): c = Customer(node=self.node, default_attributes={}, id='01') e = Education(customer=c, id='01', schoolType='COLLEGE', startYear=1994, endYear=2000, schoolName='schoolName', schoolConcentration='schoolConcentration', isCurrent=True) e.post() mock_post.assert_called_with(self.base_url_customer + '/' + c.id + '/educations', headers=self.headers_expected, json=e.attributes) assert c.base.educations[0].attributes == e.attributes, c.customer.attributes
def test_update_subscription(self, mock_post, mock_get): s1 = Subscription(name='name', kind='SERVICE', id='01', customer=Customer(node=self.node, id='123')) s = self.node.update_subscription(customer_id='123', **s1.to_dict()) assert isinstance(s, Subscription), type(s) assert s.id == '01', s.id mock_post.assert_called_with(self.base_url + '/123/subscriptions/01', headers=self.headers_expected, json=dict(name='name', kind='SERVICE'))
def update_customer(self, id, full_update=False, **attributes): """ Update a customer in contacthub with new data. If full_update is true, this method will update the full customer (PUT) and not only the changed data (PATCH) :param id: the customer ID for updating the customer with new attributes :param full_update: a flag for execute a full update to the customer :param attributes: the attributes to patch or put in the customer :return: the customer updated """ convert_properties_obj_in_prop(properties=attributes, properties_class=Properties) if full_update: attributes['id'] = id return Customer(node=self, **self.customer_api_manager.put(_id=id, body=attributes)) else: return Customer(node=self, **self.customer_api_manager.patch(_id=id, body=attributes))
def test_post_with_force_update(self, mock_patch, mock_post): body = { 'extra': 'extra', 'base': { 'contacts': { 'email': '*****@*****.**' } } } c = Customer.from_dict(node=self.node, attributes=body) posted = c.post(force_update=True) mock_patch.assert_called_with(self.base_url_customer + '/01', headers=self.headers_expected, json=body)
def add_customer(self, force_update=False, **attributes): """ Add a new customer in contacthub. If the customer already exist and force update is true, this method will update the entire customer with new data :param attributes: the attributes for inserting the customer in the node :param force_update: a flag for update an already present customer :return: the customer added or updated """ convert_properties_obj_in_prop(properties=attributes, properties_class=Properties) return Customer(node=self, **self.customer_api_manager.post( body=attributes, force_update=force_update))
def test_update_customer_full(self, mock_get): c = Customer( node=self.node, id='01', base=Properties(contacts=Properties(email='email', fax='fax'))) c.base.contacts.email = 'email1234' self.node.update_customer(full_update=True, **c.to_dict()) body = { 'id': '01', 'base': { 'contacts': { 'email': 'email1234', 'fax': 'fax' } }, 'extended': {}, 'tags': { 'auto': [], 'manual': [] } } mock_get.assert_called_with(self.base_url + '/01', headers=self.headers_expected, json=body)
def test_update_like(self, mock_put, mock_get): now = datetime.now() now_s = now.strftime("%Y-%m-%dT%H:%M:%SZ") l1 = Like(name='name', category='category1', createdTime=now, id='01', customer=Customer(node=self.node, id='123')) l = self.node.update_like(customer_id='123', **l1.to_dict()) assert isinstance(l, Like), type(l) assert l.name == 'name', l.name mock_put.assert_called_with(self.base_url + '/123/likes/01', headers=self.headers_expected, json=dict(name='name', category='category1', createdTime=now_s))
def test_create_customer_with_default_schema(self): c = Customer(node=self.node, default_attributes={ 'prop': { 'prop1': 'value1' }, 'prop2': 'value2' }, prop3=Properties(prop4='value4')) internal = { 'prop': { 'prop1': 'value1' }, 'prop2': 'value2', 'prop3': { 'prop4': 'value4' } } assert c.attributes == internal, c.attributes
def test_update_education(self, mock_post, mock_get): e1 = Education(schoolType='schoolType1', schoolName='schoolName1', schoolConcentration='schoolConcentration1', isCurrent=True, id='01', startYear='1994', endYear='2000', customer=Customer(node=self.node, id='123')) e = self.node.update_education(customer_id='123', **e1.to_dict()) assert isinstance(e, Education), type(e) assert e.isCurrent, e.isCurrent mock_post.assert_called_with( self.base_url + '/123/educations/01', headers=self.headers_expected, json=dict(schoolType='schoolType1', schoolName='schoolName1', schoolConcentration='schoolConcentration1', isCurrent=True, startYear='1994', endYear='2000'))
def test_update_job(self, mock_post, mock_get): j1 = Job(jobTitle='jobTitle1', companyName='companyName', companyIndustry='companyIndustry1', isCurrent=True, id='01', startDate='1994-10-06', endDate='1994-10-06', customer=Customer(node=self.node, id='123')) j = self.node.update_job(customer_id='123', **j1.to_dict()) assert isinstance(j, Job), type(j) assert j.isCurrent, j.isCurrent mock_post.assert_called_with(self.base_url + '/123/jobs/01', headers=self.headers_expected, json=dict( jobTitle='jobTitle1', companyName='companyName', companyIndustry='companyIndustry1', isCurrent=True, startDate='1994-10-06', endDate='1994-10-06'))
def get_customer(self, id=None, external_id=None): """ Retrieve a customer from the associated node by its id or external ID. Only one parameter can be specified for getting a customer. :param id: the id of the customer to retrieve :param external_id: the external id of the customer to retrieve :return: a Customer object representing the fetched customer """ if id and external_id: raise ValueError( 'Cannot get a customer by both its id and external_id') if not id and not external_id: raise ValueError('Insert an id or an external_id') if external_id: customers = self.get_customers(external_id=external_id) if len(customers) == 1: return customers[0] else: return customers else: return Customer(node=self, **self.customer_api_manager.get(_id=id))
def test_delete_customer(self, mock_get): c = Customer(node=self.node, id='01') self.node.delete_customer(c.id) mock_get.assert_called_with(self.base_url + '/01', headers=self.headers_expected)
def test_create_job_new_c(self): j = Job(customer=Customer(node=self.node), a='b') try: j.post() except AttributeError as e: assert "Customer object has no attribute 'id'" in str(e), str(e)
def test_to_dict(self): p = Properties(parent=Customer(node=self.node)) assert p.to_dict() == p.attributes, p.to_dict()
def test_customer_create_extra(self): c = Customer(node=self.node, extra='extra') assert c.attributes['extra'] == 'extra', c.attributes['extra'] assert c.extra == 'extra', c.extra
def test_from_dict_no_props(self): c = Customer.from_dict(node=self.node) assert c.attributes == {}, c.attributes prop = {} c = Customer.from_dict(node=self.node, attributes=prop) assert c.attributes is prop, c.attributes
def test_delete_created_new_customer(self): try: Customer(node=self.node).delete() except KeyError as e: assert 'id' in str(e), str(e)
def test_delete_created(self, mock_delete): Customer(id='01', node=self.node).delete() mock_delete.assert_called_with(self.base_url_customer + '/01', headers=self.headers_expected)
def test_all_events_new_customer(self): try: Customer(node=self.node).get_events() except Exception as e: assert 'events' in str(e), str(e)
def test_generate_mutation_tracker_new(self): p = Properties(parent=Customer(node=self.node)) p.prova = Properties(a='b') assert p.mute == {'prova': {'a': 'b'}}, p.mute