|
| 1 | +# -*- coding: utf-8 -*- # noqa |
| 2 | + |
| 3 | +import mock |
| 4 | +import unittest |
| 5 | + |
| 6 | +from intercom.collection_proxy import CollectionProxy |
| 7 | +from intercom.client import Client |
| 8 | +from intercom.lead import Lead |
| 9 | +from intercom.user import User |
| 10 | +from mock import patch |
| 11 | +from nose.tools import istest |
| 12 | +from tests.unit import get_user |
| 13 | + |
| 14 | + |
| 15 | +class LeadTest(unittest.TestCase): # noqa |
| 16 | + |
| 17 | + def setUp(self): # noqa |
| 18 | + self.client = Client() |
| 19 | + |
| 20 | + @istest |
| 21 | + def it_should_be_listable(self): # noqa |
| 22 | + proxy = self.client.leads.all() |
| 23 | + self.assertEquals('contacts', proxy.resource_name) |
| 24 | + self.assertEquals('/contacts', proxy.finder_url) |
| 25 | + self.assertEquals(Lead, proxy.resource_class) |
| 26 | + |
| 27 | + @istest |
| 28 | + def it_should_not_throw_errors_when_there_are_no_parameters(self): # noqa |
| 29 | + with patch.object(Client, 'post') as mock_method: # noqa |
| 30 | + self.client.leads.create() |
| 31 | + |
| 32 | + @istest |
| 33 | + def it_can_update_a_lead_with_an_id(self): # noqa |
| 34 | + lead = Lead(id="de45ae78gae1289cb") |
| 35 | + with patch.object(Client, 'put') as mock_method: # noqa |
| 36 | + self.client.leads.save(lead) |
| 37 | + mock_method.assert_called_once_with( |
| 38 | + '/contacts/de45ae78gae1289cb', {'custom_attributes': {}}) |
| 39 | + |
| 40 | + @istest |
| 41 | + def it_can_convert(self): # noqa |
| 42 | + lead = Lead.from_api({'user_id': 'contact_id'}) |
| 43 | + user = User.from_api({'id': 'user_id'}) |
| 44 | + |
| 45 | + with patch.object(Client, 'post', returns=get_user()) as mock_method: # noqa |
| 46 | + self.client.leads.convert(lead, user) |
| 47 | + mock_method.assert_called_once_with( |
| 48 | + '/contacts/convert', |
| 49 | + { |
| 50 | + 'contact': {'user_id': lead.user_id}, |
| 51 | + 'user': {'id': user.id} |
| 52 | + }) |
| 53 | + |
| 54 | + @istest |
| 55 | + def it_returns_a_collectionproxy_for_all_without_making_any_requests(self): # noqa |
| 56 | + with mock.patch('intercom.request.Request.send_request_to_path', new_callable=mock.NonCallableMock): # noqa |
| 57 | + res = self.client.leads.all() |
| 58 | + self.assertIsInstance(res, CollectionProxy) |
| 59 | + |
| 60 | + @istest |
| 61 | + def it_deletes_a_contact(self): # noqa |
| 62 | + lead = Lead(id="1") |
| 63 | + with patch.object(Client, 'delete') as mock_method: # noqa |
| 64 | + self.client.leads.delete(lead) |
| 65 | + mock_method.assert_called_once_with('/contacts/1', {}) |
0 commit comments