def test_handle_request_invalid_method(self): json = JSON() request = MagicMock() json_data = {'method': 'no-existing-module.test', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) request_id, result, error = json._handle_request(request) self.assertEqual(error, {'message': 'Unknown method', 'code': 2})
def test_handle_request_method_raise_delugeerror(self): json = JSON() def get_session_id(s_id): return s_id self.patch(deluge.ui.web.auth, 'get_session_id', get_session_id) auth_conf = {'session_timeout': 10, 'sessions': {}} auth = Auth(auth_conf) request = Request(MagicMock(), False) request.base = '' auth._create_session(request) methods = yield json.get_remote_methods() # Verify the function has been registered self.assertTrue('testclass.test' in methods) request = MagicMock() request.getCookie = MagicMock(return_value=list(auth.config['sessions'])[0]) json_data = {'method': 'testclass.test', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) request_id, result, error = json._handle_request(request) result.addCallback(self.fail) def on_error(error): self.assertEqual(error.type, DelugeError) result.addErrback(on_error) yield result
def test_render_fail_disconnected(self): json = JSON() request = MagicMock() request.method = 'POST' request._disconnected = True # When disconnected, returns empty string self.assertEqual(json.render(request), '')
def test_render_fail(self): json = JSON() request = MagicMock() request.method = 'POST' def compress(contents, request): return contents self.patch(deluge.ui.web.json_api, 'compress', compress) def write(response_str): request.write_was_called = True response = json_lib.loads(response_str) self.assertEqual(response['result'], None) self.assertEqual(response['id'], None) self.assertEqual( response['error']['message'], 'JSONException: JSON not decodable' ) self.assertEqual(response['error']['code'], 5) request.write = write request.write_was_called = False request._disconnected = False request.getHeader.return_value = 'application/json' self.assertEqual(json.render(request), server.NOT_DONE_YET) self.assertTrue(request.write_was_called)
def test_handle_request_auth_error(self): yield self.connect_client() json = JSON() auth_conf = {'session_timeout': 10, 'sessions': {}} Auth(auth_conf) # Must create the component # Must be called to update remote methods in json object yield json.get_remote_methods() request = MagicMock() request.getCookie = MagicMock(return_value='bad_value') json_data = {'method': 'core.get_libtorrent_version', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) request_id, result, error = json._handle_request(request) self.assertEqual(error, {'message': 'Not authenticated', 'code': 1})
def test_on_json_request_invalid_content_type(self): """Test for exception with content type not application/json""" json = JSON() request = MagicMock() request.getHeader.return_value = 'text/plain' json_data = {'method': 'some.method', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) self.assertRaises(JSONException, json._on_json_request, request)
def test_handle_request_invalid_json_request(self): json = JSON() request = MagicMock() request.json = json_lib.dumps({'id': 0, 'params': []}) self.assertRaises(JSONException, json._handle_request, request) request.json = json_lib.dumps({'method': 'some.method', 'params': []}) self.assertRaises(JSONException, json._handle_request, request) request.json = json_lib.dumps({'method': 'some.method', 'id': 0}) self.assertRaises(JSONException, json._handle_request, request)
def __init__(self): resource.Resource.__init__(self) self.putChild(b'css', LookupResource('Css', rpath('css'))) if os.path.isfile(rpath('js', 'gettext.js')): self.putChild(b'gettext.js', GetText()) else: log.warning('Cannot find "gettext.js" translation file!' ' Text will only be available in English.') self.putChild(b'gettext.js', MockGetText()) self.putChild(b'flag', Flag()) self.putChild(b'icons', LookupResource('Icons', rpath('icons'))) self.putChild(b'images', LookupResource('Images', rpath('images'))) js = ScriptResource() # configure the dev scripts js.add_script('ext-base-debug.js', rpath('js', 'extjs', 'ext-base-debug.js'), 'dev') js.add_script('ext-all-debug.js', rpath('js', 'extjs', 'ext-all-debug.js'), 'dev') js.add_script_folder('ext-extensions', rpath('js', 'extjs', 'ext-extensions'), 'dev') js.add_script_folder('deluge-all', rpath('js', 'deluge-all'), 'dev') # configure the debug scripts js.add_script('ext-base-debug.js', rpath('js', 'extjs', 'ext-base-debug.js'), 'debug') js.add_script('ext-all-debug.js', rpath('js', 'extjs', 'ext-all-debug.js'), 'debug') js.add_script( 'ext-extensions-debug.js', rpath('js', 'extjs', 'ext-extensions-debug.js'), 'debug', ) js.add_script('deluge-all-debug.js', rpath('js', 'deluge-all-debug.js'), 'debug') # configure the normal scripts js.add_script('ext-base.js', rpath('js', 'extjs', 'ext-base.js')) js.add_script('ext-all.js', rpath('js', 'extjs', 'ext-all.js')) js.add_script('ext-extensions.js', rpath('js', 'extjs', 'ext-extensions.js')) js.add_script('deluge-all.js', rpath('js', 'deluge-all.js')) self.js = js self.putChild(b'js', js) self.putChild(b'json', JSON()) self.putChild(b'upload', Upload()) self.putChild(b'render', Render()) self.putChild(b'themes', static.File(rpath('themes'))) self.putChild(b'tracker', Tracker()) theme = component.get('DelugeWeb').config['theme'] if not os.path.isfile(rpath('themes', 'css', 'xtheme-%s.css' % theme)): theme = CONFIG_DEFAULTS.get('theme') self.__stylesheets.insert(1, 'themes/css/xtheme-%s.css' % theme)
def test_render_on_rpc_request_failed(self): json = JSON() methods = yield json.get_remote_methods() # Verify the function has been registered self.assertTrue('testclass.test' in methods) request = MagicMock() # Circumvent authentication auth = Auth({}) self.mock_authentication_ignore(auth) self.mock_compress_body() def write(response_str): request.write_was_called = True response = json_lib.loads(response_str) self.assertEqual(response['result'], None, 'BAD RESULT') self.assertEqual(response['id'], 0) self.assertEqual( response['error']['message'], 'Failure: [Failure instance: Traceback (failure with no frames):' " <class 'deluge.error.DelugeError'>: DelugeERROR\n]", ) self.assertEqual(response['error']['code'], 4) request.write = write request.write_was_called = False request._disconnected = False request.getHeader.return_value = 'application/json' json_data = {'method': 'testclass.test', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) d = json._on_json_request(request) def on_success(arg): self.assertEqual(arg, server.NOT_DONE_YET) return True d.addCallbacks(on_success, self.fail) yield d
def test_render_on_rpc_request_failed(self): json = JSON() methods = yield json.get_remote_methods() # Verify the function has been registered self.assertTrue('testclass.test' in methods) request = MagicMock() # Circumvent authentication auth = Auth({}) self.mock_authentication_ignore(auth) self.mock_compress_body() def write(response_str): request.write_was_called = True response = json_lib.loads(response_str) self.assertEqual(response['result'], None, 'BAD RESULT') self.assertEqual(response['id'], 0) self.assertEqual( response['error']['message'], 'Failure: [Failure instance: Traceback (failure with no frames):' " <class 'deluge.error.DelugeError'>: DelugeERROR\n]") self.assertEqual(response['error']['code'], 4) request.write = write request.write_was_called = False request._disconnected = False request.getHeader.return_value = 'application/json' json_data = {'method': 'testclass.test', 'id': 0, 'params': []} request.json = json_lib.dumps(json_data) d = json._on_json_request(request) def on_success(arg): self.assertEqual(arg, server.NOT_DONE_YET) return True d.addCallbacks(on_success, self.fail) yield d
def test_render_fail(self): json = JSON() request = MagicMock() request.method = 'POST' def compress(contents, request): return contents self.patch(deluge.ui.web.json_api, 'compress', compress) def write(response_str): request.write_was_called = True response = json_lib.loads(response_str) self.assertEqual(response['result'], None) self.assertEqual(response['id'], None) self.assertEqual(response['error']['message'], 'JSONException: JSON not decodable') self.assertEqual(response['error']['code'], 5) request.write = write request.write_was_called = False request._disconnected = False request.getHeader.return_value = 'application/json' self.assertEqual(json.render(request), server.NOT_DONE_YET) self.assertTrue(request.write_was_called)
def __init__(self): resource.Resource.__init__(self) self.putChild("css", LookupResource("Css", rpath("css"))) self.putChild("gettext.js", GetText()) self.putChild("flag", Flag()) self.putChild("icons", LookupResource("Icons", rpath("icons"))) self.putChild("images", LookupResource("Images", rpath("images"))) js = ScriptResource() # configure the dev scripts js.add_script("ext-base-debug.js", rpath("js", "ext-base-debug.js"), "dev") js.add_script("ext-all-debug.js", rpath("js", "ext-all-debug.js"), "dev") js.add_script_folder("ext-extensions", rpath("js", "ext-extensions"), "dev") js.add_script_folder("deluge-all", rpath("js", "deluge-all"), "dev") # configure the debug scripts js.add_script("ext-base-debug.js", rpath("js", "ext-base-debug.js"), "debug") js.add_script("ext-all-debug.js", rpath("js", "ext-all-debug.js"), "debug") js.add_script("ext-extensions-debug.js", rpath("js", "ext-extensions-debug.js"), "debug") js.add_script("deluge-all-debug.js", rpath("js", "deluge-all-debug.js"), "debug") # configure the normal scripts js.add_script("ext-base.js", rpath("js", "ext-base.js")) js.add_script("ext-all.js", rpath("js", "ext-all.js")) js.add_script("ext-extensions.js", rpath("js", "ext-extensions.js")) js.add_script("deluge-all.js", rpath("js", "deluge-all.js")) self.putChild("js", js) self.putChild("json", JSON()) self.putChild("upload", Upload()) self.putChild("render", Render()) self.putChild("themes", static.File(rpath("themes"))) self.putChild("tracker", Tracker()) theme = component.get("DelugeWeb").config["theme"] if not os.path.isfile(rpath("themes", "css", "xtheme-%s.css" % theme)): theme = CONFIG_DEFAULTS.get("theme") self.__stylesheets.insert(1, "themes/css/xtheme-%s.css" % theme)
def test_get_remote_methods(self): json = JSON() methods = yield json.get_remote_methods() self.assertEqual(type(methods), tuple) self.assertTrue(len(methods) > 0)