From 2b1ea5c60429e7f39d7aef8898d484796da757c2 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Tue, 15 Dec 2020 18:30:12 -0500 Subject: [PATCH] Remove the client Mock object It wasn't used by anything so that was easy. Clean up the test as long as we're here. --- src/allmydata/test/web/common.py | 3 ++- src/allmydata/test/web/test_root.py | 40 +++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/allmydata/test/web/common.py b/src/allmydata/test/web/common.py index 1f568ad8d..00a40e3c5 100644 --- a/src/allmydata/test/web/common.py +++ b/src/allmydata/test/web/common.py @@ -25,7 +25,8 @@ def assert_soup_has_tag_with_attributes(testcase, soup, tag_name, attrs): tags = soup.find_all(tag_name) for tag in tags: if all(v in tag.attrs.get(k, []) for k, v in attrs.items()): - return # we found every attr in this tag; done + # we found every attr in this tag; done + return tag testcase.fail( u"No <{}> tags contain attributes: {}".format(tag_name, attrs) ) diff --git a/src/allmydata/test/web/test_root.py b/src/allmydata/test/web/test_root.py index 139441a6c..0715c8102 100644 --- a/src/allmydata/test/web/test_root.py +++ b/src/allmydata/test/web/test_root.py @@ -1,7 +1,13 @@ -from mock import Mock - import time +from urllib import ( + quote, +) + +from bs4 import ( + BeautifulSoup, +) + from twisted.trial import unittest from twisted.web.template import Tag from twisted.web.test.requesthelper import DummyRequest @@ -16,6 +22,9 @@ from ...util.connection_status import ConnectionStatus from allmydata.web.root import URIHandler from allmydata.client import _Client +from .common import ( + assert_soup_has_tag_with_attributes, +) from ..common_web import ( render, ) @@ -30,28 +39,37 @@ class RenderSlashUri(unittest.TestCase): """ def setUp(self): - self.client = Mock() + self.client = object() self.res = URIHandler(self.client) - def test_valid(self): + def test_valid_query_redirect(self): """ - A valid capbility does not result in error + A syntactically valid capability given in the ``uri`` query argument + results in a redirect. """ - query_args = {b"uri": [ + cap = ( b"URI:CHK:nt2xxmrccp7sursd6yh2thhcky:" b"mukesarwdjxiyqsjinbfiiro6q7kgmmekocxfjcngh23oxwyxtzq:2:5:5874882" - ]} + ) + query_args = {b"uri": [cap]} response_body = self.successResultOf( render(self.res, query_args), ) - self.assertNotEqual( - response_body, - "Invalid capability", + soup = BeautifulSoup(response_body, 'html5lib') + tag = assert_soup_has_tag_with_attributes( + self, + soup, + u"meta", + {u"http-equiv": "refresh"}, + ) + self.assertIn( + quote(cap, safe=""), + tag.attrs.get(u"content"), ) def test_invalid(self): """ - A (trivially) invalid capbility is an error + A syntactically invalid capbility results in an error. """ query_args = {b"uri": [b"not a capability"]} response_body = self.successResultOf(