1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00
supysonic/tests/testbase.py

117 lines
3.3 KiB
Python
Raw Normal View History

2017-11-17 22:26:25 +00:00
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
2022-12-10 14:48:06 +00:00
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
2017-11-17 22:26:25 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
2017-12-05 22:18:39 +00:00
import inspect
2017-12-19 22:16:55 +00:00
import os
import os.path
2017-11-27 21:55:18 +00:00
import shutil
import sys
2017-11-27 21:55:18 +00:00
import tempfile
import unittest
2017-11-17 22:26:25 +00:00
2022-12-18 15:50:03 +00:00
from supysonic.db import release_database
2017-11-27 21:30:13 +00:00
from supysonic.config import DefaultConfig
2017-11-17 22:26:25 +00:00
from supysonic.managers.user import UserManager
2017-12-19 22:16:55 +00:00
from supysonic.web import create_application
2017-11-27 21:30:13 +00:00
2019-06-29 15:25:44 +00:00
2017-11-27 21:30:13 +00:00
class TestConfig(DefaultConfig):
TESTING = True
2019-06-29 15:25:44 +00:00
LOGGER_HANDLER_POLICY = "never"
MIMETYPES = {"mp3": "audio/mpeg", "weirdextension": "application/octet-stream"}
2017-12-07 22:33:32 +00:00
TRANSCODING = {
2019-06-29 15:25:44 +00:00
"transcoder_mp3_mp3": "echo -n %srcpath %outrate",
2020-11-14 18:01:39 +00:00
"transcoder_mp3_rnd": "dd if=/dev/urandom bs=1kB count=52 status=none",
2019-06-29 15:25:44 +00:00
"decoder_mp3": "echo -n Pushing out some mp3 data...",
"encoder_cat": "cat -",
"encoder_md5": "md5sum",
2017-12-07 22:33:32 +00:00
}
2017-11-27 21:30:13 +00:00
def __init__(self, with_webui, with_api):
2020-11-22 15:12:14 +00:00
super().__init__()
2017-12-05 08:11:53 +00:00
2017-12-05 22:18:39 +00:00
for cls in reversed(inspect.getmro(self.__class__)):
for attr, value in cls.__dict__.items():
2019-06-29 15:25:44 +00:00
if attr.startswith("_") or attr != attr.upper():
2017-12-05 22:18:39 +00:00
continue
2017-12-05 08:11:53 +00:00
2017-12-05 22:18:39 +00:00
if isinstance(value, dict):
setattr(self, attr, value.copy())
else:
setattr(self, attr, value)
2017-11-17 22:26:25 +00:00
2019-06-29 15:25:44 +00:00
self.WEBAPP.update({"mount_webui": with_webui, "mount_api": with_api})
with tempfile.NamedTemporaryFile() as tf:
if sys.platform == "win32":
self.DAEMON["socket"] = "\\\\.\\pipe\\" + os.path.basename(tf.name)
else:
self.DAEMON["socket"] = tf.name
2017-11-17 22:26:25 +00:00
2020-11-22 15:12:14 +00:00
class MockResponse:
def __init__(self, response):
self.__status_code = response.status_code
2019-06-29 15:25:44 +00:00
self.__data = response.get_data(as_text=True)
self.__mimetype = response.mimetype
@property
def status_code(self):
return self.__status_code
@property
def data(self):
return self.__data
@property
def mimetype(self):
return self.__mimetype
2019-06-29 15:25:44 +00:00
def patch_method(f):
original = f
2019-06-29 15:25:44 +00:00
def patched(*args, **kwargs):
rv = original(*args, **kwargs)
return MockResponse(rv)
return patched
2019-06-29 15:25:44 +00:00
2017-11-17 22:26:25 +00:00
class TestBase(unittest.TestCase):
2017-11-27 21:30:13 +00:00
__with_webui__ = False
__with_api__ = False
2017-11-17 22:26:25 +00:00
def setUp(self):
self.__db = tempfile.mkstemp()
2017-11-27 21:55:18 +00:00
self.__dir = tempfile.mkdtemp()
2020-11-24 12:44:36 +00:00
self.config = TestConfig(self.__with_webui__, self.__with_api__)
self.config.BASE["database_uri"] = "sqlite:///" + self.__db[1]
self.config.WEBAPP["cache_dir"] = self.__dir
2017-11-27 21:55:18 +00:00
2020-11-24 12:44:36 +00:00
self.__app = create_application(self.config)
self.client = self.__app.test_client()
2017-11-17 22:26:25 +00:00
2022-12-10 14:48:06 +00:00
UserManager.add("alice", "Alic3", admin=True)
UserManager.add("bob", "B0b")
2017-12-19 22:16:55 +00:00
def _patch_client(self):
self.client.get = patch_method(self.client.get)
self.client.post = patch_method(self.client.post)
2020-11-14 18:01:39 +00:00
def app_context(self, *args, **kwargs):
return self.__app.app_context(*args, **kwargs)
def request_context(self, *args, **kwargs):
return self.__app.test_request_context(*args, **kwargs)
2017-11-17 22:26:25 +00:00
def tearDown(self):
2017-12-19 22:16:55 +00:00
release_database()
2017-11-27 21:55:18 +00:00
shutil.rmtree(self.__dir)
os.close(self.__db[0])
os.remove(self.__db[1])