From f5710b2b838c93973e018de70560d392c1eefa88 Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Mon, 23 Oct 2023 22:25:19 +1300 Subject: [PATCH 1/2] nixos/tandoor-recipes: Fix working directory Run `tandoor-recipes` from within its `MEDIA_ROOT` directory to support the `SCRIPT_NAME` setting. Closes #262857. --- nixos/modules/services/misc/tandoor-recipes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/misc/tandoor-recipes.nix b/nixos/modules/services/misc/tandoor-recipes.nix index a2210f3d7db5..4488e350d7b2 100644 --- a/nixos/modules/services/misc/tandoor-recipes.nix +++ b/nixos/modules/services/misc/tandoor-recipes.nix @@ -22,7 +22,7 @@ let ${lib.toShellVars env} eval "$(${config.systemd.package}/bin/systemctl show -pUID,GID,MainPID tandoor-recipes.service)" exec ${pkgs.util-linux}/bin/nsenter \ - -t $MainPID -m -S $UID -G $GID \ + -t $MainPID -m -S $UID -G $GID --wdns=${env.MEDIA_ROOT} \ ${pkg}/bin/tandoor-recipes "$@" ''; in From d70be9d2115907ad1c12c1eb47c810e86460ac9b Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Wed, 19 Jun 2024 23:55:33 +1200 Subject: [PATCH 2/2] nixosTests.tandoor-recipes-script-name: init Verify that all `href` attributes emitted as part of the entrypoint page after logging in are reachable. Co-authored-by: Bruno BELANYI --- .../modules/services/misc/tandoor-recipes.nix | 2 +- nixos/tests/all-tests.nix | 1 + nixos/tests/tandoor-recipes-script-name.nix | 95 +++++++++++++++++++ .../misc/tandoor-recipes/default.nix | 2 +- 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 nixos/tests/tandoor-recipes-script-name.nix diff --git a/nixos/modules/services/misc/tandoor-recipes.nix b/nixos/modules/services/misc/tandoor-recipes.nix index 4488e350d7b2..1c903d280378 100644 --- a/nixos/modules/services/misc/tandoor-recipes.nix +++ b/nixos/modules/services/misc/tandoor-recipes.nix @@ -88,7 +88,7 @@ in Group = "tandoor_recipes"; DynamicUser = true; StateDirectory = "tandoor-recipes"; - WorkingDirectory = "/var/lib/tandoor-recipes"; + WorkingDirectory = env.MEDIA_ROOT; RuntimeDirectory = "tandoor-recipes"; BindReadOnlyPaths = [ diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 746b29fd2725..e12a83381478 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -955,6 +955,7 @@ in { systemd-homed = handleTest ./systemd-homed.nix {}; systemtap = handleTest ./systemtap.nix {}; tandoor-recipes = handleTest ./tandoor-recipes.nix {}; + tandoor-recipes-script-name = handleTest ./tandoor-recipes-script-name.nix {}; tang = handleTest ./tang.nix {}; taskserver = handleTest ./taskserver.nix {}; tayga = handleTest ./tayga.nix {}; diff --git a/nixos/tests/tandoor-recipes-script-name.nix b/nixos/tests/tandoor-recipes-script-name.nix new file mode 100644 index 000000000000..9010ffa3ccfa --- /dev/null +++ b/nixos/tests/tandoor-recipes-script-name.nix @@ -0,0 +1,95 @@ +import ./make-test-python.nix ( + { pkgs, lib, ... }: + { + name = "tandoor-recipes-script-name"; + + nodes.machine = + { pkgs, nodes, ... }: + { + services.tandoor-recipes = { + enable = true; + extraConfig = { + SCRIPT_NAME = "/any/path"; + STATIC_URL = "${nodes.machine.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/static/"; + }; + }; + }; + + testScript = + { nodes, ... }: + let + inherit (nodes.machine.services.tandoor-recipes) address port; + inherit (nodes.machine.services.tandoor-recipes.extraConfig) SCRIPT_NAME; + in + '' + from html.parser import HTMLParser + + origin_url = "http://${address}:${toString port}" + base_url = f"{origin_url}${SCRIPT_NAME}" + login_path = "/admin/login/" + login_url = f"{base_url}{login_path}" + + cookie_jar_path = "/tmp/cookies.txt" + curl = f"curl --cookie {cookie_jar_path} --cookie-jar {cookie_jar_path} --fail --header 'Origin: {origin_url}' --show-error --silent" + + print("Wait for the service to respond") + machine.wait_for_unit("tandoor-recipes.service") + machine.wait_until_succeeds(f"{curl} {login_url}") + + username = "username" + password = "password" + + print("Create admin user") + machine.succeed( + f"DJANGO_SUPERUSER_PASSWORD='{password}' /var/lib/tandoor-recipes/tandoor-recipes-manage createsuperuser --no-input --username='{username}' --email=nobody@example.com" + ) + + print("Get CSRF token for later requests") + csrf_token = machine.succeed(f"grep csrftoken {cookie_jar_path} | cut --fields=7").rstrip() + + print("Log in as admin user") + machine.succeed( + f"{curl} --data 'csrfmiddlewaretoken={csrf_token}' --data 'username={username}' --data 'password={password}' {login_url}" + ) + + print("Get the contents of the logged in main page") + logged_in_page = machine.succeed(f"{curl} --location {base_url}") + + class UrlParser(HTMLParser): + def __init__(self): + super().__init__() + + self.urls: list[str] = [] + + def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: + if tag == "form": + for name, value in attrs: + if name == "action" and value is not None: + assert not value.endswith(login_path) + break + + if tag != "a": + return + + for name, value in attrs: + if name == "href" and value is not None: + if value.startswith(base_url): + self.urls.append(value) + elif value.startswith("/"): + self.urls.append(f"{origin_url}{value}") + else: + print("Ignoring external URL: {value}") + + break + + parser = UrlParser() + parser.feed(logged_in_page) + + for url in parser.urls: + with subtest(f"Verify that {url} can be reached"): + machine.succeed(f"{curl} {url}") + ''; + + meta.maintainers = with lib.maintainers; [ l0b0 ]; + } +) diff --git a/pkgs/applications/misc/tandoor-recipes/default.nix b/pkgs/applications/misc/tandoor-recipes/default.nix index 906a266b14e3..b2bda05d5496 100644 --- a/pkgs/applications/misc/tandoor-recipes/default.nix +++ b/pkgs/applications/misc/tandoor-recipes/default.nix @@ -165,7 +165,7 @@ python.pkgs.pythonPackages.buildPythonPackage rec { updateScript = ./update.sh; tests = { - inherit (nixosTests) tandoor-recipes; + inherit (nixosTests) tandoor-recipes tandoor-recipes-script-name; }; };