mirror of
https://github.com/spl0k/supysonic.git
synced 2024-11-09 19:52:16 +00:00
WIP: Added unit tests for API and frontend
This commit is contained in:
parent
a7ec45f2fd
commit
f8254532ed
@ -6,15 +6,7 @@
|
||||
#
|
||||
# Distributed under terms of the GNU GPLv3 license.
|
||||
|
||||
from .test_api_user import ApiUserTestCase
|
||||
#from .test_folder_manager import FolderManagerTestCase
|
||||
#from .test_user_manager import UserManagerTestCase
|
||||
|
||||
#import unittest
|
||||
|
||||
#def suite():
|
||||
# suite = unittest.TestSuite()
|
||||
# suite.addTest(ApiUserTestCase())
|
||||
#suite.addTest(FolderManagerTestCase())
|
||||
#suite.addTest(UserManagerTestCase())
|
||||
# return suite
|
||||
from .test_manager_folder import FolderManagerTestCase
|
||||
from .test_manager_user import UserManagerTestCase
|
||||
from .test_api import ApiTestCase
|
||||
from .test_frontend import FrontendTestCase
|
||||
|
@ -35,33 +35,235 @@ class web():
|
||||
sys.modules['supysonic.web'] = web()
|
||||
|
||||
# Import module and set app in test mode
|
||||
#from supysonic.api import user
|
||||
import supysonic.api
|
||||
|
||||
class ApiTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app.test_client()
|
||||
|
||||
def test_user_info(self):
|
||||
# Missing username
|
||||
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')
|
||||
assert 'message="Missing username"' in rv.data
|
||||
# Non-admin request for other user
|
||||
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')
|
||||
assert 'message="Admin restricted"' in rv.data
|
||||
# Non-existent user
|
||||
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')
|
||||
assert 'message="Unknown user"' in rv.data
|
||||
# Admin request
|
||||
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')
|
||||
assert 'adminRole="true"' in rv.data
|
||||
# Non-admin request
|
||||
rv = self.app.get('/rest/getUser.view?u=bob&p=bob&c=test&username=bob')
|
||||
assert 'adminRole="false"' in rv.data
|
||||
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')
|
||||
print rv.data
|
||||
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()
|
||||
|
@ -1,49 +0,0 @@
|
||||
#! /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
|
||||
|
||||
class ApiUserTestCase(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)
|
||||
# Create a mockup of web
|
||||
from flask import Flask
|
||||
self.app = Flask(__name__)
|
||||
class web():
|
||||
app = self.app
|
||||
store = self.store
|
||||
sys.modules['supysonic.web'] = web()
|
||||
# Import module and set app in test mode
|
||||
from supysonic.api.user import user_info
|
||||
self.app.testing = True
|
||||
self.app = self.app.test_client()
|
||||
|
||||
def test_user_info(self):
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test')
|
||||
assert 'message="Missing username"' in rv.data
|
||||
rv = self.app.get('/rest/getUser.view?u=alice&p=alice&c=test&username=alice')
|
||||
assert 'adminRole="true"' in 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()
|
Loading…
Reference in New Issue
Block a user