[MERGE] web, hr, test_website: Add multi-company tests
test_website: Add multi-company test ============================= Purpose ------------ This commit tests some behaviors with multi company mode after changes brought by f847a46. Specification ------------------- The website company should be automatically set in the context as 'allowed_company_ids'. That way, we can remove the switch company menu on the website, which doesn't make a lot of sense. This test enforce this behavior. web: Add QUnit tests for the SwitchCompanyMenu ========================================= Purpose ------------ This commit tests some behaviors with multi company mode after changes brought by f847a46. Specification ------------------ Ensure the robustness of the switch company menu for multiple cases like: - Bar rendering - Switch to another company in mono/multi environment - Toggle another company in mono/multi environment - Remove a company in mono/multi environment The widget itself has been slighty modified to make it more testable: - Reading from session is now in the `willStart` function instead of `init` to allow mocking the session (in tests, `init` is executed before the mock is applied). - In the event handlers, don't use `bbq` to retrieve current allowed company ids. `bbq` is not easily nor cleanly patched. - When reading allowed company ids from session, cast ids from string to integer. They are everywhere casted except there. and some linting hr: Add multi company tests ====================== Purpose ------------- This commit tests some behaviors with multi company mode after changes brought by f847a46. Specification ------------------ Check that a report printing for several records on different companies: - Works if the context is correctly set, i.e. both companies are in the allowed_company_ids - Doesn't work if the context is not correctly set
This commit is contained in:
@@ -5,3 +5,4 @@ from . import test_hr_employee
|
||||
from . import test_channel
|
||||
from . import test_hr_flow
|
||||
from . import test_self_user_access
|
||||
from . import test_multi_company
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# -*- 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.qweb import QWebException
|
||||
|
||||
|
||||
class TestMultiCompany(TestHrCommon):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.company_1 = self.env['res.company'].create({'name': 'Opoo'})
|
||||
self.company_2 = self.env['res.company'].create({'name': 'Otoo'})
|
||||
self.employees = self.env['hr.employee'].create([
|
||||
{'name': 'Bidule', 'company_id': self.company_1.id},
|
||||
{'name': 'Machin', 'company_id': self.company_2.id},
|
||||
])
|
||||
self.res_users_hr_officer.company_ids = [
|
||||
(4, self.company_1.id),
|
||||
(4, self.company_2.id),
|
||||
]
|
||||
|
||||
def test_multi_company_report(self):
|
||||
content, content_type = self.env.ref('hr.hr_employee_print_badge').sudo(self.res_users_hr_officer).with_context(
|
||||
allowed_company_ids=[self.company_1.id, self.company_2.id]
|
||||
).render_qweb_pdf(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
|
||||
content, content_type = self.env.ref('hr.hr_employee_print_badge').sudo(self.res_users_hr_officer).with_context(
|
||||
allowed_company_ids=[self.company_1.id]
|
||||
).render_qweb_pdf(res_ids=self.employees.ids)
|
||||
@@ -36,3 +36,7 @@ class WebsiteTest(Home):
|
||||
@http.route('/ignore_args/converter/<string:a>/nokw', type='http', auth="public", website=True)
|
||||
def test_ignore_args_converter_nokw(self, a, b='youhou'):
|
||||
return request.make_response(json.dumps(dict(a=a, b=b)))
|
||||
|
||||
@http.route('/multi_company_website', type='http', auth="public", website=True)
|
||||
def test_company_context(self):
|
||||
return request.make_response(json.dumps(request.context.get('allowed_company_ids')))
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
from . import test_controller_args
|
||||
from . import test_reset_views
|
||||
from . import test_views_during_module_operation
|
||||
from . import test_multi_company
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo.tests.common import HttpCase, tagged
|
||||
|
||||
|
||||
@tagged('post_install', '-at_install')
|
||||
class TestMultiCompany(HttpCase):
|
||||
|
||||
def test_company_in_context(self):
|
||||
""" Test website company is set in context """
|
||||
website = self.env['website'].browse(1)
|
||||
company = self.env['res.company'].create({'name': "Adaa"})
|
||||
website.company_id = company
|
||||
response = self.url_open('/multi_company_website')
|
||||
self.assertEqual(response.json()[0], company.id)
|
||||
@@ -25,21 +25,25 @@ var SwitchCompanyMenu = Widget.extend({
|
||||
* @override
|
||||
*/
|
||||
init: function () {
|
||||
var self = this;
|
||||
this._super.apply(this, arguments);
|
||||
this.isMobile = config.device.isMobile;
|
||||
this._onSwitchCompanyClick = _.debounce(this._onSwitchCompanyClick, 1500, true);
|
||||
this.allowed_company_ids = String(session.user_context.allowed_company_ids).split(',');
|
||||
this.user_companies = session.user_companies.allowed_companies;
|
||||
},
|
||||
|
||||
var hash = $.bbq.getState()
|
||||
if (!hash.cids || hash.cids === undefined) {
|
||||
hash.cids = String(session.user_companies.current_company[0]);
|
||||
}
|
||||
this.current_company = parseInt(hash.cids.split(',')[0]);
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
willStart: function () {
|
||||
var self = this;
|
||||
this.allowed_company_ids = String(session.user_context.allowed_company_ids)
|
||||
.split(',')
|
||||
.map(function (id) {return parseInt(id);});
|
||||
this.user_companies = session.user_companies.allowed_companies;
|
||||
this.current_company = this.allowed_company_ids[0];
|
||||
this.current_company_name = _.find(session.user_companies.allowed_companies, function (company) {
|
||||
return company[0] === self.current_company;
|
||||
})[1];
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -52,11 +56,10 @@ var SwitchCompanyMenu = Widget.extend({
|
||||
*/
|
||||
_onSwitchCompanyClick: function (ev) {
|
||||
ev.stopPropagation();
|
||||
var dropdownItem = $(ev.currentTarget).parent()
|
||||
var dropdownMenu = dropdownItem.parent()
|
||||
var dropdownItem = $(ev.currentTarget).parent();
|
||||
var dropdownMenu = dropdownItem.parent();
|
||||
var companyID = dropdownItem.data('company-id');
|
||||
var hash = $.bbq.getState()
|
||||
var allowed_company_ids = _.map(hash.cids.split(','), function(company_id) {return parseInt(company_id);});
|
||||
var allowed_company_ids = this.allowed_company_ids;
|
||||
if (dropdownItem.find('.fa-square-o').length) {
|
||||
// 1 enabled company: Stay in single company mode
|
||||
if (this.allowed_company_ids.length === 1) {
|
||||
@@ -81,11 +84,9 @@ var SwitchCompanyMenu = Widget.extend({
|
||||
*/
|
||||
_onToggleCompanyClick: function (ev) {
|
||||
ev.stopPropagation();
|
||||
var dropdownItem = $(ev.currentTarget).parent()
|
||||
var dropdownMenu = dropdownItem.parent()
|
||||
var dropdownItem = $(ev.currentTarget).parent();
|
||||
var companyID = dropdownItem.data('company-id');
|
||||
var hash = $.bbq.getState()
|
||||
var allowed_company_ids = _.map(hash.cids.split(','), function(company_id) {return parseInt(company_id);});
|
||||
var allowed_company_ids = this.allowed_company_ids;
|
||||
var current_company_id = allowed_company_ids[0];
|
||||
if (dropdownItem.find('.fa-square-o').length) {
|
||||
allowed_company_ids.push(companyID);
|
||||
|
||||
@@ -1469,21 +1469,21 @@
|
||||
<div role="menuitem" href="#" class="dropdown-item d-flex py-0 px-0" data-menu="company" t-att-data-company-id="company[0]">
|
||||
<div class="ml-auto pl-3 pr-3 border border-top-0 border-left-0 border-bottom-0 toggle_company o_py">
|
||||
<span style="height: 2rem;">
|
||||
<t t-if="widget.allowed_company_ids.includes(company[0].toString())">
|
||||
<t t-if="widget.allowed_company_ids.includes(company[0])">
|
||||
<i class="fa fa-fw fa-check-square pt-2"></i>
|
||||
</t>
|
||||
<t t-if="!widget.allowed_company_ids.includes(company[0].toString())">
|
||||
<t t-if="!widget.allowed_company_ids.includes(company[0])">
|
||||
<i class="fa fa-fw fa-square-o pt-2"></i>
|
||||
</t>
|
||||
</span>
|
||||
</div>
|
||||
<div class="d-flex flex-grow-1 align-items-center py-0 log_into pl-3 o_py" t-att-style="company[0] === widget.current_company ? 'background-color: lightgrey;' : ''">
|
||||
<t t-if="widget.allowed_company_ids.includes(company[0].toString())">
|
||||
<t t-if="widget.allowed_company_ids.includes(company[0])">
|
||||
<span class='mr-3 company_label'>
|
||||
<t t-esc="company[1]"/>
|
||||
</span>
|
||||
</t>
|
||||
<t t-if="!widget.allowed_company_ids.includes(company[0].toString())">
|
||||
<t t-if="!widget.allowed_company_ids.includes(company[0])">
|
||||
<span class='mr-3 company_label text-muted'>
|
||||
<t t-esc="company[1]"/>
|
||||
</span>
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
odoo.define('web.SwitchCompanyMenu_tests', function (require) {
|
||||
"use strict";
|
||||
|
||||
var SwitchCompanyMenu = require('web.SwitchCompanyMenu');
|
||||
var testUtils = require('web.test_utils');
|
||||
|
||||
|
||||
async function createSwitchCompanyMenu(params) {
|
||||
params = params || {};
|
||||
var target = params.debug ? document.body : $('#qunit-fixture');
|
||||
var menu = new SwitchCompanyMenu();
|
||||
testUtils.mock.addMockEnvironment(menu, params);
|
||||
await menu.appendTo(target)
|
||||
return menu
|
||||
}
|
||||
|
||||
|
||||
async function initMockCompanyMenu(assert, params) {
|
||||
var menu = await createSwitchCompanyMenu({
|
||||
session: {
|
||||
...params.session,
|
||||
setCompanies: function (mainCompanyId, companyIds) {
|
||||
assert.equal(mainCompanyId, params.assertMainCompany[0], params.assertMainCompany[1]);
|
||||
assert.equal(_.intersection(companyIds, params.asserCompanies[0]).length, params.asserCompanies[0].length, params.asserCompanies[1]);
|
||||
},
|
||||
}
|
||||
})
|
||||
await testUtils.dom.click(menu.$('.dropdown-toggle')); // open company switcher dropdown
|
||||
return menu
|
||||
}
|
||||
|
||||
async function testSwitchCompany(assert, params) {
|
||||
assert.expect(2);
|
||||
var menu = await initMockCompanyMenu(assert, params);
|
||||
await testUtils.dom.click(menu.$(`div[data-company-id=${params.company}] div.log_into`));
|
||||
menu.destroy();
|
||||
}
|
||||
|
||||
async function testToggleCompany(assert, params) {
|
||||
assert.expect(2);
|
||||
var menu = await initMockCompanyMenu(assert, params);
|
||||
await testUtils.dom.click(menu.$(`div[data-company-id=${params.company}] div.toggle_company`));
|
||||
menu.destroy();
|
||||
}
|
||||
|
||||
QUnit.module('widgets', {
|
||||
beforeEach: async function () {
|
||||
this.session_mock_multi = {
|
||||
user_companies: {
|
||||
current_company: [1, "Company 1"],
|
||||
allowed_companies: [[1, "Company 1"], [2, "Company 2"], [3, "Company 3"]],
|
||||
},
|
||||
user_context: { allowed_company_ids: [1, 3] },
|
||||
},
|
||||
this.session_mock_single = {
|
||||
user_companies: {
|
||||
current_company: [1, "Company 1"],
|
||||
allowed_companies: [[1, "Company 1"], [2, "Company 2"], [3, "Company 3"]],
|
||||
},
|
||||
user_context: { allowed_company_ids: [1] },
|
||||
}
|
||||
},
|
||||
|
||||
}, function () {
|
||||
|
||||
QUnit.module('SwitchCompanyMenu', {}, function () {
|
||||
|
||||
QUnit.test("Company switcher basic rendering", async function (assert) {
|
||||
assert.expect(6);
|
||||
var menu = await createSwitchCompanyMenu({ session: this.session_mock_multi });
|
||||
assert.equal(menu.$('.company_label:contains(Company 1)').length, 1, "it should display Company 1")
|
||||
assert.equal(menu.$('.company_label:contains(Company 2)').length, 1, "it should display Company 2")
|
||||
assert.equal(menu.$('.company_label:contains(Company 3)').length, 1, "it should display Company 3")
|
||||
|
||||
assert.equal(menu.$('div[data-company-id=1] .fa-check-square').length, 1, "Company 1 should be checked")
|
||||
assert.equal(menu.$('div[data-company-id=2] .fa-square-o').length, 1, "Company 2 should not be checked")
|
||||
assert.equal(menu.$('div[data-company-id=3] .fa-check-square').length, 1, "Company 3 should be checked")
|
||||
menu.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.module('SwitchCompanyMenu', {}, function () {
|
||||
|
||||
QUnit.test("Toggle new company in multiple company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [x] **Company 1**
|
||||
* toggle->[ ] Company 2 ====> [x] Company 2
|
||||
* [x] Company 3 [x] Company 3
|
||||
*/
|
||||
await testToggleCompany(assert, {
|
||||
company: 2,
|
||||
session: this.session_mock_multi,
|
||||
assertMainCompany: [1, "Main company should not have changed"],
|
||||
asserCompanies: [[1, 2, 3], "All companies should be activated"],
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test("Toggle active company in mutliple company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [x] **Company 1**
|
||||
* [ ] Company 2 ====> [ ] Company 2
|
||||
* toggle->[x] Company 3 [ ] Company 3
|
||||
*/
|
||||
await testToggleCompany(assert, {
|
||||
company: 3,
|
||||
session: this.session_mock_multi,
|
||||
assertMainCompany: [1, "Main company should not have changed"],
|
||||
asserCompanies: [[1], "Companies 3 should be unactivated"],
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test("Switch main company in mutliple company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [x] Company 1
|
||||
* [ ] Company 2 ====> [ ] Company 2
|
||||
* switch->[x] Company 3 [x] **Company 3**
|
||||
*/
|
||||
await testSwitchCompany(assert, {
|
||||
company: 3,
|
||||
session: this.session_mock_multi,
|
||||
assertMainCompany: [3, "Main company should switch to Company 3"],
|
||||
asserCompanies: [[1, 3], "Companies 1 and 3 should still be active"],
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test("Switch new company in mutliple company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [x] Company 1
|
||||
* switch->[ ] Company 2 ====> [x] **Company 2**
|
||||
* [x] Company 3 [x] Company 3
|
||||
*/
|
||||
await testSwitchCompany(assert, {
|
||||
company: 2,
|
||||
session: this.session_mock_multi,
|
||||
assertMainCompany: [2, "Company 2 should become the main company"],
|
||||
asserCompanies: [[1, 2, 3], "Companies 1 and 3 should still be active"],
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test("Switch main company in single company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [ ] Company 1
|
||||
* [ ] Company 2 ====> [ ] Company 2
|
||||
* switch->[ ] Company 3 [x] **Company 3**
|
||||
*/
|
||||
await testSwitchCompany(assert, {
|
||||
company: 3,
|
||||
session: this.session_mock_single,
|
||||
assertMainCompany: [3, "Main company should switch to Company 3"],
|
||||
asserCompanies: [[3], "Companies 1 should no longer be active"],
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test("Toggle new company in single company mode", async function (assert) {
|
||||
/**
|
||||
* [x] **Company 1** [ ] **Company 1**
|
||||
* [ ] Company 2 ====> [ ] Company 2
|
||||
* toggle->[ ] Company 3 [x] Company 3
|
||||
*/
|
||||
await testToggleCompany(assert, {
|
||||
company: 3,
|
||||
session: this.session_mock_single,
|
||||
assertMainCompany: [1, "Company 1 should still be the main company"],
|
||||
asserCompanies: [[1, 3], "Company 3 should be activated"],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -689,6 +689,7 @@
|
||||
<script type="text/javascript" src="/web/static/tests/chrome/user_menu_tests.js"></script>
|
||||
<script type="text/javascript" src="/web/static/tests/chrome/systray_tests.js"></script>
|
||||
|
||||
<script type="text/javascript" src="/web/static/tests/widgets/company_switcher_tests.js"/>
|
||||
<script type="text/javascript" src="/web/static/tests/widgets/data_export_tests.js"/>
|
||||
<script type="text/javascript" src="/web/static/tests/widgets/domain_selector_tests.js"/>
|
||||
<script type="text/javascript" src="/web/static/tests/widgets/model_field_selector_tests.js"/>
|
||||
|
||||
Reference in New Issue
Block a user