Files
computer-playbook/tests/unit/python/plugins/lookup/test_sso.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

189 lines
6.4 KiB
Python

"""Unit tests for the ``sso`` lookup plugin.
The plugin is a thin wrapper around
``utils.roles.applications.services.sso.get_sso_config`` — these tests
focus on the lookup-shape contract (term parsing, want-path selection,
error paths). Resolver semantics are covered by
``tests/unit/python/utils/roles/applications/services/test_sso.py``.
"""
import importlib
import importlib.util
import sys
import unittest
import unittest.mock as mock
from ansible.errors import AnsibleError
from . import PROJECT_ROOT
def _load_module(rel_path: str, name: str):
for key in (
"utils.roles.applications.config",
"utils.roles.applications",
"utils",
"utils.cache.applications",
"utils.cache",
"utils.roles.applications.services.sso",
"utils.roles.applications.services",
):
sys.modules.pop(key, None)
importlib.import_module("utils.roles.applications.config")
importlib.import_module("utils.cache.applications")
importlib.import_module("utils.roles.applications.services.sso")
path = PROJECT_ROOT / rel_path
spec = importlib.util.spec_from_file_location(name, str(path))
mod = importlib.util.module_from_spec(spec)
assert spec and spec.loader
spec.loader.exec_module(mod)
return mod
def _apps(
*,
enabled=False,
shared=False,
flavor=None,
oauth2_origin_host=None,
oauth2_origin_port=None,
oauth2_acl=None,
oauth2_allowed_groups=None,
include_app=True,
):
if not include_app:
return {}
sso: dict = {"enabled": enabled, "shared": shared}
if flavor is not None:
sso["flavor"] = flavor
oauth2_sub: dict = {}
if oauth2_origin_host is not None or oauth2_origin_port is not None:
oauth2_sub["origin"] = {
**({"host": oauth2_origin_host} if oauth2_origin_host is not None else {}),
**({"port": oauth2_origin_port} if oauth2_origin_port is not None else {}),
}
if oauth2_acl is not None:
oauth2_sub["acl"] = oauth2_acl
if oauth2_allowed_groups is not None:
oauth2_sub["allowed_groups"] = oauth2_allowed_groups
if oauth2_sub:
sso["oauth2"] = oauth2_sub
return {"web-app-x": {"services": {"sso": sso}}}
class SsoLookupTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.mod = _load_module("plugins/lookup/sso.py", "sso")
def _run(self, applications, terms):
lk = self.mod.LookupModule()
lk._loader = mock.MagicMock()
with mock.patch.object(self.mod, "lookup_loader") as loader_mock:
loader_mock.get.return_value = mock.MagicMock(
run=lambda *_a, **_k: [applications]
)
return lk.run(terms, variables={"applications": applications})
def test_zero_terms_raises(self):
with self.assertRaises(AnsibleError):
self._run(_apps(), [])
def test_three_terms_raises(self):
with self.assertRaises(AnsibleError):
self._run(_apps(), ["web-app-x", "is_enabled", "extra"])
def test_empty_application_id_raises(self):
with self.assertRaises(AnsibleError):
self._run(_apps(), [""])
def test_unknown_want_path_raises(self):
with self.assertRaises(AnsibleError) as ctx:
self._run(_apps(enabled=True), ["web-app-x", "bogus_key"])
self.assertIn("unknown want_path", str(ctx.exception))
def test_default_returns_full_dict(self):
result = self._run(_apps(enabled=True, flavor="oauth2"), ["web-app-x"])
self.assertEqual(len(result), 1)
payload = result[0]
self.assertEqual(payload["enabled"], True)
self.assertEqual(payload["flavor"], "oauth2")
self.assertTrue(payload["is_proxy_gated"])
def test_explicit_all_returns_full_dict(self):
result = self._run(_apps(enabled=True), ["web-app-x", "all"])
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], dict)
def test_is_enabled_returns_bool(self):
result = self._run(_apps(enabled=True), ["web-app-x", "is_enabled"])
self.assertEqual(result, [True])
def test_is_proxy_gated_true_for_oauth2(self):
result = self._run(
_apps(enabled=True, flavor="oauth2"), ["web-app-x", "is_proxy_gated"]
)
self.assertEqual(result, [True])
def test_is_proxy_gated_false_for_oidc(self):
result = self._run(
_apps(enabled=True, flavor="oidc"), ["web-app-x", "is_proxy_gated"]
)
self.assertEqual(result, [False])
def test_is_proxy_gated_false_when_disabled(self):
result = self._run(
_apps(enabled=False, flavor="oauth2"), ["web-app-x", "is_proxy_gated"]
)
self.assertEqual(result, [False])
def test_flavor_returns_string(self):
result = self._run(
_apps(enabled=True, flavor="oauth2"), ["web-app-x", "flavor"]
)
self.assertEqual(result, ["oauth2"])
def test_oauth2_origin_host_returns_value(self):
result = self._run(
_apps(enabled=True, flavor="oauth2", oauth2_origin_host="application"),
["web-app-x", "oauth2_origin_host"],
)
self.assertEqual(result, ["application"])
def test_oauth2_origin_port_stringified(self):
result = self._run(
_apps(enabled=True, flavor="oauth2", oauth2_origin_port=8000),
["web-app-x", "oauth2_origin_port"],
)
self.assertEqual(result, ["8000"])
def test_oauth2_acl_returns_dict(self):
acl = {"whitelist": ["/api/", "/static/"]}
result = self._run(
_apps(enabled=True, flavor="oauth2", oauth2_acl=acl),
["web-app-x", "oauth2_acl"],
)
self.assertEqual(result, [acl])
def test_oauth2_allowed_groups_returns_list(self):
groups = ["/roles/web-app-x/admin"]
result = self._run(
_apps(enabled=True, flavor="oauth2", oauth2_allowed_groups=groups),
["web-app-x", "oauth2_allowed_groups"],
)
self.assertEqual(result, [groups])
def test_missing_app_returns_defaults(self):
result = self._run({}, ["web-app-x", "is_enabled"])
self.assertEqual(result, [False])
def test_empty_want_path_treated_as_all(self):
result = self._run(_apps(enabled=True), ["web-app-x", " "])
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], dict)
if __name__ == "__main__":
unittest.main()