Currently, the slide.embed statistics only work if a 'Referer' header is passed in the request object. The issue with that strategy is that it's not very reliable, indeed some modern browsers now completely avoid passing the referer header (mostly for security and privacy reasons). This commit refactors the slide.embed feature to work even without a referer header. When the header is missing, the incremented count will be stored under a common slide.embed record with a url field = False. This count is still useful because the end-user will want to know if the slide is popular externally, even if we don't know on which website it has been embedded exactly. In addition, we add more visibility to the slide.embed feature by introducing a stat-button on the slide.slide form view, that redirects to the list of slide.embeds for this slide. Finally, we also renamed the 'embedcount_ids' field to 'embed_ids' to get a better / clearer naming. A few tests about embed detection were added to ensure this behavior. (Tests are based on the work of Nathan Marotte <nama@odoo.com>). Task-2654009 Part-of: odoo/odoo#77736
52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo.addons.website_slides.tests import common
|
|
from odoo.tests import HttpCase
|
|
|
|
|
|
class TestEmbedDetection(HttpCase, common.SlidesCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
super(TestEmbedDetection, cls).setUpClass()
|
|
|
|
cls.website = cls.env['website'].create({
|
|
'name': 'Test Website',
|
|
'domain': 'https://testwebsite.com'
|
|
})
|
|
|
|
cls.channel.website_id = cls.website.id
|
|
|
|
def test_embed_external_no_referer(self):
|
|
""" When hitting the external URL without a referer header, the global embed record is
|
|
incremented. """
|
|
self.url_open(f'/slides/embed_external/{self.slide.id}')
|
|
embed_views = self.env['slide.embed'].search([('slide_id', '=', self.slide.id)])
|
|
self.assertEqual(len(embed_views), 1)
|
|
self.assertEqual(embed_views.website_name, 'Unknown Website')
|
|
|
|
def test_embed_external_referer(self):
|
|
""" When hitting the external URL with a referer header, the embed record is incremented
|
|
based on the referer URL. """
|
|
|
|
self.assertFalse(bool(self.env['slide.embed'].search([
|
|
('slide_id', '=', self.slide.id)
|
|
])))
|
|
|
|
self.url_open(
|
|
f'/slides/embed_external/{self.slide.id}',
|
|
headers={'Referer': 'https://someexternalwebsite.com'}
|
|
)
|
|
|
|
embed_views = self.env['slide.embed'].search([('slide_id', '=', self.slide.id)])
|
|
self.assertEqual(len(embed_views), 1)
|
|
self.assertEqual(embed_views.count_views, 1)
|
|
self.assertEqual(embed_views.website_name, 'https://someexternalwebsite.com')
|
|
|
|
def test_embed_not_external(self):
|
|
""" When hitting the non-external URL, we should not add a slide_embed record. """
|
|
self.url_open(f'/slides/embed/{self.slide.id}')
|
|
self.assertFalse(bool(self.env['slide.embed'].search([
|
|
('slide_id', '=', self.slide.id)
|
|
])))
|