| 1 |
import json |
| 2 |
import logging |
| 3 |
import os |
| 4 |
import socket |
| 5 |
from time import sleep |
| 6 |
|
| 7 |
import pytest |
| 8 |
import requests |
| 9 |
|
| 10 |
from errbot.backends.test import FullStackTest, testbot |
| 11 |
|
| 12 |
log = logging.getLogger(__name__) |
| 13 |
|
| 14 |
PYTHONOBJECT = ["foo", {"bar": ("baz", None, 1.0, 2)}] |
| 15 |
JSONOBJECT = json.dumps(PYTHONOBJECT) |
| 16 |
# Webserver port is picked based on the process ID so that when tests |
| 17 |
# are run in parallel with pytest-xdist, each process runs the server |
| 18 |
# on a different port |
| 19 |
WEBSERVER_PORT = 5000 + (os.getpid() % 1000) |
| 20 |
WEBSERVER_SSL_PORT = WEBSERVER_PORT + 1000 |
| 21 |
|
| 22 |
|
| 23 |
def webserver_ready(host, port): |
| 24 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 25 |
try: |
| 26 |
s.connect((host, port)) |
| 27 |
s.shutdown(socket.SHUT_RDWR) |
| 28 |
s.close() |
| 29 |
return True |
| 30 |
except Exception: |
| 31 |
return False |
| 32 |
|
| 33 |
|
| 34 |
extra_plugin_dir = os.path.join( |
| 35 |
os.path.dirname(os.path.realpath(__file__)), "webhooks_plugin" |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def wait_for_server(port: int): |
| 40 |
failure_count = 10 |
| 41 |
while not webserver_ready("localhost", port): |
| 42 |
waiting_time = 1.0 / failure_count |
| 43 |
log.info("Webserver not ready yet, sleeping for %f second.", waiting_time) |
| 44 |
sleep(waiting_time) |
| 45 |
failure_count -= 1 |
| 46 |
if failure_count == 0: |
| 47 |
raise TimeoutError("Could not start the internal Webserver to test.") |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def webhook_testbot(request, testbot): |
| 52 |
testbot.push_message( |
| 53 |
"!plugin config Webserver {'HOST': 'localhost', 'PORT': %s, 'SSL': None}" |
| 54 |
% WEBSERVER_PORT |
| 55 |
) |
| 56 |
log.info(testbot.pop_message()) |
| 57 |
wait_for_server(WEBSERVER_PORT) |
| 58 |
return testbot |
| 59 |
|
| 60 |
|
| 61 |
def test_not_configured_url_returns_404(webhook_testbot): |
| 62 |
assert ( |
| 63 |
requests.post( |
| 64 |
"http://localhost:{}/randomness_blah".format(WEBSERVER_PORT), |
| 65 |
"{'toto': 'titui'}", |
| 66 |
).status_code |
| 67 |
== 404 |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def test_webserver_plugin_ok(webhook_testbot): |
| 72 |
assert "/echo" in webhook_testbot.exec_command("!webstatus") |
| 73 |
|
| 74 |
|
| 75 |
def test_trailing_no_slash_ok(webhook_testbot): |
| 76 |
assert requests.post( |
| 77 |
"http://localhost:{}/echo".format(WEBSERVER_PORT), JSONOBJECT |
| 78 |
).text == repr(json.loads(JSONOBJECT)) |
| 79 |
|
| 80 |
|
| 81 |
def test_trailing_slash_also_ok(webhook_testbot): |
| 82 |
assert requests.post( |
| 83 |
"http://localhost:{}/echo/".format(WEBSERVER_PORT), JSONOBJECT |
| 84 |
).text == repr(json.loads(JSONOBJECT)) |
| 85 |
|
| 86 |
|
| 87 |
def test_json_is_automatically_decoded(webhook_testbot): |
| 88 |
assert requests.post( |
| 89 |
"http://localhost:{}/webhook1".format(WEBSERVER_PORT), JSONOBJECT |
| 90 |
).text == repr(json.loads(JSONOBJECT)) |
| 91 |
|
| 92 |
|
| 93 |
def test_json_on_custom_url_is_automatically_decoded(webhook_testbot): |
| 94 |
assert requests.post( |
| 95 |
"http://localhost:{}/custom_webhook".format(WEBSERVER_PORT), JSONOBJECT |
| 96 |
).text == repr(json.loads(JSONOBJECT)) |
| 97 |
|
| 98 |
|
| 99 |
def test_post_form_on_webhook_without_form_param_is_automatically_decoded( |
| 100 |
webhook_testbot, |
| 101 |
): |
| 102 |
assert requests.post( |
| 103 |
"http://localhost:{}/webhook1".format(WEBSERVER_PORT), data=JSONOBJECT |
| 104 |
).text == repr(json.loads(JSONOBJECT)) |
| 105 |
|
| 106 |
|
| 107 |
def test_post_form_on_webhook_with_custom_url_and_without_form_param_is_automatically_decoded( |
| 108 |
webhook_testbot, |
| 109 |
): |
| 110 |
assert requests.post( |
| 111 |
"http://localhost:{}/custom_webhook".format(WEBSERVER_PORT), data=JSONOBJECT |
| 112 |
).text == repr(json.loads(JSONOBJECT)) |
| 113 |
|
| 114 |
|
| 115 |
def test_webhooks_with_form_parameter_decode_json_automatically(webhook_testbot): |
| 116 |
form = {"form": JSONOBJECT} |
| 117 |
assert requests.post( |
| 118 |
"http://localhost:{}/form".format(WEBSERVER_PORT), data=form |
| 119 |
).text == repr(json.loads(JSONOBJECT)) |
| 120 |
|
| 121 |
|
| 122 |
def test_webhooks_with_form_parameter_on_custom_url_decode_json_automatically( |
| 123 |
webhook_testbot, |
| 124 |
): |
| 125 |
form = {"form": JSONOBJECT} |
| 126 |
assert requests.post( |
| 127 |
"http://localhost:{}/custom_form".format(WEBSERVER_PORT), data=form |
| 128 |
).text, repr(json.loads(JSONOBJECT)) |
| 129 |
|
| 130 |
|
| 131 |
def test_webhooks_with_raw_request(webhook_testbot): |
| 132 |
form = {"form": JSONOBJECT} |
| 133 |
assert ( |
| 134 |
"LocalProxy" |
| 135 |
in requests.post( |
| 136 |
"http://localhost:{}/raw".format(WEBSERVER_PORT), data=form |
| 137 |
).text |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
def test_webhooks_with_naked_decorator_raw_request(webhook_testbot): |
| 142 |
form = {"form": JSONOBJECT} |
| 143 |
assert ( |
| 144 |
"LocalProxy" |
| 145 |
in requests.post( |
| 146 |
"http://localhost:{}/raw2".format(WEBSERVER_PORT), data=form |
| 147 |
).text |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def test_generate_certificate_creates_usable_cert(webhook_testbot): |
| 152 |
d = webhook_testbot.bot.bot_config.BOT_DATA_DIR |
| 153 |
key_path = os.sep.join((d, "webserver_key.pem")) |
| 154 |
cert_path = os.sep.join((d, "webserver_certificate.pem")) |
| 155 |
|
| 156 |
assert "Generating" in webhook_testbot.exec_command( |
| 157 |
"!generate_certificate", timeout=1 |
| 158 |
) |
| 159 |
|
| 160 |
# Generating a certificate could be slow on weak hardware, so keep a safe |
| 161 |
# timeout on the first pop_message() |
| 162 |
assert "successfully generated" in webhook_testbot.pop_message(timeout=60) |
| 163 |
assert "is recommended" in webhook_testbot.pop_message(timeout=1) |
| 164 |
assert key_path in webhook_testbot.pop_message(timeout=1) |
| 165 |
|
| 166 |
webserver_config = { |
| 167 |
"HOST": "localhost", |
| 168 |
"PORT": WEBSERVER_PORT, |
| 169 |
"SSL": { |
| 170 |
"certificate": cert_path, |
| 171 |
"key": key_path, |
| 172 |
"host": "localhost", |
| 173 |
"port": WEBSERVER_SSL_PORT, |
| 174 |
"enabled": True, |
| 175 |
}, |
| 176 |
} |
| 177 |
webhook_testbot.push_message( |
| 178 |
"!plugin config Webserver {!r}".format(webserver_config) |
| 179 |
) |
| 180 |
assert "Plugin configuration done." in webhook_testbot.pop_message(timeout=2) |
| 181 |
|
| 182 |
wait_for_server(WEBSERVER_SSL_PORT) |
| 183 |
|
| 184 |
requests.packages.urllib3.disable_warnings( |
| 185 |
requests.packages.urllib3.exceptions.InsecureRequestWarning |
| 186 |
) |
| 187 |
|
| 188 |
assert requests.post( |
| 189 |
"https://localhost:{}/webhook1".format(WEBSERVER_SSL_PORT), |
| 190 |
JSONOBJECT, |
| 191 |
verify=False, |
| 192 |
).text == repr(json.loads(JSONOBJECT)) |
| 193 |
|
| 194 |
|
| 195 |
def test_custom_headers_and_status_codes(webhook_testbot): |
| 196 |
assert ( |
| 197 |
requests.post("http://localhost:{}/webhook6".format(WEBSERVER_PORT)).headers[ |
| 198 |
"X-Powered-By" |
| 199 |
] |
| 200 |
== "Errbot" |
| 201 |
) |
| 202 |
|
| 203 |
assert ( |
| 204 |
requests.post("http://localhost:{}/webhook7".format(WEBSERVER_PORT)).status_code |
| 205 |
== 403 |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
def test_lambda_webhook(webhook_testbot): |
| 210 |
assert ( |
| 211 |
requests.post("http://localhost:{}/lambda".format(WEBSERVER_PORT)).status_code |
| 212 |
== 200 |
| 213 |
) |