summaryrefslogtreecommitdiffstats
path: root/nixos/tests/zipline.nix
blob: 45da2e7fc9c1c17a360bc08f082e853dad714b18 (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
{ lib, pkgs, ... }:

{
  name = "zipline";
  meta.maintainers = with lib.maintainers; [ defelo ];

  nodes.machine = {
    # On x86, testing with a CPU without SSE 4.2 support
    # to ensure native libvips is used
    virtualisation.qemu.options = lib.mkIf pkgs.stdenv.hostPlatform.isx86 [ "-cpu core2duo" ];
    services.zipline = {
      enable = true;
      settings = {
        CORE_HOSTNAME = "127.0.0.1";
        CORE_PORT = 8000;
      };
      environmentFiles = [
        (builtins.toFile "zipline.env" ''
          CORE_SECRET=DMlouex3W0QLRbVwkUafNnNws5jpgRDX
        '')
      ];
    };

    networking.hosts."127.0.0.1" = [ "zipline.local" ];
  };

  interactive.nodes.machine = {
    services.zipline.settings.CORE_HOSTNAME = lib.mkForce "0.0.0.0";
    networking.firewall.allowedTCPPorts = [ 8000 ];
    virtualisation.forwardPorts = [
      {
        from = "host";
        host.port = 8000;
        guest.port = 8000;
      }
    ];
  };

  testScript = ''
    import json
    import re

    machine.wait_for_unit("zipline.service")
    machine.wait_for_open_port(8000, timeout=300)

    resp = machine.succeed("curl zipline.local:8000/api/setup -v -X POST -H 'Content-Type: application/json' -d '{\"username\": \"administrator\", \"password\": \"password\"}' 2>&1")
    data = json.loads(resp.splitlines()[-1])
    assert data["user"]["username"] == "administrator"
    assert data["user"]["role"] == "SUPERADMIN"

    resp = machine.succeed("curl zipline.local:8000/api/auth/login -v -X POST -H 'Content-Type: application/json' -d '{\"username\": \"administrator\", \"password\": \"password\"}' 2>&1")

    assert (cookie := re.search(r"(?m)^< set-cookie: ([^;]*)", resp))
    resp = machine.succeed(f"curl zipline.local:8000/api/user -H 'Cookie: {cookie[1]}'")
    assert json.loads(resp)["user"]["id"] == data["user"]["id"]
  '';
}