1 import ./make-test-python.nix ({ lib, pkgs, ... }:
5 meta.maintainers = with lib.maintainers; [ minijackson ];
10 services.jellyfin.enable = true;
11 environment.systemPackages = with pkgs; [ ffmpeg ];
14 # Documentation of the Jellyfin API: https://api.jellyfin.org/
15 # Beware, this link can be resource intensive
19 auth = pkgs.writeText "auth.json" (builtins.toJSON {
20 Username = "jellyfin";
22 empty = pkgs.writeText "empty.json" (builtins.toJSON { });
27 from urllib.parse import urlencode
29 machine.wait_for_unit("jellyfin.service")
30 machine.wait_for_open_port(8096)
31 machine.succeed("curl --fail http://localhost:8096/")
33 machine.wait_until_succeeds("curl --fail http://localhost:8096/health | grep Healthy")
35 auth_header = 'MediaBrowser Client="NixOS Integration Tests", DeviceId="1337", Device="Apple II", Version="20.09"'
39 return f"curl --fail 'http://localhost:8096{path}' -H 'X-Emby-Authorization:{auth_header}'"
42 def api_post(path, json_file=None):
44 return f"curl --fail -X post 'http://localhost:8096{path}' -d '@{json_file}' -H Content-Type:application/json -H 'X-Emby-Authorization:{auth_header}'"
46 return f"curl --fail -X post 'http://localhost:8096{path}' -H 'X-Emby-Authorization:{auth_header}'"
49 with machine.nested("Wizard completes"):
50 machine.wait_until_succeeds(api_get("/Startup/Configuration"))
51 machine.succeed(api_get("/Startup/FirstUser"))
52 machine.succeed(api_post("/Startup/Complete"))
54 with machine.nested("Can login"):
55 auth_result_str = machine.succeed(
57 "/Users/AuthenticateByName",
61 auth_result = json.loads(auth_result_str)
62 auth_token = auth_result["AccessToken"]
63 auth_header += f", Token={auth_token}"
65 sessions_result_str = machine.succeed(api_get("/Sessions"))
66 sessions_result = json.loads(sessions_result_str)
69 session for session in sessions_result if session["DeviceId"] == "1337"
71 if len(this_session) != 1:
72 raise Exception("Session not created")
74 me_str = machine.succeed(api_get("/Users/Me"))
75 me = json.loads(me_str)["Id"]
77 with machine.nested("Can add library"):
78 tempdir = machine.succeed("mktemp -d -p /var/lib/jellyfin").strip()
79 machine.succeed(f"chmod 755 '{tempdir}'")
81 # Generate a dummy video that we can test later
82 videofile = f"{tempdir}/Big Buck Bunny (2008) [1080p].mkv"
83 machine.succeed(f"ffmpeg -f lavfi -i testsrc2=duration=5 '{videofile}'")
85 add_folder_query = urlencode(
88 "collectionType": "Movies",
90 "refreshLibrary": "true",
96 f"/Library/VirtualFolders?{add_folder_query}",
103 folders_str = machine.succeed(api_get("/Library/VirtualFolders"))
104 folders = json.loads(folders_str)
106 return all(folder["RefreshStatus"] == "Idle" for folder in folders)
111 with machine.nested("Can identify videos"):
114 # For some reason, having the folder refreshed doesn't mean the
119 items_str = machine.succeed(
120 api_get(f"/Users/{me}/Items?IncludeItemTypes=Movie&Recursive=true")
122 items = json.loads(items_str)["Items"]
124 return len(items) == 1
128 video = items[0]["Id"]
130 item_info_str = machine.succeed(api_get(f"/Users/{me}/Items/{video}"))
131 item_info = json.loads(item_info_str)
133 if item_info["Name"] != "Big Buck Bunny":
134 raise Exception("Jellyfin failed to properly identify file")
136 with machine.nested("Can read videos"):
137 media_source_id = item_info["MediaSources"][0]["Id"]
141 + f" -headers 'X-Emby-Authorization:{auth_header}'"
142 + f" -i http://localhost:8096/Videos/{video}/master.m3u8?mediaSourceId={media_source_id}"
146 duration = machine.succeed(
147 "ffprobe /tmp/test.mkv"
148 + " -show_entries format=duration"
149 + " -of compact=print_section=0:nokey=1"
152 if duration.strip() != "5.000000":
153 raise Exception("Downloaded video has wrong duration")