Skip to content
Snippets Groups Projects
Commit 37503108 authored by std-odoo's avatar std-odoo
Browse files

[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: default avatarThibault Delavallee (tde) <tde@openerp.com>
parent da18d801
No related branches found
No related tags found
No related merge requests found
...@@ -262,6 +262,10 @@ class MailPluginController(http.Controller): ...@@ -262,6 +262,10 @@ class MailPluginController(http.Controller):
Returns enrichment data for a given domain, in case an error happens the response will Returns enrichment data for a given domain, in case an error happens the response will
contain an enrichment_info key explaining what went wrong 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 = {} enriched_data = {}
try: try:
response = request.env['iap.enrich.api']._request_enrich({domain: domain}) # The key doesn't matter response = request.env['iap.enrich.api']._request_enrich({domain: domain}) # The key doesn't matter
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details. # 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.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): class TestMailPluginController(TestMailPluginControllerCommon):
...@@ -29,49 +30,33 @@ class TestMailPluginController(TestMailPluginControllerCommon): ...@@ -29,49 +30,33 @@ class TestMailPluginController(TestMailPluginControllerCommon):
partner.invalidate_cache() partner.invalidate_cache()
self.assertEqual(partner.parent_id, company, "Should change the company of the partner") 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): def test_get_partner_blacklisted_domain(self):
"""Test enrichment on a blacklisted domain. """Test enrichment on a blacklisted domain, should return an error."""
Even is the domain is blacklisted, we should not duplicate the company each
time a request is made.
"""
domain = list(iap_tools._MAIL_DOMAIN_BLACKLIST)[0] domain = list(iap_tools._MAIL_DOMAIN_BLACKLIST)[0]
result = self.mock_plugin_partner_get( data = {
"Test", "qsd@" + domain, "id": 0,
lambda _, __: { "jsonrpc": "2.0",
"name": "Name", "method": "call",
"email": ["contact@" + domain], "params": {"email": "contact@" + domain, "name": "test"},
"iap_information": "test", }
},
) mocked_request_enrich = Mock()
first_company_id = result["partner"]["company"]["id"] with patch(
self.assertTrue(first_company_id and first_company_id > 0) "odoo.addons.iap.models.iap_enrich_api.IapEnrichAPI"
self.assertEqual(result["partner"]["company"]["additionalInfo"]["iap_information"], "test") "._request_enrich",
new=mocked_request_enrich,
first_company = self.env["res.partner"].browse(first_company_id) ):
self.assertEqual(first_company.name, "Name") result = self.url_open(
self.assertEqual(first_company.email, "contact@" + domain) "/mail_plugin/partner/get",
data=json.dumps(data).encode(),
# Test that we do not duplicate the company and that we return the previous one headers={"Content-Type": "application/json"},
mock_iap_enrich = Mock() ).json().get("result", {})
result = self.mock_plugin_partner_get("Test", "qsd@" + domain, mock_iap_enrich)
self.assertFalse( self.assertFalse(mocked_request_enrich.called)
mock_iap_enrich.called, self.assertEqual(result['partner']['enrichment_info']['type'], 'missing_data')
"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")
def test_get_partner_company_found(self): def test_get_partner_company_found(self):
company = self.env["res.partner"].create({ company = self.env["res.partner"].create({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment