Test webhook in CLI

This commit is contained in:
Micah Lee 2021-04-12 09:34:12 -07:00
parent 839b23f87a
commit 09975fb0f1
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73

View File

@ -4,6 +4,7 @@ import re
import zipfile import zipfile
import tempfile import tempfile
import base64 import base64
from io import BytesIO
import pytest import pytest
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
@ -12,6 +13,21 @@ from onionshare_cli.common import Common
from onionshare_cli.web import Web from onionshare_cli.web import Web
from onionshare_cli.settings import Settings from onionshare_cli.settings import Settings
from onionshare_cli.mode_settings import ModeSettings from onionshare_cli.mode_settings import ModeSettings
import onionshare_cli.web.receive_mode
# Stub requests.post, for receive mode webhook tests
webhook_url = None
webhook_data = None
def requests_post_stub(url, data, timeout, proxies):
global webhook_url, webhook_data
webhook_url = url
webhook_data = data
onionshare_cli.web.receive_mode.requests.post = requests_post_stub
DEFAULT_ZW_FILENAME_REGEX = re.compile(r"^onionshare_[a-z2-7]{6}.zip$") DEFAULT_ZW_FILENAME_REGEX = re.compile(r"^onionshare_[a-z2-7]{6}.zip$")
RANDOM_STR_REGEX = re.compile(r"^[a-z2-7]+$") RANDOM_STR_REGEX = re.compile(r"^[a-z2-7]+$")
@ -129,6 +145,38 @@ class TestWeb:
res.get_data() res.get_data()
assert res.status_code == 200 assert res.status_code == 200
def test_receive_mode_webhook(self, temp_dir, common_obj):
global webhook_url, webhook_data
webhook_url = None
webhook_data = None
web = web_obj(temp_dir, common_obj, "receive")
assert web.mode == "receive"
web.settings.set("receive", "webhook_url", "http://127.0.0.1:1337/example")
web.proxies = None
assert (
web.settings.get("receive", "webhook_url")
== "http://127.0.0.1:1337/example"
)
with web.app.test_client() as c:
res = c.get("/", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
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),
)
res.get_data()
assert res.status_code == 200
assert webhook_url == "http://127.0.0.1:1337/example"
assert webhook_data == "1 file uploaded to OnionShare"
def test_public_mode_on(self, temp_dir, common_obj): def test_public_mode_on(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive") web = web_obj(temp_dir, common_obj, "receive")
web.settings.set("general", "public", True) web.settings.set("general", "public", True)