join room: fix error code & unit tests

This commit is contained in:
Guilhem Saurel 2021-09-28 10:39:41 +02:00
parent 7ffa47c267
commit d2a3e618f4
2 changed files with 11 additions and 8 deletions

View file

@ -15,6 +15,13 @@ LOGGER = logging.getLogger("matrix_webhook.utils")
CLIENT = AsyncClient(conf.MATRIX_URL, conf.MATRIX_ID) CLIENT = AsyncClient(conf.MATRIX_URL, conf.MATRIX_ID)
def error_map(resp):
"""Map response errors to HTTP status."""
if resp.status_code == "M_UNKNOWN":
return resp.transport_response.status
return ERROR_MAP[resp.status_code]
def create_json_response(status, ret): def create_json_response(status, ret):
"""Create a JSON response.""" """Create a JSON response."""
LOGGER.debug(f"Creating json response: {status=}, {ret=}") LOGGER.debug(f"Creating json response: {status=}, {ret=}")
@ -34,9 +41,7 @@ async def join_room(room_id):
LOGGER.warning("Reconnecting") LOGGER.warning("Reconnecting")
await CLIENT.login(conf.MATRIX_PW) await CLIENT.login(conf.MATRIX_PW)
else: else:
return create_json_response( return create_json_response(error_map(resp), resp.message)
ERROR_MAP[resp.status_code], resp.message
)
else: else:
return None return None
except LocalProtocolError as e: except LocalProtocolError as e:
@ -59,9 +64,7 @@ async def send_room_message(room_id, content):
LOGGER.warning("Reconnecting") LOGGER.warning("Reconnecting")
await CLIENT.login(conf.MATRIX_PW) await CLIENT.login(conf.MATRIX_PW)
else: else:
return create_json_response( return create_json_response(error_map(resp), resp.message)
ERROR_MAP[resp.status_code], resp.message
)
else: else:
return create_json_response(HTTPStatus.OK, "OK") return create_json_response(HTTPStatus.OK, "OK")
except LocalProtocolError as e: except LocalProtocolError as e:

View file

@ -33,11 +33,11 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
# this won't be a 403 from synapse, but a LocalProtocolError from matrix_webhook # this won't be a 403 from synapse, but a LocalProtocolError from matrix_webhook
self.assertEqual( self.assertEqual(
bot_req({"body": 3}, KEY, "wrong_room"), bot_req({"body": 3}, KEY, "wrong_room"),
{"status": 403, "ret": "Unknown room"}, {"status": 400, "ret": "wrong_room was not legal room ID or room alias"},
) )
self.assertEqual( self.assertEqual(
bot_req({"body": 3}, KEY, "wrong_room", key_as_param=True), bot_req({"body": 3}, KEY, "wrong_room", key_as_param=True),
{"status": 403, "ret": "Unknown room"}, {"status": 400, "ret": "wrong_room was not legal room ID or room alias"},
) )
async def test_message(self): async def test_message(self):