1/*
2Snipe-IT NixOS test
3
4It covers the following scenario:
5- Installation
6- Backup and restore
7
8Scenarios NOT covered by this test (but perhaps in the future):
9- Sending and receiving emails
10*/
11{ pkgs, ... }: let
12 siteName = "NixOS Snipe-IT Test Instance";
13in {
14 name = "snipe-it";
15
16 meta.maintainers = with pkgs.lib.maintainers; [ yayayayaka ];
17
18 nodes = {
19 snipeit = { ... }: {
20 services.snipe-it = {
21 enable = true;
22 appKeyFile = toString (pkgs.writeText "snipe-it-app-key" "uTqGUN5GUmUrh/zSAYmhyzRk62pnpXICyXv9eeITI8k=");
23 hostName = "localhost";
24 database.createLocally = true;
25 mail = {
26 driver = "smtp";
27 encryption = "tls";
28 host = "localhost";
29 port = 1025;
30 from.name = "Snipe-IT NixOS test";
31 from.address = "snipe-it@localhost";
32 replyTo.address = "snipe-it@localhost";
33 user = "snipe-it@localhost";
34 passwordFile = toString (pkgs.writeText "snipe-it-mail-pass" "a-secure-mail-password");
35 };
36 };
37 };
38 };
39
40 testScript = { nodes }: let
41 backupPath = "${nodes.snipeit.services.snipe-it.dataDir}/storage/app/backups";
42
43 # Snipe-IT has been installed successfully if the site name shows up on the login page
44 checkLoginPage = { shouldSucceed ? true }: ''
45 snipeit.${if shouldSucceed then "succeed" else "fail"}("""curl http://localhost/login | grep '${siteName}'""")
46 '';
47 in ''
48 start_all()
49
50 snipeit.wait_for_unit("nginx.service")
51 snipeit.wait_for_unit("snipe-it-setup.service")
52
53 # Create an admin user
54 snipeit.succeed(
55 """
56 snipe-it snipeit:create-admin \
57 --username="admin" \
58 --email="janedoe@localhost" \
59 --password="extremesecurepassword" \
60 --first_name="Jane" \
61 --last_name="Doe"
62 """
63 )
64
65 with subtest("Circumvent the pre-flight setup by just writing some settings into the database ourself"):
66 snipeit.succeed(
67 """
68 mysql -D ${nodes.snipeit.services.snipe-it.database.name} -e "INSERT INTO settings (id, user_id, site_name) VALUES ('1', '1', '${siteName}');"
69 """
70 )
71
72 # Usually these are generated during the pre-flight setup
73 snipeit.succeed("snipe-it passport:keys")
74
75
76 # Login page should now contain the configured site name
77 ${checkLoginPage {}}
78
79 with subtest("Test Backup and restore"):
80 snipeit.succeed("snipe-it snipeit:backup")
81
82 # One zip file should have been created
83 snipeit.succeed("""[ "$(ls -1 "${backupPath}" | wc -l)" -eq 1 ]""")
84
85 # Purge the state
86 snipeit.succeed("snipe-it migrate:fresh --force")
87
88 # Login page should disappear
89 ${checkLoginPage { shouldSucceed = false; }}
90
91 # Restore the state
92 snipeit.succeed(
93 """
94 snipe-it snipeit:restore --force $(find "${backupPath}/" -type f -name "*.zip")
95 """
96 )
97
98 # Login page should be back again
99 ${checkLoginPage {}}
100 '';
101}