mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 17:06:17 +00:00
Merge remote-tracking branch 'origin/tests' into config
This commit is contained in:
commit
32e3cce954
5
.travis.yml
Normal file
5
.travis.yml
Normal file
@ -0,0 +1,5 @@
|
||||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
install: "pip install -r requirements.txt"
|
||||
script: "python -m unittest -v tests"
|
12
tests/__init__.py
Normal file
12
tests/__init__.py
Normal file
@ -0,0 +1,12 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 Óscar García Amor <ogarcia@connectical.com>
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from .test_manager_folder import FolderManagerTestCase
|
||||
from .test_manager_user import UserManagerTestCase
|
||||
from .test_api import ApiTestCase
|
||||
from .test_frontend import FrontendTestCase
|
269
tests/test_api.py
Normal file
269
tests/test_api.py
Normal file
@ -0,0 +1,269 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 Óscar García Amor <ogarcia@connectical.com>
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from supysonic import db
|
||||
from supysonic.managers.user import UserManager
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
# Create an empty sqlite database in memory
|
||||
store = db.get_store("sqlite:")
|
||||
# Read schema from file
|
||||
with open('schema/sqlite.sql') as sql:
|
||||
schema = sql.read()
|
||||
# Create tables on memory database
|
||||
for command in schema.split(';'):
|
||||
store.execute(command)
|
||||
# Create some users
|
||||
UserManager.add(store, 'alice', 'alice', 'test@example.com', True)
|
||||
UserManager.add(store, 'bob', 'bob', 'bob@example.com', False)
|
||||
UserManager.add(store, 'charlie', 'charlie', 'charlie@example.com', False)
|
||||
|
||||
# Create a mockup of web
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
class web():
|
||||
app = app
|
||||
store = store
|
||||
sys.modules['supysonic.web'] = web()
|
||||
|
||||
# Import module and set app in test mode
|
||||
import supysonic.api
|
||||
|
||||
class ApiTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app.test_client()
|
||||
|
||||
def test_ping(self):
|
||||
# GET non-existent user
|
||||
rv = self.app.get('/rest/ping.view?u=null&p=null&c=test')
|
||||
self.assertIn('status="failed"', rv.data)
|
||||
self.assertIn('message="Unauthorized"', rv.data)
|
||||
# POST non-existent user
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='null', p='null', c='test'))
|
||||
self.assertIn('status="failed"', rv.data)
|
||||
self.assertIn('message="Unauthorized"', rv.data)
|
||||
# GET user request
|
||||
rv = self.app.get('/rest/ping.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# POST user request
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# GET user request with bad password
|
||||
rv = self.app.get('/rest/ping.view?u=alice&p=bad&c=test')
|
||||
self.assertIn('status="failed"', rv.data)
|
||||
self.assertIn('message="Unauthorized"', rv.data)
|
||||
# POST user request with bad password
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='alice', p='bad', c='test'))
|
||||
self.assertIn('status="failed"', rv.data)
|
||||
self.assertIn('message="Unauthorized"', rv.data)
|
||||
|
||||
def test_ping_in_jsonp(self):
|
||||
# If ping in jsonp works all other endpoints must work OK
|
||||
# GET non-existent user
|
||||
rv = self.app.get('/rest/ping.view?u=null&p=null&c=test&f=jsonp&callback=test')
|
||||
self.assertIn('"status": "failed"', rv.data)
|
||||
self.assertIn('"message": "Unauthorized"', rv.data)
|
||||
# POST non-existent user
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='null', p='null', c='test', f='jsonp', callback='test'))
|
||||
self.assertIn('"status": "failed"', rv.data)
|
||||
self.assertIn('"message": "Unauthorized"', rv.data)
|
||||
# GET user request
|
||||
rv = self.app.get('/rest/ping.view?u=alice&p=alice&c=test&f=jsonp&callback=test')
|
||||
self.assertIn('"status": "ok"', rv.data)
|
||||
# POST user request
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='alice', p='alice', c='test', f='jsonp', callback='test'))
|
||||
self.assertIn('"status": "ok"', rv.data)
|
||||
# GET user request with bad password
|
||||
rv = self.app.get('/rest/ping.view?u=alice&p=bad&c=test&f=jsonp&callback=test')
|
||||
self.assertIn('"status": "failed"', rv.data)
|
||||
self.assertIn('"message": "Unauthorized"', rv.data)
|
||||
# POST user request with bad password
|
||||
rv = self.app.post('/rest/ping.view', data=dict(u='alice', p='bad', c='test', f='jsonp', callback='test'))
|
||||
self.assertIn('"status": "failed"', rv.data)
|
||||
self.assertIn('"message": "Unauthorized"', rv.data)
|
||||
|
||||
def test_not_implemented(self):
|
||||
# Access to not implemented endpoint
|
||||
rv = self.app.get('/rest/not-implemented?u=alice&p=alice&c=test')
|
||||
self.assertIn('message="Not implemented"', rv.data)
|
||||
rv = self.app.post('/rest/not-implemented', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('message="Not implemented"', rv.data)
|
||||
|
||||
def test_get_license(self):
|
||||
# GET user request
|
||||
rv = self.app.get('/rest/getLicense.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
self.assertIn('license valid="true"', rv.data)
|
||||
# POST user request
|
||||
rv = self.app.post('/rest/getLicense.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
self.assertIn('license valid="true"', rv.data)
|
||||
|
||||
def test_get_user(self):
|
||||
# GET missing username
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('message="Missing username"', rv.data)
|
||||
# POST missing username
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('message="Missing username"', rv.data)
|
||||
# GET non-admin request for other user
|
||||
rv = self.app.get('/rest/getUser.view?u=bob&p=bob&c=test&username=alice')
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# POST non-admin request for other user
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='bob', p='bob', c='test', username='alice'))
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# GET non-existent user
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test&username=null')
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# POST non-existent user
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='alice', p='alice', c='test', username='null'))
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# GET admin request
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test&username=alice')
|
||||
self.assertIn('adminRole="true"', rv.data)
|
||||
self.assertIn('username="alice"', rv.data)
|
||||
# POST admin request
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='alice', p='alice', c='test', username='alice'))
|
||||
self.assertIn('adminRole="true"', rv.data)
|
||||
self.assertIn('username="alice"', rv.data)
|
||||
# GET admin request for other user
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test&username=bob')
|
||||
self.assertIn('username="bob"', rv.data)
|
||||
self.assertIn('adminRole="false"', rv.data)
|
||||
# POST admin request for other user
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='alice', p='alice', c='test', username='bob'))
|
||||
self.assertIn('username="bob"', rv.data)
|
||||
self.assertIn('adminRole="false"', rv.data)
|
||||
# GET non-admin request
|
||||
rv = self.app.get('/rest/getUser.view?u=charlie&p=charlie&c=test&username=charlie')
|
||||
self.assertIn('username="charlie"', rv.data)
|
||||
self.assertIn('adminRole="false"', rv.data)
|
||||
# POST non-admin request
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='charlie', p='charlie', c='test', username='charlie'))
|
||||
self.assertIn('username="charlie"', rv.data)
|
||||
self.assertIn('adminRole="false"', rv.data)
|
||||
|
||||
def test_get_users(self):
|
||||
# GET admin request
|
||||
rv = self.app.get('/rest/getUsers.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('alice', rv.data)
|
||||
self.assertIn('bob', rv.data)
|
||||
self.assertIn('charlie', rv.data)
|
||||
# POST admin request
|
||||
rv = self.app.post('/rest/getUsers.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('alice', rv.data)
|
||||
self.assertIn('bob', rv.data)
|
||||
self.assertIn('charlie', rv.data)
|
||||
# GET non-admin request
|
||||
rv = self.app.get('/rest/getUsers.view?u=bob&p=bob&c=test')
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# POST non-admin request
|
||||
rv = self.app.post('/rest/getUsers.view', data=dict(u='bob', p='bob', c='test'))
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
|
||||
def test_create_user(self):
|
||||
# GET non-admin request
|
||||
rv = self.app.get('/rest/createUser.view?u=bob&p=bob&c=test')
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# POST non-admin request
|
||||
rv = self.app.post('/rest/createUser.view', data=dict(u='bob', p='bob', c='test'))
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# GET incomplete request
|
||||
rv = self.app.get('/rest/createUser.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('message="Missing parameter"', rv.data)
|
||||
# POST incomplete request
|
||||
rv = self.app.post('/rest/createUser.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('message="Missing parameter"', rv.data)
|
||||
# GET create user and test that user is created
|
||||
rv = self.app.get('/rest/createUser.view?u=alice&p=alice&c=test&username=david&password=david&email=david%40example.com&adminRole=True')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
rv = self.app.get('/rest/getUser.view?u=david&p=david&c=test&username=david')
|
||||
self.assertIn('username="david"', rv.data)
|
||||
self.assertIn('email="david@example.com"', rv.data)
|
||||
self.assertIn('adminRole="true"', rv.data)
|
||||
# POST create user and test that user is created
|
||||
rv = self.app.post('/rest/createUser.view', data=dict(u='alice', p='alice', c='test', username='elanor', password='elanor', email='elanor@example.com', adminRole=True))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='elanor', p='elanor', c='test', username='elanor'))
|
||||
self.assertIn('username="elanor"', rv.data)
|
||||
self.assertIn('email="elanor@example.com"', rv.data)
|
||||
self.assertIn('adminRole="true"', rv.data)
|
||||
# GET create duplicate
|
||||
rv = self.app.get('/rest/createUser.view?u=alice&p=alice&c=test&username=david&password=david&email=david%40example.com&adminRole=True')
|
||||
self.assertIn('message="There is already a user with that username"', rv.data)
|
||||
# POST create duplicate
|
||||
rv = self.app.post('/rest/createUser.view', data=dict(u='alice', p='alice', c='test', username='elanor', password='elanor', email='elanor@example.com', adminRole=True))
|
||||
self.assertIn('message="There is already a user with that username"', rv.data)
|
||||
|
||||
def test_delete_user(self):
|
||||
# GET non-admin request
|
||||
rv = self.app.get('/rest/deleteUser.view?u=bob&p=bob&c=test')
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# POST non-admin request
|
||||
rv = self.app.post('/rest/deleteUser.view', data=dict(u='bob', p='bob', c='test'))
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# GET incomplete request
|
||||
rv = self.app.get('/rest/deleteUser.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# POST incomplete request
|
||||
rv = self.app.post('/rest/deleteUser.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# GET delete non-existent user
|
||||
rv = self.app.get('/rest/deleteUser.view?u=alice&p=alice&c=test&username=nonexistent')
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# POST delete non-existent user
|
||||
rv = self.app.post('/rest/deleteUser.view', data=dict(u='alice', p='alice', c='test', username='nonexistent'))
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# GET delete existent user
|
||||
rv = self.app.get('/rest/deleteUser.view?u=alice&p=alice&c=test&username=elanor')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test&username=elanor')
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
# POST delete existent user
|
||||
rv = self.app.post('/rest/deleteUser.view', data=dict(u='alice', p='alice', c='test', username='david'))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
rv = self.app.post('/rest/getUser.view', data=dict(u='alice', p='alice', c='test', username='david'))
|
||||
self.assertIn('message="Unknown user"', rv.data)
|
||||
|
||||
def test_change_password(self):
|
||||
# GET incomplete request
|
||||
rv = self.app.get('/rest/changePassword.view?u=alice&p=alice&c=test')
|
||||
self.assertIn('message="Missing parameter"', rv.data)
|
||||
# POST incomplete request
|
||||
rv = self.app.post('/rest/changePassword.view', data=dict(u='alice', p='alice', c='test'))
|
||||
self.assertIn('message="Missing parameter"', rv.data)
|
||||
# GET non-admin change own password
|
||||
rv = self.app.get('/rest/changePassword.view?u=bob&p=bob&c=test&username=bob&password=newpassword')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# POST non-admin change own password
|
||||
rv = self.app.post('/rest/changePassword.view', data=dict(u='bob', p='newpassword', c='test', username='bob', password='bob'))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# GET non-admin change other user password
|
||||
rv = self.app.get('/rest/changePassword.view?u=bob&p=bob&c=test&username=alice&password=newpassword')
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# POST non-admin change other user password
|
||||
rv = self.app.post('/rest/changePassword.view', data=dict(u='bob', p='bob', c='test', username='alice', password='newpassword'))
|
||||
self.assertIn('message="Admin restricted"', rv.data)
|
||||
# GET admin change other user password
|
||||
rv = self.app.get('/rest/changePassword.view?u=bob&p=bob&c=test&username=bob&password=newpassword')
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# POST admin change other user password
|
||||
rv = self.app.post('/rest/changePassword.view', data=dict(u='bob', p='newpassword', c='test', username='bob', password='bob'))
|
||||
self.assertIn('status="ok"', rv.data)
|
||||
# GET change non-existent user password
|
||||
rv = self.app.get('/rest/changePassword.view?u=alice&p=alice&c=test&username=nonexistent&password=nonexistent')
|
||||
self.assertIn('message="No such user"', rv.data)
|
||||
# POST change non-existent user password
|
||||
rv = self.app.post('/rest/changePassword.view', data=dict(u='alice', p='alice', c='test', username='nonexistent', password='nonexistent'))
|
||||
self.assertIn('message="No such user"', rv.data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
101
tests/test_frontend.py
Normal file
101
tests/test_frontend.py
Normal file
@ -0,0 +1,101 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 Óscar García Amor <ogarcia@connectical.com>
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from supysonic import db
|
||||
from supysonic.managers.user import UserManager
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
# Create an empty sqlite database in memory
|
||||
store = db.get_store("sqlite:")
|
||||
# Read schema from file
|
||||
with open('schema/sqlite.sql') as sql:
|
||||
schema = sql.read()
|
||||
# Create tables on memory database
|
||||
for command in schema.split(';'):
|
||||
store.execute(command)
|
||||
# Create some users
|
||||
UserManager.add(store, 'alice', 'alice', 'test@example.com', True)
|
||||
UserManager.add(store, 'bob', 'bob', 'bob@example.com', False)
|
||||
UserManager.add(store, 'charlie', 'charlie', 'charlie@example.com', False)
|
||||
|
||||
# Create a mockup of web
|
||||
from flask import Flask
|
||||
app = Flask(__name__, template_folder='../supysonic/templates')
|
||||
class web():
|
||||
app = app
|
||||
store = store
|
||||
sys.modules['supysonic.web'] = web()
|
||||
|
||||
# Import module and set app in test mode
|
||||
import supysonic.frontend
|
||||
app.secret_key = 'test-suite'
|
||||
|
||||
class FrontendTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app.test_client()
|
||||
|
||||
def test_unauthorized_request(self):
|
||||
# Unauthorized request
|
||||
rv = self.app.get('/', follow_redirects=True)
|
||||
self.assertIn('Please login', rv.data)
|
||||
|
||||
def test_login_with_bad_data(self):
|
||||
# Login with not blank user or password
|
||||
rv = self.app.post('/user/login', data=dict(name='', password=''), follow_redirects=True)
|
||||
self.assertIn('Missing user name', rv.data)
|
||||
self.assertIn('Missing password', rv.data)
|
||||
# Login with not valid user or password
|
||||
rv = self.app.post('/user/login', data=dict(user='nonexistent', password='nonexistent'), follow_redirects=True)
|
||||
self.assertIn('No such user', rv.data)
|
||||
rv = self.app.post('/user/login', data=dict(user='alice', password='badpassword'), follow_redirects=True)
|
||||
self.assertIn('Wrong password', rv.data)
|
||||
|
||||
def test_login_admin(self):
|
||||
# Login with a valid admin user
|
||||
rv = self.app.post('/user/login', data=dict(user='alice', password='alice'), follow_redirects=True)
|
||||
self.assertIn('Logged in', rv.data)
|
||||
self.assertIn('Users', rv.data)
|
||||
self.assertIn('Folders', rv.data)
|
||||
|
||||
def test_login_non_admin(self):
|
||||
# Login with a valid non-admin user
|
||||
rv = self.app.post('/user/login', data=dict(user='bob', password='bob'), follow_redirects=True)
|
||||
self.assertIn('Logged in', rv.data)
|
||||
# Non-admin user cannot acces to users and folders
|
||||
self.assertNotIn('Users', rv.data)
|
||||
self.assertNotIn('Folders', rv.data)
|
||||
|
||||
def test_root_with_valid_session(self):
|
||||
# Root with valid session
|
||||
with self.app.session_transaction() as sess:
|
||||
sess['userid'] = store.find(db.User, db.User.name == 'alice').one().id
|
||||
sess['username'] = 'alice'
|
||||
rv = self.app.get('/', follow_redirects=True)
|
||||
self.assertIn('alice', rv.data)
|
||||
self.assertIn('Log out', rv.data)
|
||||
self.assertIn('There\'s nothing much to see here.', rv.data)
|
||||
|
||||
def test_root_with_non_valid_session(self):
|
||||
# Root with a no-valid session
|
||||
with self.app.session_transaction() as sess:
|
||||
sess['userid'] = uuid.uuid4()
|
||||
sess['username'] = 'alice'
|
||||
rv = self.app.get('/', follow_redirects=True)
|
||||
self.assertIn('Please login', rv.data)
|
||||
# Root with a no-valid user
|
||||
with self.app.session_transaction() as sess:
|
||||
sess['userid'] = store.find(db.User, db.User.name == 'alice').one().id
|
||||
sess['username'] = 'nonexistent'
|
||||
rv = self.app.get('/', follow_redirects=True)
|
||||
self.assertIn('Please login', rv.data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
87
tests/test_manager_folder.py
Normal file
87
tests/test_manager_folder.py
Normal file
@ -0,0 +1,87 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 Óscar García Amor <ogarcia@connectical.com>
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from supysonic import db
|
||||
from supysonic.managers.folder import FolderManager
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
class FolderManagerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create an empty sqlite database in memory
|
||||
self.store = db.get_store("sqlite:")
|
||||
# Read schema from file
|
||||
with open('schema/sqlite.sql') as sql:
|
||||
schema = sql.read()
|
||||
# Create tables on memory database
|
||||
for command in schema.split(';'):
|
||||
self.store.execute(command)
|
||||
# Create some temporary directories
|
||||
self.media_dir = tempfile.mkdtemp()
|
||||
self.music_dir = tempfile.mkdtemp()
|
||||
# Add test folders
|
||||
self.assertEqual(FolderManager.add(self.store, 'media', self.media_dir), FolderManager.SUCCESS)
|
||||
self.assertEqual(FolderManager.add(self.store, 'music', self.music_dir), FolderManager.SUCCESS)
|
||||
folder = db.Folder()
|
||||
folder.root = False
|
||||
folder.name = 'non-root'
|
||||
folder.path = os.path.join(self.music_dir, 'subfolder')
|
||||
self.store.add(folder)
|
||||
self.store.commit()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.media_dir)
|
||||
shutil.rmtree(self.music_dir)
|
||||
|
||||
def test_get_folder(self):
|
||||
# Get existing folders
|
||||
for name in ['media', 'music']:
|
||||
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))
|
||||
# Get with invalid UUID
|
||||
self.assertEqual(FolderManager.get(self.store, 'invalid-uuid'), (FolderManager.INVALID_ID, None))
|
||||
# Non-existent folder
|
||||
self.assertEqual(FolderManager.get(self.store, uuid.uuid4()), (FolderManager.NO_SUCH_FOLDER, None))
|
||||
|
||||
def test_add_folder(self):
|
||||
# Create duplicate
|
||||
self.assertEqual(FolderManager.add(self.store,'media', self.media_dir), FolderManager.NAME_EXISTS)
|
||||
# Duplicate path
|
||||
self.assertEqual(FolderManager.add(self.store,'new-folder', self.media_dir), FolderManager.PATH_EXISTS)
|
||||
# Invalid path
|
||||
self.assertEqual(FolderManager.add(self.store,'invalid-path', os.path.abspath('/this/not/is/valid')), FolderManager.INVALID_PATH)
|
||||
# Subfolder of already added path
|
||||
os.mkdir(os.path.join(self.media_dir, 'subfolder'))
|
||||
self.assertEqual(FolderManager.add(self.store,'subfolder', os.path.join(self.media_dir, 'subfolder')), FolderManager.PATH_EXISTS)
|
||||
|
||||
def test_delete_folder(self):
|
||||
# Delete existing folders
|
||||
for name in ['media', 'music']:
|
||||
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)
|
||||
# Delete invalid UUID
|
||||
self.assertEqual(FolderManager.delete(self.store, 'invalid-uuid'), FolderManager.INVALID_ID)
|
||||
# Delete non-existent folder
|
||||
self.assertEqual(FolderManager.delete(self.store, uuid.uuid4()), FolderManager.NO_SUCH_FOLDER)
|
||||
# Delete non-root folder
|
||||
folder = self.store.find(db.Folder, db.Folder.name == 'non-root').one()
|
||||
self.assertEqual(FolderManager.delete(self.store, folder.id), FolderManager.NO_SUCH_FOLDER)
|
||||
|
||||
def test_delete_by_name(self):
|
||||
# Delete existing folders
|
||||
for name in ['media', 'music']:
|
||||
self.assertEqual(FolderManager.delete_by_name(self.store, name), FolderManager.SUCCESS)
|
||||
# Delete non-existent folder
|
||||
self.assertEqual(FolderManager.delete_by_name(self.store, 'null'), FolderManager.NO_SUCH_FOLDER)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
92
tests/test_manager_user.py
Normal file
92
tests/test_manager_user.py
Normal file
@ -0,0 +1,92 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 Óscar García Amor <ogarcia@connectical.com>
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from supysonic import db
|
||||
from supysonic.managers.user import UserManager
|
||||
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
class UserManagerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create an empty sqlite database in memory
|
||||
self.store = db.get_store("sqlite:")
|
||||
# Read schema from file
|
||||
with open('schema/sqlite.sql') as sql:
|
||||
schema = sql.read()
|
||||
# Create tables on memory database
|
||||
for command in schema.split(';'):
|
||||
self.store.execute(command)
|
||||
# Create some users
|
||||
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, 'charlie', 'charlie', 'charlie@example.com', False), UserManager.SUCCESS)
|
||||
|
||||
def test_encrypt_password(self):
|
||||
self.assertEqual(UserManager._UserManager__encrypt_password('password','salt'), ('59b3e8d637cf97edbe2384cf59cb7453dfe30789', 'salt'))
|
||||
self.assertEqual(UserManager._UserManager__encrypt_password('pass-word','pepper'), ('d68c95a91ed7773aa57c7c044d2309a5bf1da2e7', 'pepper'))
|
||||
|
||||
def test_get_user(self):
|
||||
# Get existing users
|
||||
for name in ['alice', 'bob', 'charlie']:
|
||||
user = self.store.find(db.User, db.User.name == name).one()
|
||||
self.assertEqual(UserManager.get(self.store, user.id), (UserManager.SUCCESS, user))
|
||||
# Get with invalid UUID
|
||||
self.assertEqual(UserManager.get(self.store, 'invalid-uuid'), (UserManager.INVALID_ID, None))
|
||||
# Non-existent user
|
||||
self.assertEqual(UserManager.get(self.store, uuid.uuid4()), (UserManager.NO_SUCH_USER, None))
|
||||
|
||||
def test_add_user(self):
|
||||
# Create duplicate
|
||||
self.assertEqual(UserManager.add(self.store, 'alice', 'alice', 'test@example.com', True), UserManager.NAME_EXISTS)
|
||||
|
||||
def test_delete_user(self):
|
||||
# Delete existing users
|
||||
for name in ['alice', 'bob', 'charlie']:
|
||||
user = self.store.find(db.User, db.User.name == name).one()
|
||||
self.assertEqual(UserManager.delete(self.store, user.id), UserManager.SUCCESS)
|
||||
# Delete invalid UUID
|
||||
self.assertEqual(UserManager.delete(self.store, 'invalid-uuid'), UserManager.INVALID_ID)
|
||||
# Delete non-existent user
|
||||
self.assertEqual(UserManager.delete(self.store, uuid.uuid4()), UserManager.NO_SUCH_USER)
|
||||
|
||||
def test_try_auth(self):
|
||||
# Test authentication
|
||||
for name in ['alice', 'bob', 'charlie']:
|
||||
user = self.store.find(db.User, db.User.name == name).one()
|
||||
self.assertEqual(UserManager.try_auth(self.store, name, name), (UserManager.SUCCESS, user))
|
||||
# Wrong password
|
||||
self.assertEqual(UserManager.try_auth(self.store, name, 'bad'), (UserManager.WRONG_PASS, None))
|
||||
# Non-existent user
|
||||
self.assertEqual(UserManager.try_auth(self.store, 'null', 'null'), (UserManager.NO_SUCH_USER, None))
|
||||
|
||||
def test_change_password(self):
|
||||
# With existing users
|
||||
for name in ['alice', 'bob', 'charlie']:
|
||||
user = self.store.find(db.User, db.User.name == name).one()
|
||||
# God password
|
||||
self.assertEqual(UserManager.change_password(self.store, user.id, name, 'newpass'), UserManager.SUCCESS)
|
||||
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
||||
# Wrong password
|
||||
self.assertEqual(UserManager.change_password(self.store, user.id, 'badpass', 'newpass'), UserManager.WRONG_PASS)
|
||||
# With invalid UUID
|
||||
self.assertEqual(UserManager.change_password(self.store, 'invalid-uuid', 'oldpass', 'newpass'), UserManager.INVALID_ID)
|
||||
# Non-existent user
|
||||
self.assertEqual(UserManager.change_password(self.store, uuid.uuid4(), 'oldpass', 'newpass'), UserManager.NO_SUCH_USER)
|
||||
|
||||
def test_change_password2(self):
|
||||
# With existing users
|
||||
for name in ['alice', 'bob', 'charlie']:
|
||||
self.assertEqual(UserManager.change_password2(self.store, name, 'newpass'), UserManager.SUCCESS)
|
||||
user = self.store.find(db.User, db.User.name == name).one()
|
||||
self.assertEqual(UserManager.try_auth(self.store, name, 'newpass'), (UserManager.SUCCESS, user))
|
||||
# Non-existent user
|
||||
self.assertEqual(UserManager.change_password2(self.store, 'null', 'newpass'), UserManager.NO_SUCH_USER)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in New Issue
Block a user