예제 #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"}'
        )
예제 #2
0
 def test_sorts(self):
     data = {'z': '1', 'a': 'strano'}
     result = utils.sortedDump(data)
     self.assertEqual(
         '{"a": "strano", "z": "1"}',
         result
     )
예제 #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())
예제 #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"}'
        )
예제 #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())
예제 #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)
예제 #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)
예제 #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)
예제 #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/"}')
예제 #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)
예제 #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())
예제 #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()
        )
예제 #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/"}'
        )
예제 #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())
예제 #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()
        )
예제 #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"}')
예제 #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"}')
예제 #18
0
 def test_sorts(self):
     data = {'z': '1', 'a': 'strano'}
     result = utils.sortedDump(data)
     self.assertEqual('{"a": "strano", "z": "1"}', result)