2021-05-13 14:42:22 +00:00
|
|
|
import ./make-test-python.nix ({ lib, ... }: {
|
2022-04-12 15:48:53 +00:00
|
|
|
name = "paperless";
|
2022-06-26 17:12:18 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ erikarvstedt Flakebi ];
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
nodes = let self = {
|
|
|
|
simple = { pkgs, ... }: {
|
|
|
|
environment.systemPackages = with pkgs; [ imagemagick jq ];
|
|
|
|
services.paperless = {
|
|
|
|
enable = true;
|
|
|
|
passwordFile = builtins.toFile "password" "admin";
|
|
|
|
};
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
2023-10-05 08:03:54 +00:00
|
|
|
postgres = { config, pkgs, ... }: {
|
|
|
|
imports = [ self.simple ];
|
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
|
|
|
ensureDatabases = [ "paperless" ];
|
|
|
|
ensureUsers = [
|
|
|
|
{ name = config.services.paperless.user;
|
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15
Closes #216989
First of all, a bit of context: in PostgreSQL, newly created users don't
have the CREATE privilege on the public schema of a database even with
`ALL PRIVILEGES` granted via `ensurePermissions` which is how most of
the DB users are currently set up "declaratively"[1]. This means e.g. a
freshly deployed Nextcloud service will break early because Nextcloud
itself cannot CREATE any tables in the public schema anymore.
The other issue here is that `ensurePermissions` is a mere hack. It's
effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how
a value is substituted in a query. You'd have to parse a subset of SQL
to actually know which object are permissions granted to for a user).
After analyzing the existing modules I realized that in every case with
a single exception[2] the UNIX system user is equal to the db user is
equal to the db name and I don't see a compelling reason why people
would change that in 99% of the cases. In fact, some modules would even
break if you'd change that because the declarations of the system user &
the db user are mixed up[3].
So I decided to go with something new which restricts the ways to use
`ensure*` options rather than expanding those[4]. Effectively this means
that
* The DB user _must_ be equal to the DB name.
* Permissions are granted via `ensureDBOwnerhip` for an attribute-set in
`ensureUsers`. That way, the user is actually the owner and can
perform `CREATE`.
* For such a postgres user, a database must be declared in
`ensureDatabases`.
For anything else, a custom state management should be implemented. This
can either be `initialScript`, doing it manual, outside of the module or
by implementing proper state management for postgresql[5], but the
current state of `ensure*` isn't even declarative, but a convergent tool
which is what Nix actually claims to _not_ do.
Regarding existing setups: there are effectively two options:
* Leave everything as-is (assuming that system user == db user == db
name): then the DB user will automatically become the DB owner and
everything else stays the same.
* Drop the `createDatabase = true;` declarations: nothing will change
because a removal of `ensure*` statements is ignored, so it doesn't
matter at all whether this option is kept after the first deploy (and
later on you'd usually restore from backups anyways).
The DB user isn't the owner of the DB then, but for an existing setup
this is irrelevant because CREATE on the public schema isn't revoked
from existing users (only not granted for new users).
[1] not really declarative though because removals of these statements
are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467
[2] `services.invidious`: I removed the `ensure*` part temporarily
because it IMHO falls into the category "manage the state on your
own" (see the commit message). See also
https://github.com/NixOS/nixpkgs/pull/265857
[3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";`
[4] As opposed to other changes that are considered a potential fix, but
also add more things like collation for DBs or passwords that are
_never_ touched again when changing those.
[5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
|
|
|
ensureDBOwnership = true;
|
2023-10-05 08:03:54 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2024-01-09 10:40:09 +00:00
|
|
|
services.paperless.settings = {
|
2023-10-05 08:03:54 +00:00
|
|
|
PAPERLESS_DBHOST = "/run/postgresql";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}; in self;
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
testScript = ''
|
2022-04-11 15:06:02 +00:00
|
|
|
import json
|
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
def test_paperless(node):
|
|
|
|
node.wait_for_unit("paperless-consumer.service")
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
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"
|
2021-05-13 14:42:22 +00:00
|
|
|
)
|
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
with subtest("Web interface gets ready"):
|
|
|
|
node.wait_for_unit("paperless-web.service")
|
2021-05-13 14:42:22 +00:00
|
|
|
# Wait until server accepts connections
|
2023-10-05 08:03:54 +00:00
|
|
|
node.wait_until_succeeds("curl -fs localhost:28981")
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
# Required for consuming documents via the web interface
|
|
|
|
with subtest("Task-queue gets ready"):
|
|
|
|
node.wait_for_unit("paperless-task-queue.service")
|
2023-01-02 11:19:27 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
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"
|
2021-08-23 06:58:16 +00:00
|
|
|
)
|
2023-10-05 08:03:54 +00:00
|
|
|
node.wait_until_succeeds("curl -u admin:admin -F document=@/tmp/webdoc.png -fs localhost:28981/api/documents/post_document/")
|
2021-08-23 06:58:16 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
with subtest("Add a txt document via the web interface"):
|
|
|
|
node.succeed(
|
|
|
|
"echo 'hello web 16-10-2005' > /tmp/webdoc.txt"
|
2023-08-05 20:06:01 +00:00
|
|
|
)
|
2023-10-05 08:03:54 +00:00
|
|
|
node.wait_until_succeeds("curl -u admin:admin -F document=@/tmp/webdoc.txt -fs localhost:28981/api/documents/post_document/")
|
2023-08-05 20:06:01 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
with subtest("Documents are consumed"):
|
|
|
|
node.wait_until_succeeds(
|
|
|
|
"(($(curl -u admin:admin -fs localhost:28981/api/documents/ | jq .count) == 3))"
|
2021-05-13 14:42:22 +00:00
|
|
|
)
|
2023-10-05 08:03:54 +00:00
|
|
|
docs = json.loads(node.succeed("curl -u admin:admin -fs localhost:28981/api/documents/"))['results']
|
2022-04-11 15:06:02 +00:00
|
|
|
assert "2005-10-16" in docs[0]['created']
|
|
|
|
assert "2005-10-16" in docs[1]['created']
|
2023-08-05 20:06:01 +00:00
|
|
|
assert "2005-10-16" in docs[2]['created']
|
2022-09-11 22:09:45 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
# Detects gunicorn issues, see PR #190888
|
|
|
|
with subtest("Document metadata can be accessed"):
|
|
|
|
metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost:28981/api/documents/1/metadata/"))
|
2022-09-11 22:09:45 +00:00
|
|
|
assert "original_checksum" in metadata
|
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost:28981/api/documents/2/metadata/"))
|
2022-09-11 22:09:45 +00:00
|
|
|
assert "original_checksum" in metadata
|
2023-08-05 20:06:01 +00:00
|
|
|
|
2023-10-05 08:03:54 +00:00
|
|
|
metadata = json.loads(node.succeed("curl -u admin:admin -fs localhost:28981/api/documents/3/metadata/"))
|
2023-08-05 20:06:01 +00:00
|
|
|
assert "original_checksum" in metadata
|
2023-10-05 08:03:54 +00:00
|
|
|
|
|
|
|
test_paperless(simple)
|
|
|
|
simple.send_monitor_command("quit")
|
|
|
|
simple.wait_for_shutdown()
|
|
|
|
test_paperless(postgres)
|
2021-05-13 14:42:22 +00:00
|
|
|
'';
|
|
|
|
})
|