def test_sync(self): '''Test the threading/transcription works when async=false''' reactor = self.buildReactor() expected = {'some': 'result'} stub_transcriber = StubTranscriber(transcribe_stub=expected) controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': [''], 'async': ['false']} finish = req.notifyFinish() finish.addCallback(lambda _: reactor.callWhenRunning(reactor.stop)) body = controller.render(req) assert_equals(body, NOT_DONE_YET) self.runReactor(reactor, timeout=1) assert_equals(len(req.written), 1) got = json.loads(req.written[0]) assert_equals(got, expected)
def dummy_request(): """ Dummy request with some arguments used by tests in this module. """ request = DummyRequest(b"/") # Mimic the way Twisted parses URL parameters: request.args = { b"id": [b"1234"], b"count": [b"20"], b"parent_id": [], # happens when you pass "parent_id=" b"child_id": [b"a", b"b"], # happens when you pass "child_id=a&child_id=b". Not supported by retwist b"debug": [b"true"], b"verbose": [b"false"], b"type": [b"int"], b"lang": [b"de"], b"v": [b"1.0"] } return request
def test_sync_cancel(self): '''Test that the controller doesn't try to render when the request is cancelled.''' reactor = self.buildReactor() stub_transcriber = StubTranscriber() controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': [''], 'async': ['false']} controller.render(req) req.processingFailed( Failure(ConnectionDone("Simulated disconnect"))) reactor.callWhenRunning(reactor.stop) self.runReactor(reactor) assert_equals(req.finished, 0)
def test_sync_cancel(self): '''Test that the controller doesn't try to render when the request is cancelled.''' reactor = self.buildReactor() stub_transcriber = StubTranscriber() controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': [''], 'async': ['false']} controller.render(req) req.processingFailed(Failure(ConnectionDone("Simulated disconnect"))) reactor.callWhenRunning(reactor.stop) self.runReactor(reactor) assert_equals(req.finished, 0)
def test_async(self): '''Test the redirect works when async=true''' reactor = self.buildReactor() uid = 'myuid' want_location = '/transcriptions/' + uid stub_transcriber = StubTranscriber(uid_stub=uid) controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': ['']} body = controller.render(req) assert_equals(body, '') assert 'location' in req.outgoingHeaders got_location = req.outgoingHeaders['location'] assert_equals(want_location, got_location)
def test_async(self): '''Test the redirect works when async=true''' reactor = self.buildReactor() uid = 'myuid' want_location = '/transcriptions/' + uid stub_transcriber = StubTranscriber(uid_stub=uid) controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': ['']} body = controller.render(req) assert_equals(body, '') assert 'location' in req.outgoingHeaders got_location = req.outgoingHeaders['location'] assert_equals(want_location, got_location)
def dummy_request(): """ Dummy request with some arguments used by tests in this module. """ request = DummyRequest(b"/") # Mimic the way Twisted parses URL parameters: request.args = { b"id": [b"1234"], b"count": [b"20"], b"parent_id": [], # happens when you pass "parent_id=" b"child_id": [ b"a", b"b" ], # happens when you pass "child_id=a&child_id=b". Not supported by retwist b"debug": [b"true"], b"verbose": [b"false"], b"type": [b"int"], b"lang": [b"de"], b"v": [b"1.0"], b"key": [b"523f2850-5646-4832-9507-e99e144328c8"], b"payload": [json.dumps({"msg": u"Ümlauts supported"})], } return request
def test_sync(self): '''Test the threading/transcription works when async=false''' reactor = self.buildReactor() expected = {'some': 'result'} stub_transcriber = StubTranscriber(transcribe_stub=expected) controller = TranscriptionsController(stub_transcriber, reactor=reactor) req = DummyRequest([]) req.method = 'POST' req.args = {'transcript': [''], 'audio': [''], 'async': ['false']} finish = req.notifyFinish() finish.addCallback(lambda _: reactor.callWhenRunning(reactor.stop)) body = controller.render(req) assert_equals(body, NOT_DONE_YET) self.runReactor(reactor, timeout=1) assert_equals(len(req.written), 1) got = json.loads(req.written[0]) assert_equals(got, expected)
def forge_request(uri=b'https://www.globaleaks.org/', headers=None, body='', client_addr=None, method=b'GET', handler_cls=None, attached_file={}): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ if headers is None: headers = {} _, host, path, query, frag = urlsplit(uri) x = host.split(b':') if len(x) > 1: port = int(x[1]) else: port = 80 request = DummyRequest([b'']) request.tid = 1 request.method = method request.uri = uri request.path = path request._serverName = host request.code = 200 request.client_ip = b'127.0.0.1' request.client_proto = b'https' request.client_using_tor = False def getResponseBody(): # Ugh, hack. Twisted returns this all as bytes, and we want it as str if isinstance(request.written[0], binary_type): return b''.join(request.written) else: return ''.join(request.written) request.getResponseBody = getResponseBody if client_addr is None: request.client = IPv4Address('TCP', b'1.2.3.4', 12345) else: request.client = client_addr def getHost(): return IPv4Address('TCP', b'127.0.0.1', port) request.getHost = getHost def notifyFinish(): return Deferred() request.notifyFinish = notifyFinish request.requestHeaders.setRawHeaders('host', [b'127.0.0.1']) for k, v in headers.items(): request.requestHeaders.setRawHeaders(k, [v]) request.headers = request.getAllHeaders() request.args = {} if attached_file is not None: request.args = {'file': [attached_file]} class fakeBody(object): def read(self): if isinstance(body, dict): ret = json.dumps(body) else: ret = body if isinstance(ret, text_type): ret = ret.encode('utf-8') return ret def close(self): pass request.content = fakeBody() return request
def forge_request(uri='https://www.globaleaks.org/', headers=None, body='', client_addr=None, method='GET', handler_cls=None, attached_file={}): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ if headers is None: headers = {} _, host, path, query, frag = urlparse.urlsplit(uri) x = host.split (':') if len(x) > 1: port = int(x[1]) else: port = 80 request = DummyRequest(['']) request.method = method request.uri = uri request.path = path request._serverName = bytes(host) request.code = 200 request.client_ip = '127.0.0.1' request.client_proto = 'https' request.client_using_tor = False def getResponseBody(): return ''.join(request.written) request.getResponseBody = getResponseBody if client_addr is None: request.client = IPv4Address('TCP', '1.2.3.4', 12345) else: request.client = client_addr def getHost(): return IPv4Address('TCP', '127.0.0.1', port) request.getHost = getHost def notifyFinish(): return Deferred() request.notifyFinish = notifyFinish for k, v in headers.items(): request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)]) request.headers = request.getAllHeaders() request.args = {} if attached_file is not None: request.args = {'file': [attached_file]} class fakeBody(object): def read(self): if isinstance(body, dict): return json.dumps(body) else: return body def close(self): pass request.content = fakeBody() return request
def request(self, jbody=None, user_id=None, role=None, headers=None, body='', path=None, remote_ip='0.0.0.0', method='MOCK', handler_cls=None, attached_file={}, kwargs={}): """ Constructs a handler for preforming mock requests using the bag of params described below. Args: jbody: The body of the request as a dict (it will be automatically converted to string) body: The body of the request as a string user_id: when simulating authentication the session should be bound to a certain user_id. role: when simulating authentication the session should be bound to a certain role. method: HTTP method, e.g. "GET" or "POST" headers: Dict of headers to pass on the request remote_ip: If a particular remote_ip should be set. handler_cls: The type of handler that will respond to the request. If this is not set self._handler is used. attached_file: A dict to place in the request.args.files obj """ if jbody and not body: body = json.dumps(jbody) elif body and jbody: raise ValueError('jbody and body in conflict') if handler_cls is None: handler_cls = self._handler request = DummyRequest(['']) def getResponseBody(): return ''.join(request.written) request.path = '' request.code = 200 request.language = 'en' request.client_ip = '127.0.0.1' request.client_proto = 'https' request.client_using_tor = False request.getResponseBody = getResponseBody request.client = IPv4Address('TCP', '1.2.3.4', 12345) request.args = {} if attached_file is not None: request.args = {'file': [attached_file]} if headers is not None: for k, v in headers.iteritems(): request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)]) request.headers = request.getAllHeaders() from globaleaks.rest import api x = api.APIResourceWrapper() x.preprocess(request) if path is not None: if not path.startswith('/'): raise ValueError('Must pass a valid url path') request.path = path class fakeBody(object): def read(self): return body def close(self): pass request.content = fakeBody() from globaleaks.rest.api import decorate_method if not getattr(handler_cls, 'decorated', False): for method in ['get', 'post', 'put', 'delete']: if getattr(handler_cls, method, None) is not None: decorate_method(handler_cls, method) handler_cls.decorated = True handler = handler_cls(request, **kwargs) if user_id is None and role is not None: if role == 'admin': user_id = self.dummyAdminUser['id'] elif role == 'receiver': user_id = self.dummyReceiverUser_1['id'] elif role == 'custodian': user_id = self.dummyCustodianUser['id'] if role is not None: session = GLSession(user_id, role, 'enabled') handler.request.headers['x-session'] = session.id return handler
def test_root_json(self): """ The 'welcome' / root page renders properly with ?t=json when some servers show None for available_space while others show a valid int See also https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3852 """ ann = { "anonymous-storage-FURL": "pb://w2hqnbaa25yw4qgcvghl5psa3srpfgw3@tcp:127.0.0.1:51309/vucto2z4fxment3vfxbqecblbf6zyp6x", "permutation-seed-base32": "w2hqnbaa25yw4qgcvghl5psa3srpfgw3", } srv0 = NativeStorageServer(b"server_id0", ann, None, {}, EMPTY_CLIENT_CONFIG) srv0.get_connection_status = lambda: ConnectionStatus( False, "summary0", {}, 0, 0) srv1 = NativeStorageServer(b"server_id1", ann, None, {}, EMPTY_CLIENT_CONFIG) srv1.get_connection_status = lambda: ConnectionStatus( False, "summary1", {}, 0, 0) # arrange for this server to have some valid available space srv1.get_available_space = lambda: 12345 class FakeClient(_Client): history = [] stats_provider = object() nickname = "" nodeid = b"asdf" _node_public_key = create_signing_keypair()[1] introducer_clients = [] helper = None def __init__(self): service.MultiService.__init__(self) self.storage_broker = StorageFarmBroker( permute_peers=True, tub_maker=None, node_config=EMPTY_CLIENT_CONFIG, ) self.storage_broker.test_add_server(b"test-srv0", srv0) self.storage_broker.test_add_server(b"test-srv1", srv1) root = Root(FakeClient(), now_fn=time.time) lines = [] req = DummyRequest(b"") req.fields = {} req.args = { b"t": [b"json"], } # for some reason, DummyRequest is already finished when we # try to add a notifyFinish handler, so override that # behavior. def nop(): return succeed(None) req.notifyFinish = nop req.write = lines.append yield root.render(req) raw_js = b"".join(lines).decode("utf8") js = json.loads(raw_js) servers = js["servers"] self.assertEquals(len(servers), 2) self.assertIn( { "connection_status": "summary0", "nodeid": "server_id0", "last_received_data": 0, "version": None, "available_space": None, "nickname": "" }, servers) self.assertIn( { "connection_status": "summary1", "nodeid": "server_id1", "last_received_data": 0, "version": None, "available_space": 12345, "nickname": "" }, servers)