summaryrefslogtreecommitdiffstats
path: root/nixos/tests/chhoto-url.nix
blob: 883b71117906a821071ef3d6ab3c70d4380725fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ config, lib, ... }:

{
  name = "chhoto-url";
  meta.maintainers = with lib.maintainers; [ defelo ];

  nodes.machine = {
    services.chhoto-url = {
      enable = true;
      settings.port = 8000;
      environmentFiles = [
        (builtins.toFile "chhoto-url.env" ''
          api_key=api_key
          password=password
        '')
      ];
    };
  };

  interactive.nodes.machine = {
    networking.firewall.allowedTCPPorts = [ 8000 ];
    virtualisation.forwardPorts = [
      {
        from = "host";
        host.port = 8000;
        guest.port = 8000;
      }
    ];
  };

  interactive.sshBackdoor.enable = true;
  interactive.defaults.virtualisation.graphics = false;

  testScript = ''
    import json
    import re
    import time

    machine.wait_for_unit("chhoto-url.service")
    machine.wait_for_open_port(8000)

    resp = json.loads(machine.succeed("curl localhost:8000/api/getconfig"))
    assert resp["success"] is False
    assert resp["reason"] == "No valid authentication."

    resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/getconfig"))
    expected_version = "${config.nodes.machine.services.chhoto-url.package.version}"
    assert resp["version"] == expected_version

    resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/new -d '{\"longlink\": \"https://nixos.org/\"}'"))
    assert resp["success"] is True
    assert (match := re.match(r"^http://localhost:8000/(.+)$", resp["shorturl"]))
    slug = match[1]

    resp = machine.succeed(f"curl -i {resp["shorturl"]}")
    assert (match := re.search(r"(?m)^location: (.+?)\r?$", resp))
    assert match[1] == "https://nixos.org/"

    time.sleep(2)
    resp = json.loads(machine.succeed(f"curl -H 'X-API-Key: api_key' localhost:8000/api/expand -d '{slug}'"))
    assert resp["success"] is True
    assert resp["longurl"] == "https://nixos.org/"
    assert resp["hits"] == 1
  '';
}