From d4b30746626fb0ee9d1b3d0d27c66473bff87f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Garc=C3=ADa=20Amor?= Date: Fri, 14 Jul 2017 15:23:44 +0200 Subject: [PATCH] WIP: Adding unit tests for API User --- tests/__init__.py | 18 +++++++++------- tests/test_api_user.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/test_api_user.py diff --git a/tests/__init__.py b/tests/__init__.py index 7da1e75..384822a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,13 +6,15 @@ # # Distributed under terms of the GNU GPLv3 license. -from .test_folder_manager import FolderManagerTestCase -from .test_user_manager import UserManagerTestCase +from .test_api_user import ApiUserTestCase +#from .test_folder_manager import FolderManagerTestCase +#from .test_user_manager import UserManagerTestCase -import unittest +#import unittest -def suite(): - suite = unittest.TestSuite() - suite.addTest(FolderManagerTestCase()) - suite.addTest(UserManagerTestCase()) - return suite +#def suite(): +# suite = unittest.TestSuite() +# suite.addTest(ApiUserTestCase()) + #suite.addTest(FolderManagerTestCase()) + #suite.addTest(UserManagerTestCase()) +# return suite diff --git a/tests/test_api_user.py b/tests/test_api_user.py new file mode 100644 index 0000000..4b62250 --- /dev/null +++ b/tests/test_api_user.py @@ -0,0 +1,49 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 Óscar García Amor +# +# 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()