summaryrefslogtreecommitdiffstats
path: root/nixos/tests/paperless.nix
blob: d9691fd2cb434f3a3c4423ce616404faa6597259 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{ lib, ... }:
{
  name = "paperless";
  meta.maintainers = with lib.maintainers; [
    leona
    SuperSandro2000
    erikarvstedt
  ];

  nodes =
    let
      self = {
        simple =
          { pkgs, ... }:
          {
            environment.systemPackages = with pkgs; [
              imagemagick
              jq
            ];
            services = {
              nginx.virtualHosts."localhost".forceSSL = false;
              paperless = {
                enable = true;
                configureNginx = true;
                domain = "localhost";
                passwordFile = builtins.toFile "password" "admin";

                exporter = {
                  enable = true;

                  settings = {
                    "no-color" = lib.mkForce false; # override a default option
                    "no-thumbnail" = true; # add a new option
                  };
                };
              };
            };

            # OOMs randomly otherwise
            virtualisation.memorySize = 2048;
          };
        postgres = {
          imports = [ self.simple ];
          services.paperless.database.createLocally = true;
          services.paperless.settings = {
            PAPERLESS_OCR_LANGUAGE = "deu";
          };
        };
      };
    in
    self;

  testScript = ''
    import json

    def test_paperless(node):
      node.wait_for_unit("paperless-consumer.service")

      with subtest("Add a document via the file system"):
        node.succeed(
          "convert -size 400x40 xc:white -font 'DejaVu-Sans' -pointsize 20 -fill black "
          "-annotate +5+20 'hello world 16-10-2005' /var/lib/paperless/consume/doc.png"
        )

      with subtest("Web interface gets ready"):
        node.wait_for_unit("paperless-web.service")
        # Wait until server accepts connections
        node.wait_until_succeeds("curl -fs localhost")

      # Required for consuming documents via the web interface
      with subtest("Task-queue gets ready"):
        node.wait_for_unit("paperless-task-queue.service")

      with subtest("Add a png document via the web interface"):
        node.succeed(
          "convert -size 400x40 xc:white -font 'DejaVu-Sans' -pointsize 20 -fill black "
          "-annotate +5+20 'hello web 16-10-2005' /tmp/webdoc.png"
        )
        node.wait_until_succeeds("curl -u admin:admin -F document=@/tmp/webdoc.png -fs localhost/api/documents/post_document/")

      with subtest("Add a txt document via the web interface"):
        node.succeed(
          "echo 'hello web 16-10-2005' > /tmp/webdoc.txt"
        )
        node.wait_until_succeeds("curl -u admin:admin -F document=@/tmp/webdoc.txt -fs localhost/api/documents/post_document/")

      with subtest("Documents are consumed"):
        node.wait_until_succeeds(
          "(($(curl -u admin:admin -fs localhost/api/documents/ | jq .count) == 3))"
        )
        docs = json.loads(node.succeed("curl -u admin:admin -fs localhost/api/documents/"))['results']
        assert "2005-10-16" in docs[0]['created']
        assert "2005-10-16" in docs[1]['created']
        assert "2005-10-16" in docs[2]['created']

      # Detects gunicorn issues, see PR #190888
      with subtest("Document metadata can be accessed"):
        metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost/api/documents/1/metadata/"))
        assert "original_checksum" in metadata

        metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost/api/documents/2/metadata/"))
        assert "original_checksum" in metadata

        metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost/api/documents/3/metadata/"))
        assert "original_checksum" in metadata

      with subtest("Exporter"):
          node.succeed("systemctl start --wait paperless-exporter")
          node.wait_for_unit("paperless-web.service")
          node.wait_for_unit("paperless-consumer.service")
          node.wait_for_unit("paperless-scheduler.service")
          node.wait_for_unit("paperless-task-queue.service")

          node.succeed("ls -lah /var/lib/paperless/export/manifest.json")

          timers = node.succeed("systemctl list-timers paperless-exporter")
          print(timers)
          assert "paperless-exporter.timer paperless-exporter.service" in timers, "missing timer"
          assert "1 timers listed." in timers, "incorrect number of timers"

          # Double check that our attrset option override works as expected
          cmdline = node.succeed("grep 'paperless-manage' $(systemctl cat paperless-exporter | grep ExecStart | cut -f 2 -d=)")
          print(f"Exporter command line {cmdline!r}")
          assert cmdline.strip() == "paperless-manage document_exporter /var/lib/paperless/export --compare-checksums --delete --no-progress-bar --no-thumbnail", "Unexpected exporter command line"

    test_paperless(simple)
    simple.send_monitor_command("quit")
    simple.wait_for_shutdown()
    test_paperless(postgres)
  '';
}