about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/web-apps/snipe-it.nix
blob: 123d7742056ba93fb1174883ccfd62051bb085fe (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Snipe-IT NixOS test

It covers the following scenario:
- Installation
- Backup and restore

Scenarios NOT covered by this test (but perhaps in the future):
- Sending and receiving emails
*/
{ pkgs, ... }: let
  siteName = "NixOS Snipe-IT Test Instance";
in {
  name = "snipe-it";

  meta.maintainers = with pkgs.lib.maintainers; [ yayayayaka ];

  nodes = {
    snipeit = { ... }: {
      services.snipe-it = {
        enable = true;
        appKeyFile = toString (pkgs.writeText "snipe-it-app-key" "uTqGUN5GUmUrh/zSAYmhyzRk62pnpXICyXv9eeITI8k=");
        hostName = "localhost";
        database.createLocally = true;
        mail = {
          driver = "smtp";
          encryption = "tls";
          host = "localhost";
          port = 1025;
          from.name = "Snipe-IT NixOS test";
          from.address = "snipe-it@localhost";
          replyTo.address = "snipe-it@localhost";
          user = "snipe-it@localhost";
          passwordFile = toString (pkgs.writeText "snipe-it-mail-pass" "a-secure-mail-password");
        };
      };
    };
  };

  testScript = { nodes }: let
    backupPath = "${nodes.snipeit.services.snipe-it.dataDir}/storage/app/backups";

    # Snipe-IT has been installed successfully if the site name shows up on the login page
    checkLoginPage = { shouldSucceed ? true }: ''
      snipeit.${if shouldSucceed then "succeed" else "fail"}("""curl http://localhost/login | grep '${siteName}'""")
    '';
  in ''
    start_all()

    snipeit.wait_for_unit("nginx.service")
    snipeit.wait_for_unit("snipe-it-setup.service")

    # Create an admin user
    snipeit.succeed(
        """
        snipe-it snipeit:create-admin \
            --username="admin" \
            --email="janedoe@localhost" \
            --password="extremesecurepassword" \
            --first_name="Jane" \
            --last_name="Doe"
        """
    )

    with subtest("Circumvent the pre-flight setup by just writing some settings into the database ourself"):
        snipeit.succeed(
            """
            mysql -D ${nodes.snipeit.services.snipe-it.database.name} -e "INSERT INTO settings (id, user_id, site_name) VALUES ('1', '1', '${siteName}');"
            """
        )

        # Usually these are generated during the pre-flight setup
        snipeit.succeed("snipe-it passport:keys")


    # Login page should now contain the configured site name
    ${checkLoginPage {}}

    with subtest("Test Backup and restore"):
        snipeit.succeed("snipe-it snipeit:backup")

        # One zip file should have been created
        snipeit.succeed("""[ "$(ls -1 "${backupPath}" | wc -l)" -eq 1 ]""")

        # Purge the state
        snipeit.succeed("snipe-it migrate:fresh --force")

        # Login page should disappear
        ${checkLoginPage { shouldSucceed = false; }}

        # Restore the state
        snipeit.succeed(
            """
            snipe-it snipeit:restore --force $(find "${backupPath}/" -type f -name "*.zip")
            """
        )

        # Login page should be back again
        ${checkLoginPage {}}
  '';
}