From b3ce523ba2cb34b17b3b575075091e4690cbc6b6 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 16 Feb 2024 10:47:51 +0100 Subject: [PATCH] fix: star control was using old post method Also, only display it if the user is already logged in, given we do not deal anymore with login during ajax calls. --- umap/static/umap/js/umap.js | 25 ++++++++++++------------- umap/tests/integration/test_star.py | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 umap/tests/integration/test_star.py diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 4d2b3707..4a0d797b 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -401,7 +401,7 @@ U.Map = L.Map.extend({ this._controls.search = new U.SearchControl() this._controls.embed = new L.Control.Embed(this, this.options.embedOptions) this._controls.tilelayersChooser = new U.TileLayerChooser(this) - this._controls.star = new U.StarControl(this) + if (this.options.user) this._controls.star = new U.StarControl(this) this._controls.editinosm = new L.Control.EditInOSM({ position: 'topleft', widgetOptions: { @@ -468,6 +468,7 @@ U.Map = L.Map.extend({ status = this.options[`${name}Control`] if (status === false) continue control = this._controls[name] + if (!control) continue control.addTo(this) if (status === undefined || status === null) L.DomUtil.addClass(control._container, 'display-on-more') @@ -1142,24 +1143,22 @@ U.Map = L.Map.extend({ }) }, - star: function () { + star: async function () { if (!this.options.umap_id) return this.ui.alert({ content: L._('Please save the map first'), level: 'error', }) const url = this.urls.get('map_star', { map_id: this.options.umap_id }) - this.post(url, { - context: this, - callback: function (data) { - this.options.starred = data.starred - let msg = data.starred - ? L._('Map has been starred') - : L._('Map has been unstarred') - this.ui.alert({ content: msg, level: 'info' }) - this.renderControls() - }, - }) + const [data, response, error] = await this.server.post(url) + if (!error) { + this.options.starred = data.starred + let msg = data.starred + ? L._('Map has been starred') + : L._('Map has been unstarred') + this.ui.alert({ content: msg, level: 'info' }) + this.renderControls() + } }, geometry: function () { diff --git a/umap/tests/integration/test_star.py b/umap/tests/integration/test_star.py new file mode 100644 index 00000000..3984fb91 --- /dev/null +++ b/umap/tests/integration/test_star.py @@ -0,0 +1,27 @@ +import re + +import pytest +from playwright.sync_api import expect + +from umap.models import Star + +pytestmark = pytest.mark.django_db + + +def test_star_control_is_visible_if_logged_in(map, live_server, page, login, user): + login(user) + assert not Star.objects.count() + page.goto(f"{live_server.url}{map.get_absolute_url()}") + page.get_by_title("More controls").click() + control = page.locator(".leaflet-control-star") + expect(control).to_be_visible() + with page.expect_response(re.compile(".*/star/")): + control.click() + assert Star.objects.count() == 1 + + +def test_no_star_control_if_not_logged_in(map, live_server, page): + page.goto(f"{live_server.url}{map.get_absolute_url()}") + page.get_by_title("More controls").click() + control = page.locator(".leaflet-control-star") + expect(control).to_be_hidden()