1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-14 14:12:17 +00:00
supysonic/tests/managers/test_manager_user.py

183 lines
6.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2018-03-04 20:49:56 +00:00
# coding: utf-8
2017-07-13 20:02:03 +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
#
2018-03-04 20:49:56 +00:00
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
# 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
2018-01-08 22:13:50 +00:00
from supysonic.py23 import strtype
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
import io
2017-07-13 20:02:03 +00:00
import unittest
import uuid
2017-12-16 16:42:53 +00:00
2017-12-19 22:16:55 +00:00
from pony.orm import db_session, commit
2017-12-16 16:42:53 +00:00
from pony.orm import ObjectNotFound
2017-07-13 20:02:03 +00:00
class UserManagerTestCase(unittest.TestCase):
def setUp(self):
# Create an empty sqlite database in memory
2017-12-19 22:16:55 +00:00
db.init_database('sqlite:', True)
def tearDown(self):
db.release_database()
2017-10-28 13:54:41 +00:00
2017-12-16 16:42:53 +00:00
@db_session
def create_data(self):
2017-07-13 20:02:03 +00:00
# Create some users
self.assertIsInstance(UserManager.add('alice', 'ALICE', 'test@example.com', True), db.User)
self.assertIsInstance(UserManager.add('bob', 'BOB', 'bob@example.com', False), db.User)
self.assertIsInstance(UserManager.add('charlie', 'CHARLIE', 'charlie@example.com', False), db.User)
2017-12-16 16:42:53 +00:00
folder = db.Folder(name = 'Root', path = 'tests/assets', root = True)
artist = db.Artist(name = 'Artist')
album = db.Album(name = 'Album', artist = artist)
track = db.Track(
title = 'Track',
disc = 1,
number = 1,
duration = 1,
artist = artist,
album = album,
path = 'tests/assets/empty',
folder = folder,
root_folder = folder,
content_type = 'audio/mpeg',
bitrate = 320,
last_modification = 0
)
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
self.assertEqual(func('password','salt'), ('59b3e8d637cf97edbe2384cf59cb7453dfe30789', 'salt'))
self.assertEqual(func('pass-word','pepper'), ('d68c95a91ed7773aa57c7c044d2309a5bf1da2e7', 'pepper'))
self.assertEqual(func(u'éèàïô', 'ABC+'), ('b639ba5217b89c906019d89d5816b407d8730898', 'ABC+'))
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
@db_session
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
for name in ['alice', 'bob', 'charlie']:
2017-12-16 16:42:53 +00:00
user = db.User.get(name = name)
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
self.assertRaises(ValueError, UserManager.get, 'invalid-uuid')
self.assertRaises(ValueError, UserManager.get, 0xfee1bad)
2017-10-28 13:54:41 +00:00
2017-07-13 20:02:03 +00:00
# Non-existent user
self.assertRaises(ObjectNotFound, UserManager.get, uuid.uuid4())
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
@db_session
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
self.assertRaises(ValueError, UserManager.add, 'alice', 'Alic3', 'alice@example.com', True)
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
@db_session
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
self.assertRaises(ValueError, UserManager.delete, 'invalid-uuid')
self.assertRaises(ValueError, 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
self.assertRaises(ObjectNotFound, 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
for name in ['alice', 'bob', 'charlie']:
2017-12-16 16:42:53 +00:00
user = db.User.get(name = name)
UserManager.delete(user.id)
2017-12-16 16:42:53 +00:00
self.assertRaises(ObjectNotFound, db.User.__getitem__, user.id)
2017-12-19 22:16:55 +00:00
commit()
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-16 16:42:53 +00:00
@db_session
def test_delete_by_name(self):
2017-12-16 16:42:53 +00:00
self.create_data()
# Delete existing users
for name in ['alice', 'bob', 'charlie']:
UserManager.delete_by_name(name)
2017-12-16 16:42:53 +00:00
self.assertFalse(db.User.exists(name = name))
# Delete non-existent user
self.assertRaises(ObjectNotFound, UserManager.delete_by_name, 'null')
2017-12-16 16:42:53 +00:00
@db_session
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
for name in ['alice', 'bob', 'charlie']:
2017-12-16 16:42:53 +00:00
user = db.User.get(name = name)
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
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
self.assertIsNone(UserManager.try_auth('null', 'null'))
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
@db_session
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
for name in ['alice', 'bob', 'charlie']:
2017-12-16 16:42:53 +00:00
user = db.User.get(name = name)
2017-10-28 13:54:41 +00:00
# Good password
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
self.assertEqual(UserManager.try_auth(name, name.upper()), None)
2017-07-13 20:02:03 +00:00
# Wrong password
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
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
self.assertRaises(ObjectNotFound, UserManager.change_password, uuid.uuid4(), 'oldpass', 'newpass')
2017-07-13 20:02:03 +00:00
2017-12-16 16:42:53 +00:00
@db_session
2017-07-13 20:02:03 +00:00
def test_change_password2(self):
2017-12-16 16:42:53 +00:00
self.create_data()
2017-07-13 20:02:03 +00:00
# With existing users
for name in ['alice', 'bob', 'charlie']:
UserManager.change_password2(name, 'newpass')
2017-12-16 16:42:53 +00:00
user = db.User.get(name = name)
self.assertEqual(UserManager.try_auth(name, 'newpass'), user)
self.assertEqual(UserManager.try_auth(name, name.upper()), None)
2017-10-28 13:54:41 +00:00
2017-07-13 20:02:03 +00:00
# Non-existent user
self.assertRaises(ObjectNotFound, UserManager.change_password2, 'null', 'newpass')
2017-10-28 13:54:41 +00:00
2017-07-13 20:02:03 +00:00
if __name__ == '__main__':
unittest.main()
2017-10-28 13:54:41 +00:00