Exemplo n.º 1
0
    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"}'
        )
Exemplo n.º 2
0
 def test_sorts(self):
     data = {'z': '1', 'a': 'strano'}
     result = utils.sortedDump(data)
     self.assertEqual(
         '{"a": "strano", "z": "1"}',
         result
     )
Exemplo n.º 3
0
    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())
Exemplo n.º 4
0
    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"}'
        )
Exemplo n.º 5
0
    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())
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
    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/"}')
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
    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())
Exemplo n.º 12
0
    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()
        )
Exemplo n.º 13
0
    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/"}'
        )
Exemplo n.º 14
0
    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())
Exemplo n.º 15
0
    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()
        )
Exemplo n.º 16
0
    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"}')
Exemplo n.º 17
0
    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"}')
Exemplo n.º 18
0
 def test_sorts(self):
     data = {'z': '1', 'a': 'strano'}
     result = utils.sortedDump(data)
     self.assertEqual('{"a": "strano", "z": "1"}', result)