From 37503108cd878412474b5486877a861dad2efc1d Mon Sep 17 00:00:00 2001 From: std-odoo <std@odoo.com> Date: Thu, 24 Nov 2022 13:50:48 +0000 Subject: [PATCH] [FIX] mail_plugin: do not enrich blacklisted domains Bug === Currently, we try to enrich the domains even if they are in the `_MAIL_DOMAIN_BLACKLIST`. IAP always return a "missing data" error because it can't enrich "gmail.com", etc except for "odoo.com". In that case the enrichment is successful, but because the domain is blacklisted, we use the entire email to find the company (and so it will create a company for each odoo.com email addresses). The test that was removed was wrong. It works because we mocked the enrichment response, but in practice it will always return a missing data error. Task-3050230 closes odoo/odoo#104621 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com> --- addons/mail_plugin/controllers/mail_plugin.py | 4 ++ addons/mail_plugin/tests/test_controller.py | 69 ++++++++----------- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/addons/mail_plugin/controllers/mail_plugin.py b/addons/mail_plugin/controllers/mail_plugin.py index dacf50d6c7c7..1d747cc41941 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 fe5aea21a41a..fa788ee1a11a 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({ -- GitLab