2017-08-07 11:10:06 +00:00
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
2017-07-13 20:02:03 +00:00
|
|
|
#
|
2022-12-10 16:12:44 +00:00
|
|
|
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
|
2017-08-07 11:10:06 +00:00
|
|
|
# 2017 Óscar García Amor
|
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
from supysonic import db
|
|
|
|
from supysonic.managers.user import UserManager
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import uuid
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
class UserManagerTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Create an empty sqlite database in memory
|
2019-06-29 15:25:44 +00:00
|
|
|
db.init_database("sqlite:")
|
2017-12-19 22:16:55 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
db.release_database()
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-12-16 16:42:53 +00:00
|
|
|
def create_data(self):
|
2017-07-13 20:02:03 +00:00
|
|
|
# Create some users
|
2020-11-10 14:38:24 +00:00
|
|
|
alice = UserManager.add("alice", "ALICE", admin=True)
|
|
|
|
self.assertIsInstance(alice, db.User)
|
|
|
|
self.assertTrue(alice.admin)
|
|
|
|
|
|
|
|
bob = UserManager.add("bob", "BOB")
|
|
|
|
self.assertIsInstance(bob, db.User)
|
|
|
|
self.assertFalse(bob.admin)
|
|
|
|
|
|
|
|
self.assertIsInstance(UserManager.add("charlie", "CHARLIE"), db.User)
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2022-12-10 16:12:44 +00:00
|
|
|
folder = db.Folder.create(name="Root", path="tests/assets", root=True)
|
|
|
|
artist = db.Artist.create(name="Artist")
|
|
|
|
album = db.Album.create(name="Album", artist=artist)
|
|
|
|
track = db.Track.create(
|
2019-06-29 15:25:44 +00:00
|
|
|
title="Track",
|
|
|
|
disc=1,
|
|
|
|
number=1,
|
|
|
|
duration=1,
|
|
|
|
artist=artist,
|
|
|
|
album=album,
|
|
|
|
path="tests/assets/empty",
|
|
|
|
folder=folder,
|
|
|
|
root_folder=folder,
|
|
|
|
bitrate=320,
|
|
|
|
last_modification=0,
|
2017-12-16 16:42:53 +00:00
|
|
|
)
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
playlist = db.Playlist(name="Playlist", user=db.User.get(name="alice"))
|
2017-11-20 20:32:35 +00:00
|
|
|
playlist.add(track)
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
def test_encrypt_password(self):
|
2017-10-28 13:54:41 +00:00
|
|
|
func = UserManager._UserManager__encrypt_password
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertEqual(
|
|
|
|
func("password", "salt"),
|
|
|
|
("59b3e8d637cf97edbe2384cf59cb7453dfe30789", "salt"),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
func("pass-word", "pepper"),
|
|
|
|
("d68c95a91ed7773aa57c7c044d2309a5bf1da2e7", "pepper"),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2020-11-22 15:12:14 +00:00
|
|
|
func("éèàïô", "ABC+"), ("b639ba5217b89c906019d89d5816b407d8730898", "ABC+")
|
2019-06-29 15:25:44 +00:00
|
|
|
)
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
def test_get_user(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Get existing users
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
|
|
|
user = db.User.get(name=name)
|
2018-03-02 21:51:49 +00:00
|
|
|
self.assertEqual(UserManager.get(user.id), user)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Get with invalid UUID
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(ValueError, UserManager.get, "invalid-uuid")
|
2020-11-10 15:56:49 +00:00
|
|
|
self.assertRaises(TypeError, UserManager.get, 0xFEE1BAD)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Non-existent user
|
2022-12-10 16:12:44 +00:00
|
|
|
self.assertRaises(db.User.DoesNotExist, UserManager.get, uuid.uuid4())
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
def test_add_user(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
self.assertEqual(db.User.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Create duplicate
|
2020-11-10 14:38:24 +00:00
|
|
|
self.assertRaises(ValueError, UserManager.add, "alice", "Alic3", admin=True)
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
def test_delete_user(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Delete invalid UUID
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(ValueError, UserManager.delete, "invalid-uuid")
|
2020-11-10 15:56:49 +00:00
|
|
|
self.assertRaises(TypeError, UserManager.delete, 0xFEE1B4D)
|
2017-12-16 16:42:53 +00:00
|
|
|
self.assertEqual(db.User.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Delete non-existent user
|
2022-12-10 16:12:44 +00:00
|
|
|
self.assertRaises(db.User.DoesNotExist, UserManager.delete, uuid.uuid4())
|
2017-12-16 16:42:53 +00:00
|
|
|
self.assertEqual(db.User.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
|
|
|
# Delete existing users
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
|
|
|
user = db.User.get(name=name)
|
2018-03-02 21:51:49 +00:00
|
|
|
UserManager.delete(user.id)
|
2022-12-10 16:12:44 +00:00
|
|
|
self.assertRaises(db.User.DoesNotExist, db.User.__getitem__, user.id)
|
2017-12-16 16:42:53 +00:00
|
|
|
self.assertEqual(db.User.select().count(), 0)
|
2017-07-13 20:02:03 +00:00
|
|
|
|
2017-12-10 14:11:50 +00:00
|
|
|
def test_delete_by_name(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2017-12-10 14:11:50 +00:00
|
|
|
# Delete existing users
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
2018-03-02 21:51:49 +00:00
|
|
|
UserManager.delete_by_name(name)
|
2022-12-10 16:12:44 +00:00
|
|
|
self.assertFalse(db.User.select().where(db.User.name == name).exists())
|
2017-12-10 14:11:50 +00:00
|
|
|
|
|
|
|
# Delete non-existent user
|
2022-12-10 16:12:44 +00:00
|
|
|
self.assertRaises(db.User.DoesNotExist, UserManager.delete_by_name, "null")
|
2017-12-10 14:11:50 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
def test_try_auth(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Test authentication
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
|
|
|
user = db.User.get(name=name)
|
2018-03-02 21:51:49 +00:00
|
|
|
authed = UserManager.try_auth(name, name.upper())
|
|
|
|
self.assertEqual(authed, user)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Wrong password
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertIsNone(UserManager.try_auth("alice", "bad"))
|
|
|
|
self.assertIsNone(UserManager.try_auth("alice", "alice"))
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Non-existent user
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertIsNone(UserManager.try_auth("null", "null"))
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
def test_change_password(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# With existing users
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
|
|
|
user = db.User.get(name=name)
|
2017-10-28 13:54:41 +00:00
|
|
|
# Good password
|
2019-06-29 15:25:44 +00:00
|
|
|
UserManager.change_password(user.id, name.upper(), "newpass")
|
|
|
|
self.assertEqual(UserManager.try_auth(name, "newpass"), user)
|
2017-10-28 13:54:41 +00:00
|
|
|
# Old password
|
2018-03-02 21:51:49 +00:00
|
|
|
self.assertEqual(UserManager.try_auth(name, name.upper()), None)
|
2017-07-13 20:02:03 +00:00
|
|
|
# Wrong password
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError, UserManager.change_password, user.id, "badpass", "newpass"
|
|
|
|
)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
|
|
|
# Ensure we still got the same number of users
|
2017-12-16 16:42:53 +00:00
|
|
|
self.assertEqual(db.User.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# With invalid UUID
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError,
|
|
|
|
UserManager.change_password,
|
|
|
|
"invalid-uuid",
|
|
|
|
"oldpass",
|
|
|
|
"newpass",
|
|
|
|
)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Non-existent user
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
2022-12-10 16:12:44 +00:00
|
|
|
db.User.DoesNotExist,
|
2019-06-29 15:25:44 +00:00
|
|
|
UserManager.change_password,
|
|
|
|
uuid.uuid4(),
|
|
|
|
"oldpass",
|
|
|
|
"newpass",
|
|
|
|
)
|
2017-07-13 20:02:03 +00:00
|
|
|
|
|
|
|
def test_change_password2(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_data()
|
|
|
|
|
2020-11-10 15:56:49 +00:00
|
|
|
self.assertRaises(TypeError, UserManager.change_password2, uuid.uuid4(), "pass")
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# With existing users
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["alice", "bob", "charlie"]:
|
|
|
|
UserManager.change_password2(name, "newpass")
|
|
|
|
user = db.User.get(name=name)
|
|
|
|
self.assertEqual(UserManager.try_auth(name, "newpass"), user)
|
2018-03-02 21:51:49 +00:00
|
|
|
self.assertEqual(UserManager.try_auth(name, name.upper()), None)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2020-11-10 15:56:49 +00:00
|
|
|
# test passing the user directly
|
|
|
|
UserManager.change_password2(user, "NEWPASS")
|
|
|
|
self.assertEqual(UserManager.try_auth(name, "NEWPASS"), user)
|
|
|
|
|
2017-07-13 20:02:03 +00:00
|
|
|
# Non-existent user
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
2022-12-10 16:12:44 +00:00
|
|
|
db.User.DoesNotExist, UserManager.change_password2, "null", "newpass"
|
2019-06-29 15:25:44 +00:00
|
|
|
)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|