1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-13 21:52:18 +00:00
supysonic/tests/base/test_cli.py

168 lines
5.7 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
2019-12-23 15:23:57 +00:00
from io import StringIO
from pony.orm import db_session
from supysonic.db import Folder, User, init_database, release_database
2017-12-10 16:53:02 +00:00
from supysonic.cli import SupysonicCLI
from ..testbase import TestConfig
2019-06-29 15:25:44 +00:00
2017-12-10 16:53:02 +00:00
class CLITestCase(unittest.TestCase):
2017-12-17 22:25:34 +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):
conf = TestConfig(False, False)
self.__db = tempfile.mkstemp()
conf.BASE["database_uri"] = "sqlite:///" + self.__db[1]
2019-06-29 15:25:44 +00:00
init_database(conf.BASE["database_uri"])
2017-12-10 16:53:02 +00:00
self.__stdout = StringIO()
self.__stderr = StringIO()
2019-06-29 15:25:44 +00:00
self.__cli = SupysonicCLI(conf, stdout=self.__stdout, stderr=self.__stderr)
2017-12-10 16:53:02 +00:00
def tearDown(self):
self.__stdout.close()
self.__stderr.close()
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 __add_folder(self, name, path):
2020-11-22 15:12:14 +00:00
self.__cli.onecmd("folder add {} {}".format(name, shlex.quote(path)))
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)
with tempfile.TemporaryDirectory() as d:
self.__add_folder("f1", d)
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("folder add f3 /invalid/path")
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)
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("folder delete randomfolder")
self.__cli.onecmd("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)
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("folder list")
self.assertIn("tmpfolder", self.__stdout.getvalue())
2017-12-10 16:53:02 +00:00
self.assertIn(d, self.__stdout.getvalue())
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.__cli.onecmd("folder scan")
self.__cli.onecmd("folder scan tmpfolder nonexistent")
2017-12-10 16:53:02 +00:00
def test_user_add(self):
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user add -p alice alice")
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):
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user delete alice")
self.__cli.onecmd("user delete bob")
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):
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user list")
self.assertIn("alice", self.__stdout.getvalue())
2017-12-10 16:53:02 +00:00
def test_user_setadmin(self):
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("user add -p Alic3 alice")
2019-11-23 14:43:27 +00:00
self.__cli.onecmd("user setroles -A alice")
self.__cli.onecmd("user setroles -A bob")
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.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user setroles -A alice")
self.__cli.onecmd("user setroles -a alice")
with db_session:
self.assertFalse(User.get(name="alice").admin)
def test_user_setjukebox(self):
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user setroles -J alice")
with db_session:
self.assertTrue(User.get(name="alice").jukebox)
def test_user_unsetjukebox(self):
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user setroles -J alice")
self.__cli.onecmd("user setroles -j alice")
with db_session:
self.assertFalse(User.get(name="alice").jukebox)
2017-12-10 16:53:02 +00:00
def test_user_changepass(self):
2019-06-29 15:25:44 +00:00
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user changepass alice newpass")
self.__cli.onecmd("user changepass bob B0b")
2017-12-10 16:53:02 +00:00
2021-02-07 15:35:55 +00:00
def test_user_rename(self):
self.__cli.onecmd("user add -p Alic3 alice")
self.__cli.onecmd("user rename alice alice")
self.__cli.onecmd("user rename bob charles")
self.__cli.onecmd("user rename alice ''")
with db_session:
self.assertEqual(User.select().first().name, "alice")
self.__cli.onecmd("user rename alice bob")
with db_session:
self.assertEqual(User.select().first().name, "bob")
self.__cli.onecmd("user add -p Ch4rl3s charles")
self.__cli.onecmd("user rename bob charles")
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
def test_other(self):
2019-06-29 15:25:44 +00:00
self.assertTrue(self.__cli.do_EOF(""))
self.__cli.onecmd("unknown command")
2017-12-10 16:53:02 +00:00
self.__cli.postloop()
2019-06-29 15:25:44 +00:00
self.__cli.completedefault("user", "user", 4, 4)
2017-12-10 16:53:02 +00:00
if __name__ == "__main__":
unittest.main()