Add website tests

This commit is contained in:
Micah Lee 2019-11-10 13:51:08 -08:00
parent 817118572f
commit 2c51f1fefb
3 changed files with 128 additions and 15 deletions

View file

@ -55,6 +55,13 @@ class GuiBaseTest(unittest.TestCase):
with open(cls.tmpfile_test2, "w") as file: with open(cls.tmpfile_test2, "w") as file:
file.write("onionshare2") file.write("onionshare2")
# A file called "index.html"
cls.tmpfile_index_html = os.path.join(cls.tmpdir.name, "index.html")
with open(cls.tmpfile_index_html, "w") as file:
file.write(
"<html><body><p>This is a test website hosted by OnionShare</p></body></html>"
)
# A large file # A large file
size = 1024 * 1024 * 155 size = 1024 * 1024 * 155
cls.tmpfile_large = os.path.join(cls.tmpdir.name, "large_file") cls.tmpfile_large = os.path.join(cls.tmpdir.name, "large_file")
@ -374,6 +381,21 @@ class GuiBaseTest(unittest.TestCase):
tab.get_mode().history.clear_button.click() tab.get_mode().history.clear_button.click()
self.assertEqual(len(tab.get_mode().history.item_list.items.keys()), count) self.assertEqual(len(tab.get_mode().history.item_list.items.keys()), count)
def file_selection_widget_has_files(self, tab, num=3):
"""Test that the number of items in the list is as expected"""
self.assertEqual(
tab.get_mode().server_status.file_selection.get_num_files(), num
)
def add_delete_buttons_hidden(self, tab):
"""Test that the add and delete buttons are hidden when the server starts"""
self.assertFalse(
tab.get_mode().server_status.file_selection.add_button.isVisible()
)
self.assertFalse(
tab.get_mode().server_status.file_selection.delete_button.isVisible()
)
# Auto-stop timer tests # Auto-stop timer tests
def set_timeout(self, tab, timeout): def set_timeout(self, tab, timeout):
"""Test that the timeout can be set""" """Test that the timeout can be set"""

View file

@ -12,12 +12,6 @@ from .gui_base_test import GuiBaseTest
class TestShare(GuiBaseTest): class TestShare(GuiBaseTest):
# Shared test methods # Shared test methods
def file_selection_widget_has_files(self, tab, num=3):
"""Test that the number of items in the list is as expected"""
self.assertEqual(
tab.get_mode().server_status.file_selection.get_num_files(), num
)
def deleting_all_files_hides_delete_button(self, tab): def deleting_all_files_hides_delete_button(self, tab):
"""Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button""" """Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button"""
rect = tab.get_mode().server_status.file_selection.file_list.visualItemRect( rect = tab.get_mode().server_status.file_selection.file_list.visualItemRect(
@ -68,15 +62,6 @@ class TestShare(GuiBaseTest):
tab.get_mode().server_status.file_selection.file_list.add_file(self.tmpfiles[1]) tab.get_mode().server_status.file_selection.file_list.add_file(self.tmpfiles[1])
self.file_selection_widget_has_files(tab, num_files + 2) self.file_selection_widget_has_files(tab, num_files + 2)
def add_delete_buttons_hidden(self, tab):
"""Test that the add and delete buttons are hidden when the server starts"""
self.assertFalse(
tab.get_mode().server_status.file_selection.add_button.isVisible()
)
self.assertFalse(
tab.get_mode().server_status.file_selection.delete_button.isVisible()
)
def download_share(self, tab): def download_share(self, tab):
"""Test that we can download the share""" """Test that we can download the share"""
url = f"http://127.0.0.1:{tab.app.port}/download" url = f"http://127.0.0.1:{tab.app.port}/download"

106
tests2/test_gui_website.py Normal file
View file

@ -0,0 +1,106 @@
import pytest
import os
import requests
import shutil
from datetime import datetime, timedelta
from PyQt5 import QtCore, QtTest
from .gui_base_test import GuiBaseTest
class TestWebsite(GuiBaseTest):
# Shared test methods
def view_website(self, tab):
"""Test that we can download the share"""
url = f"http://127.0.0.1:{tab.app.port}/"
if tab.settings.get("general", "public"):
r = requests.get(url)
else:
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", tab.get_mode().server_status.web.password
),
)
QtTest.QTest.qWait(500)
self.assertTrue("This is a test website hosted by OnionShare" in r.text)
def check_csp_header(self, tab):
"""Test that the CSP header is present when enabled or vice versa"""
url = f"http://127.0.0.1:{tab.app.port}/"
if tab.settings.get("general", "public"):
r = requests.get(url)
else:
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", tab.get_mode().server_status.web.password
),
)
QtTest.QTest.qWait(500)
if tab.settings.get("website", "disable_csp"):
self.assertFalse("Content-Security-Policy" in r.headers)
else:
self.assertTrue("Content-Security-Policy" in r.headers)
def run_all_website_mode_setup_tests(self, tab):
"""Tests in website mode prior to starting a share"""
tab.get_mode().server_status.file_selection.file_list.add_file(
self.tmpfile_index_html
)
for filename in self.tmpfiles:
tab.get_mode().server_status.file_selection.file_list.add_file(filename)
self.file_selection_widget_has_files(tab, 11)
self.history_is_not_visible(tab)
self.click_toggle_history(tab)
self.history_is_visible(tab)
def run_all_website_mode_started_tests(self, tab, startup_time=500):
"""Tests in website mode after starting a share"""
self.server_working_on_start_button_pressed(tab)
self.server_status_indicator_says_starting(tab)
self.add_delete_buttons_hidden(tab)
self.server_is_started(tab, startup_time)
self.web_server_is_running(tab)
self.have_a_password(tab)
self.url_description_shown(tab)
self.have_copy_url_button(tab)
self.server_status_indicator_says_started(tab)
def run_all_website_mode_download_tests(self, tab):
"""Tests in website mode after viewing the site"""
self.run_all_website_mode_setup_tests(tab)
self.run_all_website_mode_started_tests(tab, startup_time=500)
self.view_website(tab)
self.check_csp_header(tab)
self.history_widgets_present(tab)
self.server_is_stopped(tab)
self.web_server_is_stopped(tab)
self.server_status_indicator_says_closed(tab)
self.add_button_visible(tab)
# Tests
@pytest.mark.gui
def test_website(self):
"""
Test website mode
"""
tab = self.new_website_tab()
self.run_all_website_mode_download_tests(tab)
self.close_all_tabs()
@pytest.mark.gui
def test_csp_enabled(self):
"""
Test disabling CSP
"""
tab = self.new_website_tab()
tab.get_mode().disable_csp_checkbox.click()
self.run_all_website_mode_download_tests(tab)
self.close_all_tabs()