From: robk-org Date: Tue, 17 Jul 2007 00:05:01 +0000 (-0700) Subject: added unit test to webish's rename function X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/module-simplejson.encoder.html?a=commitdiff_plain;h=920fed7f2aeb4f6ed1ce4f13f5f39b5a686268bf;p=tahoe-lafs%2Ftahoe-lafs.git added unit test to webish's rename function added unit tests to test various permutations of the rename function, and some sanity checks on the rename-form function. also added a guard to prevent '/' in from_/to_name in rename calls. --- diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index d8914b0f..f39e00ce 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -843,6 +843,57 @@ class Web(unittest.TestCase): d.addCallback(_check) return d + def test_POST_rename_file(self): # YES + d = self.POST("/vdrive/global/foo", t="rename", + from_name="bar.txt", to_name='wibble.txt') + def _check(res): + self.failIf("bar.txt" in self._foo_node.children) + self.failUnless("wibble.txt" in self._foo_node.children) + d.addCallback(_check) + d.addCallback(lambda res: self.GET("/vdrive/global/foo/wibble.txt")) + d.addCallback(self.failUnlessIsBarDotTxt) + d.addCallback(lambda res: self.GET("/vdrive/global/foo/wibble.txt?t=json")) + d.addCallback(self.failUnlessIsBarJSON) + return d + + def test_POST_rename_file_slash_fail(self): # YES + d = self.POST("/vdrive/global/foo", t="rename", + from_name="bar.txt", to_name='kirk/spock.txt') + d.addBoth(self.shouldFail, error.Error, + "test_POST_rename_file_slash_fail", + "400 Bad Request", + "to_name= may not contain a slash", + ) + def _check1(res): + self.failUnless("bar.txt" in self._foo_node.children) + d.addCallback(_check1) + d.addCallback(lambda res: self.POST("/vdrive/global", t="rename", + from_name="foo/bar.txt", to_name='george.txt')) + d.addBoth(self.shouldFail, error.Error, + "test_POST_rename_file_slash_fail", + "400 Bad Request", + "from_name= may not contain a slash", + ) + def _check2(res): + self.failUnless("foo" in self.public_root.children) + self.failIf("george.txt" in self.public_root.children) + self.failUnless("bar.txt" in self._foo_node.children) + d.addCallback(_check2) + d.addCallback(lambda res: self.GET("/vdrive/global/foo?t=json")) + d.addCallback(self.failUnlessIsFooJSON) + return d + + def test_POST_rename_dir(self): # YES + d = self.POST("/vdrive/global", t="rename", + from_name="foo", to_name='plunk') + def _check(res): + self.failIf("foo" in self.public_root.children) + self.failUnless("plunk" in self.public_root.children) + d.addCallback(_check) + d.addCallback(lambda res: self.GET("/vdrive/global/plunk?t=json")) + d.addCallback(self.failUnlessIsFooJSON) + return d + def shouldRedirect(self, res, target): if not isinstance(res, failure.Failure): self.fail("we were expecting to get redirected to %s, not get an" @@ -874,6 +925,15 @@ class Web(unittest.TestCase): return d + def test_GET_rename_form(self): # YES + d = self.GET("/vdrive/global/foo?t=rename-form&name=bar.txt", + followRedirect=True) # XXX [ ] todo: figure out why '.../foo' doesn't work + def _check(res): + self.failUnless(re.search(r'name="when_done" value=".*vdrive/global/foo/', res)) + self.failUnless(re.search(r'name="from_name" value="bar\.txt"', res)) + d.addCallback(_check) + return d + def log(self, res, msg): #print "MSG: %s RES: %s" % (msg, res) log.msg(msg) diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py index eba44991..2283d591 100644 --- a/src/allmydata/webish.py +++ b/src/allmydata/webish.py @@ -545,6 +545,11 @@ class POSTHandler(rend.Page): raise RuntimeError("rename requires from_name and to_name") if not IDirectoryNode.providedBy(self._node): raise RuntimeError("rename must only be called on directories") + for k,v in [ ('from_name', from_name), ('to_name', to_name) ]: + if v and "/" in v: + req.setResponseCode(http.BAD_REQUEST) + req.setHeader("content-type", "text/plain") + return "%s= may not contain a slash" % (k,) d = self._node.get(from_name) def add_dest(child): uri = child.get_uri()