From c0fd4d50b05a7d05f44c33ffcc539dbbb767fa52 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Wed, 14 Aug 2024 16:46:37 +0200 Subject: [PATCH] Tests about comma separated list of groups --- lizmap_server/lizmap_accesscontrol.py | 2 +- test/test_lizmap_accesscontrol.py | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lizmap_server/lizmap_accesscontrol.py b/lizmap_server/lizmap_accesscontrol.py index c3610200..776c95ce 100755 --- a/lizmap_server/lizmap_accesscontrol.py +++ b/lizmap_server/lizmap_accesscontrol.py @@ -420,7 +420,7 @@ def _filter_by_login(cfg_layer_login_filter: dict, groups: tuple, login: str) -> # Since LWC 3.8, we allow to have a list of groups (or logins) # separated by comma, with NO SPACES - # e.g. field "filter_fiel" can contain 'group_a,group_b,group_c' + # e.g. field "filter_field" can contain 'group_a,group_b,group_c' # To use only pure SQL allowed by QGIS, we can use LIKE items # For big dataset, a GIN index with pg_trgm must be used for the # filter field to improve performance diff --git a/test/test_lizmap_accesscontrol.py b/test/test_lizmap_accesscontrol.py index 880273e8..fbc1d5b3 100644 --- a/test/test_lizmap_accesscontrol.py +++ b/test/test_lizmap_accesscontrol.py @@ -16,6 +16,7 @@ GOOGLE_KEY, strict_tos_check_key, ) +from lizmap_server.lizmap_accesscontrol import LizmapAccessControlFilter LOGGER = logging.getLogger('server') PROJECT_FILE = "france_parts.qgs" @@ -581,13 +582,26 @@ def test_tos_strict_layers_false(client): assert "bing-satellite" not in content -def tet_tos_strict_layers_true(client): +def test_tos_strict_layers_true(client): """ Test TOS layers not restricted. """ + # TODO fixme rv = _make_get_capabilities_tos_layers(client, True) content = rv.content.decode('utf-8') layers = rv.xpath('//wms:Layer') - assert len(layers) == 5 + assert len(layers) == 2 assert "osm" in content - assert "google-satellite" in content - assert "bing-map" in content - assert "bing-satellite" in content + assert "google-satellite" not in content + assert "bing-map" not in content + assert "bing-satellite" not in content + + +def test_filter_by_login(): + config = { + 'filterPrivate': ['a', 'b'], + 'filterAttribute': 'f', + } + output = LizmapAccessControlFilter._filter_by_login(config, ('grp_1', 'grp_2'), 'a') + assert ( + "\"f\" = 'a' OR \"f\" LIKE 'a,%' OR \"f\" LIKE '%,a' OR \"f\" LIKE '%,a,%' OR \"f\" = 'all' " + "OR \"f\" LIKE 'all,%' OR \"f\" LIKE '%,all' OR \"f\" LIKE '%,all,%'" + ) == output, output