nixos/kavita: accept freeform settings, ensure locales in tests

This commit is contained in:
Yongun Seong 2023-10-27 05:31:20 +09:00 committed by melvyn
parent 44cdc74071
commit 477dce3b9a
No known key found for this signature in database
GPG Key ID: 25A2B7280CE3AFF6
2 changed files with 69 additions and 40 deletions

View File

@ -2,7 +2,18 @@
let
cfg = config.services.kavita;
in {
settingsFormat = pkgs.formats.json { };
appsettings = settingsFormat.generate "appsettings.json" ({ TokenKey = "@TOKEN@"; } // cfg.settings);
in
{
imports = [
(lib.mkChangedOptionModule [ "services" "kavita" "ipAdresses" ] [ "services" "kavita" "settings" "IpAddresses" ] (config:
let value = lib.getAttrFromPath [ "services" "kavita" "ipAdresses" ] config; in
lib.concatStringsSep "," value
))
(lib.mkRenamedOptionModule [ "services" "kavita" "port" ] [ "services" "kavita" "settings" "Port" ])
];
options.services.kavita = {
enable = lib.mkEnableOption (lib.mdDoc "Kavita reading server");
@ -27,16 +38,31 @@ in {
It can be generated with `head -c 32 /dev/urandom | base64`.
'';
};
port = lib.mkOption {
settings = lib.mkOption {
default = { };
description = lib.mdDoc ''
Kavita configuration options, as configured in {file}`appsettings.json`.
'';
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
Port = lib.mkOption {
default = 5000;
type = lib.types.port;
description = lib.mdDoc "Port to bind to.";
};
ipAdresses = lib.mkOption {
default = ["0.0.0.0" "::"];
type = lib.types.listOf lib.types.str;
description = lib.mdDoc "IP Addresses to bind to. The default is to bind
to all IPv4 and IPv6 addresses.";
IpAddresses = lib.mkOption {
default = "0.0.0.0,::";
type = lib.types.commas;
description = lib.mdDoc ''
IP Addresses to bind to. The default is to bind to all IPv4 and IPv6 addresses.
'';
};
};
};
};
};
@ -46,18 +72,15 @@ in {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = ''
umask u=rwx,g=rx,o=
cat > "${cfg.dataDir}/config/appsettings.json" <<EOF
{
"TokenKey": "$(cat ${cfg.tokenKeyFile})",
"Port": ${toString cfg.port},
"IpAddresses": "${lib.concatStringsSep "," cfg.ipAdresses}"
}
EOF
install -m600 ${appsettings} ${lib.escapeShellArg cfg.dataDir}/config/appsettings.json
${pkgs.replace-secret}/bin/replace-secret '@TOKEN@' \
''${CREDENTIALS_DIRECTORY}/token \
'${cfg.dataDir}/config/appsettings.json'
'';
serviceConfig = {
WorkingDirectory = cfg.dataDir;
ExecStart = "${lib.getExe cfg.package}";
LoadCredential = [ "token:${cfg.tokenKeyFile}" ];
ExecStart = lib.getExe cfg.package;
Restart = "always";
User = cfg.user;
};

View File

@ -1,4 +1,4 @@
import ./make-test-python.nix ({ pkgs, ...} : {
import ./make-test-python.nix ({ pkgs, ... }: {
name = "kavita";
meta = with pkgs.lib.maintainers; {
maintainers = [ misterio77 ];
@ -8,20 +8,20 @@ import ./make-test-python.nix ({ pkgs, ...} : {
kavita = { config, pkgs, ... }: {
services.kavita = {
enable = true;
port = 5000;
tokenKeyFile = builtins.toFile "kavita.key" "QfpjFvjT83BLtZ74GE3U3Q==";
tokenKeyFile = builtins.toFile "kavita.key" "d26ba694b455271a8872415830fb7b5c58f8da98f9ef7f58b2ca4c34bd406512";
};
};
};
testScript = let
testScript =
let
regUrl = "http://kavita:5000/api/Account/register";
payload = builtins.toFile "payload.json" (builtins.toJSON {
username = "foo";
password = "correcthorsebatterystaple";
email = "foo@bar";
});
in ''
loginUrl = "http://kavita:5000/api/Account/login";
localeUrl = "http://kavita:5000/api/locale";
in
''
import json
kavita.start
kavita.wait_for_unit("kavita.service")
@ -29,8 +29,14 @@ import ./make-test-python.nix ({ pkgs, ...} : {
kavita.wait_until_succeeds("curl http://kavita:5000/site.webmanifest | grep Kavita")
# Check that registration is working
kavita.succeed("curl -fX POST ${regUrl} --json @${payload}")
kavita.succeed("""curl -fX POST ${regUrl} --json '{"username": "foo", "password": "correcthorsebatterystaple"}'""")
# But only for the first one
kavita.fail("curl -fX POST ${regUrl} --json @${payload}")
kavita.fail("""curl -fX POST ${regUrl} --json '{"username": "foo", "password": "correcthorsebatterystaple"}'""")
# Log in and retrieve token
session = json.loads(kavita.succeed("""curl -fX POST ${loginUrl} --json '{"username": "foo", "password": "correcthorsebatterystaple"}'"""))
# Check list of locales
locales = json.loads(kavita.succeed(f"curl -fX GET ${localeUrl} -H 'Authorization: Bearer {session['token']}'"))
assert len(locales) > 0, "expected a list of locales"
'';
})