Files
odoo_source/addons/website/tests/test_unsplash_beacon.py
T
Benoit Socias 8a750c4b44 [FIX] website: fix failing unsplash beacon test
The unsplash beacon public widget is started before the test assets JS
is loaded. Because of this, the approach in [1] fails sporadically.
If the result from the `/web_unsplash/get_app_id` RPC is obtained
before the test assets JS is loaded, the beacon patch is not applied
in time, and the test fails.

This commit applies the patch within the actual page HTML to avoid this
issue.

[1]: https://github.com/odoo/odoo/commit/a5abc766424f34074be3ae7a97ad7c6e9583f9c5

runbot-22610

closes odoo/odoo#127809

X-original-commit: 3f64135ff136addfad7c4c79e8c19cc33f8b19cf
Signed-off-by: Guillaume Dieleman (gdi) <gdi@odoo.com>
2023-07-10 08:56:22 +02:00

50 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import odoo.tests
@odoo.tests.common.tagged('post_install', '-at_install')
class TestUnsplashBeacon(odoo.tests.HttpCase):
def test_01_beacon(self):
self.env['ir.config_parameter'].sudo().set_param('unsplash.app_id', '123456')
# Create page with unsplash image.
page = self.env['website.page'].search([('url', '=', '/'), ('website_id', '=', 1)])
page.arch = '''<t name="Homepage" t-name="website.homepage1">
<t t-call="website.layout">
<t t-set="pageName" t-value="'homepage'"/>
<div id="wrap" class="oe_structure oe_empty">
<img src="/unsplash/pYyOZ8q7AII/306/fairy.jpg"/>
<!--
Keeping this javascript inline instead of extracting it
to avoid tempting users to publish such a file on their
production system.
-->
<script>
Object.defineProperty(window, "$", {
get() {
return this._patched$;
},
set(value) {
delete this.$;
this._patched$ = value;
// Patch RPC call.
const oldGet = value.get.bind(this);
value.get = (url, data, success, dataType) => {
if (url === "https://views.unsplash.com/v") {
const imageEl = document.querySelector(`img[src^="/unsplash/${data.photo_id}/"]`);
imageEl.dataset.beacon = "sent";
return;
}
return oldGet(url, data, success, dataType);
};
},
});
</script>
</div>
</t>
</t>'''
# Access page.
self.start_tour('/', 'test_unsplash_beacon')