mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Improved existing managers tests
This commit is contained in:
parent
033a86381b
commit
955a678df3
@ -14,6 +14,7 @@ from supysonic import db
|
|||||||
from supysonic.managers.folder import FolderManager
|
from supysonic.managers.folder import FolderManager
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import io
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
@ -24,17 +25,19 @@ class FolderManagerTestCase(unittest.TestCase):
|
|||||||
# Create an empty sqlite database in memory
|
# Create an empty sqlite database in memory
|
||||||
self.store = db.get_store("sqlite:")
|
self.store = db.get_store("sqlite:")
|
||||||
# Read schema from file
|
# Read schema from file
|
||||||
with open('schema/sqlite.sql') as sql:
|
with io.open('schema/sqlite.sql', 'r') as sql:
|
||||||
schema = sql.read()
|
schema = sql.read()
|
||||||
# Create tables on memory database
|
# Create tables on memory database
|
||||||
for command in schema.split(';'):
|
for command in schema.split(';'):
|
||||||
self.store.execute(command)
|
self.store.execute(command)
|
||||||
|
|
||||||
# Create some temporary directories
|
# Create some temporary directories
|
||||||
self.media_dir = tempfile.mkdtemp()
|
self.media_dir = tempfile.mkdtemp()
|
||||||
self.music_dir = tempfile.mkdtemp()
|
self.music_dir = tempfile.mkdtemp()
|
||||||
# Add test folders
|
# Add test folders
|
||||||
self.assertEqual(FolderManager.add(self.store, 'media', self.media_dir), FolderManager.SUCCESS)
|
self.assertEqual(FolderManager.add(self.store, 'media', self.media_dir), FolderManager.SUCCESS)
|
||||||
self.assertEqual(FolderManager.add(self.store, 'music', self.music_dir), FolderManager.SUCCESS)
|
self.assertEqual(FolderManager.add(self.store, 'music', self.music_dir), FolderManager.SUCCESS)
|
||||||
|
|
||||||
folder = db.Folder()
|
folder = db.Folder()
|
||||||
folder.root = False
|
folder.root = False
|
||||||
folder.name = 'non-root'
|
folder.name = 'non-root'
|
||||||
@ -51,41 +54,79 @@ class FolderManagerTestCase(unittest.TestCase):
|
|||||||
for name in ['media', 'music']:
|
for name in ['media', 'music']:
|
||||||
folder = self.store.find(db.Folder, db.Folder.name == name, db.Folder.root == True).one()
|
folder = self.store.find(db.Folder, db.Folder.name == name, db.Folder.root == True).one()
|
||||||
self.assertEqual(FolderManager.get(self.store, folder.id), (FolderManager.SUCCESS, folder))
|
self.assertEqual(FolderManager.get(self.store, folder.id), (FolderManager.SUCCESS, folder))
|
||||||
|
|
||||||
# Get with invalid UUID
|
# Get with invalid UUID
|
||||||
self.assertEqual(FolderManager.get(self.store, 'invalid-uuid'), (FolderManager.INVALID_ID, None))
|
self.assertEqual(FolderManager.get(self.store, 'invalid-uuid'), (FolderManager.INVALID_ID, None))
|
||||||
|
self.assertEqual(FolderManager.get(self.store, 0xdeadbeef), (FolderManager.INVALID_ID, None))
|
||||||
|
|
||||||
# Non-existent folder
|
# Non-existent folder
|
||||||
self.assertEqual(FolderManager.get(self.store, uuid.uuid4()), (FolderManager.NO_SUCH_FOLDER, None))
|
self.assertEqual(FolderManager.get(self.store, uuid.uuid4()), (FolderManager.NO_SUCH_FOLDER, None))
|
||||||
|
|
||||||
def test_add_folder(self):
|
def test_add_folder(self):
|
||||||
|
# Added in setUp()
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 3)
|
||||||
|
|
||||||
# Create duplicate
|
# Create duplicate
|
||||||
self.assertEqual(FolderManager.add(self.store,'media', self.media_dir), FolderManager.NAME_EXISTS)
|
self.assertEqual(FolderManager.add(self.store,'media', self.media_dir), FolderManager.NAME_EXISTS)
|
||||||
|
self.assertEqual(self.store.find(db.Folder, db.Folder.name == 'media').count(), 1)
|
||||||
|
|
||||||
# Duplicate path
|
# Duplicate path
|
||||||
self.assertEqual(FolderManager.add(self.store,'new-folder', self.media_dir), FolderManager.PATH_EXISTS)
|
self.assertEqual(FolderManager.add(self.store,'new-folder', self.media_dir), FolderManager.PATH_EXISTS)
|
||||||
|
self.assertEqual(self.store.find(db.Folder, db.Folder.path == self.media_dir).count(), 1)
|
||||||
|
|
||||||
# Invalid path
|
# Invalid path
|
||||||
self.assertEqual(FolderManager.add(self.store,'invalid-path', os.path.abspath('/this/not/is/valid')), FolderManager.INVALID_PATH)
|
path = os.path.abspath('/this/not/is/valid')
|
||||||
|
self.assertEqual(FolderManager.add(self.store,'invalid-path', path), FolderManager.INVALID_PATH)
|
||||||
|
self.assertEqual(self.store.find(db.Folder, db.Folder.path == path).count(), 0)
|
||||||
|
|
||||||
# Subfolder of already added path
|
# Subfolder of already added path
|
||||||
os.mkdir(os.path.join(self.media_dir, 'subfolder'))
|
path = os.path.join(self.media_dir, 'subfolder')
|
||||||
self.assertEqual(FolderManager.add(self.store,'subfolder', os.path.join(self.media_dir, 'subfolder')), FolderManager.PATH_EXISTS)
|
os.mkdir(path)
|
||||||
|
self.assertEqual(FolderManager.add(self.store,'subfolder', path), FolderManager.PATH_EXISTS)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 3)
|
||||||
|
|
||||||
|
# Parent folder of an already added path
|
||||||
|
path = os.path.join(self.media_dir, '..')
|
||||||
|
self.assertEqual(FolderManager.add(self.store, 'parent', path), FolderManager.SUBPATH_EXISTS)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 3)
|
||||||
|
|
||||||
def test_delete_folder(self):
|
def test_delete_folder(self):
|
||||||
# Delete existing folders
|
# Delete existing folders
|
||||||
for name in ['media', 'music']:
|
for name in ['media', 'music']:
|
||||||
folder = self.store.find(db.Folder, db.Folder.name == name, db.Folder.root == True).one()
|
folder = self.store.find(db.Folder, db.Folder.name == name, db.Folder.root == True).one()
|
||||||
self.assertEqual(FolderManager.delete(self.store, folder.id), FolderManager.SUCCESS)
|
self.assertEqual(FolderManager.delete(self.store, folder.id), FolderManager.SUCCESS)
|
||||||
|
self.assertIsNone(self.store.get(db.Folder, folder.id))
|
||||||
|
|
||||||
# Delete invalid UUID
|
# Delete invalid UUID
|
||||||
self.assertEqual(FolderManager.delete(self.store, 'invalid-uuid'), FolderManager.INVALID_ID)
|
self.assertEqual(FolderManager.delete(self.store, 'invalid-uuid'), FolderManager.INVALID_ID)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 1) # 'non-root' remaining
|
||||||
|
|
||||||
# Delete non-existent folder
|
# Delete non-existent folder
|
||||||
self.assertEqual(FolderManager.delete(self.store, uuid.uuid4()), FolderManager.NO_SUCH_FOLDER)
|
self.assertEqual(FolderManager.delete(self.store, uuid.uuid4()), FolderManager.NO_SUCH_FOLDER)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 1) # 'non-root' remaining
|
||||||
|
|
||||||
# Delete non-root folder
|
# Delete non-root folder
|
||||||
folder = self.store.find(db.Folder, db.Folder.name == 'non-root').one()
|
folder = self.store.find(db.Folder, db.Folder.name == 'non-root').one()
|
||||||
self.assertEqual(FolderManager.delete(self.store, folder.id), FolderManager.NO_SUCH_FOLDER)
|
self.assertEqual(FolderManager.delete(self.store, folder.id), FolderManager.NO_SUCH_FOLDER)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 1) # 'non-root' remaining
|
||||||
|
|
||||||
def test_delete_by_name(self):
|
def test_delete_by_name(self):
|
||||||
# Delete existing folders
|
# Delete existing folders
|
||||||
for name in ['media', 'music']:
|
for name in ['media', 'music']:
|
||||||
self.assertEqual(FolderManager.delete_by_name(self.store, name), FolderManager.SUCCESS)
|
self.assertEqual(FolderManager.delete_by_name(self.store, name), FolderManager.SUCCESS)
|
||||||
|
self.assertEqual(self.store.find(db.Folder, db.Folder.name == name).count(), 0)
|
||||||
|
|
||||||
# Delete non-existent folder
|
# Delete non-existent folder
|
||||||
self.assertEqual(FolderManager.delete_by_name(self.store, 'null'), FolderManager.NO_SUCH_FOLDER)
|
self.assertEqual(FolderManager.delete_by_name(self.store, 'null'), FolderManager.NO_SUCH_FOLDER)
|
||||||
|
self.assertEqual(self.store.find(db.Folder).count(), 1) # 'non-root' remaining
|
||||||
|
|
||||||
|
def test_human_readable_error(self):
|
||||||
|
values = [ FolderManager.SUCCESS, FolderManager.INVALID_ID, FolderManager.NAME_EXISTS,
|
||||||
|
FolderManager.INVALID_PATH, FolderManager.PATH_EXISTS, FolderManager.NO_SUCH_FOLDER,
|
||||||
|
FolderManager.SUBPATH_EXISTS, 1594826, 'string', uuid.uuid4() ]
|
||||||
|
for value in values:
|
||||||
|
self.assertIsInstance(FolderManager.error_str(value), basestring)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
@ -15,57 +15,77 @@ from supysonic.managers.user import UserManager
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import uuid
|
import uuid
|
||||||
|
import io
|
||||||
|
|
||||||
class UserManagerTestCase(unittest.TestCase):
|
class UserManagerTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Create an empty sqlite database in memory
|
# Create an empty sqlite database in memory
|
||||||
self.store = db.get_store("sqlite:")
|
self.store = db.get_store("sqlite:")
|
||||||
# Read schema from file
|
# Read schema from file
|
||||||
with open('schema/sqlite.sql') as sql:
|
with io.open('schema/sqlite.sql', 'r') as sql:
|
||||||
schema = sql.read()
|
schema = sql.read()
|
||||||
# Create tables on memory database
|
# Create tables on memory database
|
||||||
for command in schema.split(';'):
|
for command in schema.split(';'):
|
||||||
self.store.execute(command)
|
self.store.execute(command)
|
||||||
|
|
||||||
# Create some users
|
# Create some users
|
||||||
self.assertEqual(UserManager.add(self.store, 'alice', 'alice', 'test@example.com', True), UserManager.SUCCESS)
|
self.assertEqual(UserManager.add(self.store, 'alice', 'ALICE', 'test@example.com', True), UserManager.SUCCESS)
|
||||||
self.assertEqual(UserManager.add(self.store, 'bob', 'bob', 'bob@example.com', False), UserManager.SUCCESS)
|
self.assertEqual(UserManager.add(self.store, 'bob', 'BOB', 'bob@example.com', False), UserManager.SUCCESS)
|
||||||
self.assertEqual(UserManager.add(self.store, 'charlie', 'charlie', 'charlie@example.com', False), UserManager.SUCCESS)
|
self.assertEqual(UserManager.add(self.store, 'charlie', 'CHARLIE', 'charlie@example.com', False), UserManager.SUCCESS)
|
||||||
|
|
||||||
def test_encrypt_password(self):
|
def test_encrypt_password(self):
|
||||||
self.assertEqual(UserManager._UserManager__encrypt_password('password','salt'), ('59b3e8d637cf97edbe2384cf59cb7453dfe30789', 'salt'))
|
func = UserManager._UserManager__encrypt_password
|
||||||
self.assertEqual(UserManager._UserManager__encrypt_password('pass-word','pepper'), ('d68c95a91ed7773aa57c7c044d2309a5bf1da2e7', 'pepper'))
|
self.assertEqual(func(u'password',u'salt'), (u'59b3e8d637cf97edbe2384cf59cb7453dfe30789', u'salt'))
|
||||||
|
self.assertEqual(func(u'pass-word',u'pepper'), (u'd68c95a91ed7773aa57c7c044d2309a5bf1da2e7', u'pepper'))
|
||||||
|
self.assertEqual(func(u'éèàïô', u'ABC+'), (u'b639ba5217b89c906019d89d5816b407d8730898', u'ABC+'))
|
||||||
|
|
||||||
def test_get_user(self):
|
def test_get_user(self):
|
||||||
# Get existing users
|
# Get existing users
|
||||||
for name in ['alice', 'bob', 'charlie']:
|
for name in ['alice', 'bob', 'charlie']:
|
||||||
user = self.store.find(db.User, db.User.name == name).one()
|
user = self.store.find(db.User, db.User.name == name).one()
|
||||||
self.assertEqual(UserManager.get(self.store, user.id), (UserManager.SUCCESS, user))
|
self.assertEqual(UserManager.get(self.store, user.id), (UserManager.SUCCESS, user))
|
||||||
|
|
||||||
# Get with invalid UUID
|
# Get with invalid UUID
|
||||||
self.assertEqual(UserManager.get(self.store, 'invalid-uuid'), (UserManager.INVALID_ID, None))
|
self.assertEqual(UserManager.get(self.store, 'invalid-uuid'), (UserManager.INVALID_ID, None))
|
||||||
|
self.assertEqual(UserManager.get(self.store, 0xfee1bad), (UserManager.INVALID_ID, None))
|
||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertEqual(UserManager.get(self.store, uuid.uuid4()), (UserManager.NO_SUCH_USER, None))
|
self.assertEqual(UserManager.get(self.store, uuid.uuid4()), (UserManager.NO_SUCH_USER, None))
|
||||||
|
|
||||||
def test_add_user(self):
|
def test_add_user(self):
|
||||||
|
# Added in setUp()
|
||||||
|
self.assertEqual(self.store.find(db.User).count(), 3)
|
||||||
|
|
||||||
# Create duplicate
|
# Create duplicate
|
||||||
self.assertEqual(UserManager.add(self.store, 'alice', 'alice', 'test@example.com', True), UserManager.NAME_EXISTS)
|
self.assertEqual(UserManager.add(self.store, 'alice', 'Alic3', 'alice@example.com', True), UserManager.NAME_EXISTS)
|
||||||
|
|
||||||
def test_delete_user(self):
|
def test_delete_user(self):
|
||||||
|
# Delete invalid UUID
|
||||||
|
self.assertEqual(UserManager.delete(self.store, 'invalid-uuid'), UserManager.INVALID_ID)
|
||||||
|
self.assertEqual(UserManager.delete(self.store, 0xfee1b4d), UserManager.INVALID_ID)
|
||||||
|
self.assertEqual(self.store.find(db.User).count(), 3)
|
||||||
|
|
||||||
|
# Delete non-existent user
|
||||||
|
self.assertEqual(UserManager.delete(self.store, uuid.uuid4()), UserManager.NO_SUCH_USER)
|
||||||
|
self.assertEqual(self.store.find(db.User).count(), 3)
|
||||||
|
|
||||||
# Delete existing users
|
# Delete existing users
|
||||||
for name in ['alice', 'bob', 'charlie']:
|
for name in ['alice', 'bob', 'charlie']:
|
||||||
user = self.store.find(db.User, db.User.name == name).one()
|
user = self.store.find(db.User, db.User.name == name).one()
|
||||||
self.assertEqual(UserManager.delete(self.store, user.id), UserManager.SUCCESS)
|
self.assertEqual(UserManager.delete(self.store, user.id), UserManager.SUCCESS)
|
||||||
# Delete invalid UUID
|
self.assertIsNone(self.store.get(db.User, user.id))
|
||||||
self.assertEqual(UserManager.delete(self.store, 'invalid-uuid'), UserManager.INVALID_ID)
|
self.assertEqual(self.store.find(db.User).count(), 0)
|
||||||
# Delete non-existent user
|
|
||||||
self.assertEqual(UserManager.delete(self.store, uuid.uuid4()), UserManager.NO_SUCH_USER)
|
|
||||||
|
|
||||||
def test_try_auth(self):
|
def test_try_auth(self):
|
||||||
# Test authentication
|
# Test authentication
|
||||||
for name in ['alice', 'bob', 'charlie']:
|
for name in ['alice', 'bob', 'charlie']:
|
||||||
user = self.store.find(db.User, db.User.name == name).one()
|
user = self.store.find(db.User, db.User.name == name).one()
|
||||||
self.assertEqual(UserManager.try_auth(self.store, name, name), (UserManager.SUCCESS, user))
|
self.assertEqual(UserManager.try_auth(self.store, name, name.upper()), (UserManager.SUCCESS, user))
|
||||||
|
|
||||||
# Wrong password
|
# Wrong password
|
||||||
self.assertEqual(UserManager.try_auth(self.store, name, 'bad'), (UserManager.WRONG_PASS, None))
|
self.assertEqual(UserManager.try_auth(self.store, 'alice', 'bad'), (UserManager.WRONG_PASS, None))
|
||||||
|
self.assertEqual(UserManager.try_auth(self.store, 'alice', 'alice'), (UserManager.WRONG_PASS, None))
|
||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertEqual(UserManager.try_auth(self.store, 'null', 'null'), (UserManager.NO_SUCH_USER, None))
|
self.assertEqual(UserManager.try_auth(self.store, 'null', 'null'), (UserManager.NO_SUCH_USER, None))
|
||||||
|
|
||||||
@ -73,13 +93,20 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
# With existing users
|
# With existing users
|
||||||
for name in ['alice', 'bob', 'charlie']:
|
for name in ['alice', 'bob', 'charlie']:
|
||||||
user = self.store.find(db.User, db.User.name == name).one()
|
user = self.store.find(db.User, db.User.name == name).one()
|
||||||
# God password
|
# Good password
|
||||||
self.assertEqual(UserManager.change_password(self.store, user.id, name, 'newpass'), UserManager.SUCCESS)
|
self.assertEqual(UserManager.change_password(self.store, user.id, name.upper(), 'newpass'), UserManager.SUCCESS)
|
||||||
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
||||||
|
# Old password
|
||||||
|
self.assertEqual(UserManager.try_auth(self.store, name, name.upper()), (UserManager.WRONG_PASS, None))
|
||||||
# Wrong password
|
# Wrong password
|
||||||
self.assertEqual(UserManager.change_password(self.store, user.id, 'badpass', 'newpass'), UserManager.WRONG_PASS)
|
self.assertEqual(UserManager.change_password(self.store, user.id, 'badpass', 'newpass'), UserManager.WRONG_PASS)
|
||||||
|
|
||||||
|
# Ensure we still got the same number of users
|
||||||
|
self.assertEqual(self.store.find(db.User).count(), 3)
|
||||||
|
|
||||||
# With invalid UUID
|
# With invalid UUID
|
||||||
self.assertEqual(UserManager.change_password(self.store, 'invalid-uuid', 'oldpass', 'newpass'), UserManager.INVALID_ID)
|
self.assertEqual(UserManager.change_password(self.store, 'invalid-uuid', 'oldpass', 'newpass'), UserManager.INVALID_ID)
|
||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertEqual(UserManager.change_password(self.store, uuid.uuid4(), 'oldpass', 'newpass'), UserManager.NO_SUCH_USER)
|
self.assertEqual(UserManager.change_password(self.store, uuid.uuid4(), 'oldpass', 'newpass'), UserManager.NO_SUCH_USER)
|
||||||
|
|
||||||
@ -89,8 +116,17 @@ class UserManagerTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(UserManager.change_password2(self.store, name, 'newpass'), UserManager.SUCCESS)
|
self.assertEqual(UserManager.change_password2(self.store, name, 'newpass'), UserManager.SUCCESS)
|
||||||
user = self.store.find(db.User, db.User.name == name).one()
|
user = self.store.find(db.User, db.User.name == name).one()
|
||||||
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
||||||
|
self.assertEqual(UserManager.try_auth(self.store, name, name.upper()), (UserManager.WRONG_PASS, None))
|
||||||
|
|
||||||
# Non-existent user
|
# Non-existent user
|
||||||
self.assertEqual(UserManager.change_password2(self.store, 'null', 'newpass'), UserManager.NO_SUCH_USER)
|
self.assertEqual(UserManager.change_password2(self.store, 'null', 'newpass'), UserManager.NO_SUCH_USER)
|
||||||
|
|
||||||
|
def test_human_readable_error(self):
|
||||||
|
values = [ UserManager.SUCCESS, UserManager.INVALID_ID, UserManager.NO_SUCH_USER, UserManager.NAME_EXISTS,
|
||||||
|
UserManager.WRONG_PASS, 1594826, 'string', uuid.uuid4() ]
|
||||||
|
for value in values:
|
||||||
|
self.assertIsInstance(UserManager.error_str(value), basestring)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user