def test_parseTransferRequest_success(self): downloadTo = "." requests = [] def fakeParser(*args): return ['it worked. this is a weak fake'] endpoint = server.InboundRequestEndpoint( requests, downloadTo, fakeParser ) kr = KleinResource(endpoint.app) body = utils.sortedDump({"type": "good request"}) request = requestMock('/', 'POST', body=body) d = _render(kr, request) self.assertFired(d) self.assertEqual( request.getWrittenData(), b'{"error": null, "status": "ok"}' )
def test_sorts(self): data = {'z': '1', 'a': 'strano'} result = utils.sortedDump(data) self.assertEqual( '{"a": "strano", "z": "1"}', result )
def test_get_all_peers_returns_peer_in_list(self): transfer = server.Transfer('1', '.', '1.1.1.1') rootUrl = b'piazza.org' mockOb = MagicMock(server.OutboundRequests) mockOb.all.return_value = iter([transfer]) app = server.OutboundRequestEndpoint(outboundRequests=mockOb, getFileNames=None, submitFileRequest=None, rootUrl=rootUrl).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertTrue(mockOb.all.called) expected = utils.sortedDump({ "files": [{ "url": "piazza.org/1/", "transferId": "1", "userIp": "1.1.1.1", "filepath": "." }] }) self.assertEqual(expected, request.getWrittenData())
def test_parseTransferRequest_failure(self): downloadTo = "." requests = [] def fakeParser(*args): raise filerequest.FileRequestError() endpoint = server.InboundRequestEndpoint( requests, downloadTo, fakeParser ) kr = KleinResource(endpoint.app) body = utils.sortedDump({}) request = requestMock('/', 'POST', body=body) d = _render(kr, request) self.assertFired(d) self.assertEqual( request.getWrittenData(), b'{"error": "error parsing request", "status": "error"}' )
def test_get_all_peers_returns_peer_in_list(self): transfer = server.Transfer('1', '.', '1.1.1.1') rootUrl = b'piazza.org' mockOb = MagicMock(server.OutboundRequests) mockOb.all.return_value = iter([transfer]) app = server.OutboundRequestEndpoint(outboundRequests=mockOb, getFileNames=None, submitFileRequest=None, rootUrl=rootUrl).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertTrue(mockOb.all.called) expected = utils.sortedDump( {"files": [{ "url": "piazza.org/1/", "transferId": "1", "userIp": "1.1.1.1", "filepath": "." }]}) self.assertEqual(expected, request.getWrittenData())
def test_parse_incoming_post_request_without_filepath(self): app = server.OutboundRequestEndpoint(outboundRequests=None, getFileNames=None, submitFileRequest=None, rootUrl=None) request = utils.sortedDump({"user": "******"}) body = StringIO(request) self.assertRaises(server.MissingFormDataError, app.parse, body)
def test_parse_incoming_post_request_returns_new_transfer(self): app = server.OutboundRequestEndpoint(outboundRequests=None, getFileNames=None, submitFileRequest=None, rootUrl=None) request = utils.sortedDump( {"filepath": "/Users/chris/Documents", "user": "******"} ) body = StringIO(request) result = app.parse(body) self.assertTrue(result.transferId is not None)
def test_newTransfer_succeeds(self): body = utils.sortedDump({"filepath": "plop.txt", "user": "******"}) request = requestMock('/', 'POST', body=body) d = _render(self.kr, request) self.assertFired(d) self.assertEqual( request.getWrittenData(), b'{"directories": [], "filenames": ["plop.txt"],' b' "transfer": "/1/"}')
def test_parse_incoming_post_request_returns_new_transfer(self): app = server.OutboundRequestEndpoint(outboundRequests=None, getFileNames=None, submitFileRequest=None, rootUrl=None) request = utils.sortedDump({ "filepath": "/Users/chris/Documents", "user": "******" }) body = StringIO(request) result = app.parse(body) self.assertTrue(result.transferId is not None)
def test_get_all_peers_returns_json_object(self): peerlist = MagicMock(peerdiscovery.PeerList) peerlist.all.return_value = iter([]) app = server.UsersEndpoint(peerlist).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertEqual(utils.sortedDump({"users": []}), request.getWrittenData())
def test_get_all_peers_returns_json_object(self): peerlist = MagicMock(peerdiscovery.PeerList) peerlist.all.return_value = iter([]) app = server.UsersEndpoint(peerlist).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertEqual( utils.sortedDump({"users": []}), request.getWrittenData() )
def test_newTransfer_succeeds(self): body = utils.sortedDump( { "filepath": "plop.txt", "user": "******" } ) request = requestMock('/', 'POST', body=body) d = _render(self.kr, request) self.assertFired(d) self.assertEqual( request.getWrittenData(), b'{"directories": [], "filenames": ["plop.txt"],' b' "transfer": "/1/"}' )
def test_get_all_peers_returns_peer_in_list(self): peerlist = MagicMock(peerdiscovery.PeerList) peer = peerdiscovery.Peer(b'paul', b'1.1.1.1', b'58673') peerlist.all.return_value = iter([peer]) app = server.UsersEndpoint(peerlist).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertTrue(peerlist.all.called) self.assertEqual(utils.sortedDump({"users": [peer.serialize()]}), request.getWrittenData())
def test_get_all_peers_returns_peer_in_list(self): peerlist = MagicMock(peerdiscovery.PeerList) peer = peerdiscovery.Peer(b'paul', b'1.1.1.1', b'58673') peerlist.all.return_value = iter([peer]) app = server.UsersEndpoint(peerlist).app kr = KleinResource(app) request = requestMock('/', 'GET') d = _render(kr, request) self.assertFired(d) self.assertTrue(peerlist.all.called) self.assertEqual( utils.sortedDump({"users": [peer.serialize()]}), request.getWrittenData() )
def test_parseTransferRequest_success(self): downloadTo = "." requests = [] def fakeParser(*args): return ['it worked. this is a weak fake'] endpoint = server.InboundRequestEndpoint(requests, downloadTo, fakeParser) kr = KleinResource(endpoint.app) body = utils.sortedDump({"type": "good request"}) request = requestMock('/', 'POST', body=body) d = _render(kr, request) self.assertFired(d) self.assertEqual(request.getWrittenData(), b'{"error": null, "status": "ok"}')
def test_parseTransferRequest_failure(self): downloadTo = "." requests = [] def fakeParser(*args): raise filerequest.FileRequestError() endpoint = server.InboundRequestEndpoint(requests, downloadTo, fakeParser) kr = KleinResource(endpoint.app) body = utils.sortedDump({}) request = requestMock('/', 'POST', body=body) d = _render(kr, request) self.assertFired(d) self.assertEqual( request.getWrittenData(), b'{"error": "error parsing request", "status": "error"}')
def test_sorts(self): data = {'z': '1', 'a': 'strano'} result = utils.sortedDump(data) self.assertEqual('{"a": "strano", "z": "1"}', result)