From 7ea21adabe007eff6b5b69aa451d756dff54442b Mon Sep 17 00:00:00 2001 From: lul-odoo Date: Thu, 27 Jun 2019 10:22:55 +0200 Subject: [PATCH 1/3] [IMP] 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. --- addons/test_website/controllers/main.py | 4 ++++ addons/test_website/tests/__init__.py | 1 + addons/test_website/tests/test_multi_company.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 addons/test_website/tests/test_multi_company.py diff --git a/addons/test_website/controllers/main.py b/addons/test_website/controllers/main.py index 259072c81ff..ae45d160b06 100644 --- a/addons/test_website/controllers/main.py +++ b/addons/test_website/controllers/main.py @@ -36,3 +36,7 @@ class WebsiteTest(Home): @http.route('/ignore_args/converter//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'))) diff --git a/addons/test_website/tests/__init__.py b/addons/test_website/tests/__init__.py index 40d4ece53e7..4bb004cd8f9 100644 --- a/addons/test_website/tests/__init__.py +++ b/addons/test_website/tests/__init__.py @@ -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 diff --git a/addons/test_website/tests/test_multi_company.py b/addons/test_website/tests/test_multi_company.py new file mode 100644 index 00000000000..8c9c6859959 --- /dev/null +++ b/addons/test_website/tests/test_multi_company.py @@ -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) From 1c99c251c9a1e7e6642efae302ac3b83e18e0d9b Mon Sep 17 00:00:00 2001 From: lul-odoo Date: Thu, 27 Jun 2019 10:23:17 +0200 Subject: [PATCH 2/3] [IMP] 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 --- .../src/js/widgets/switch_company_menu.js | 33 ++-- addons/web/static/src/xml/base.xml | 8 +- .../tests/widgets/company_switcher_tests.js | 169 ++++++++++++++++++ addons/web/views/webclient_templates.xml | 1 + 4 files changed, 191 insertions(+), 20 deletions(-) create mode 100644 addons/web/static/tests/widgets/company_switcher_tests.js diff --git a/addons/web/static/src/js/widgets/switch_company_menu.js b/addons/web/static/src/js/widgets/switch_company_menu.js index 62e2a27f3d0..75bc918768b 100644 --- a/addons/web/static/src/js/widgets/switch_company_menu.js +++ b/addons/web/static/src/js/widgets/switch_company_menu.js @@ -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); diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index e34d017b1a4..878a9cfaef5 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -1469,21 +1469,21 @@