diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/crm_iap_lead_enrich/tests | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/crm_iap_lead_enrich/tests')
| -rw-r--r-- | addons/crm_iap_lead_enrich/tests/__init__.py | 5 | ||||
| -rw-r--r-- | addons/crm_iap_lead_enrich/tests/common.py | 51 | ||||
| -rw-r--r-- | addons/crm_iap_lead_enrich/tests/test_lead_enrich.py | 58 |
3 files changed, 114 insertions, 0 deletions
diff --git a/addons/crm_iap_lead_enrich/tests/__init__.py b/addons/crm_iap_lead_enrich/tests/__init__.py new file mode 100644 index 00000000..19c6636d --- /dev/null +++ b/addons/crm_iap_lead_enrich/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import common +from . import test_lead_enrich diff --git a/addons/crm_iap_lead_enrich/tests/common.py b/addons/crm_iap_lead_enrich/tests/common.py new file mode 100644 index 00000000..fb4b40e4 --- /dev/null +++ b/addons/crm_iap_lead_enrich/tests/common.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +from contextlib import contextmanager +from functools import partial +from unittest.mock import patch + +from odoo import exceptions +from odoo.addons.iap.tools import iap_tools +from odoo.addons.crm_iap_lead_enrich.models.iap_enrich_api import IapEnrichAPI +from odoo.tests import common + + +class MockIAPEnrich(common.BaseCase): + + @contextmanager + def mockIAPEnrichGateway(self, default_data=None, email_data=None, sim_error=None, failing_emails=None): + + def _contact_iap(local_endpoint, params): + sim_result = { + 'name': 'Simulator INC', + 'location': 'Simulator Street', + 'city': 'SimCity', + 'postal_code': '9876', + 'country_code': 'BE', + 'clearbit_id': 'idontknow', + 'phone_numbers': ['+3269001122', '+32456001122'], + 'twitter': 'testtwitter', + 'facebook': 'testfacebook', + } + if default_data: + sim_result.update(default_data) + # mock single sms sending + if local_endpoint == '/iap/clearbit/1/lead_enrichment_email': + result = {} + for lead_id, email in params['domains'].items(): + if sim_error and sim_error == 'credit': + raise iap_tools.InsufficientCreditError('InsufficientCreditError') + elif sim_error and sim_error == 'jsonrpc_exception': + raise exceptions.AccessError( + 'The url that this service requested returned an error. Please contact the author of the app. The url it tried to contact was ' + local_endpoint + ) + result[str(lead_id)] = dict(sim_result) + if email_data and email_data.get(email): + result[str(lead_id)].update(email_data[email]) + return result + + try: + with patch.object(IapEnrichAPI, '_contact_iap', side_effect=_contact_iap) as contact_iap_mock: + yield + finally: + pass diff --git a/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py b/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py new file mode 100644 index 00000000..2d936b26 --- /dev/null +++ b/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.crm.tests.common import TestCrmCommon +from odoo.addons.crm_iap_lead_enrich.tests.common import MockIAPEnrich +from odoo.tests.common import users + + +class TestLeadEnrich(TestCrmCommon, MockIAPEnrich): + + @classmethod + def setUpClass(cls): + super(TestLeadEnrich, cls).setUpClass() + cls.registry.enter_test_mode(cls.cr) + + cls.leads = cls.env['crm.lead'] + for x in range(0, 4): + cls.leads += cls.env['crm.lead'].create({ + 'name': 'Test %s' % x, + 'email_from': 'test_mail_%s@example.com' % x + }) + + @classmethod + def tearDownClass(cls): + cls.registry.leave_test_mode() + super().tearDownClass() + + @users('user_sales_manager') + def test_enrich_internals(self): + leads = self.env['crm.lead'].browse(self.leads.ids) + leads[0].write({'partner_name': 'Already set', 'email_from': 'test@test1'}) + leads.flush() + with self.mockIAPEnrichGateway(email_data={'test1': {'country_code': 'AU', 'state_code': 'NSW'}}): + leads.iap_enrich() + + leads.flush() + self.assertEqual(leads[0].partner_name, 'Already set') + self.assertEqual(leads[0].country_id, self.env.ref('base.au')) + self.assertEqual(leads[0].state_id, self.env.ref('base.state_au_2')) + for lead in leads[1:]: + self.assertEqual(lead.partner_name, 'Simulator INC') + for lead in leads: + self.assertEqual(lead.street, 'Simulator Street') + + # @users('sales_manager') + # def test_enrich_error_credit(self): + # leads = self.env['crm.lead'].browse(self.leads.ids) + # with self.mockIAPEnrichGateway(sim_error='credit'): + # leads.iap_enrich() + + @users('user_sales_manager') + def test_enrich_error_jsonrpc_exception(self): + leads = self.env['crm.lead'].browse(self.leads.ids) + with self.mockIAPEnrichGateway(sim_error='jsonrpc_exception'): + leads.iap_enrich() + + for lead in leads: + self.assertEqual(lead.street, False) |
