summaryrefslogtreecommitdiffstats
path: root/nixos/tests/rmfakecloud.nix
blob: f226c20b9577e2ab228e1148904d069379015a6c (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
66
67
{ pkgs, ... }:
{
  name = "rmfakecloud";
  meta = with pkgs.lib.maintainers; {
    maintainers = [ martinetd ];
  };

  nodes.machine = {
    services.rmfakecloud = {
      enable = true;
      storageUrl = "https://local.appspot.com";
    };
  };

  testScript = ''
    machine.wait_for_unit("rmfakecloud.service")
    machine.wait_for_open_port(3000)

    # first login creates user
    login_token = machine.succeed("""
      curl -sSf -b cookie -c cookie -H "Content-Type: application/json" \
        -d'{"email":"test","password":"test"}' -X POST \
        http://localhost:3000/ui/api/login
    """)

    # subsequent different pass or mail should fail, but same login works
    machine.fail("""
      curl -sSf -H "Content-Type: application/json" \
        -d'{"email":"test","password":"test2"}' -X POST \
        http://localhost:3000/ui/api/login
    """)
    machine.fail("""
      curl -sSf -H "Content-Type: application/json" \
        -d'{"email":"test2","password":"test"}' -X POST
        http://localhost:3000/ui/api/login
    """)
    machine.succeed("""
      curl -sSf -H "Content-Type: application/json" \
        -d'{"email":"test","password":"test"}' -X POST \
        http://localhost:3000/ui/api/login
    """)

    # can get code from cookie or bearer
    machine.succeed("""
      curl -sSf -b cookie -c cookie http://localhost:3000/ui/api/newcode
    """)
    newcode = machine.succeed(f"""
      curl -sSf -H "Authorization: Bearer {login_token}" \
        http://localhost:3000/ui/api/newcode
    """).strip('"')

    # ... but not junk
    machine.fail(f"""
      curl -sSf -H "Authorization: Bearer abc{login_token}" \
          http://localhost:3000/ui/api/newcode
    """)

    # can connect "device" with said code
    machine.succeed(f"""
      curl -sSf -d '{{"code":"{newcode}", "deviceDesc": "desc", "deviceID":"rm100-123"}}' \
        http://localhost:3000/token/json/2/device/new
    """)

    # for future improvements
    machine.log(machine.execute("systemd-analyze security rmfakecloud.service")[1])
  '';
}