def test_move_directory(self): d = new_file(self.user, self.project, path="/dir/", save=True) f = new_file(self.user, self.project, path="/dir/f1.txt", save=True) self.login() response, data = self.putJSON(self.base_url("move"), query_string={"path": "/dir/"}, data={"path": "/moved_dir/"}) self.assertStatus(200, response) with self.assertRaises(NotFoundError): f.reload() with self.assertRaises(NotFoundError): d.reload() self.assertFalse(os.path.exists(f.fspath)) self.assertFalse(os.path.exists(d.fspath)) d = File.get_by_project_path(self.project, "/moved_dir/") f = File.get_by_project_path(self.project, "/moved_dir/f1.txt") self._c.append(d) self.assertEquals(1, len(list(d.children))) self.assertEquals("hello world", f.content) self.assertTrue(os.path.exists(d.fspath)) self.assertTrue(os.path.exists(f.fspath))
def test_create_file_missing_intermediate_directories(self): with self.assertRaises(NotFoundError): new_file(self.user, self.project, path="/does/not/exist.txt", save=True) with self.assertRaises(NotFoundError): File.get_by_project_path(self.project, "/does/not/exists.txt")
def test_delete_file(self): f = new_file(self.user, self.project, save=True) fspath = f.fspath self.assertTrue(os.path.exists(fspath)) f.delete() self.assertFalse(os.path.exists(fspath)) with self.assertRaises(NotFoundError): File.get(f.key)
def test_delete_directory(self): d = new_directory(self.user, self.project, save=True) fspath = d.fspath self.assertTrue(os.path.exists(fspath)) d.delete() self.assertFalse(os.path.exists(fspath)) with self.assertRaises(NotFoundError): File.get(d.key)
def test_delete_directory_with_subtree(self): d = new_directory(self.user, self.project, path="/directory/", save=True) fspath = d.fspath self.assertTrue(os.path.exists(fspath)) f1 = new_file(self.user, self.project, path="/directory/file1.txt", save=True) f2 = new_file(self.user, self.project, path="/directory/file2.txt", save=True) d1 = new_directory(self.user, self.project, path="/directory/d1/", save=True) f3 = new_file(self.user, self.project, path="/directory/d1/file3.txt", save=True) d.delete() self.assertFalse(os.path.exists(f1.fspath)) self.assertFalse(os.path.exists(f2.fspath)) self.assertFalse(os.path.exists(d1.fspath)) self.assertFalse(os.path.exists(f3.fspath)) with self.assertRaises(NotFoundError): File.get(f1.key) with self.assertRaises(NotFoundError): File.get(f2.key) with self.assertRaises(NotFoundError): File.get(d1.key) with self.assertRaises(NotFoundError): File.get(f3.key)
def test_move_within_directory(self): new_directory(self.user, self.project, path="/dir/", save=True) d1 = new_directory(self.user, self.project, path="/dir/one/", save=True) old_fspath = d1.fspath d1.move("/dir/two/") with self.assertRaises(NotFoundError): File.get_by_project_path(self.project, "/dir/one/") self.assertFalse(os.path.exists(old_fspath)) self.assertTrue(os.path.exists(d1.fspath)) d1_g = File.get_by_project_path(self.project, "/dir/two/") self.assertEquals(d1.fspath, d1_g.fspath)
def test_update_file_content(self): f = new_file(self.user, self.project, save=True) f.update_content("yay!") now = f.date g = File.get_by_project_path(self.project, f.path) self.assertEquals("yay!", g.content) self.assertNotEquals(now, g.date)
def test_move_fail_with_non_existing_directories(self): new_directory(self.user, self.project, path="/dir/", save=True) d1 = new_directory(self.user, self.project, path="/dir/d1/", save=True) new_file(self.user, self.project, path="/dir/d1/test.txt", save=True) with self.assertRaises(NotFoundError): d1.move("/dir/not/d2/") self.assertTrue(d1.fspath.rstrip("/").endswith("d1")) d11 = File.get_by_project_path(self.project, "/dir/d1/") self.assertTrue(os.path.exists(d11.fspath)) with self.assertRaises(NotFoundError): File.get_by_project_path(self.project, "/dir1/d2/") with self.assertRaises(NotFoundError): File.get_by_project_path(self.project, "/dir1/d2/test.txt")
def test_create_file_security(self): f = File.create(data={ "project": self.project, "path": "/../../../../evil/path" }) self.assertTrue(".." not in f.fspath) self.assertEquals("/evil/path", f.path) self.assertEquals(os.path.join(File.FILES_FOLDER, self.project.key, "evil/path"), f.fspath)
def new_directory(user, project, **kwargs): save = kwargs.pop("save", False) kwargs["author"] = user kwargs["project"] = project kwargs["path"] = kwargs.get("path", "/testdir/") f = File.create(data=kwargs) if save: f.save() return f
def test_lsroot(self): new_directory(self.user, self.project, path="/dir1/", save=True) new_file(self.user, self.project, path="/test.file", save=True) new_file(self.user, self.project, path="/dir1/test1.txt", save=True) children = list(File.lsroot(self.project)) self.assertEquals(2, len(children)) paths = [c.path for c in children] self.assertTrue("/dir1/" in paths) self.assertTrue("/test.file" in paths)
def new_file(user, project, **kwargs): save = kwargs.pop("save", False) kwargs["author"] = user kwargs["project"] = project kwargs["path"] = kwargs.get("path", "/testfile.txt") kwargs["file"] = kwargs.get("file", FileStorage(StringIO("hello world"), "testfile.txt")) f = File.create(data=kwargs) if save: f.save() return f
def test_get_file(self): f = new_file(self.user, self.project, save=True) g = File.get_by_project_path(self.project, f.path) self.assertEquals(f.key, g.key) self.assertEquals(f.author.key, g.author.key) self.assertEquals(f.project.key, g.project.key) self.assertEquals(f.fspath, g.fspath) self.assertEquals(f.path, g.path) self.assertFalse(g.is_directory) self.assertEquals("hello world", g.content)
def test_create_directory(self): self.login() response = self.post(self.base_url(), query_string={"path": "/test_dir/"}) _, data = self._get_json_from_response(response) self.assertStatus(200, response) d = File.get_by_project_path(self.project, "/test_dir/") self._c.append(d) self.assertTrue(os.path.exists(d.fspath)) self.assertEquals(self.user.key, d.author.key) self.assertEquals(self.project.key, d.project.key)
def new_file(user, project, **kwargs): save = kwargs.pop("save", False) kwargs["author"] = user kwargs["project"] = project kwargs["path"] = kwargs.get("path", "/testfile.txt") kwargs["file"] = kwargs.get( "file", FileStorage(StringIO("hello world"), "testfile.txt")) f = File.create(data=kwargs) if save: f.save() return f
def test_create_file_security(self): f = File.create(data={ "project": self.project, "path": "/../../../../evil/path" }) self.assertTrue(".." not in f.fspath) self.assertEquals("/evil/path", f.path) self.assertEquals( os.path.join(File.FILES_FOLDER, self.project.key, "evil/path"), f.fspath)
def test_create_directory(self): f = File.create(data={"project": self.project, "path": "/directory/"}) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "directory") + "/" self.assertEquals(fspath, f.fspath) self.assertFalse(os.path.exists(fspath)) f.save() self.assertTrue(os.path.exists(fspath)) self.assertTrue(os.path.isdir(fspath))
def test_get_directory(self): d = new_directory(self.user, self.project, save=True) g = File.get_by_project_path(self.project, d.path) self.assertEquals(d.key, g.key) self.assertEquals(d.author.key, g.author.key) self.assertEquals(d.project.key, g.project.key) self.assertEquals(d.fspath, g.fspath) self.assertEquals(d.path, g.path) self.assertTrue(g.is_directory) with self.assertRaises(AttributeError): g.content
def test_create_directory(self): f = File.create(data={ "project": self.project, "path": "/directory/" }) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "directory") + "/" self.assertEquals(fspath, f.fspath) self.assertFalse(os.path.exists(fspath)) f.save() self.assertTrue(os.path.exists(fspath)) self.assertTrue(os.path.isdir(fspath))
def test_create_file(self): self.login() response = self.post(self.base_url(), query_string={"path": "/test_file.txt"}, data={"file": test_file("meh")}) _, data = self._get_json_from_response(response) self.assertStatus(200, response) self.assertTrue("date" in data) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "test_file.txt") self.assertTrue(os.path.exists(fspath)) with open(fspath) as f: self.assertEquals("hello world", f.read()) f = File.get_by_project_path(self.project, "/test_file.txt") self._c.append(f) self.assertEquals(self.user.key, f.author.key) self.assertEquals(self.project.key, f.project.key)
def test_move_file(self): f = new_file(self.user, self.project, path="/test.txt", save=True) self.login() response, data = self.putJSON(self.base_url("move"), query_string={"path": "/test.txt"}, data={"path": "/test_moved.txt"}) self.assertStatus(200, response) with self.assertRaises(NotFoundError): f.reload() self.assertFalse(os.path.exists(f.fspath)) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "test_moved.txt") self.assertTrue(os.path.exists(fspath)) f = File.get_by_project_path(self.project, "/test_moved.txt") self._c.append(f)
def test_create_file(self): f = File.create(data={ "project": self.project, "path": "/newfile.txt", "file": FileStorage(*test_file("newfile.txt")) }) self.assertEquals("{}`{}".format(self.project.key, "/newfile.txt"), f.key) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "newfile.txt") self.assertEquals(fspath, f.fspath) self.assertFalse(os.path.exists(fspath)) f.save() self.assertEquals("/newfile.txt", f.path) self.assertTrue(os.path.exists(fspath)) with open(fspath) as fi: c = fi.read().strip() self.assertEquals("hello world", c)
def test_create_file(self): f = File.create( data={ "project": self.project, "path": "/newfile.txt", "file": FileStorage(*test_file("newfile.txt")) }) self.assertEquals("{}`{}".format(self.project.key, "/newfile.txt"), f.key) fspath = os.path.join(File.FILES_FOLDER, self.project.key, "newfile.txt") self.assertEquals(fspath, f.fspath) self.assertFalse(os.path.exists(fspath)) f.save() self.assertEquals("/newfile.txt", f.path) self.assertTrue(os.path.exists(fspath)) with open(fspath) as fi: c = fi.read().strip() self.assertEquals("hello world", c)
def test_move_directory_with_subtree(self): d = new_directory(self.user, self.project, path="/directory1/", save=True) f1 = new_file(self.user, self.project, path="/directory1/file1.txt", save=True) f2 = new_file(self.user, self.project, path="/directory1/file2.txt", save=True) d1 = new_directory(self.user, self.project, path="/directory1/d1/", save=True) f3 = new_file(self.user, self.project, path="/directory1/d1/file3.txt", save=True) old_key = d.key old_fspath = d.fspath d.move("/moved_directory1/") self.assertNotEquals(old_fspath, d.fspath) self.assertNotEquals(old_key, d.key) self.assertFalse(os.path.exists(old_fspath)) self.assertFalse(os.path.exists(f1.fspath)) self.assertFalse(os.path.exists(f2.fspath)) self.assertFalse(os.path.exists(d1.fspath)) self.assertFalse(os.path.exists(f3.fspath)) with self.assertRaises(NotFoundError): File.get(old_key) with self.assertRaises(NotFoundError): File.get(f1.key) with self.assertRaises(NotFoundError): File.get(f2.key) with self.assertRaises(NotFoundError): File.get(d1.key) with self.assertRaises(NotFoundError): File.get(f3.key) f1 = File.get_by_project_path(d.project, "/moved_directory1/file1.txt") f2 = File.get_by_project_path(d.project, "/moved_directory1/file2.txt") d1 = File.get_by_project_path(d.project, "/moved_directory1/d1/") f3 = File.get_by_project_path(d.project, "/moved_directory1/d1/file3.txt") self.assertTrue(os.path.exists(f1.fspath)) self.assertTrue(os.path.exists(f2.fspath)) self.assertTrue(os.path.exists(d1.fspath)) self.assertTrue(os.path.exists(f3.fspath)) with open(f1.fspath) as f: self.assertEquals("hello world", f.read().strip()) with open(f2.fspath) as f: self.assertEquals("hello world", f.read().strip()) with open(f3.fspath) as f: self.assertEquals("hello world", f.read().strip())