1{ config, lib, ... }:
2
3{
4 name = "chhoto-url";
5 meta.maintainers = with lib.maintainers; [ defelo ];
6
7 nodes.machine = {
8 services.chhoto-url = {
9 enable = true;
10 settings.port = 8000;
11 environmentFiles = [
12 (builtins.toFile "chhoto-url.env" ''
13 api_key=api_key
14 password=password
15 '')
16 ];
17 };
18 };
19
20 interactive.nodes.machine = {
21 services.glitchtip.listenAddress = "0.0.0.0";
22 networking.firewall.allowedTCPPorts = [ 8000 ];
23 virtualisation.forwardPorts = [
24 {
25 from = "host";
26 host.port = 8000;
27 guest.port = 8000;
28 }
29 ];
30 };
31
32 testScript = ''
33 import json
34 import re
35
36 machine.wait_for_unit("chhoto-url.service")
37 machine.wait_for_open_port(8000)
38
39 resp = json.loads(machine.succeed("curl localhost:8000/api/getconfig"))
40 assert resp["success"] is False
41 assert resp["reason"] == "No valid authentication was found"
42
43 resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/getconfig"))
44 expected_version = "${config.nodes.machine.services.chhoto-url.package.version}"
45 assert resp["version"] == expected_version
46
47 resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/new -d '{\"longlink\": \"https://nixos.org/\"}'"))
48 assert resp["success"] is True
49 assert (match := re.match(r"^http://localhost:8000/(.+)$", resp["shorturl"]))
50 slug = match[1]
51
52 resp = machine.succeed(f"curl -i {resp["shorturl"]}")
53 assert (match := re.search(r"(?m)^location: (.+?)\r?$", resp))
54 assert match[1] == "https://nixos.org/"
55
56 resp = json.loads(machine.succeed(f"curl -H 'X-API-Key: api_key' localhost:8000/api/expand -d '{slug}'"))
57 assert resp["success"] is True
58 assert resp["hits"] == 1
59 '';
60}