This commit is the 14th commit of a comprehensive refactor of our HTTP framework. See odoo/odoo#78857 for complete historic, discussions and rationnals. * `request.uid = x` => `request.update_env(user=x)`. * `request.context = x` => `request.update_env(context=x)`. * `request.context = dict(request.context, x=y)` => `request.update_context(x=y)`. * `request.cr = None` => `request.cr.close()`. * `http.mono_db()` => `request.db`. * `http.dispatch_rpc()` => `service.dispatch_rpc()`. * `@service.model.check` => `service.model.retrying()`. * `request.endpoint` => `env['ir.http']._match(request.httprequest.path)[0].endpoint`. * `request.routing_iteration `=> `removed`. * `request.jsonrequest` => `request.dispatcher.jsonrequest`. Note that `request.params` is now set much later in the process. If you are in a situation where you values from the query string or the http body you can use `request.get_http_params()`. Note that using the new `request.future_response`, it is possible to add headers and cookies on the response object before the response object is initialized. Please note that headers/cookies saved on the future response will NOT be injected in case of error. PR: odoo#78857 Task: 2571224
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.update_env(user=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.update_env(user=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", {})
|