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