1{ pkgs, ... }:
2let
3 ankiSyncTest = pkgs.writeScript "anki-sync-test.py" ''
4 #!${pkgs.python3}/bin/python
5
6 import sys
7
8 # get site paths from anki itself
9 from runpy import run_path
10 run_path("${pkgs.anki}/bin/.anki-wrapped")
11 import anki
12
13 col = anki.collection.Collection('test_collection')
14 endpoint = 'http://localhost:27701'
15
16 # Sanity check: verify bad login fails
17 try:
18 col.sync_login('baduser', 'badpass', endpoint)
19 print("bad user login worked?!")
20 sys.exit(1)
21 except anki.errors.SyncError:
22 pass
23
24 # test logging in to users
25 col.sync_login('user', 'password', endpoint)
26 col.sync_login('passfileuser', 'passfilepassword', endpoint)
27
28 # Test actual sync. login apparently doesn't remember the endpoint...
29 login = col.sync_login('user', 'password', endpoint)
30 login.endpoint = endpoint
31 sync = col.sync_collection(login, False)
32 assert sync.required == sync.NO_CHANGES
33 # TODO: create an archive with server content including a test card
34 # and check we got it?
35 '';
36 testPasswordFile = pkgs.writeText "anki-password" "passfilepassword";
37in
38{
39 name = "anki-sync-server";
40 meta = with pkgs.lib.maintainers; {
41 maintainers = [ martinetd ];
42 };
43
44 nodes.machine = {
45 services.anki-sync-server = {
46 enable = true;
47 users = [
48 {
49 username = "user";
50 password = "password";
51 }
52 {
53 username = "passfileuser";
54 passwordFile = testPasswordFile;
55 }
56 ];
57 };
58 };
59
60 testScript = ''
61 start_all()
62
63 with subtest("Server starts successfully"):
64 # service won't start without users
65 machine.wait_for_unit("anki-sync-server.service")
66 machine.wait_for_open_port(27701)
67
68 with subtest("Can sync"):
69 machine.succeed("${ankiSyncTest}")
70 '';
71}