Write receive mode CLI tests to confirm messages and files get uploaded, and when no files are uploaded an empty folder is not created

This commit is contained in:
Micah Lee 2021-05-02 12:42:56 -07:00
parent ce6b28dca3
commit 8b9d9fcf46
No known key found for this signature in database
GPG key ID: 403C2657CD994F73

View file

@ -7,6 +7,7 @@ import time
import zipfile
import tempfile
import base64
import shutil
from io import BytesIO
import pytest
@ -185,6 +186,134 @@ class TestWeb:
assert webhook_url == "http://127.0.0.1:1337/example"
assert webhook_data == "1 file submitted to OnionShare"
def test_receive_mode_message_no_files(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive")
data_dir = os.path.join(temp_dir, "OnionShare")
os.makedirs(data_dir, exist_ok=True)
web.settings.set("receive", "data_dir", data_dir)
with web.app.test_client() as c:
res = c.post(
"/upload-ajax",
buffered=True,
content_type="multipart/form-data",
data={"text": "you know just sending an anonymous message"},
headers=self._make_auth_headers(web.password),
)
content = res.get_data()
assert res.status_code == 200
assert b"Message submitted" in content
# ~/OnionShare should have a folder for the date
filenames = os.listdir(data_dir)
assert len(filenames) == 1
data_dir_date = os.path.join(data_dir, filenames[0])
# The date folder should have a single message txt file, no folders
filenames = os.listdir(data_dir_date)
assert len(filenames) == 1
assert filenames[0].endswith("-message.txt")
shutil.rmtree(data_dir)
def test_receive_mode_message_and_files(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive")
data_dir = os.path.join(temp_dir, "OnionShare")
os.makedirs(data_dir, exist_ok=True)
web.settings.set("receive", "data_dir", data_dir)
with web.app.test_client() as c:
res = c.post(
"/upload-ajax",
buffered=True,
content_type="multipart/form-data",
data={
"file[]": (BytesIO(b"THIS IS A TEST FILE"), "new_york.jpg"),
"text": "you know just sending an anonymous message",
},
headers=self._make_auth_headers(web.password),
)
content = res.get_data()
assert res.status_code == 200
assert b"Message submitted, uploaded new_york.jpg" in content
# Date folder should have a time folder with new_york.jpg, and a text message file
data_dir_date = os.path.join(data_dir, os.listdir(data_dir)[0])
filenames = os.listdir(data_dir_date)
assert len(filenames) == 2
time_str = filenames[0][0:6]
assert time_str in filenames
assert f"{time_str}-message.txt" in filenames
data_dir_time = os.path.join(data_dir_date, time_str)
assert os.path.isdir(data_dir_time)
assert os.path.exists(os.path.join(data_dir_time, "new_york.jpg"))
shutil.rmtree(data_dir)
def test_receive_mode_files_no_message(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive")
data_dir = os.path.join(temp_dir, "OnionShare")
os.makedirs(data_dir, exist_ok=True)
web.settings.set("receive", "data_dir", data_dir)
with web.app.test_client() as c:
res = c.post(
"/upload-ajax",
buffered=True,
content_type="multipart/form-data",
data={"file[]": (BytesIO(b"THIS IS A TEST FILE"), "new_york.jpg")},
headers=self._make_auth_headers(web.password),
)
content = res.get_data()
assert res.status_code == 200
assert b"Uploaded new_york.jpg" in content
# Date folder should have just a time folder with new_york.jpg
data_dir_date = os.path.join(data_dir, os.listdir(data_dir)[0])
filenames = os.listdir(data_dir_date)
assert len(filenames) == 1
time_str = filenames[0][0:6]
assert time_str in filenames
assert f"{time_str}-message.txt" not in filenames
data_dir_time = os.path.join(data_dir_date, time_str)
assert os.path.isdir(data_dir_time)
assert os.path.exists(os.path.join(data_dir_time, "new_york.jpg"))
shutil.rmtree(data_dir)
def test_receive_mode_no_message_no_files(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive")
data_dir = os.path.join(temp_dir, "OnionShare")
os.makedirs(data_dir, exist_ok=True)
web.settings.set("receive", "data_dir", data_dir)
with web.app.test_client() as c:
res = c.post(
"/upload-ajax",
buffered=True,
content_type="multipart/form-data",
data={},
headers=self._make_auth_headers(web.password),
)
content = res.get_data()
assert res.status_code == 200
assert b"Nothing submitted" in content
# Date folder should be empty
data_dir_date = os.path.join(data_dir, os.listdir(data_dir)[0])
filenames = os.listdir(data_dir_date)
assert len(filenames) == 0
shutil.rmtree(data_dir)
def test_public_mode_on(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive")
web.settings.set("general", "public", True)