From 441195914b8df4467afbe6a8727d4b5bba1ddac1 Mon Sep 17 00:00:00 2001 From: eversio Date: Fri, 29 Nov 2024 20:58:14 +1100 Subject: [PATCH] test: fix alldebrid downloader tests --- src/program/services/downloaders/alldebrid.py | 2 +- src/tests/test_alldebrid_downloader.py | 171 ++++++++---------- .../test_data/alldebrid_magnet_files.json | 27 +++ .../test_data/alldebrid_magnet_instant.json | 16 -- .../alldebrid_magnet_instant_unavailable.json | 12 -- ...ldebrid_magnet_status_one_downloading.json | 9 +- .../alldebrid_magnet_status_one_ready.json | 68 ++++--- .../alldebrid_magnet_upload_not_ready.json | 6 +- .../alldebrid_magnet_upload_ready.json | 31 ++-- src/tests/test_data/alldebrid_user.json | 31 ++++ 10 files changed, 190 insertions(+), 183 deletions(-) create mode 100644 src/tests/test_data/alldebrid_magnet_files.json delete mode 100644 src/tests/test_data/alldebrid_magnet_instant.json delete mode 100644 src/tests/test_data/alldebrid_magnet_instant_unavailable.json create mode 100644 src/tests/test_data/alldebrid_user.json diff --git a/src/program/services/downloaders/alldebrid.py b/src/program/services/downloaders/alldebrid.py index 1b64dbbe..e3bf1ec0 100644 --- a/src/program/services/downloaders/alldebrid.py +++ b/src/program/services/downloaders/alldebrid.py @@ -260,4 +260,4 @@ def get_files_and_links(self, torrent_id: str) -> List[DebridFile]: except Exception as e: logger.error(f"Failed to get files for {torrent_id}: {e}") - raise \ No newline at end of file + raise diff --git a/src/tests/test_alldebrid_downloader.py b/src/tests/test_alldebrid_downloader.py index e22cca9f..4bcdb6fe 100644 --- a/src/tests/test_alldebrid_downloader.py +++ b/src/tests/test_alldebrid_downloader.py @@ -1,136 +1,102 @@ import json import pytest +from unittest.mock import patch from program.services.downloaders import alldebrid from program.services.downloaders.alldebrid import ( AllDebridDownloader, - add_torrent, - get_instant_availability, - get_status, - get_torrents, +) +from program.services.downloaders.models import ( + VIDEO_EXTENSIONS, + DebridFile, + TorrentContainer, + TorrentInfo, + DownloadStatus, ) from program.settings.manager import settings_manager as settings +from program.utils.request import HttpMethod @pytest.fixture -def downloader(instant, upload, status, status_all, delete): +def downloader(user, upload, status, status_all, files, delete): """Instance of AllDebridDownloader with API calls mocked""" - # mock API calls - _get = alldebrid.get - def get(url, **params): - match url: + + def execute(self, method: HttpMethod, endpoint: str, **kwargs) -> dict: + params = kwargs.get("params", {}) + match endpoint: case "user": - return {"data": { "user": { "isPremium": True, "premiumUntil": 1735514599, } } } - case "magnet/instant": - return instant(url, **params) + return user()["data"] case "magnet/upload": - return upload(url, **params) + return upload(endpoint, **params)["data"] case "magnet/delete": - return delete(url, **params) + return delete(endpoint, **params)["data"] case "magnet/status": if params.get("id", False): - return status(url, **params) + return status(endpoint, **params)["data"] else: - return status_all(url, **params) + return status_all(endpoint, **params)["data"] + case "magnet/files": + return files(endpoint, **params)["data"] case _: - raise Exception("unmatched api call") - alldebrid.get = get - - alldebrid_settings = settings.settings.downloaders.all_debrid - alldebrid_settings.enabled = True - alldebrid_settings.api_key = "key" - - downloader = AllDebridDownloader() - assert downloader.initialized - yield downloader - - # tear down mock - alldebrid.get = get - - -## Downloader tests -def test_process_hashes(downloader): - hashes = downloader.process_hashes(["abc"], None, [False, True]) - assert len(hashes) == 1 + raise Exception("unmatched api call %s" % endpoint) + with patch.object(alldebrid.AllDebridRequestHandler, 'execute', execute): + alldebrid_settings = settings.settings.downloaders.all_debrid + alldebrid_settings.enabled = True + alldebrid_settings.api_key = "key" -def test_download_cached(downloader): - torrent_id = downloader.download_cached({"infohash": "abc"}) - assert torrent_id == MAGNET_ID + downloader = AllDebridDownloader() + assert downloader.initialized + yield downloader -def test_get_torrent_names(downloader): - names = downloader.get_torrent_names(123) - assert names == ("Ubuntu 24.04", None) +## DownloaderBase tests +def test_validate(downloader): + assert downloader.validate() == True -## API parsing tests -def test_get_instant_availability(instant): - alldebrid.get = instant - infohashes = [UBUNTU] - availability = get_instant_availability(infohashes) - assert len(availability[0].get("files", [])) == 2 +def test_get_instant_availability(downloader): + assert downloader.get_instant_availability(MAGNET, "movie") == TorrentContainer( + infohash=MAGNET, + files=[DebridFile.create(filename="", filesize_bytes=123, filetype="movie")], + ) -def test_get_instant_availability_unavailable(instant_unavailable): - alldebrid.get = instant_unavailable - infohashes = [UBUNTU] - availability = get_instant_availability(infohashes) - assert availability[0]["hash"] == UBUNTU +def test_add_torrent(downloader): + assert downloader.add_torrent(MAGNET) == ID -def test_add_torrent(upload): - alldebrid.get = upload - torrent_id = add_torrent(UBUNTU) - assert torrent_id == 251993753 +def test_select_files(downloader): + assert downloader.select_files(ID, [1, 2, 3]) is None -def test_add_torrent_cached(upload_ready): - alldebrid.get = upload_ready - torrent_id = add_torrent(UBUNTU) - assert torrent_id == 251993753 +def test_get_torrent_info(downloader): + torrent_info = downloader.get_torrent_info(ID) + assert torrent_info.id == int(ID) + assert torrent_info.name == "Big Buck Bunny" + assert torrent_info.status == DownloadStatus.READY + assert torrent_info.bytes == 276445467 + assert torrent_info.progress == 100 -def test_get_status(status): - alldebrid.get = status - torrent_status = get_status(251993753) - assert torrent_status["filename"] == "Ubuntu 24.04" +def test_delete_torrent(downloader): + assert downloader.delete_torrent(ID) is None # TODO: assert that delete was called -def test_get_status_unfinished(status_downloading): - alldebrid.get = status_downloading - torrent_status = get_status(251993753) - assert torrent_status["status"] == "Downloading" +# MAGNET is the infohash of the torrent. +MAGNET = "dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c" +# ID is AllDebrid's internal ID for this torrent. +ID = "264640947" -def test_get_torrents(status_all): - alldebrid.get = status_all - torrents = get_torrents() - assert torrents[0]["status"] == "Ready" - - -def test_delete(delete): - alldebrid.get = delete - delete(123) - - -# Example requests - taken from real API calls -UBUNTU = "3648baf850d5930510c1f172b534200ebb5496e6" -MAGNET_ID = "251993753" @pytest.fixture -def instant(): - """GET /magnet/instant?magnets[0]=infohash (torrent available)""" - with open("src/tests/test_data/alldebrid_magnet_instant.json") as f: +def user(): + """GET /user""" + with open("src/tests/test_data/alldebrid_user.json") as f: body = json.load(f) - return lambda url, **params: body + return lambda: body -@pytest.fixture -def instant_unavailable(): - """GET /magnet/instant?magnets[0]=infohash (torrent unavailable)""" - with open("src/tests/test_data/alldebrid_magnet_instant_unavailable.json") as f: - body = json.load(f) - return lambda url, **params: body @pytest.fixture def upload(): @@ -139,6 +105,7 @@ def upload(): body = json.load(f) return lambda url, **params: body + @pytest.fixture def upload_ready(): """GET /magnet/upload?magnets[]=infohash (torrent ready)""" @@ -146,6 +113,7 @@ def upload_ready(): body = json.load(f) return lambda url, **params: body + @pytest.fixture def status(): """GET /magnet/status?id=123 (debrid links ready)""" @@ -153,6 +121,7 @@ def status(): body = json.load(f) return lambda url, **params: body + @pytest.fixture def status_downloading(): """GET /magnet/status?id=123 (debrid links not ready yet)""" @@ -160,18 +129,30 @@ def status_downloading(): body = json.load(f) return lambda url, **params: body + @pytest.fixture def status_all(): """GET /magnet/status (gets a list of all links instead of a single object)""" # The body is the same as a single item, but with all your magnets in a list. with open("src/tests/test_data/alldebrid_magnet_status_one_ready.json") as f: body = json.load(f) - return lambda url, **params: {"status": "success", "data": {"magnets": [body["data"]["magnets"]]}} + return lambda url, **params: { + "status": "success", + "data": {"magnets": [body["data"]["magnets"]]}, + } + + +@pytest.fixture +def files(): + """GET /magnet/files?id[]=123 (gets files and links for a magnet)""" + with open("src/tests/test_data/alldebrid_magnet_files.json") as f: + body = json.load(f) + return lambda url, **params: body + @pytest.fixture def delete(): - """GET /delete""" + """GET /delete?id=123""" with open("src/tests/test_data/alldebrid_magnet_delete.json") as f: body = json.load(f) return lambda url, **params: body - diff --git a/src/tests/test_data/alldebrid_magnet_files.json b/src/tests/test_data/alldebrid_magnet_files.json new file mode 100644 index 00000000..d9324f34 --- /dev/null +++ b/src/tests/test_data/alldebrid_magnet_files.json @@ -0,0 +1,27 @@ +{ + "status": "success", + "data": { + "magnets": [ + { + "id": "264640947", + "files": [ + { + "n": "Big Buck Bunny.en.srt", + "s": 140, + "l": "https://alldebrid.com/f/some-hash" + }, + { + "n": "Big Buck Bunny.mp4", + "s": 276134947, + "l": "https://alldebrid.com/f/some-other-hash" + }, + { + "n": "poster.jpg", + "s": 310380, + "l": "https://alldebrid.com/f/some-different-hash" + } + ] + } + ] + } +} diff --git a/src/tests/test_data/alldebrid_magnet_instant.json b/src/tests/test_data/alldebrid_magnet_instant.json deleted file mode 100644 index 4730cdc0..00000000 --- a/src/tests/test_data/alldebrid_magnet_instant.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "status": "success", - "data": { - "magnets": [ - { - "magnet": "3648baf850d5930510c1f172b534200ebb5496e6", - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", - "instant": true, - "files": [ - {"n": "ubuntu-24.04-desktop-amd64.iso", "s": 6114656256}, - {"n": "ubuntu-24.04-live-server-amd64.iso", "s": 2754981888} - ] - } - ] - } -} diff --git a/src/tests/test_data/alldebrid_magnet_instant_unavailable.json b/src/tests/test_data/alldebrid_magnet_instant_unavailable.json deleted file mode 100644 index e553e258..00000000 --- a/src/tests/test_data/alldebrid_magnet_instant_unavailable.json +++ /dev/null @@ -1,12 +0,0 @@ - { - "status": "success", - "data": { - "magnets": [ - { - "magnet": "3648baf850d5930510c1f172b534200ebb5496e6", - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", - "instant": false - } - ] - } -} diff --git a/src/tests/test_data/alldebrid_magnet_status_one_downloading.json b/src/tests/test_data/alldebrid_magnet_status_one_downloading.json index 016f6f9f..6975eed0 100644 --- a/src/tests/test_data/alldebrid_magnet_status_one_downloading.json +++ b/src/tests/test_data/alldebrid_magnet_status_one_downloading.json @@ -2,10 +2,10 @@ "status": "success", "data": { "magnets": { - "id": 251993753, - "filename": "Ubuntu 24.04", - "size": 8869638144, - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", + "id": 264640947, + "filename": "Big Buck Bunny", + "size": 276445467, + "hash": "dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", "status": "Downloading", "statusCode": 1, "downloaded": 165063971, @@ -16,7 +16,6 @@ "uploadSpeed": 0, "uploadDate": 1727454272, "completionDate": 0, - "links": [], "type": "m", "notified": false, "version": 2 diff --git a/src/tests/test_data/alldebrid_magnet_status_one_ready.json b/src/tests/test_data/alldebrid_magnet_status_one_ready.json index a7a0c621..1b274c2e 100644 --- a/src/tests/test_data/alldebrid_magnet_status_one_ready.json +++ b/src/tests/test_data/alldebrid_magnet_status_one_ready.json @@ -1,40 +1,36 @@ { - "status": "success", - "data": { - "magnets": { - "id": 251993753, - "filename": "Ubuntu 24.04", - "size": 8869638144, - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", - "status": "Ready", - "statusCode": 4, - "downloaded": 8869638144, - "uploaded": 0, - "seeders": 0, - "downloadSpeed": 0, - "processingPerc": 0, - "uploadSpeed": 0, - "uploadDate": 1727454272, - "completionDate": 1727454803, - "links": [ - { - "filename": "ubuntu-24.04-desktop-amd64.iso", - "size": 6114656256, - "files": [{"n": "ubuntu-24.04-desktop-amd64.iso", "s": 6114656256}], - "link": "https://alldebrid.com/f/REDACTED" - }, - { - "filename": "ubuntu-24.04-live-server-amd64.iso", - "size": 2754981888, - "files": [ - {"n": "ubuntu-24.04-live-server-amd64.iso", "s": 2754981888} - ], - "link": "https://alldebrid.com/f/REDACTED" - } - ], - "type": "m", - "notified": false, - "version": 2 + "status": "success", + "data": { + "magnets": { + "id": 264640947, + "filename": "Big Buck Bunny", + "size": 276445467, + "hash": "dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", + "status": "Ready", + "statusCode": 4, + "uploadDate": 1732876541, + "completionDate": 1732876541, + "type": "m", + "notified": false, + "version": 2, + "nbLinks": 3, + "files": [ + { + "n": "Big Buck Bunny.en.srt", + "s": 140, + "l": "https://alldebrid.com/f/some-hash" + }, + { + "n": "Big Buck Bunny.mp4", + "s": 276134947, + "l": "https://alldebrid.com/f/some-other-hash" + }, + { + "n": "poster.jpg", + "s": 310380, + "l": "https://alldebrid.com/f/some-different-hash" } + ] } + } } diff --git a/src/tests/test_data/alldebrid_magnet_upload_not_ready.json b/src/tests/test_data/alldebrid_magnet_upload_not_ready.json index 1f4ae8cc..de5ae072 100644 --- a/src/tests/test_data/alldebrid_magnet_upload_not_ready.json +++ b/src/tests/test_data/alldebrid_magnet_upload_not_ready.json @@ -3,13 +3,13 @@ "data": { "magnets": [ { - "magnet": "magnet:?xt=urn:btih:3648baf850d5930510c1f172b534200ebb5496e6", - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", + "magnet": "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", + "hash": "dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", "name": "noname", "filename_original": "", "size": 0, "ready": false, - "id": 251993753 + "id": 264640947 } ] } diff --git a/src/tests/test_data/alldebrid_magnet_upload_ready.json b/src/tests/test_data/alldebrid_magnet_upload_ready.json index c49b65c1..86b651ab 100644 --- a/src/tests/test_data/alldebrid_magnet_upload_ready.json +++ b/src/tests/test_data/alldebrid_magnet_upload_ready.json @@ -1,16 +1,17 @@ -{ - "status": "success", - "data": { - "magnets": [ - { - "magnet": "magnet:?xt=urn:btih:3648baf850d5930510c1f172b534200ebb5496e6", - "hash": "3648baf850d5930510c1f172b534200ebb5496e6", - "name": "Ubuntu 24.04", - "filename_original": "", - "size": 8869638144, - "ready": true, - "id": 251993753 - } - ] - } + + "status": "success", + "data": { + "magnets": [ + { + "magnet": "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", + "hash": "dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c", + "name": "Big Buck Bunny", + "filename_original": "", + "size": 276445467, + "ready": true, + "id": 264640947 + } + ] + } } + diff --git a/src/tests/test_data/alldebrid_user.json b/src/tests/test_data/alldebrid_user.json new file mode 100644 index 00000000..5535dfcf --- /dev/null +++ b/src/tests/test_data/alldebrid_user.json @@ -0,0 +1,31 @@ +{ + "status": "success", + "data": { + "user": { + "username": "user1", + "email": "alldebrid-user@example.com", + "isPremium": true, + "isTrial": false, + "isSubscribed": false, + "premiumUntil": 2035514599, + "lang": "en", + "preferedDomain": "com", + "fidelityPoints": 140, + "limitedHostersQuotas": { + "ddl": 1000, + "dropgalaxy": 5000, + "fileal": 5000, + "filedot": 1000, + "filefactory": 3000, + "filespace": 3000, + "flashbit": 5000, + "gigapeta": 5000, + "isra": 3000, + "katfile": 3000, + "worldbytez": 3000 + }, + "notifications": [], + "history": true + } + } +}