Skip to content

Commit 0ad87e0

Browse files
celladorJenkins
authored andcommitted
Inject connection choices into site auth list widget
Pass the LDAP and SAML connection picks into _editable_connections_form_spec instead of reaching for the module-level choice providers; saml_choices=None expresses "distributed SAML unsupported". This drops the duplicate distributed_saml_supported() call and lets the tests build the widget from plain data instead of monkeypatching. CMK-37102 Change-Id: I1eb185784fda9e943140a9a8d8a4380275d54237
1 parent f430620 commit 0ad87e0

2 files changed

Lines changed: 29 additions & 36 deletions

File tree

cmk/gui/watolib/sites.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import queue
1313
import re
1414
import time
15-
from collections.abc import Collection, Mapping
15+
from collections.abc import Collection, Mapping, Sequence
1616
from multiprocessing import JoinableQueue, Process
1717
from typing import Any, cast, NamedTuple, Protocol
1818

@@ -399,7 +399,10 @@ def authentication_connections_form_spec(cls) -> FormSpec[Any]:
399399
CascadingSingleChoiceElement(
400400
name="list",
401401
title=Title("Use the following"),
402-
parameter_form=cls._editable_connections_form_spec(),
402+
parameter_form=cls._editable_connections_form_spec(
403+
ldap_choices=connection_choices(),
404+
saml_choices=saml_connection_choices() if saml_supported else None,
405+
),
403406
),
404407
CascadingSingleChoiceElement(
405408
name="all",
@@ -479,14 +482,19 @@ def _saml_acs_endpoint_widget() -> StaticText:
479482
)
480483

481484
@classmethod
482-
def _editable_connections_form_spec(cls) -> List[tuple[str, object]]:
485+
def _editable_connections_form_spec(
486+
cls,
487+
*,
488+
ldap_choices: Sequence[tuple[str, str]],
489+
saml_choices: Sequence[tuple[str, str]] | None,
490+
) -> List[tuple[str, object]]:
483491
"""Editable list of LDAP/SAML connection picks (the ``"list"`` form)."""
484492
ldap_elements = [
485493
SingleChoiceElementExtended( # astrein: disable=localization-checker
486494
name=id_,
487495
title=Title(label), # astrein: disable=localization-checker
488496
)
489-
for id_, label in connection_choices()
497+
for id_, label in ldap_choices
490498
]
491499
connection_elements: list[CascadingSingleChoiceElement[Any]] = [
492500
CascadingSingleChoiceElement(
@@ -498,13 +506,13 @@ def _editable_connections_form_spec(cls) -> List[tuple[str, object]]:
498506
),
499507
),
500508
]
501-
if distributed_saml_supported():
509+
if saml_choices is not None:
502510
saml_elements = [
503511
SingleChoiceElementExtended( # astrein: disable=localization-checker
504512
name=id_,
505513
title=Title(label), # astrein: disable=localization-checker
506514
)
507-
for id_, label in saml_connection_choices()
515+
for id_, label in saml_choices
508516
]
509517
connection_elements.append(
510518
CascadingSingleChoiceElement(

tests/unit/cmk/gui/watolib/test_sites.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -305,28 +305,22 @@ def test_user_attribute_sync_form_spec_choices(
305305
]
306306

307307

308-
def _editable_connection_elements(
309-
monkeypatch: pytest.MonkeyPatch, *, saml_supported: bool
310-
) -> list[Any]:
308+
def _editable_connection_elements(*, saml_supported: bool) -> list[Any]:
311309
"""Return the per-entry connection choices of the nested ``"list"`` widget
312-
with stubbed connection choices."""
313-
monkeypatch.setattr("cmk.gui.watolib.sites.connection_choices", lambda: [("ldap_a", "LDAP A")])
314-
monkeypatch.setattr(
315-
"cmk.gui.watolib.sites.saml_connection_choices", lambda: [("saml_a", "SAML A")]
316-
)
317-
monkeypatch.setattr("cmk.gui.watolib.sites.distributed_saml_supported", lambda: saml_supported)
318-
template = SiteManagement._editable_connections_form_spec().element_template
310+
built from stubbed connection choices."""
311+
template = SiteManagement._editable_connections_form_spec(
312+
ldap_choices=[("ldap_a", "LDAP A")],
313+
saml_choices=[("saml_a", "SAML A")] if saml_supported else None,
314+
).element_template
319315
assert hasattr(template, "elements")
320316
return list(template.elements)
321317

322318

323-
def test_editable_connections_form_spec_offers_ldap_and_saml_when_supported(
324-
request_context: None, monkeypatch: pytest.MonkeyPatch
325-
) -> None:
319+
def test_editable_connections_form_spec_offers_ldap_and_saml_when_supported() -> None:
326320
"""The nested "list" connection widget renders an LDAP pick and, when distributed
327321
SAML is supported, a SAML pick whose sub-form carries the connection_id,
328322
metadata_endpoint and acs_endpoint fields."""
329-
elements = _editable_connection_elements(monkeypatch, saml_supported=True)
323+
elements = _editable_connection_elements(saml_supported=True)
330324
assert [element.name for element in elements] == ["ldap", "saml"]
331325
assert [choice.name for choice in elements[0].parameter_form.elements] == ["ldap_a"]
332326
saml_subform = elements[1].parameter_form
@@ -336,9 +330,7 @@ def test_editable_connections_form_spec_offers_ldap_and_saml_when_supported(
336330
] == ["saml_a"]
337331

338332

339-
def test_connection_pick_accepts_dash_in_connection_id(
340-
request_context: None, monkeypatch: pytest.MonkeyPatch
341-
) -> None:
333+
def test_connection_pick_accepts_dash_in_connection_id() -> None:
342334
"""A connection id containing a dash can be offered as a per-site pick.
343335
344336
The product's own id rule (the ``ID`` valuespec behind the connection's "ID"
@@ -347,15 +339,10 @@ def test_connection_pick_accepts_dash_in_connection_id(
347339
otherwise required to be Python identifiers, which a dash is not — so
348340
building the pick must not choke on an id the creation form let through.
349341
"""
350-
monkeypatch.setattr(
351-
"cmk.gui.watolib.sites.connection_choices", lambda: [("ldap-with-dash", "LDAP dashed")]
352-
)
353-
monkeypatch.setattr(
354-
"cmk.gui.watolib.sites.saml_connection_choices", lambda: [("saml-with-dash", "SAML dashed")]
355-
)
356-
monkeypatch.setattr("cmk.gui.watolib.sites.distributed_saml_supported", lambda: True)
357-
358-
template = SiteManagement._editable_connections_form_spec().element_template
342+
template = SiteManagement._editable_connections_form_spec(
343+
ldap_choices=[("ldap-with-dash", "LDAP dashed")],
344+
saml_choices=[("saml-with-dash", "SAML dashed")],
345+
).element_template
359346
assert hasattr(template, "elements")
360347
elements = list(template.elements)
361348

@@ -377,9 +364,7 @@ def test_auth_connections_round_trip_dashed_connection_id() -> None:
377364
assert _auth_connections_to_disk(("list", entries)) == entries
378365

379366

380-
def test_editable_connections_form_spec_omits_saml_when_not_supported(
381-
request_context: None, monkeypatch: pytest.MonkeyPatch
382-
) -> None:
367+
def test_editable_connections_form_spec_omits_saml_when_not_supported() -> None:
383368
"""Without distributed SAML support the nested "list" widget offers only the LDAP pick."""
384-
elements = _editable_connection_elements(monkeypatch, saml_supported=False)
369+
elements = _editable_connection_elements(saml_supported=False)
385370
assert [element.name for element in elements] == ["ldap"]

0 commit comments

Comments
 (0)