Purpose ======= Avoid creating / enriching multiple time the same company. It can occur in many situation; when IAP do not return an email address the the company, when the domain is blacklisted... We also want to remove the field "iap_enrich_info" from the res.partner model because this field can be heavy (JSON field) and the res.partner model is used a lot. Technical ========= For this purpose, we create a new model <res.partner.iap> which will store the IAP response and the requested domain. So we can retrieve the previously enriched company regardless of the partner's values Task 2466653 See odoo/odoo/pull/68286 See odoo/upgrade/pull/2301 closes odoo/odoo#68286 Related: odoo/upgrade#2301 Related: odoo/enterprise#17426 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import json
|
|
from unittest.mock import patch
|
|
|
|
from odoo.addons.mail.tests.common import mail_new_test_user
|
|
from odoo.http import request
|
|
from odoo.tests.common import HttpCase
|
|
|
|
|
|
class TestMailPluginControllerCommon(HttpCase):
|
|
def setUp(self):
|
|
super(TestMailPluginControllerCommon, self).setUp()
|
|
self.user_test = mail_new_test_user(
|
|
self.env,
|
|
login="employee",
|
|
groups="base.group_user,base.group_partner_manager",
|
|
)
|
|
|
|
def mock_plugin_partner_get(self, name, email, patched_iap_enrich):
|
|
"""Simulate a HTTP call to /partner/get with the given email and name.
|
|
|
|
The authentication process is patched to allow all queries.
|
|
The third argument "patched_iap_enrich" allow you to mock the IAP request and
|
|
to return the response you want.
|
|
"""
|
|
def patched_auth_method_outlook(*args, **kwargs):
|
|
request.uid = self.user_test.id
|
|
|
|
data = {
|
|
"id": 0,
|
|
"jsonrpc": "2.0",
|
|
"method": "call",
|
|
"params": {"email": email, "name": name},
|
|
}
|
|
|
|
with patch(
|
|
"odoo.addons.mail_plugin.models.ir_http.IrHttp"
|
|
"._auth_method_outlook",
|
|
new=patched_auth_method_outlook,
|
|
), patch(
|
|
"odoo.addons.mail_plugin.controllers.mail_plugin.MailPluginController"
|
|
"._iap_enrich",
|
|
new=patched_iap_enrich,
|
|
):
|
|
result = self.url_open(
|
|
"/mail_plugin/partner/get",
|
|
data=json.dumps(data).encode(),
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
|
|
if not result.ok:
|
|
return {}
|
|
|
|
return result.json().get("result", {})
|
|
|
|
def mock_enrich_and_create_company(self, partner_id, patched_iap_enrich):
|
|
"""Simulate a HTTP call to /partner/enrich_and_create_company on the given partner.
|
|
|
|
The third argument "patched_iap_enrich" allow you to mock the IAP request and
|
|
to return the response you want.
|
|
"""
|
|
def patched_auth_method_outlook(*args, **kwargs):
|
|
request.uid = self.user_test.id
|
|
|
|
data = {
|
|
"id": 0,
|
|
"jsonrpc": "2.0",
|
|
"method": "call",
|
|
"params": {"partner_id": partner_id},
|
|
}
|
|
|
|
with patch(
|
|
"odoo.addons.mail_plugin.models.ir_http.IrHttp"
|
|
"._auth_method_outlook",
|
|
new=patched_auth_method_outlook,
|
|
), patch(
|
|
"odoo.addons.mail_plugin.controllers.mail_plugin.MailPluginController"
|
|
"._iap_enrich",
|
|
new=patched_iap_enrich,
|
|
):
|
|
result = self.url_open(
|
|
"/mail_plugin/partner/enrich_and_create_company",
|
|
data=json.dumps(data).encode(),
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
|
|
if not result.ok:
|
|
return {}
|
|
|
|
return result.json().get("result", {})
|