2017-11-17 18:56:14 +00:00
|
|
|
#!/usr/bin/env python
|
2018-03-04 20:49:56 +00:00
|
|
|
# coding: utf-8
|
2017-11-17 18:56:14 +00:00
|
|
|
#
|
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Alban 'spl0k' Féron
|
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import uuid
|
2017-12-23 21:59:04 +00:00
|
|
|
|
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 pony.orm import db_session
|
2017-11-17 18:56:14 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
class MediaTestCase(ApiTestBase):
|
|
|
|
def setUp(self):
|
|
|
|
super(MediaTestCase, self).setUp()
|
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
with db_session:
|
|
|
|
folder = Folder(
|
|
|
|
name = 'Root',
|
|
|
|
path = os.path.abspath('tests/assets'),
|
|
|
|
root = True,
|
2018-05-21 14:16:06 +00:00
|
|
|
cover_art = 'cover.jpg'
|
2017-12-23 21:59:04 +00:00
|
|
|
)
|
|
|
|
self.folderid = folder.id
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
artist = Artist(name = 'Artist')
|
|
|
|
album = Album(artist = artist, name = 'Album')
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
track = Track(
|
|
|
|
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,
|
|
|
|
content_type = 'audio/mpeg',
|
|
|
|
last_modification = 0
|
|
|
|
)
|
|
|
|
self.trackid = track.id
|
2018-10-08 19:30:38 +00:00
|
|
|
track_embeded_art = Track(
|
|
|
|
title = '[silence]',
|
|
|
|
number = 1,
|
|
|
|
disc = 1,
|
|
|
|
artist = artist,
|
|
|
|
album = album,
|
|
|
|
path = os.path.abspath('tests/assets/folder/silence.mp3'),
|
2018-10-09 00:05:45 +00:00
|
|
|
root_folder = folder,
|
|
|
|
folder = folder,
|
2018-10-08 19:30:38 +00:00
|
|
|
duration = 2,
|
|
|
|
bitrate = 320,
|
|
|
|
content_type = 'audio/mpeg',
|
|
|
|
last_modification = 0
|
|
|
|
)
|
2018-10-09 00:05:45 +00:00
|
|
|
self.trackid_embeded_art = track_embeded_art.id
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
def test_stream(self):
|
|
|
|
self._make_request('stream', error = 10)
|
|
|
|
self._make_request('stream', { 'id': 'string' }, error = 0)
|
|
|
|
self._make_request('stream', { 'id': str(uuid.uuid4()) }, error = 70)
|
2017-12-23 21:59:04 +00:00
|
|
|
self._make_request('stream', { 'id': str(self.folderid) }, error = 70)
|
|
|
|
self._make_request('stream', { 'id': str(self.trackid), 'maxBitRate': 'string' }, error = 0)
|
2018-03-10 21:15:40 +00:00
|
|
|
self._make_request('stream', { 'id': str(self.trackid), 'timeOffset': 2 }, error = 0)
|
|
|
|
self._make_request('stream', { 'id': str(self.trackid), 'size': '640x480' }, error = 0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
rv = self.client.get('/rest/stream.view', query_string = { 'u': 'alice', 'p': 'Alic3', 'c': 'tests', 'id': str(self.trackid) })
|
2017-11-17 18:56:14 +00:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'audio/mpeg')
|
|
|
|
self.assertEqual(len(rv.data), 23)
|
2017-12-23 21:59:04 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(Track[self.trackid].play_count, 1)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
def test_download(self):
|
|
|
|
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-12-23 21:59:04 +00:00
|
|
|
self._make_request('download', { 'id': str(self.folderid) }, error = 70)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
rv = self.client.get('/rest/download.view', query_string = { 'u': 'alice', 'p': 'Alic3', 'c': 'tests', 'id': str(self.trackid) })
|
2017-11-17 18:56:14 +00:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'audio/mpeg')
|
|
|
|
self.assertEqual(len(rv.data), 23)
|
2017-12-23 21:59:04 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(Track[self.trackid].play_count, 0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
|
|
|
def test_get_cover_art(self):
|
|
|
|
self._make_request('getCoverArt', error = 10)
|
|
|
|
self._make_request('getCoverArt', { 'id': 'string' }, error = 0)
|
|
|
|
self._make_request('getCoverArt', { 'id': str(uuid.uuid4()) }, error = 70)
|
2017-12-23 21:59:04 +00:00
|
|
|
self._make_request('getCoverArt', { 'id': str(self.trackid) }, error = 70)
|
|
|
|
self._make_request('getCoverArt', { 'id': str(self.folderid), 'size': 'large' }, error = 0)
|
2017-11-17 18:56:14 +00:00
|
|
|
|
2017-12-23 21:59:04 +00:00
|
|
|
args = { 'u': 'alice', 'p': 'Alic3', 'c': 'tests', 'id': str(self.folderid) }
|
2017-11-17 18:56:14 +00:00
|
|
|
rv = self.client.get('/rest/getCoverArt.view', query_string = args)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'image/jpeg')
|
|
|
|
im = Image.open(BytesIO(rv.data))
|
|
|
|
self.assertEqual(im.format, 'JPEG')
|
|
|
|
self.assertEqual(im.size, (420, 420))
|
|
|
|
|
|
|
|
args['size'] = 600
|
|
|
|
rv = self.client.get('/rest/getCoverArt.view', query_string = args)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'image/jpeg')
|
|
|
|
im = Image.open(BytesIO(rv.data))
|
|
|
|
self.assertEqual(im.format, 'JPEG')
|
|
|
|
self.assertEqual(im.size, (420, 420))
|
|
|
|
|
|
|
|
args['size'] = 120
|
|
|
|
rv = self.client.get('/rest/getCoverArt.view', query_string = args)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'image/jpeg')
|
|
|
|
im = Image.open(BytesIO(rv.data))
|
|
|
|
self.assertEqual(im.format, 'JPEG')
|
|
|
|
self.assertEqual(im.size, (120, 120))
|
|
|
|
|
2017-11-27 21:46:09 +00:00
|
|
|
# rerequest, just in case
|
|
|
|
rv = self.client.get('/rest/getCoverArt.view', query_string = args)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'image/jpeg')
|
|
|
|
im = Image.open(BytesIO(rv.data))
|
|
|
|
self.assertEqual(im.format, 'JPEG')
|
|
|
|
self.assertEqual(im.size, (120, 120))
|
|
|
|
|
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
|
2018-10-09 00:05:45 +00:00
|
|
|
args['id'] = str(self.trackid_embeded_art)
|
2018-10-08 19:30:38 +00:00
|
|
|
rv = self.client.get('/rest/getCoverArt.view', query_string = args)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.mimetype, 'image/png')
|
|
|
|
im = Image.open(BytesIO(rv.data))
|
|
|
|
self.assertEqual(im.format, 'PNG')
|
|
|
|
self.assertEqual(im.size, (120, 120))
|
|
|
|
|
2017-11-17 18:56:14 +00:00
|
|
|
def test_get_lyrics(self):
|
|
|
|
self._make_request('getLyrics', error = 10)
|
|
|
|
self._make_request('getLyrics', { 'artist': 'artist' }, error = 10)
|
|
|
|
self._make_request('getLyrics', { 'title': 'title' }, error = 10)
|
|
|
|
|
|
|
|
rv, child = self._make_request('getLyrics', { 'artist': 'some really long name hoping', 'title': 'to get absolutely no result' }, tag = 'lyrics')
|
|
|
|
self.assertIsNone(child.text)
|
|
|
|
|
|
|
|
# ChartLyrics
|
|
|
|
rv, child = self._make_request('getLyrics', { 'artist': 'The Clash', 'title': 'London Calling' }, tag = 'lyrics')
|
|
|
|
self.assertIn('live by the river', child.text)
|
|
|
|
|
|
|
|
# Local file
|
|
|
|
rv, child = self._make_request('getLyrics', { 'artist': 'artist', 'title': '23bytes' }, tag = 'lyrics')
|
|
|
|
self.assertIn('null', child.text)
|
|
|
|
|
|
|
|
def test_get_avatar(self):
|
|
|
|
self._make_request('getAvatar', error = 0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|