From 5defd4a10b47a4934c7585b86b41b45e7aa39fbb Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 11:51:59 +1000 Subject: [PATCH] Add a basic website test --- tests/GuiBaseTest.py | 4 + tests/GuiWebsiteTest.py | 98 +++++++++++++++++++++ tests/local_onionshare_website_mode_test.py | 25 ++++++ 3 files changed, 127 insertions(+) create mode 100644 tests/GuiWebsiteTest.py create mode 100644 tests/local_onionshare_website_mode_test.py diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index 2f340396..d2a43362 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -14,6 +14,7 @@ from onionshare.web import Web from onionshare_gui import Application, OnionShare, OnionShareGui from onionshare_gui.mode.share_mode import ShareMode from onionshare_gui.mode.receive_mode import ReceiveMode +from onionshare_gui.mode.website_mode import WebsiteMode class GuiBaseTest(object): @@ -103,6 +104,9 @@ class GuiBaseTest(object): if type(mode) == ShareMode: QtTest.QTest.mouseClick(self.gui.share_mode_button, QtCore.Qt.LeftButton) self.assertTrue(self.gui.mode, self.gui.MODE_SHARE) + if type(mode) == WebsiteMode: + QtTest.QTest.mouseClick(self.gui.website_mode_button, QtCore.Qt.LeftButton) + self.assertTrue(self.gui.mode, self.gui.MODE_WEBSITE) def click_toggle_history(self, mode): diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py new file mode 100644 index 00000000..1f0eb310 --- /dev/null +++ b/tests/GuiWebsiteTest.py @@ -0,0 +1,98 @@ +import json +import os +import requests +import socks +import zipfile +import tempfile +from PyQt5 import QtCore, QtTest +from onionshare import strings +from onionshare.common import Common +from onionshare.settings import Settings +from onionshare.onion import Onion +from onionshare.web import Web +from onionshare_gui import Application, OnionShare, OnionShareGui +from onionshare_gui.mode.website_mode import WebsiteMode +from .GuiShareTest import GuiShareTest + +class GuiWebsiteTest(GuiShareTest): + @staticmethod + def set_up(test_settings): + '''Create GUI with given settings''' + # Create our test file + testfile = open('/tmp/index.html', 'w') + testfile.write('This is a test website hosted by OnionShare') + testfile.close() + + common = Common() + common.settings = Settings(common) + common.define_css() + strings.load_strings(common) + + # Get all of the settings in test_settings + test_settings['data_dir'] = '/tmp/OnionShare' + for key, val in common.settings.default_settings.items(): + if key not in test_settings: + test_settings[key] = val + + # Start the Onion + testonion = Onion(common) + global qtapp + qtapp = Application(common) + app = OnionShare(common, testonion, True, 0) + + web = Web(common, False, True) + open('/tmp/settings.json', 'w').write(json.dumps(test_settings)) + + gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/index.html'], '/tmp/settings.json', True) + return gui + + @staticmethod + def tear_down(): + '''Clean up after tests''' + try: + os.remove('/tmp/index.html') + os.remove('/tmp/settings.json') + except: + pass + + + def view_website(self, public_mode): + '''Test that we can download the share''' + url = "http://127.0.0.1:{}/".format(self.gui.app.port) + if public_mode: + r = requests.get(url) + else: + r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.website_mode.server_status.web.password)) + + QtTest.QTest.qWait(2000) + self.assertTrue('This is a test website hosted by OnionShare' in r.text) + + def run_all_website_mode_setup_tests(self): + """Tests in website mode prior to starting a share""" + self.click_mode(self.gui.website_mode) + self.file_selection_widget_has_files(1) + self.history_is_not_visible(self.gui.website_mode) + self.click_toggle_history(self.gui.website_mode) + self.history_is_visible(self.gui.website_mode) + + def run_all_website_mode_started_tests(self, public_mode, startup_time=2000): + """Tests in website mode after starting a share""" + self.server_working_on_start_button_pressed(self.gui.website_mode) + self.server_status_indicator_says_starting(self.gui.website_mode) + self.add_delete_buttons_hidden() + self.settings_button_is_hidden() + self.server_is_started(self.gui.website_mode, startup_time) + self.web_server_is_running() + self.have_a_password(self.gui.website_mode, public_mode) + self.url_description_shown(self.gui.website_mode) + self.have_copy_url_button(self.gui.website_mode, public_mode) + self.server_status_indicator_says_started(self.gui.website_mode) + + + def run_all_website_mode_download_tests(self, public_mode, stay_open): + """Tests in website mode after viewing the site""" + self.run_all_website_mode_setup_tests() + self.run_all_website_mode_started_tests(public_mode, startup_time=2000) + self.view_website(public_mode) + self.history_widgets_present(self.gui.website_mode) + diff --git a/tests/local_onionshare_website_mode_test.py b/tests/local_onionshare_website_mode_test.py new file mode 100644 index 00000000..5a6dc10f --- /dev/null +++ b/tests/local_onionshare_website_mode_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiWebsiteTest import GuiWebsiteTest + +class LocalWebsiteModeTest(unittest.TestCase, GuiWebsiteTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = GuiWebsiteTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiWebsiteTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + #self.run_all_common_setup_tests() + self.run_all_website_mode_download_tests(False, False) + +if __name__ == "__main__": + unittest.main()