mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 08:56:17 +00:00
Port supysonic.managers.user.UserManager
This commit is contained in:
parent
ccdd73f8a0
commit
e589247458
@ -28,27 +28,25 @@ class UserManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add(name, password, **kwargs):
|
def add(name, password, **kwargs):
|
||||||
if User.exists(name=name):
|
if User.select().where(User.name == name).exists():
|
||||||
raise ValueError("User '{}' exists".format(name))
|
raise ValueError("User '{}' exists".format(name))
|
||||||
|
|
||||||
crypt, salt = UserManager.__encrypt_password(password)
|
crypt, salt = UserManager.__encrypt_password(password)
|
||||||
return User(name=name, password=crypt, salt=salt, **kwargs)
|
return User.create(name=name, password=crypt, salt=salt, **kwargs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete(uid):
|
def delete(uid):
|
||||||
user = UserManager.get(uid)
|
user = UserManager.get(uid)
|
||||||
user.delete()
|
user.delete_instance()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_by_name(name):
|
def delete_by_name(name):
|
||||||
user = User.get(name=name)
|
user = User.get(name=name)
|
||||||
if user is None:
|
user.delete_instance()
|
||||||
raise ObjectNotFound(User)
|
|
||||||
user.delete()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def try_auth(name, password):
|
def try_auth(name, password):
|
||||||
user = User.get(name=name)
|
user = User.get_or_none(name=name)
|
||||||
if user is None:
|
if user is None:
|
||||||
return None
|
return None
|
||||||
elif UserManager.__encrypt_password(password, user.salt)[0] != user.password:
|
elif UserManager.__encrypt_password(password, user.salt)[0] != user.password:
|
||||||
@ -63,6 +61,7 @@ class UserManager:
|
|||||||
raise ValueError("Wrong password")
|
raise ValueError("Wrong password")
|
||||||
|
|
||||||
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
|
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
|
||||||
|
user.save()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def change_password2(name_or_user, new_pass):
|
def change_password2(name_or_user, new_pass):
|
||||||
@ -70,12 +69,11 @@ class UserManager:
|
|||||||
user = name_or_user
|
user = name_or_user
|
||||||
elif isinstance(name_or_user, str):
|
elif isinstance(name_or_user, str):
|
||||||
user = User.get(name=name_or_user)
|
user = User.get(name=name_or_user)
|
||||||
if user is None:
|
|
||||||
raise ObjectNotFound(User)
|
|
||||||
else:
|
else:
|
||||||
raise TypeError("Requires a User instance or a user name (string)")
|
raise TypeError("Requires a User instance or a user name (string)")
|
||||||
|
|
||||||
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
|
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
|
||||||
|
user.save()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __encrypt_password(password, salt=None):
|
def __encrypt_password(password, salt=None):
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# This file is part of Supysonic.
|
# This file is part of Supysonic.
|
||||||
# Supysonic is a Python implementation of the Subsonic server API.
|
# Supysonic is a Python implementation of the Subsonic server API.
|
||||||
#
|
#
|
||||||
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
|
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
|
||||||
# 2017 Óscar García Amor
|
# 2017 Óscar García Amor
|
||||||
#
|
#
|
||||||
# Distributed under terms of the GNU AGPLv3 license.
|
# Distributed under terms of the GNU AGPLv3 license.
|
||||||
@ -12,9 +12,6 @@ from supysonic.managers.user import UserManager
|
|||||||
import unittest
|
import unittest
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from pony.orm import db_session, commit
|
|
||||||
from pony.orm import ObjectNotFound
|
|
||||||
|
|
||||||
|
|
||||||
class UserManagerTestCase(unittest.TestCase):
|
class UserManagerTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -24,7 +21,6 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
db.release_database()
|
db.release_database()
|
||||||
|
|
||||||
@db_session
|
|
||||||
def create_data(self):
|
def create_data(self):
|
||||||
# Create some users
|
# Create some users
|
||||||
alice = UserManager.add("alice", "ALICE", admin=True)
|
alice = UserManager.add("alice", "ALICE", admin=True)
|
||||||
@ -37,10 +33,10 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertIsInstance(UserManager.add("charlie", "CHARLIE"), db.User)
|
self.assertIsInstance(UserManager.add("charlie", "CHARLIE"), db.User)
|
||||||
|
|
||||||
folder = db.Folder(name="Root", path="tests/assets", root=True)
|
folder = db.Folder.create(name="Root", path="tests/assets", root=True)
|
||||||
artist = db.Artist(name="Artist")
|
artist = db.Artist.create(name="Artist")
|
||||||
album = db.Album(name="Album", artist=artist)
|
album = db.Album.create(name="Album", artist=artist)
|
||||||
track = db.Track(
|
track = db.Track.create(
|
||||||
title="Track",
|
title="Track",
|
||||||
disc=1,
|
disc=1,
|
||||||
number=1,
|
number=1,
|
||||||
@ -71,7 +67,6 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
func("éèàïô", "ABC+"), ("b639ba5217b89c906019d89d5816b407d8730898", "ABC+")
|
func("éèàïô", "ABC+"), ("b639ba5217b89c906019d89d5816b407d8730898", "ABC+")
|
||||||
)
|
)
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_get_user(self):
|
def test_get_user(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
@ -85,9 +80,8 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
self.assertRaises(TypeError, UserManager.get, 0xFEE1BAD)
|
self.assertRaises(TypeError, UserManager.get, 0xFEE1BAD)
|
||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertRaises(ObjectNotFound, UserManager.get, uuid.uuid4())
|
self.assertRaises(db.User.DoesNotExist, UserManager.get, uuid.uuid4())
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_add_user(self):
|
def test_add_user(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
self.assertEqual(db.User.select().count(), 3)
|
self.assertEqual(db.User.select().count(), 3)
|
||||||
@ -95,7 +89,6 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
# Create duplicate
|
# Create duplicate
|
||||||
self.assertRaises(ValueError, UserManager.add, "alice", "Alic3", admin=True)
|
self.assertRaises(ValueError, UserManager.add, "alice", "Alic3", admin=True)
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_delete_user(self):
|
def test_delete_user(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
@ -105,30 +98,27 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(db.User.select().count(), 3)
|
self.assertEqual(db.User.select().count(), 3)
|
||||||
|
|
||||||
# Delete non-existent user
|
# Delete non-existent user
|
||||||
self.assertRaises(ObjectNotFound, UserManager.delete, uuid.uuid4())
|
self.assertRaises(db.User.DoesNotExist, UserManager.delete, uuid.uuid4())
|
||||||
self.assertEqual(db.User.select().count(), 3)
|
self.assertEqual(db.User.select().count(), 3)
|
||||||
|
|
||||||
# Delete existing users
|
# Delete existing users
|
||||||
for name in ["alice", "bob", "charlie"]:
|
for name in ["alice", "bob", "charlie"]:
|
||||||
user = db.User.get(name=name)
|
user = db.User.get(name=name)
|
||||||
UserManager.delete(user.id)
|
UserManager.delete(user.id)
|
||||||
self.assertRaises(ObjectNotFound, db.User.__getitem__, user.id)
|
self.assertRaises(db.User.DoesNotExist, db.User.__getitem__, user.id)
|
||||||
commit()
|
|
||||||
self.assertEqual(db.User.select().count(), 0)
|
self.assertEqual(db.User.select().count(), 0)
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_delete_by_name(self):
|
def test_delete_by_name(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
# Delete existing users
|
# Delete existing users
|
||||||
for name in ["alice", "bob", "charlie"]:
|
for name in ["alice", "bob", "charlie"]:
|
||||||
UserManager.delete_by_name(name)
|
UserManager.delete_by_name(name)
|
||||||
self.assertFalse(db.User.exists(name=name))
|
self.assertFalse(db.User.select().where(db.User.name == name).exists())
|
||||||
|
|
||||||
# Delete non-existent user
|
# Delete non-existent user
|
||||||
self.assertRaises(ObjectNotFound, UserManager.delete_by_name, "null")
|
self.assertRaises(db.User.DoesNotExist, UserManager.delete_by_name, "null")
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_try_auth(self):
|
def test_try_auth(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
@ -145,7 +135,6 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertIsNone(UserManager.try_auth("null", "null"))
|
self.assertIsNone(UserManager.try_auth("null", "null"))
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_change_password(self):
|
def test_change_password(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
@ -176,14 +165,13 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
ObjectNotFound,
|
db.User.DoesNotExist,
|
||||||
UserManager.change_password,
|
UserManager.change_password,
|
||||||
uuid.uuid4(),
|
uuid.uuid4(),
|
||||||
"oldpass",
|
"oldpass",
|
||||||
"newpass",
|
"newpass",
|
||||||
)
|
)
|
||||||
|
|
||||||
@db_session
|
|
||||||
def test_change_password2(self):
|
def test_change_password2(self):
|
||||||
self.create_data()
|
self.create_data()
|
||||||
|
|
||||||
@ -202,7 +190,7 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
ObjectNotFound, UserManager.change_password2, "null", "newpass"
|
db.User.DoesNotExist, UserManager.change_password2, "null", "newpass"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user