2017-12-10 16:53:02 +00:00
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
#
|
2021-02-07 15:35:55 +00:00
|
|
|
# Copyright (C) 2017-2021 Alban 'spl0k' Féron
|
2017-12-10 16:53:02 +00:00
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
2020-11-08 14:39:09 +00:00
|
|
|
import shlex
|
2017-12-10 16:53:02 +00:00
|
|
|
import unittest
|
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
from click.testing import CliRunner
|
2018-08-11 14:16:34 +00:00
|
|
|
from pony.orm import db_session
|
2018-01-05 23:33:45 +00:00
|
|
|
|
2018-08-11 14:16:34 +00:00
|
|
|
from supysonic.db import Folder, User, init_database, release_database
|
2021-11-11 15:47:37 +00:00
|
|
|
from supysonic.cli import cli
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
from ..testbase import TestConfig
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
|
2017-12-10 16:53:02 +00:00
|
|
|
class CLITestCase(unittest.TestCase):
|
2021-11-11 15:47:37 +00:00
|
|
|
"""Really basic tests. Some even don't check anything but are just there for coverage"""
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__conf = TestConfig(False, False)
|
2020-11-08 14:39:09 +00:00
|
|
|
self.__db = tempfile.mkstemp()
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__conf.BASE["database_uri"] = "sqlite:///" + self.__db[1]
|
|
|
|
init_database(self.__conf.BASE["database_uri"])
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__runner = CliRunner()
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2017-12-19 22:16:55 +00:00
|
|
|
release_database()
|
2020-11-08 14:39:09 +00:00
|
|
|
os.close(self.__db[0])
|
|
|
|
os.remove(self.__db[1])
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
def __invoke(self, cmd, expect_fail=False):
|
|
|
|
rv = self.__runner.invoke(cli, shlex.split(cmd), obj=self.__conf)
|
|
|
|
func = self.assertNotEqual if expect_fail else self.assertEqual
|
|
|
|
func(rv.exit_code, 0)
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def __add_folder(self, name, path, expect_fail=False):
|
|
|
|
self.__invoke("folder add {} {}".format(name, shlex.quote(path)), expect_fail)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_folder_add(self):
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
self.__add_folder("tmpfolder", d)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
with db_session:
|
|
|
|
f = Folder.select().first()
|
|
|
|
self.assertIsNotNone(f)
|
|
|
|
self.assertEqual(f.path, d)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_folder_add_errors(self):
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
self.__add_folder("f1", d)
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__add_folder("f2", d, True)
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__add_folder("f1", d, True)
|
|
|
|
self.__invoke("folder add f3 /invalid/path", True)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(Folder.select().count(), 1)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_folder_delete(self):
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
self.__add_folder("tmpfolder", d)
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("folder delete randomfolder", True)
|
|
|
|
self.__invoke("folder delete tmpfolder")
|
2017-12-17 22:25:34 +00:00
|
|
|
|
|
|
|
with db_session:
|
|
|
|
self.assertEqual(Folder.select().count(), 0)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_folder_list(self):
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
self.__add_folder("tmpfolder", d)
|
2021-11-11 15:47:37 +00:00
|
|
|
rv = self.__invoke("folder list")
|
|
|
|
self.assertIn("tmpfolder", rv.output)
|
|
|
|
self.assertIn(d, rv.output)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_folder_scan(self):
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
self.__add_folder("tmpfolder", d)
|
2019-06-29 15:25:44 +00:00
|
|
|
with tempfile.NamedTemporaryFile(dir=d):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("folder scan")
|
|
|
|
self.__invoke("folder scan tmpfolder nonexistent")
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_user_add(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user add -p alice alice", True)
|
2017-12-17 22:25:34 +00:00
|
|
|
|
|
|
|
with db_session:
|
|
|
|
self.assertEqual(User.select().count(), 1)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_user_delete(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user delete alice")
|
|
|
|
self.__invoke("user delete bob", True)
|
2017-12-17 22:25:34 +00:00
|
|
|
|
|
|
|
with db_session:
|
|
|
|
self.assertEqual(User.select().count(), 0)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_user_list(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
rv = self.__invoke("user list")
|
|
|
|
self.assertIn("alice", rv.output)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
def test_user_setadmin(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user setroles -A alice")
|
|
|
|
self.__invoke("user setroles -A bob", True)
|
2017-12-17 22:25:34 +00:00
|
|
|
with db_session:
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertTrue(User.get(name="alice").admin)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2019-11-23 14:43:27 +00:00
|
|
|
def test_user_unsetadmin(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user setroles -A alice")
|
|
|
|
self.__invoke("user setroles -a alice")
|
2019-11-23 14:43:27 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertFalse(User.get(name="alice").admin)
|
|
|
|
|
|
|
|
def test_user_setjukebox(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user setroles -J alice")
|
2019-11-23 14:43:27 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertTrue(User.get(name="alice").jukebox)
|
|
|
|
|
|
|
|
def test_user_unsetjukebox(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user setroles -J alice")
|
|
|
|
self.__invoke("user setroles -j alice")
|
2019-11-23 14:43:27 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertFalse(User.get(name="alice").jukebox)
|
|
|
|
|
2017-12-10 16:53:02 +00:00
|
|
|
def test_user_changepass(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user changepass alice -p newpass")
|
|
|
|
self.__invoke("user changepass bob -p B0b", True)
|
2017-12-10 16:53:02 +00:00
|
|
|
|
2021-02-07 15:35:55 +00:00
|
|
|
def test_user_rename(self):
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Alic3 alice")
|
|
|
|
self.__invoke("user rename alice alice")
|
|
|
|
self.__invoke("user rename bob charles", True)
|
2021-02-07 15:35:55 +00:00
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user rename alice ''", True)
|
2021-02-07 15:35:55 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(User.select().first().name, "alice")
|
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user rename alice bob")
|
2021-02-07 15:35:55 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(User.select().first().name, "bob")
|
|
|
|
|
2021-11-11 15:47:37 +00:00
|
|
|
self.__invoke("user add -p Ch4rl3s charles")
|
|
|
|
self.__invoke("user rename bob charles", True)
|
2021-02-07 15:35:55 +00:00
|
|
|
with db_session:
|
|
|
|
self.assertEqual(User.select(lambda u: u.name == "bob").count(), 1)
|
|
|
|
self.assertEqual(User.select(lambda u: u.name == "charles").count(), 1)
|
|
|
|
|
2017-12-10 16:53:02 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|