2017-11-17 18:56:14 +00:00
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
#
|
2024-06-02 10:33:08 +00:00
|
|
|
# Copyright (C) 2017-2024 Alban 'spl0k' Féron
|
2017-11-17 18:56:14 +00:00
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
|
|
|
|
|
|
|
import os.path
|
2020-11-29 16:24:28 +00:00
|
|
|
import unittest
|
2017-11-17 18:56:14 +00:00
|
|
|
import uuid
|
2017-12-23 21:59:04 +00:00
|
|
|
|
2020-11-08 14:40:37 +00:00
|
|
|
from contextlib import closing
|
2017-11-17 18:56:14 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
|
|
|
|
2018-08-11 14:16:34 +00:00
|
|
|
from supysonic.db import Folder, Artist, Album, Track
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
from .apitestbase import ApiTestBase
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
|
2017-11-17 18:56:14 +00:00
|
|
|
class MediaTestCase(ApiTestBase):
|
|
|
|
def setUp(self):
|
2020-11-22 15:12:14 +00:00
|
|
|
super().setUp()
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2022-12-23 14:36:40 +00:00
|
|
|
folder = Folder.create(
|
|
|
|
name="Root",
|
|
|
|
path=os.path.abspath("tests/assets"),
|
|
|
|
root=True,
|
|
|
|
cover_art="cover.jpg",
|
|
|
|
)
|
|
|
|
folder = Folder.get(name="Root")
|
|
|
|
self.folderid = folder.id
|
|
|
|
|
|
|
|
artist = Artist.create(name="Artist")
|
|
|
|
album = Album.create(artist=artist, name="Album")
|
|
|
|
|
|
|
|
track = Track.create(
|
|
|
|
title="23bytes",
|
|
|
|
number=1,
|
|
|
|
disc=1,
|
|
|
|
artist=artist,
|
|
|
|
album=album,
|
|
|
|
path=os.path.abspath("tests/assets/23bytes"),
|
|
|
|
root_folder=folder,
|
|
|
|
folder=folder,
|
|
|
|
duration=2,
|
|
|
|
bitrate=320,
|
|
|
|
last_modification=0,
|
|
|
|
)
|
|
|
|
self.trackid = track.id
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2022-12-23 14:36:40 +00:00
|
|
|
self.formats = ["mp3", "flac", "ogg", "m4a"]
|
|
|
|
for i in range(len(self.formats)):
|
|
|
|
track_embeded_art = Track.create(
|
|
|
|
title="[silence]",
|
2019-06-29 15:25:44 +00:00
|
|
|
number=1,
|
|
|
|
disc=1,
|
|
|
|
artist=artist,
|
|
|
|
album=album,
|
2023-01-08 15:16:28 +00:00
|
|
|
path=os.path.abspath(f"tests/assets/formats/silence.{self.formats[i]}"),
|
2019-06-29 15:25:44 +00:00
|
|
|
root_folder=folder,
|
|
|
|
folder=folder,
|
|
|
|
duration=2,
|
|
|
|
bitrate=320,
|
|
|
|
last_modification=0,
|
2017-12-23 21:59:04 +00:00
|
|
|
)
|
2022-12-23 14:36:40 +00:00
|
|
|
self.formats[i] = track_embeded_art.id
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
def test_stream(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
self._make_request("stream", error=10)
|
|
|
|
self._make_request("stream", {"id": "string"}, error=0)
|
|
|
|
self._make_request("stream", {"id": str(uuid.uuid4())}, error=70)
|
2019-10-01 14:55:23 +00:00
|
|
|
self._make_request("stream", {"id": str(self.folderid)}, error=0)
|
2019-06-29 15:25:44 +00:00
|
|
|
self._make_request(
|
|
|
|
"stream", {"id": str(self.trackid), "maxBitRate": "string"}, error=0
|
|
|
|
)
|
|
|
|
self._make_request(
|
|
|
|
"stream", {"id": str(self.trackid), "timeOffset": 2}, error=0
|
|
|
|
)
|
|
|
|
self._make_request(
|
|
|
|
"stream", {"id": str(self.trackid), "size": "640x480"}, error=0
|
|
|
|
)
|
|
|
|
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get(
|
|
|
|
"/rest/stream.view",
|
|
|
|
query_string={
|
|
|
|
"u": "alice",
|
|
|
|
"p": "Alic3",
|
|
|
|
"c": "tests",
|
|
|
|
"id": str(self.trackid),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(len(rv.data), 23)
|
2024-06-02 10:33:08 +00:00
|
|
|
self.assertEqual(Track[self.trackid].play_count, 0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
def test_download(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
self._make_request("download", error=10)
|
|
|
|
self._make_request("download", {"id": "string"}, error=0)
|
|
|
|
self._make_request("download", {"id": str(uuid.uuid4())}, error=70)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2019-01-13 16:18:27 +00:00
|
|
|
# download single file
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get(
|
|
|
|
"/rest/download.view",
|
|
|
|
query_string={
|
|
|
|
"u": "alice",
|
|
|
|
"p": "Alic3",
|
|
|
|
"c": "tests",
|
|
|
|
"id": str(self.trackid),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(len(rv.data), 23)
|
2022-12-23 14:36:40 +00:00
|
|
|
self.assertEqual(Track[self.trackid].play_count, 0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2019-01-13 16:18:27 +00:00
|
|
|
# dowload folder
|
2019-06-29 15:25:44 +00:00
|
|
|
rv = self.client.get(
|
|
|
|
"/rest/download.view",
|
|
|
|
query_string={
|
|
|
|
"u": "alice",
|
|
|
|
"p": "Alic3",
|
|
|
|
"c": "tests",
|
|
|
|
"id": str(self.folderid),
|
|
|
|
},
|
|
|
|
)
|
2019-01-13 16:18:27 +00:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertEqual(rv.mimetype, "application/zip")
|
2019-01-13 16:18:27 +00:00
|
|
|
|
2020-11-22 17:09:15 +00:00
|
|
|
def __assert_image_data(self, resp, format, size):
|
|
|
|
with Image.open(BytesIO(resp.data)) as im:
|
|
|
|
self.assertEqual(im.format, format)
|
|
|
|
self.assertEqual(im.size, (size, size))
|
|
|
|
|
2017-11-17 18:56:14 +00:00
|
|
|
def test_get_cover_art(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
self._make_request("getCoverArt", error=10)
|
|
|
|
self._make_request("getCoverArt", {"id": "string"}, error=0)
|
|
|
|
self._make_request("getCoverArt", {"id": str(uuid.uuid4())}, error=70)
|
|
|
|
self._make_request("getCoverArt", {"id": str(self.trackid)}, error=70)
|
|
|
|
self._make_request(
|
|
|
|
"getCoverArt", {"id": str(self.folderid), "size": "large"}, error=0
|
|
|
|
)
|
|
|
|
|
|
|
|
args = {"u": "alice", "p": "Alic3", "c": "tests", "id": str(self.folderid)}
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get("/rest/getCoverArt.view", query_string=args)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, "image/jpeg")
|
2020-11-22 17:09:15 +00:00
|
|
|
self.__assert_image_data(rv, "JPEG", 420)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
args["size"] = 600
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get("/rest/getCoverArt.view", query_string=args)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, "image/jpeg")
|
2020-11-22 17:09:15 +00:00
|
|
|
self.__assert_image_data(rv, "JPEG", 420)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
args["size"] = 120
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get("/rest/getCoverArt.view", query_string=args)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, "image/jpeg")
|
2020-11-22 17:09:15 +00:00
|
|
|
self.__assert_image_data(rv, "JPEG", 120)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-11-27 21:46:09 +00:00
|
|
|
# rerequest, just in case
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get("/rest/getCoverArt.view", query_string=args)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, "image/jpeg")
|
2020-11-22 17:09:15 +00:00
|
|
|
self.__assert_image_data(rv, "JPEG", 120)
|
2017-11-27 21:46:09 +00:00
|
|
|
|
2017-11-17 18:56:14 +00:00
|
|
|
# TODO test non square covers
|
|
|
|
|
2018-10-08 19:30:38 +00:00
|
|
|
# Test extracting cover art from embeded media
|
2019-06-29 15:25:44 +00:00
|
|
|
for args["id"] in self.formats:
|
2020-11-08 14:40:37 +00:00
|
|
|
with closing(
|
|
|
|
self.client.get("/rest/getCoverArt.view", query_string=args)
|
|
|
|
) as rv:
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, "image/png")
|
2020-11-22 17:09:15 +00:00
|
|
|
self.__assert_image_data(rv, "PNG", 120)
|
2018-10-08 19:30:38 +00:00
|
|
|
|
2017-11-17 18:56:14 +00:00
|
|
|
def test_get_avatar(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
self._make_request("getAvatar", error=0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|