[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [bridgedb/master] Add a mocked DummyBridge class to test_HTTPServer.py.
commit f0ca59c8d1792d2fcbea54b0eb29feb10ecacdb0
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date: Wed Mar 19 17:46:32 2014 +0000
Add a mocked DummyBridge class to test_HTTPServer.py.
This is used to test that bridges are actually returned on
'bridges.html' results pages, and is missing all of the real class's
methods except for `getConfigLine()`. The latter method needs to be
mocked because is used in `HTTPServer.WebResourceBridges.render()` to
get the bridge line to display to a client.
---
lib/bridgedb/test/test_HTTPServer.py | 49 ++++++++++++++++++++++++++++++++--
1 file changed, 47 insertions(+), 2 deletions(-)
diff --git a/lib/bridgedb/test/test_HTTPServer.py b/lib/bridgedb/test/test_HTTPServer.py
index 119d2fc..05e041c 100644
--- a/lib/bridgedb/test/test_HTTPServer.py
+++ b/lib/bridgedb/test/test_HTTPServer.py
@@ -11,8 +11,11 @@
"""Unittests for :mod:`bridgedb.HTTPServer`."""
+from __future__ import print_function
+
import logging
import os
+import random
import shutil
import ipaddr
@@ -354,7 +357,6 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase):
self.request.addArg('captcha_response_field', '')
page = self.captchaResource.render_POST(self.request)
- print page
self.assertEqual(BeautifulSoup(page).find('meta')['http-equiv'],
'refresh')
@@ -370,6 +372,49 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase):
self.request.addArg('captcha_response_field', expectedResponse)
page = self.captchaResource.render_POST(self.request)
- print page
self.assertEqual(BeautifulSoup(page).find('meta')['http-equiv'],
'refresh')
+
+
+class DummyBridge(object):
+ """A mock :class:`bridgedb.Bridges.Bridge` which only supports a mocked
+ ``getConfigLine`` method."""
+
+ def _randORPort(self): return random.randint(9001, 9999)
+ def _randPTPort(self): return random.randint(6001, 6666)
+ def _returnFour(self): return random.randint(2**24, 2**32-1)
+ def _returnSix(self): return random.randint(2**24, 2**128-1)
+
+ def __init__(self, transports=[]):
+ """Create a mocked bridge suitable for testing distributors and web
+ resource rendering.
+ """
+ self.nickname = "bridge-{0}".format(self._returnFour())
+ self.ip = ipaddr.IPv4Address(self._returnFour())
+ self.orport = self._randORPort()
+ self.transports = transports
+ self.running = True
+ self.stable = True
+ self.blockingCountries = {}
+ self.desc_digest = None
+ self.ei_digest = None
+ self.verified = False
+ self.fingerprint = "".join(random.choice('abcdef0123456789')
+ for _ in xrange(40))
+ self.or_addresses = {ipaddr.IPv6Address(self._returnSix()):
+ self._randORPort()}
+
+ def getConfigLine(self, includeFingerprint=True,
+ addressClass=ipaddr.IPv4Address,
+ transport=None,
+ request=None):
+ """Get a "torrc" bridge config line to give to a client."""
+ line = []
+ if transport is not None:
+ #print("getConfigLine got transport=%r" % transport)
+ line.append(str(transport))
+ line.append("%s:%s" % (self.ip, self.orport))
+ if includeFingerprint is True: line.append(self.fingerprint)
+ bridgeLine = " ".join([item for item in line])
+ #print "Created config line: %r" % bridgeLine
+ return bridgeLine
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits