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