1import ./make-test-python.nix (
2 { pkgs, lib, ... }:
3 {
4 name = "tandoor-recipes-script-name";
5
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 };
17
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
26
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}"
31
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"
34
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}")
38
39 username = "username"
40 password = "password"
41
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 )
46
47 print("Get CSRF token for later requests")
48 csrf_token = machine.succeed(f"grep csrftoken {cookie_jar_path} | cut --fields=7").rstrip()
49
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 )
54
55 print("Get the contents of the logged in main page")
56 logged_in_page = machine.succeed(f"{curl} --location {base_url}")
57
58 class UrlParser(HTMLParser):
59 def __init__(self):
60 super().__init__()
61
62 self.urls: list[str] = []
63
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
70
71 if tag != "a":
72 return
73
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}")
82
83 break
84
85 parser = UrlParser()
86 parser.feed(logged_in_page)
87
88 for url in parser.urls:
89 with subtest(f"Verify that {url} can be reached"):
90 machine.succeed(f"{curl} {url}")
91 '';
92
93 meta.maintainers = with lib.maintainers; [ l0b0 ];
94 }
95)