Files
odoo_source/addons/hr/tests/test_multi_company.py
T
Martin Trigaux ffc525419a [IMP] base: avoid render with env mixup
The render API was confusing as mixing the access to the report and
the rendering env.

The ambiguity was present for code such as
`report.sudo()._render(record_ids)` where it was not clear if the
`sudo()` is needed to access to `report` or to `record_ids`. For low
priviledge users (such as portal or public), it was common to use
`report.with_user(SUPERUSER_ID)._render(record_ids)`.

This PR changes the render methods signature to be `api.model`. The
`report_ref` can be:
- ir.actions.report external id
- ir.actions.report id
- ir.actions.report recod
- `report_name` value

This will allow to call the report methods with any user and no longer
need to use `with_user(1)` to render reports as public user.

Task-id 2670865

closes odoo/odoo#91341

Related: odoo/upgrade#3650
Related: odoo/enterprise#27323
Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2022-08-02 11:48:46 +02:00

42 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import Form
from odoo.addons.hr.tests.common import TestHrCommon
from odoo.addons.base.models.ir_qweb import QWebException
class TestMultiCompany(TestHrCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.company_1 = cls.env['res.company'].create({'name': 'Opoo'})
cls.company_2 = cls.env['res.company'].create({'name': 'Otoo'})
cls.employees = cls.env['hr.employee'].create([
{'name': 'Bidule', 'company_id': cls.company_1.id},
{'name': 'Machin', 'company_id': cls.company_2.id},
])
cls.res_users_hr_officer.company_ids = [
(4, cls.company_1.id),
(4, cls.company_2.id),
]
cls.res_users_hr_officer.company_id = cls.company_1.id
# flush and invalidate the cache, otherwise a full cache may prevent
# access rights to be checked
cls.env.flush_all()
cls.env.invalidate_all()
def test_multi_company_report(self):
content, _ = self.env['ir.actions.report'].with_user(self.res_users_hr_officer).with_context(
allowed_company_ids=[self.company_1.id, self.company_2.id]
)._render_qweb_pdf('hr.hr_employee_print_badge', res_ids=self.employees.ids)
self.assertIn(b'Bidule', content)
self.assertIn(b'Machin', content)
def test_single_company_report(self):
with self.assertRaises(QWebException): # CacheMiss followed by AccessError
self.env['ir.actions.report'].with_user(self.res_users_hr_officer).with_company(
self.company_1
)._render_qweb_pdf('hr.hr_employee_print_badge', res_ids=self.employees.ids)