Add test_upload_non_writable_dir, test_public_upload, and test_public_upload_non_writable_dir

This commit is contained in:
Micah Lee 2019-11-10 13:33:01 -08:00
parent 350325aafe
commit 413baba376
2 changed files with 70 additions and 23 deletions

View File

@ -73,6 +73,7 @@ class GuiBaseTest(unittest.TestCase):
def verify_new_tab(self, tab): def verify_new_tab(self, tab):
# Make sure the new tab widget is showing, and no mode has been started # Make sure the new tab widget is showing, and no mode has been started
QtTest.QTest.qWait(500)
self.assertTrue(tab.new_tab.isVisible()) self.assertTrue(tab.new_tab.isVisible())
self.assertFalse(hasattr(tab, "share_mode")) self.assertFalse(hasattr(tab, "share_mode"))
self.assertFalse(hasattr(tab, "receive_mode")) self.assertFalse(hasattr(tab, "receive_mode"))

View File

@ -1,6 +1,7 @@
import pytest import pytest
import os import os
import requests import requests
import shutil
from datetime import datetime, timedelta from datetime import datetime, timedelta
from PyQt5 import QtCore, QtTest from PyQt5 import QtCore, QtTest
@ -16,18 +17,18 @@ class TestReceive(GuiBaseTest):
): ):
"""Test that we can upload the file""" """Test that we can upload the file"""
# Wait 2 seconds to make sure the filename, based on timestamp, isn't accidentally reused # Wait 1.1 seconds to make sure the filename, based on timestamp, isn't accidentally reused
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(1100)
files = {"file[]": open(file_to_upload, "rb")} files = {"file[]": open(file_to_upload, "rb")}
url = f"http://127.0.0.1:{tab.app.port}/upload" url = f"http://127.0.0.1:{tab.app.port}/upload"
if tab.settings.get("general", "public"): if tab.settings.get("general", "public"):
r = requests.post(url, files=files) requests.post(url, files=files)
if identical_files_at_once: if identical_files_at_once:
# Send a duplicate upload to test for collisions # Send a duplicate upload to test for collisions
r = requests.post(url, files=files) requests.post(url, files=files)
else: else:
r = requests.post( requests.post(
url, url,
files=files, files=files,
auth=requests.auth.HTTPBasicAuth( auth=requests.auth.HTTPBasicAuth(
@ -36,7 +37,7 @@ class TestReceive(GuiBaseTest):
) )
if identical_files_at_once: if identical_files_at_once:
# Send a duplicate upload to test for collisions # Send a duplicate upload to test for collisions
r = requests.post( requests.post(
url, url,
files=files, files=files,
auth=requests.auth.HTTPBasicAuth( auth=requests.auth.HTTPBasicAuth(
@ -44,12 +45,12 @@ class TestReceive(GuiBaseTest):
), ),
) )
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(500)
# Make sure the file is within the last 10 seconds worth of fileames # Make sure the file is within the last 10 seconds worth of fileames
exists = False exists = False
now = datetime.now() now = datetime.now()
for i in range(10): for _ in range(10):
date_dir = now.strftime("%Y-%m-%d") date_dir = now.strftime("%Y-%m-%d")
if identical_files_at_once: if identical_files_at_once:
time_dir = now.strftime("%H.%M.%S-1") time_dir = now.strftime("%H.%M.%S-1")
@ -81,12 +82,13 @@ class TestReceive(GuiBaseTest):
), ),
) )
QtCore.QTimer.singleShot(1000, self.accept_dialog) def accept_dialog():
self.assertTrue("Error uploading, please inform the OnionShare user" in r.text) window = tab.common.gui.qtapp.activeWindow()
if window:
window.close()
def upload_dir_permissions(self, mode=0o755): QtCore.QTimer.singleShot(200, accept_dialog)
"""Manipulate the permissions on the upload dir in between tests""" self.assertTrue("Error uploading, please inform the OnionShare user" in r.text)
os.chmod("/tmp/OnionShare", mode)
def try_without_auth_in_non_public_mode(self, tab): def try_without_auth_in_non_public_mode(self, tab):
r = requests.post(f"http://127.0.0.1:{tab.app.port}/upload") r = requests.post(f"http://127.0.0.1:{tab.app.port}/upload")
@ -136,16 +138,6 @@ class TestReceive(GuiBaseTest):
self.server_is_started(tab) self.server_is_started(tab)
self.history_indicator(tab, "2") self.history_indicator(tab, "2")
def run_all_receive_mode_unwritable_dir_tests(self, tab):
"""Attempt to upload (unwritable) files in receive mode and stop the share"""
self.run_all_receive_mode_setup_tests(tab)
self.upload_dir_permissions(0o400)
self.upload_file_should_fail(tab)
self.server_is_stopped(tab, True)
self.web_server_is_stopped(tab)
self.server_status_indicator_says_closed(tab, False)
self.upload_dir_permissions(0o755)
def run_all_clear_all_button_tests(self, tab): def run_all_clear_all_button_tests(self, tab):
"""Test the Clear All history button""" """Test the Clear All history button"""
self.run_all_receive_mode_setup_tests(tab) self.run_all_receive_mode_setup_tests(tab)
@ -155,6 +147,24 @@ class TestReceive(GuiBaseTest):
self.upload_file(tab, self.tmpfile_test, "test.txt") self.upload_file(tab, self.tmpfile_test, "test.txt")
self.clear_all_history_items(tab, 2) self.clear_all_history_items(tab, 2)
def run_all_upload_non_writable_dir_tests(self, tab):
"""Test uploading a file when the data_dir is non-writable"""
upload_dir = os.path.join(self.tmpdir.name, "OnionShare")
shutil.rmtree(upload_dir, ignore_errors=True)
os.makedirs(upload_dir, 0o700)
# Set the upload dir setting
tab.get_mode().data_dir_lineedit.setText(upload_dir)
tab.settings.set("receive", "data_dir", upload_dir)
self.run_all_receive_mode_setup_tests(tab)
os.chmod(upload_dir, 0o400)
self.upload_file_should_fail(tab)
self.server_is_stopped(tab)
self.web_server_is_stopped(tab)
self.server_status_indicator_says_closed(tab)
os.chmod(upload_dir, 0o700)
# Tests # Tests
@pytest.mark.gui @pytest.mark.gui
@ -198,3 +208,39 @@ class TestReceive(GuiBaseTest):
self.run_all_receive_mode_tests(tab) self.run_all_receive_mode_tests(tab)
self.close_all_tabs() self.close_all_tabs()
@pytest.mark.gui
def test_upload_non_writable_dir(self):
"""
Test uploading files to a non-writable directory
"""
tab = self.new_receive_tab()
self.run_all_upload_non_writable_dir_tests(tab)
self.close_all_tabs()
@pytest.mark.gui
def test_public_upload(self):
"""
Test uploading files in public mode
"""
tab = self.new_receive_tab()
tab.get_mode().mode_settings_widget.public_checkbox.click()
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(tab)
self.close_all_tabs()
@pytest.mark.gui
def test_public_upload_non_writable_dir(self):
"""
Test uploading files to a non-writable directory in public mode
"""
tab = self.new_receive_tab()
tab.get_mode().mode_settings_widget.public_checkbox.click()
self.run_all_upload_non_writable_dir_tests(tab)
self.close_all_tabs()