Esempio n. 1
0
    def setUp(self):
        self.updown_auth_manager = JWTUpDownAuthManager("secret",
                                                        timedelta(seconds=1))

        self.storage_dir = temp_folder()
        self.service = FileUploadDownloadService(self.updown_auth_manager, self.storage_dir)
        self.disk_path = os.path.join(self.storage_dir, "dir", "other")
        self.relative_file_path = "dir/other/thefile.txt"
        self.absolute_file_path = os.path.join(self.disk_path, "thefile.txt")
        mkdir(self.disk_path)
        self.content = "the content"
Esempio n. 2
0
class FileUploadDownloadServiceTest(unittest.TestCase):
    def setUp(self):
        self.updown_auth_manager = JWTUpDownAuthManager(
            "secret", timedelta(seconds=1))

        self.storage_dir = temp_folder()
        self.service = FileUploadDownloadService(self.updown_auth_manager,
                                                 self.storage_dir)
        self.disk_path = os.path.join(self.storage_dir, "dir", "other")
        self.relative_file_path = "dir/other/thefile.txt"
        self.absolute_file_path = os.path.join(self.disk_path, "thefile.txt")
        mkdir(self.disk_path)
        self.content = "the content"

    def test_file_download(self):
        save(os.path.join(self.disk_path, "thefile.txt"), self.content)
        token = self.updown_auth_manager.get_token_for(self.relative_file_path,
                                                       "pepe",
                                                       len(self.content))
        path_to_file = self.service.get_file_path(self.relative_file_path,
                                                  token)

        self.assertEqual(path_to_file, self.absolute_file_path)

        readed_content = load(self.absolute_file_path)
        self.assertEqual(readed_content, self.content)

        # Expire token
        sleep(2)
        self.assertRaises(NotFoundException, self.service.get_file_path,
                          self.relative_file_path, token)

    def test_file_upload(self):
        token = self.updown_auth_manager.get_token_for(self.relative_file_path,
                                                       "pepe",
                                                       len(self.content))

        file_saver = MockFileSaver("thefile.txt", self.content)
        self.assertFalse(os.path.exists(self.absolute_file_path))
        self.service.put_file(file_saver, self.absolute_file_path, token,
                              len(self.content))

        self.assertTrue(os.path.exists(self.absolute_file_path))

        # Raises if wrong size
        self.assertRaises(RequestErrorException, self.service.put_file,
                          file_saver, self.absolute_file_path, token,
                          len(self.content) + 1)
Esempio n. 3
0
    def attach_to(app):
        r = BottleRoutes()
        storage_path = app.server_store.store
        service = FileUploadDownloadService(app.updown_auth_manager,
                                            storage_path)

        @app.route(r.v1_updown_file, method=["GET"])
        def get(the_path):
            token = request.query.get("signature", None)
            file_path = service.get_file_path(the_path, token)
            # https://github.com/kennethreitz/requests/issues/1586
            return static_file(os.path.basename(file_path),
                               root=os.path.dirname(file_path),
                               mimetype=get_mime_type(file_path))

        @app.route(r.v1_updown_file, method=["PUT"])
        def put(the_path):
            token = request.query.get("signature", None)
            file_saver = ConanFileUpload(request.body,
                                         None,
                                         filename=os.path.basename(the_path),
                                         headers=request.headers)
            abs_path = os.path.abspath(
                os.path.join(storage_path, os.path.normpath(the_path)))
            # Body is a stringIO (generator)
            service.put_file(file_saver, abs_path, token,
                             request.content_length)