vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / tandoor-recipes-script-name.nix
blob6216d67b8084acfcbc159762914b8886ebbf0fc7
1 import ./make-test-python.nix (
2   { pkgs, lib, ... }:
3   {
4     name = "tandoor-recipes-script-name";
6     nodes.machine =
7       { pkgs, nodes, ... }:
8       {
9         services.tandoor-recipes = {
10           enable = true;
11           extraConfig = {
12             SCRIPT_NAME = "/any/path";
13             STATIC_URL = "${nodes.machine.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/static/";
14           };
15         };
16       };
18     testScript =
19       { nodes, ... }:
20       let
21         inherit (nodes.machine.services.tandoor-recipes) address port;
22         inherit (nodes.machine.services.tandoor-recipes.extraConfig) SCRIPT_NAME;
23       in
24       ''
25         from html.parser import HTMLParser
27         origin_url = "http://${address}:${toString port}"
28         base_url = f"{origin_url}${SCRIPT_NAME}"
29         login_path = "/admin/login/"
30         login_url = f"{base_url}{login_path}"
32         cookie_jar_path = "/tmp/cookies.txt"
33         curl = f"curl --cookie {cookie_jar_path} --cookie-jar {cookie_jar_path} --fail --header 'Origin: {origin_url}' --show-error --silent"
35         print("Wait for the service to respond")
36         machine.wait_for_unit("tandoor-recipes.service")
37         machine.wait_until_succeeds(f"{curl} {login_url}")
39         username = "username"
40         password = "password"
42         print("Create admin user")
43         machine.succeed(
44             f"DJANGO_SUPERUSER_PASSWORD='{password}' /var/lib/tandoor-recipes/tandoor-recipes-manage createsuperuser --no-input --username='{username}' --email=nobody@example.com"
45         )
47         print("Get CSRF token for later requests")
48         csrf_token = machine.succeed(f"grep csrftoken {cookie_jar_path} | cut --fields=7").rstrip()
50         print("Log in as admin user")
51         machine.succeed(
52             f"{curl} --data 'csrfmiddlewaretoken={csrf_token}' --data 'username={username}' --data 'password={password}' {login_url}"
53         )
55         print("Get the contents of the logged in main page")
56         logged_in_page = machine.succeed(f"{curl} --location {base_url}")
58         class UrlParser(HTMLParser):
59             def __init__(self):
60                 super().__init__()
62                 self.urls: list[str] = []
64             def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
65                 if tag == "form":
66                     for name, value in attrs:
67                         if name == "action" and value is not None:
68                             assert not value.endswith(login_path)
69                             break
71                 if tag != "a":
72                     return
74                 for name, value in attrs:
75                     if name == "href" and value is not None:
76                         if value.startswith(base_url):
77                             self.urls.append(value)
78                         elif value.startswith("/"):
79                             self.urls.append(f"{origin_url}{value}")
80                         else:
81                             print(f"Ignoring non-path URL: {value}")
83                         break
85         parser = UrlParser()
86         parser.feed(logged_in_page)
88         for url in parser.urls:
89             with subtest(f"Verify that {url} can be reached"):
90                 machine.succeed(f"{curl} {url}")
91       '';
93     meta.maintainers = with lib.maintainers; [ l0b0 ];
94   }