[stem/master] Move running_hidden_service test

commit 469a925991acdd3e99cc486215aac673a5f81062 Author: Damian Johnson <atagar@torproject.org> Date: Sun Sep 27 17:11:24 2020 -0700 Move running_hidden_service test --- test/unit/examples.py | 37 +++++++++++++++++++++++-- test/unit/tutorial.py | 76 +-------------------------------------------------- 2 files changed, 36 insertions(+), 77 deletions(-) diff --git a/test/unit/examples.py b/test/unit/examples.py index cadf3048..ef1d9ed9 100644 --- a/test/unit/examples.py +++ b/test/unit/examples.py @@ -155,6 +155,14 @@ Checking for outdated relays... EXPECTED_PERSISTING_A_CONSENSUS = """\ A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB: caerSidi """ + +EXPECTED_RUNNING_HIDDEN_SERVICE = """\ + * Connecting to tor + * Creating our hidden service in /home/atagar/.tor/hello_world + * Our service is available at uxiuaxejc3sxrb6i.onion, press ctrl+c to quit + * Shutting down our hidden service +""" + EXPECTED_VOTES_BY_BANDWIDTH_AUTHORITIES = """\ Getting gabelmoo's vote from http://131.188.40.189:80/tor/status-vote/current/authority: 5935 measured entries and 1332 unmeasured @@ -570,8 +578,33 @@ class TestExamples(unittest.TestCase): def test_resuming_ephemeral_hidden_service(self): pass - def test_running_hidden_service(self): - pass + @patch('stem.control.Controller.from_port', spec = Controller) + @patch('shutil.rmtree') + @patch('sys.stdout', new_callable = io.StringIO) + def test_running_hidden_service(self, stdout_mock, rmtree_mock, from_port_mock): + original_modules = dict(sys.modules) + + try: + flask_mock = Mock() + + hidden_service_data = Mock() + hidden_service_data.hostname = 'uxiuaxejc3sxrb6i.onion' + + controller = from_port_mock().__enter__() + controller.get_conf.return_value = '/home/atagar/.tor' + controller.create_hidden_service.return_value = hidden_service_data + + sys.modules['flask'] = flask_mock + + import running_hidden_service + + controller.get_conf.assert_called_once_with('DataDirectory', '/tmp') + controller.create_hidden_service.assert_called_once_with('/home/atagar/.tor/hello_world', 80, target_port = 5000) + rmtree_mock.assert_called_once_with('/home/atagar/.tor/hello_world') + + self.assertEqual(EXPECTED_RUNNING_HIDDEN_SERVICE, stdout_mock.getvalue()) + finally: + sys.modules = original_modules def test_saving_and_loading_descriptors(self): pass diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py index f35a6b6d..d6b6ee3b 100644 --- a/test/unit/tutorial.py +++ b/test/unit/tutorial.py @@ -7,20 +7,12 @@ import unittest import stem.descriptor.remote -from unittest.mock import Mock, patch +from unittest.mock import patch -from stem.control import Controller from stem.descriptor.router_status_entry import RouterStatusEntryV3 from stem.descriptor.networkstatus import NetworkStatusDocumentV3 from stem.descriptor.server_descriptor import RelayDescriptor -OVER_THE_RIVER_OUTPUT = """\ - * Connecting to tor - * Creating our hidden service in /home/atagar/.tor/hello_world - * Our service is available at uxiuaxejc3sxrb6i.onion, press ctrl+c to quit - * Shutting down our hidden service -""" - MIRROR_MIRROR_OUTPUT = """\ 1. speedyexit (102.13 KB/s) 2. speedyexit (102.13 KB/s) @@ -35,72 +27,6 @@ class TestTutorial(unittest.TestCase): stem.descriptor.remote.SINGLETON_DOWNLOADER = None - @patch('sys.stdout', new_callable = io.StringIO) - @patch('shutil.rmtree') - @patch('stem.control.Controller.from_port', spec = Controller) - def test_over_the_river(self, from_port_mock, rmtree_mock, stdout_mock): - def tutorial_example(app): - import os - import shutil - - from stem.control import Controller - - @app.route('/') - def index(): - return '<h1>Hi Grandma!</h1>' - - print(' * Connecting to tor') - - with Controller.from_port() as controller: - controller.authenticate() - - # All hidden services have a directory on disk. Lets put ours in tor's data - # directory. - - hidden_service_dir = os.path.join(controller.get_conf('DataDirectory', '/tmp'), 'hello_world') - - # Create a hidden service where visitors of port 80 get redirected to local - # port 5000 (this is where flask runs by default). - - print(' * Creating our hidden service in %s' % hidden_service_dir) - result = controller.create_hidden_service(hidden_service_dir, 80, target_port = 5000) - - # The hostname is only available we can read the hidden service directory. - # This requires us to be running with the same user as tor. - - if result.hostname: - print(' * Our service is available at %s, press ctrl+c to quit' % result.hostname) - else: - print(" * Unable to determine our service's hostname, probably due to being unable to read the hidden service directory") - - try: - app.run() - finally: - # Shut down the hidden service and clean it off disk. Note that you *don't* - # want to delete the hidden service directory if you'd like to have this - # same *.onion address in the future. - - print(' * Shutting down our hidden service') - controller.remove_hidden_service(hidden_service_dir) - shutil.rmtree(hidden_service_dir) - - flask_mock = Mock() - - hidden_service_data = Mock() - hidden_service_data.hostname = 'uxiuaxejc3sxrb6i.onion' - - controller = from_port_mock().__enter__() - controller.get_conf.return_value = '/home/atagar/.tor' - controller.create_hidden_service.return_value = hidden_service_data - - tutorial_example(flask_mock) - - controller.get_conf.assert_called_once_with('DataDirectory', '/tmp') - controller.create_hidden_service.assert_called_once_with('/home/atagar/.tor/hello_world', 80, target_port = 5000) - rmtree_mock.assert_called_once_with('/home/atagar/.tor/hello_world') - - self.assertEqual(OVER_THE_RIVER_OUTPUT, stdout_mock.getvalue()) - @patch('sys.stdout', new_callable = io.StringIO) @patch('%s.open' % __name__, create = True) def test_mirror_mirror_on_the_wall_3(self, open_mock, stdout_mock):
participants (1)
-
atagar@torproject.org