1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00
supysonic/tests/base/test_cli.py

164 lines
5.5 KiB
Python
Raw Normal View History

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
import shlex
2017-12-10 16:53:02 +00:00
import unittest
from click.testing import CliRunner
from pony.orm import db_session
from supysonic.db import Folder, User, init_database, release_database
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):
"""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):
self.__conf = TestConfig(False, False)
self.__db = tempfile.mkstemp()
self.__conf.BASE["database_uri"] = "sqlite:///" + self.__db[1]
init_database(self.__conf.BASE["database_uri"])
2017-12-10 16:53:02 +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()
os.close(self.__db[0])
os.remove(self.__db[1])
2017-12-10 16:53:02 +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):
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):
with tempfile.TemporaryDirectory() as d:
self.__add_folder("f1", d)
self.__add_folder("f2", d, True)
with tempfile.TemporaryDirectory() as d:
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):
with tempfile.TemporaryDirectory() as d:
self.__add_folder("tmpfolder", d)
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):
with tempfile.TemporaryDirectory() as d:
self.__add_folder("tmpfolder", d)
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):
with tempfile.TemporaryDirectory() as d:
self.__add_folder("tmpfolder", d)
2019-06-29 15:25:44 +00:00
with tempfile.NamedTemporaryFile(dir=d):
self.__invoke("folder scan")
self.__invoke("folder scan tmpfolder nonexistent")
2017-12-10 16:53:02 +00:00
def test_user_add(self):
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):
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):
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):
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):
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):
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):
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):
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):
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
self.__invoke("user rename alice ''", True)
2021-02-07 15:35:55 +00:00
with db_session:
self.assertEqual(User.select().first().name, "alice")
self.__invoke("user rename alice bob")
2021-02-07 15:35:55 +00:00
with db_session:
self.assertEqual(User.select().first().name, "bob")
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()