1{ pkgs, ... }:
2{
3 name = "grocy";
4 meta = with pkgs.lib.maintainers; {
5 maintainers = [
6 diogotcorreia
7 ma27
8 ];
9 };
10
11 nodes.machine =
12 { pkgs, ... }:
13 {
14 services.grocy = {
15 enable = true;
16 hostName = "localhost";
17 nginx.enableSSL = false;
18 };
19 environment.systemPackages = [ pkgs.jq ];
20 };
21
22 testScript = ''
23 from base64 import b64encode
24 from urllib.parse import quote
25
26 machine.start()
27 machine.wait_for_open_port(80)
28 machine.wait_for_unit("multi-user.target")
29
30 # This establishes _something_
31 machine.succeed("curl -sSf http://localhost")
32 # The second request creates the database, unsure why both are required
33 machine.succeed("curl -sSf http://localhost/")
34
35 machine.succeed(
36 "curl -c cookies -sSf -X POST http://localhost/login -d 'username=admin&password=admin'"
37 )
38
39 cookie = machine.succeed(
40 "grep -v '^#' cookies | awk '{ print $7 }' | sed -e '/^$/d' | perl -pe 'chomp'"
41 )
42
43 machine.succeed(
44 f"curl -sSf -X POST http://localhost/api/objects/tasks -b 'grocy_session={cookie}' "
45 + '-d \'{"assigned_to_user_id":1,"name":"Test Task","due_date":"1970-01-01"}\'''
46 + " --header 'Content-Type: application/json'"
47 )
48
49 task_name = machine.succeed(
50 f"curl -sSf http://localhost/api/tasks -b 'grocy_session={cookie}' --header 'Accept: application/json' | jq '.[].name' | xargs echo | perl -pe 'chomp'"
51 )
52
53 assert task_name == "Test Task"
54
55 machine.succeed("curl -sSI http://localhost/api/tasks 2>&1 | grep '401 Unauthorized'")
56
57 file_name = "test.txt"
58 file_name_base64 = b64encode(file_name.encode('ascii')).decode('ascii')
59 file_name_base64_urlencode = quote(file_name_base64)
60
61 machine.succeed(
62 f"echo Sample equipment manual > /tmp/{file_name}"
63 )
64
65 machine.succeed(
66 f"curl -sSf -X 'PUT' -b 'grocy_session={cookie}' "
67 + f" 'http://localhost/api/files/equipmentmanuals/{file_name_base64_urlencode}' "
68 + " --header 'Accept: */*' "
69 + " --header 'Content-Type: application/octet-stream' "
70 + f" --data-binary '@/tmp/{file_name}' "
71 )
72
73 machine.succeed(
74 f"curl -sSf -X 'GET' -b 'grocy_session={cookie}' "
75 + f" 'http://localhost/api/files/equipmentmanuals/{file_name_base64_urlencode}' "
76 + " --header 'Accept: application/octet-stream' "
77 + f" | cmp /tmp/{file_name}"
78 )
79
80 machine.shutdown()
81 '';
82}