[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) <wbr@odoo.com>
This commit is contained in:
david
2022-06-17 09:18:16 +02:00
committed by William Braeckman
parent 4ced83fa8b
commit ada4acf334
@@ -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)