From ada4acf3349e1475fe9ea2f753f75b89f298cda2 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 24 May 2022 14:42:45 +0000 Subject: [PATCH] [FIX] website_sale: recently viewd multi company If a user has a website.track with a product from another company than the one he is currently using, an access rule error will popup. Several scenarios can trigger this error, but the easiest one would be: - Activate recently viewed products in the e-commerce. - Create a product and publish it. - The portal user visits it and a website.track linked to that product is created. - From backend, change the company of the product to one that is unreachable for the portal user or the website environment he is in. - Now when the portal user visits another product, an error will show up warning about the unability to load the recently viewed product. So only products in the context of the request should be loaded as recently viewed. TT36698 closes odoo/odoo#93589 X-original-commit: bded9cc3649e0178d95452d5ef861285d5558478 Signed-off-by: William Braeckman (wbr) --- .../tests/test_website_sale_visitor.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/addons/website_sale/tests/test_website_sale_visitor.py b/addons/website_sale/tests/test_website_sale_visitor.py index 7f24f33d618..50d828917ec 100644 --- a/addons/website_sale/tests/test_website_sale_visitor.py +++ b/addons/website_sale/tests/test_website_sale_visitor.py @@ -51,3 +51,26 @@ class WebsiteSaleVisitorTests(TransactionCase): new_tracks = self.env['website.track'].search([('id', 'not in', existing_tracks.ids)]) self.assertEqual(len(new_visitors), 1, "No visitor should be created after visiting another tracked product") self.assertEqual(len(new_tracks), 2, "A track should be created after visiting another tracked product") + + def test_recently_viewed_company_changed(self): + # Test that, by changing the company of a tracked product, the recently viewed product do not crash + new_company = self.env['res.company'].create({ + 'name': 'Test Company', + }) + public_user = self.env.ref('base.public_user') + + product = self.env['product.product'].create({ + 'name': 'Test Product', + 'website_published': True, + 'sale_ok': True, + }) + + self.website = self.website.with_user(public_user).with_context(website_id=self.website.id) + with MockRequest(self.website.env, website=self.website): + self.cookies = self.WebsiteSaleController.products_recently_viewed_update(product.id) + product.product_tmpl_id.company_id = new_company + product.product_tmpl_id.flush_recordset(['company_id']) + with MockRequest(self.website.env, website=self.website, cookies=self.cookies): + # Should not raise an error + res = self.website.env['website.snippet.filter']._get_products_latest_viewed(self.website, 16, [], {}) + self.assertFalse(res)