def setUp(self): self.temp_dir = tempfile.mkdtemp() self.collection_manager = CollectionManager() self.rest_app = RestApp(self.temp_dir, collection_manager=self.collection_manager) # disable all but critical errors! logging.disable(logging.CRITICAL)
def test_lifecycle(self): with mock.patch('AnkiServer.collection.CollectionManager.collection_wrapper') as CollectionWrapper: wrapper = MagicMock() CollectionWrapper.return_value = wrapper manager = CollectionManager() # check getting a new collection ret = manager.get_collection('path1') CollectionWrapper.assert_called_with(os.path.realpath('path1'), None) self.assertEqual(ret, wrapper) # change the return value, so that it would return a new object new_wrapper = MagicMock() CollectionWrapper.return_value = new_wrapper CollectionWrapper.reset_mock() # get the new wrapper ret = manager.get_collection('path2') CollectionWrapper.assert_called_with(os.path.realpath('path2'), None) self.assertEqual(ret, new_wrapper) # make sure the wrapper and new_wrapper are different self.assertNotEqual(wrapper, new_wrapper) # assert that calling with the first path again, returns the first wrapper ret = manager.get_collection('path1') self.assertEqual(ret, wrapper) manager.shutdown() wrapper.close.assert_called_with() new_wrapper.close.assert_called_with()
class RestAppTest(unittest.TestCase): def setUp(self): self.temp_dir = tempfile.mkdtemp() self.collection_manager = CollectionManager() self.rest_app = RestApp(self.temp_dir, collection_manager=self.collection_manager) # disable all but critical errors! logging.disable(logging.CRITICAL) def tearDown(self): self.collection_manager.shutdown() self.collection_manager = None self.rest_app = None shutil.rmtree(self.temp_dir) def test_list_collections(self): os.mkdir(os.path.join(self.temp_dir, 'test1')) os.mkdir(os.path.join(self.temp_dir, 'test2')) with open(os.path.join(self.temp_dir, 'test1', 'collection.anki2'), 'wt') as fd: fd.write('Testing!') self.assertEqual(self.rest_app.list_collections(), ['test1']) def test_parsePath(self): tests = [ ('collection/user', ('collection', 'index', ['user'])), ('collection/user/handler', ('collection', 'handler', ['user'])), ('collection/user/note/123', ('note', 'index', ['user', '123'])), ('collection/user/note/123/handler', ('note', 'handler', ['user', '123'])), ('collection/user/deck/name', ('deck', 'index', ['user', 'name'])), ('collection/user/deck/name/handler', ('deck', 'handler', ['user', 'name'])), #('collection/user/deck/name/card/123', ('card', 'index', ['user', 'name', '123'])), #('collection/user/deck/name/card/123/handler', ('card', 'handler', ['user', 'name', '123'])), ('collection/user/card/123', ('card', 'index', ['user', '123'])), ('collection/user/card/123/handler', ('card', 'handler', ['user', '123'])), # the leading slash should make no difference! ('/collection/user', ('collection', 'index', ['user'])), ] for path, result in tests: self.assertEqual(self.rest_app._parsePath(path), result) def test_parsePath_not_found(self): tests = [ 'bad', 'bad/oaeu', 'collection', 'collection/user/handler/bad', '', '/', ] for path in tests: self.assertRaises(HTTPNotFound, self.rest_app._parsePath, path) def test_getCollectionPath(self): def fullpath(collection_id): return os.path.normpath(os.path.join(self.temp_dir, collection_id, 'collection.anki2')) # This is simple and straight forward! self.assertEqual(self.rest_app._getCollectionPath('user'), fullpath('user')) # These are dangerous - the user is trying to hack us! dangerous = ['../user', '/etc/passwd', '/tmp/aBaBaB', '/root/.ssh/id_rsa'] for collection_id in dangerous: self.assertRaises(HTTPBadRequest, self.rest_app._getCollectionPath, collection_id) def test_getHandler(self): def handlerOne(): pass def handlerTwo(): pass handlerTwo.hasReturnValue = False self.rest_app.add_handler('collection', 'handlerOne', handlerOne) self.rest_app.add_handler('deck', 'handlerTwo', handlerTwo) (handler, hasReturnValue) = self.rest_app._getHandler('collection', 'handlerOne') self.assertEqual(handler, handlerOne) self.assertEqual(hasReturnValue, True) (handler, hasReturnValue) = self.rest_app._getHandler('deck', 'handlerTwo') self.assertEqual(handler, handlerTwo) self.assertEqual(hasReturnValue, False) # try some bad handler names and types self.assertRaises(HTTPNotFound, self.rest_app._getHandler, 'collection', 'nonExistantHandler') self.assertRaises(HTTPNotFound, self.rest_app._getHandler, 'nonExistantType', 'handlerOne') def test_parseRequestBody(self): req = MagicMock() req.body = '{"key":"value"}' data = self.rest_app._parseRequestBody(req) self.assertEqual(data, {'key': 'value'}) self.assertEqual(data.keys(), ['key']) self.assertEqual(type(data.keys()[0]), str) # test some bad data req.body = '{aaaaaaa}' self.assertRaises(HTTPBadRequest, self.rest_app._parseRequestBody, req)
class RestAppTest(unittest.TestCase): def setUp(self): self.temp_dir = tempfile.mkdtemp() self.collection_manager = CollectionManager() self.rest_app = RestApp(self.temp_dir, collection_manager=self.collection_manager) # disable all but critical errors! logging.disable(logging.CRITICAL) def tearDown(self): self.collection_manager.shutdown() self.collection_manager = None self.rest_app = None shutil.rmtree(self.temp_dir) def test_list_collections(self): os.mkdir(os.path.join(self.temp_dir, 'test1')) os.mkdir(os.path.join(self.temp_dir, 'test2')) with open(os.path.join(self.temp_dir, 'test1', 'collection.anki2'), 'wt') as fd: fd.write('Testing!') self.assertEqual(self.rest_app.list_collections(), ['test1']) def test_parsePath(self): tests = [ ('collection/user', ('collection', 'index', ['user'])), ('collection/user/handler', ('collection', 'handler', ['user'])), ('collection/user/note/123', ('note', 'index', ['user', '123'])), ('collection/user/note/123/handler', ('note', 'handler', ['user', '123'])), ('collection/user/deck/name', ('deck', 'index', ['user', 'name'])), ('collection/user/deck/name/handler', ('deck', 'handler', ['user', 'name'])), #('collection/user/deck/name/card/123', ('card', 'index', ['user', 'name', '123'])), #('collection/user/deck/name/card/123/handler', ('card', 'handler', ['user', 'name', '123'])), ('collection/user/card/123', ('card', 'index', ['user', '123'])), ('collection/user/card/123/handler', ('card', 'handler', ['user', '123'])), # the leading slash should make no difference! ('/collection/user', ('collection', 'index', ['user'])), ] for path, result in tests: self.assertEqual(self.rest_app._parsePath(path), result) def test_parsePath_not_found(self): tests = [ 'bad', 'bad/oaeu', 'collection', 'collection/user/handler/bad', '', '/', ] for path in tests: self.assertRaises(HTTPNotFound, self.rest_app._parsePath, path) def test_getCollectionPath(self): def fullpath(collection_id): return os.path.normpath( os.path.join(self.temp_dir, collection_id, 'collection.anki2')) # This is simple and straight forward! self.assertEqual(self.rest_app._getCollectionPath('user'), fullpath('user')) # These are dangerous - the user is trying to hack us! dangerous = [ '../user', '/etc/passwd', '/tmp/aBaBaB', '/root/.ssh/id_rsa' ] for collection_id in dangerous: self.assertRaises(HTTPBadRequest, self.rest_app._getCollectionPath, collection_id) def test_getHandler(self): def handlerOne(): pass def handlerTwo(): pass handlerTwo.hasReturnValue = False self.rest_app.add_handler('collection', 'handlerOne', handlerOne) self.rest_app.add_handler('deck', 'handlerTwo', handlerTwo) (handler, hasReturnValue) = self.rest_app._getHandler('collection', 'handlerOne') self.assertEqual(handler, handlerOne) self.assertEqual(hasReturnValue, True) (handler, hasReturnValue) = self.rest_app._getHandler('deck', 'handlerTwo') self.assertEqual(handler, handlerTwo) self.assertEqual(hasReturnValue, False) # try some bad handler names and types self.assertRaises(HTTPNotFound, self.rest_app._getHandler, 'collection', 'nonExistantHandler') self.assertRaises(HTTPNotFound, self.rest_app._getHandler, 'nonExistantType', 'handlerOne') def test_parseRequestBody(self): req = MagicMock() req.body = '{"key":"value"}' data = self.rest_app._parseRequestBody(req) self.assertEqual(data, {'key': 'value'}) self.assertEqual(data.keys(), ['key']) self.assertEqual(type(data.keys()[0]), str) # test some bad data req.body = '{aaaaaaa}' self.assertRaises(HTTPBadRequest, self.rest_app._parseRequestBody, req)