test_web: remove all other uses of shouldRedirect

This commit is contained in:
Brian Warner 2017-07-24 17:26:07 -05:00
parent 76063b1c12
commit 3f03367d2f

View File

@ -3410,30 +3410,30 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
json.dumps(newkids)) json.dumps(newkids))
return d return d
@inlineCallbacks
def test_welcome_page_mkdir_button(self): def test_welcome_page_mkdir_button(self):
# Fetch the welcome page. # Fetch the welcome page.
d = self.GET("/") res = yield self.GET("/")
def _after_get_welcome_page(res): MKDIR_BUTTON_RE = re.compile(
MKDIR_BUTTON_RE = re.compile( '<form(?: action="([^"]*)"| method="post"| enctype="multipart/form-data"){3}>.*'
'<form(?: action="([^"]*)"| method="post"| enctype="multipart/form-data"){3}>.*' '<input (?:type="hidden" |name="t" |value="([^"]*?)" ){3}/>[ ]*'
'<input (?:type="hidden" |name="t" |value="([^"]*?)" ){3}/>[ ]*' '<input (?:type="hidden" |name="([^"]*)" |value="([^"]*)" ){3}/>[ ]*'
'<input (?:type="hidden" |name="([^"]*)" |value="([^"]*)" ){3}/>[ ]*' '<input (type="submit" |class="btn" |value="Create a directory[^"]*" ){3}/>')
'<input (type="submit" |class="btn" |value="Create a directory[^"]*" ){3}/>') html = res.replace('\n', ' ')
html = res.replace('\n', ' ') mo = MKDIR_BUTTON_RE.search(html)
mo = MKDIR_BUTTON_RE.search(html) self.failUnless(mo, html)
self.failUnless(mo, html) formaction = mo.group(1)
formaction = mo.group(1) formt = mo.group(2)
formt = mo.group(2) formaname = mo.group(3)
formaname = mo.group(3) formavalue = mo.group(4)
formavalue = mo.group(4)
return (formaction, formt, formaname, formavalue) url = self.webish_url + "/%s?t=%s&%s=%s" % (formaction, formt,
d.addCallback(_after_get_welcome_page) formaname, formavalue)
def _after_parse_form(res): target = yield self.shouldRedirectTo(url, None,
(formaction, formt, formaname, formavalue) = res method="post",
return self.POST("/%s?t=%s&%s=%s" % (formaction, formt, formaname, formavalue)) code=http.SEE_OTHER)
d.addCallback(_after_parse_form) target = urllib.unquote(target)
d.addBoth(self.shouldRedirect, None, statuscode='303') self.failUnless(target.startswith("uri/URI:DIR2:"), target)
return d
def test_POST_mkdir_replace(self): # return value? def test_POST_mkdir_replace(self): # return value?
d = self.POST(self.public_url + "/foo", t="mkdir", name="sub") d = self.POST(self.public_url + "/foo", t="mkdir", name="sub")
@ -3463,21 +3463,26 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
d.addCallback(self.failUnlessNodeKeysAre, [u"baz.txt"]) d.addCallback(self.failUnlessNodeKeysAre, [u"baz.txt"])
return d return d
@inlineCallbacks
def test_POST_mkdir_whendone_field(self): def test_POST_mkdir_whendone_field(self):
d = self.POST(self.public_url + "/foo", body, headers = self.build_form(t="mkdir", name="newdir",
t="mkdir", name="newdir", when_done="/THERE") when_done="/THERE")
d.addBoth(self.shouldRedirect, "/THERE") yield self.shouldRedirectTo(self.webish_url + self.public_url + "/foo",
d.addCallback(lambda res: self._foo_node.get(u"newdir")) self.webish_url + "/THERE",
d.addCallback(self.failUnlessNodeKeysAre, []) method="post", data=body, headers=headers,
return d code=http.FOUND)
res = yield self._foo_node.get(u"newdir")
self.failUnlessNodeKeysAre(res, [])
@inlineCallbacks
def test_POST_mkdir_whendone_queryarg(self): def test_POST_mkdir_whendone_queryarg(self):
d = self.POST(self.public_url + "/foo?when_done=/THERE", body, headers = self.build_form(t="mkdir", name="newdir")
t="mkdir", name="newdir") url = self.webish_url + self.public_url + "/foo?when_done=/THERE"
d.addBoth(self.shouldRedirect, "/THERE") yield self.shouldRedirectTo(url, self.webish_url + "/THERE",
d.addCallback(lambda res: self._foo_node.get(u"newdir")) method="post", data=body, headers=headers,
d.addCallback(self.failUnlessNodeKeysAre, []) code=http.FOUND)
return d res = yield self._foo_node.get(u"newdir")
self.failUnlessNodeKeysAre(res, [])
def test_POST_bad_t(self): def test_POST_bad_t(self):
d = self.shouldFail2(error.Error, "POST_bad_t", d = self.shouldFail2(error.Error, "POST_bad_t",
@ -3980,26 +3985,6 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
d.addCallback(self.failUnlessIsBarDotTxt) d.addCallback(self.failUnlessIsBarDotTxt)
return d return d
def shouldRedirect(self, res, target=None, statuscode=None, which=""):
""" If target is not None then the redirection has to go to target. If
statuscode is not None then the redirection has to be accomplished with
that HTTP status code."""
if not isinstance(res, failure.Failure):
to_where = (target is None) and "somewhere" or ("to " + target)
self.fail("%s: we were expecting to get redirected %s, not get an"
" actual page: %s" % (which, to_where, res))
res.trap(error.PageRedirect)
if statuscode is not None:
self.failUnlessReallyEqual(res.value.status, statuscode,
"%s: not a redirect" % which)
if target is not None:
# the PageRedirect does not seem to capture the uri= query arg
# properly, so we can't check for it.
realtarget = self.webish_url + target
self.failUnlessReallyEqual(res.value.location, realtarget,
"%s: wrong target" % which)
return res.value.location
@inlineCallbacks @inlineCallbacks
def shouldRedirectTo(self, url, target_location, method="get", def shouldRedirectTo(self, url, target_location, method="get",
code=None, **args): code=None, **args):