diff --git a/addons/mail_plugin/controllers/mail_plugin.py b/addons/mail_plugin/controllers/mail_plugin.py index dacf50d6c7c75e6270b18ef578f2d1cca82c9014..1d747cc419416dd4fd528a80e24d4911c69a60f2 100644 --- a/addons/mail_plugin/controllers/mail_plugin.py +++ b/addons/mail_plugin/controllers/mail_plugin.py @@ -262,6 +262,10 @@ class MailPluginController(http.Controller): Returns enrichment data for a given domain, in case an error happens the response will contain an enrichment_info key explaining what went wrong """ + if domain in iap_tools._MAIL_DOMAIN_BLACKLIST: + # Can not enrich the provider domain names (gmail.com; outlook.com, etc) + return {'enrichment_info': {'type': 'missing_data'}} + enriched_data = {} try: response = request.env['iap.enrich.api']._request_enrich({domain: domain}) # The key doesn't matter diff --git a/addons/mail_plugin/tests/test_controller.py b/addons/mail_plugin/tests/test_controller.py index fe5aea21a41a8bfbca94b0f0a58e53731bdc97a1..fa788ee1a11a3d32619eee9bfdca84af7d6f5f4c 100644 --- a/addons/mail_plugin/tests/test_controller.py +++ b/addons/mail_plugin/tests/test_controller.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from unittest.mock import Mock +import json +from unittest.mock import Mock, patch from odoo.addons.iap.tools import iap_tools -from odoo.addons.mail_plugin.tests.common import TestMailPluginControllerCommon +from odoo.addons.mail_plugin.tests.common import TestMailPluginControllerCommon, mock_auth_method_outlook class TestMailPluginController(TestMailPluginControllerCommon): @@ -29,49 +30,33 @@ class TestMailPluginController(TestMailPluginControllerCommon): partner.invalidate_cache() self.assertEqual(partner.parent_id, company, "Should change the company of the partner") + @mock_auth_method_outlook('employee') def test_get_partner_blacklisted_domain(self): - """Test enrichment on a blacklisted domain. - - Even is the domain is blacklisted, we should not duplicate the company each - time a request is made. - """ + """Test enrichment on a blacklisted domain, should return an error.""" domain = list(iap_tools._MAIL_DOMAIN_BLACKLIST)[0] - result = self.mock_plugin_partner_get( - "Test", "qsd@" + domain, - lambda _, __: { - "name": "Name", - "email": ["contact@" + domain], - "iap_information": "test", - }, - ) - - first_company_id = result["partner"]["company"]["id"] - self.assertTrue(first_company_id and first_company_id > 0) - self.assertEqual(result["partner"]["company"]["additionalInfo"]["iap_information"], "test") - - first_company = self.env["res.partner"].browse(first_company_id) - self.assertEqual(first_company.name, "Name") - self.assertEqual(first_company.email, "contact@" + domain) - - # Test that we do not duplicate the company and that we return the previous one - mock_iap_enrich = Mock() - result = self.mock_plugin_partner_get("Test", "qsd@" + domain, mock_iap_enrich) - self.assertFalse( - mock_iap_enrich.called, - "We already enriched this company, should not call IAP a second time") - - second_company_id = result["partner"]["company"]["id"] - self.assertEqual(first_company_id, second_company_id, "Should not create a new company") - - # But the same blacklisted domain on a different local part - # should create a new company (e.g.: asbl_XXXX@gmail.com VS asbl_YYYY@gmail.com) - result = self.mock_plugin_partner_get( - "Test", "asbl@" + domain, - lambda _, domain: {"name": "Name", "email": ["asbl@" + domain]}, - ) - second_company_id = result["partner"]["company"]["id"] - self.assertNotEqual(first_company_id, second_company_id, "Should create a new company") + data = { + "id": 0, + "jsonrpc": "2.0", + "method": "call", + "params": {"email": "contact@" + domain, "name": "test"}, + } + + mocked_request_enrich = Mock() + + with patch( + "odoo.addons.iap.models.iap_enrich_api.IapEnrichAPI" + "._request_enrich", + new=mocked_request_enrich, + ): + result = self.url_open( + "/mail_plugin/partner/get", + data=json.dumps(data).encode(), + headers={"Content-Type": "application/json"}, + ).json().get("result", {}) + + self.assertFalse(mocked_request_enrich.called) + self.assertEqual(result['partner']['enrichment_info']['type'], 'missing_data') def test_get_partner_company_found(self): company = self.env["res.partner"].create({