allow "key" to be passed as a parameter

This was initially designed and implemented in #4

Co-authored-by: Sven Seeberg <mail@sven-seeberg.de>
This commit is contained in:
Guilhem Saurel 2021-07-31 11:31:31 +02:00
parent 292d77274d
commit fa8f9b4a51
4 changed files with 24 additions and 6 deletions

View file

@ -25,12 +25,17 @@ parser.add_argument(
)
def bot_req(req=None, key=None, room_id=None):
def bot_req(req=None, key=None, room_id=None, key_as_param=False):
"""Bot requests boilerplate."""
params = {}
if key is not None:
req["key"] = key
if key_as_param:
params["key"] = key
else:
req["key"] = key
url = BOT_URL if room_id is None else f"{BOT_URL}/{room_id}"
return httpx.post(url, json=req).json()
return httpx.post(url, params=params, json=req).json()
def wait_available(url: str, key: str, timeout: int = 10) -> bool:

View file

@ -18,13 +18,21 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
{"status": 400, "ret": "Missing body and/or API key property"},
)
self.assertEqual(
bot_req({"body": 3, "key": None}), {"status": 401, "ret": "Invalid API key"}
bot_req({"body": 3}, "wrong_key"), {"status": 401, "ret": "Invalid API key"}
)
self.assertEqual(
bot_req({"body": 3}, "wrong_key", key_as_param=True),
{"status": 401, "ret": "Invalid API key"},
)
# TODO: if the client from matrix_webhook has olm support, this won't be a 403 from synapse,
# but a LocalProtocolError from matrix_webhook
self.assertEqual(
bot_req({"body": 3}, KEY), {"status": 403, "ret": "Unknown room"}
)
self.assertEqual(
bot_req({"body": 3}, KEY, key_as_param=True),
{"status": 403, "ret": "Unknown room"},
)
async def test_message(self):
"""Send a markdown message with the old format, and check the result."""