]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_logging.py
Merge pull request #4163 from Itz-Agasta/category-support
[nominatim.git] / test / python / api / test_logging.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for the HTML debug logger.
9
10 The debug output is served as ``text/html``, so values that come from user input
11 or database should be HTML-escaped, while the markup the logger emits itself
12 should remain intact.
13 """
14 from types import SimpleNamespace
15
16 import nominatim_api.logging as loglib
17
18 # A value containing HTML-significant characters (&, <, >).
19 HTML_VALUE = 'H-T-M-L & <b>BoldValue</b>'
20
21
22 def _fake_result(**kwargs):
23     """ Build a minimal object with the attributes result_dump() reads. """
24     defaults = dict(source_table=SimpleNamespace(name='PLACEX'),
25                     names={'name': 'a place'},
26                     housenumber=None,
27                     category=('place', 'hamlet'),
28                     rank_address=16,
29                     osm_object=('N', 123),
30                     country_code='ad',
31                     importance=0.5)
32     defaults.update(kwargs)
33     return SimpleNamespace(**defaults)
34
35
36 def test_result_dump_escapes_html_in_values():
37     logger = loglib.HTMLLogger()
38     res = _fake_result(names={'name': HTML_VALUE}, country_code='a<b')
39     logger.result_dump('Results', iter([(0.0, res)]))
40     out = logger.get_buffer()
41
42     assert '<b>BoldValue</b>' not in out
43     assert '&lt;b&gt;BoldValue&lt;/b&gt;' in out
44     assert 'a&lt;b' in out
45
46
47 def test_result_dump_preserves_own_markup():
48     logger = loglib.HTMLLogger()
49     logger.result_dump('Results', iter([(0.0, _fake_result())]))
50     out = logger.get_buffer()
51
52     # The OSM link the logger builds itself must not be escaped away.
53     assert '<a href="https://www.openstreetmap.org/node/123">N123</a>' in out