Example #1
0
    def test_create_or_update_with_error(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        try:
            patch_open = mock.patch("io.open")
            mock_open = patch_open.start()
            mock_open.side_effect = IOError

            path = ""
            filename = "foo-file.txt"
            content_type = None

            ret_dict = upload.create_or_update_file(
                path, filename, content_type, content, base_path=self.temp_dir)

            self.assertIsInstance(ret_dict, types.DictionaryType)
            self.assertEqual(ret_dict["status"], 500)
            self.assertEqual(ret_dict["filename"], filename)
            self.assertIsNotNone(ret_dict["error"])
            self.assertFalse(
                os.path.isfile(os.path.join(self.temp_dir, filename)))
        finally:
            patch_open.stop()
Example #2
0
    def test_create_or_update_with_error(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        try:
            patch_open = mock.patch("io.open")
            mock_open = patch_open.start()
            mock_open.side_effect = IOError

            path = ""
            filename = "foo-file.txt"
            content_type = None

            ret_dict = upload.create_or_update_file(
                path, filename, content_type, content, base_path=self.temp_dir)

            self.assertIsInstance(ret_dict, types.DictionaryType)
            self.assertEqual(ret_dict["status"], 500)
            self.assertEqual(ret_dict["filename"], filename)
            self.assertIsNotNone(ret_dict["error"])
            self.assertFalse(
                os.path.isfile(os.path.join(self.temp_dir, filename)))
        finally:
            patch_open.stop()
Example #3
0
    def test_create_or_update_simple(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = ""
        filename = "foo-file.txt"
        content_type = None

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 201)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
        self.assertTrue(os.path.isfile(os.path.join(self.temp_dir, filename)))
Example #4
0
    def test_create_or_update_simple(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = ""
        filename = "foo-file.txt"
        content_type = None

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 201)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
        self.assertTrue(os.path.isfile(os.path.join(self.temp_dir, filename)))
Example #5
0
    def test_create_or_update_file_already_exists(self, mock_check):
        mock_check.return_value = True

        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = ""
        filename = "foo-file.txt"
        content_type = None

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 200)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
Example #6
0
    def test_create_or_update_file_already_exists(self, mock_check):
        mock_check.return_value = True

        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = ""
        filename = "foo-file.txt"
        content_type = None

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 200)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
Example #7
0
    def test_create_or_update_with_error_2(self, mock_check):
        mock_check.return_value = (500, "error")

        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = "dest-path/"
        filename = "subdir/foo-file.txt"
        content_type = None

        subdir = os.path.join(self.temp_dir, path, "subdir/")
        file_path = os.path.join(subdir, "foo-file.txt")

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 500)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNotNone(ret_dict["error"])
        self.assertFalse(os.path.isdir(subdir))
        self.assertFalse(os.path.isfile(file_path))
Example #8
0
    def test_create_or_update_subdir(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        os.makedirs(os.path.join(self.temp_dir, "dest-path/"), mode=0775)

        path = "dest-path/"
        filename = "subdir/foo-file.txt"
        content_type = None

        subdir = os.path.join(self.temp_dir, path, "subdir/")
        file_path = os.path.join(subdir, "foo-file.txt")

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 201)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
        self.assertTrue(os.path.isdir(subdir))
        self.assertTrue(os.path.isfile(file_path))
Example #9
0
    def test_create_or_update_with_error_2(self, mock_check):
        mock_check.return_value = (500, "error")

        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        path = "dest-path/"
        filename = "subdir/foo-file.txt"
        content_type = None

        subdir = os.path.join(self.temp_dir, path, "subdir/")
        file_path = os.path.join(subdir, "foo-file.txt")

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 500)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNotNone(ret_dict["error"])
        self.assertFalse(os.path.isdir(subdir))
        self.assertFalse(os.path.isfile(file_path))
Example #10
0
    def test_create_or_update_subdir(self):
        content = ""
        with io.open(self.upload_file, mode="rb") as f:
            content = f.read()

        os.makedirs(os.path.join(self.temp_dir, "dest-path/"), mode=0775)

        path = "dest-path/"
        filename = "subdir/foo-file.txt"
        content_type = None

        subdir = os.path.join(self.temp_dir, path, "subdir/")
        file_path = os.path.join(subdir, "foo-file.txt")

        ret_dict = upload.create_or_update_file(
            path, filename, content_type, content, base_path=self.temp_dir)

        self.assertIsInstance(ret_dict, types.DictionaryType)
        self.assertEqual(ret_dict["status"], 201)
        self.assertEqual(ret_dict["filename"], filename)
        self.assertIsNone(ret_dict["error"])
        self.assertTrue(os.path.isdir(subdir))
        self.assertTrue(os.path.isfile(file_path))