Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_create_image(self):
event = await self.event_creator.create_event(self.image_json, "hello")
assert isinstance(event, events.Image)
assert event.url == "mxc://aurl"
assert event.user == "Rabbit Hole"
assert event.user_id == "@neo:matrix.org"
assert event.target == "hello"
assert event.event_id == "$15548652221495790FYlHC:matrix.org"
assert event.raw_event == self.image_json
async def test_react(self):
with OpsDroid() as opsdroid:
mock_connector = Connector(
{"name": "shell", "thinking-delay": 2, "type": "connector"},
opsdroid=opsdroid,
)
with amock.patch("asyncio.sleep") as mocksleep:
message = events.Message(
"Hello world", "user_id", "user", "default", mock_connector
)
with self.assertRaises(TypeError):
await message.respond(events.Reaction("emoji"))
self.assertTrue(mocksleep.called)
async def load(self):
"""Load modules."""
self.modules = self.loader.load_modules_from_config(self.config)
_LOGGER.debug(_("Loaded %i skills."), len(self.modules["skills"]))
self.setup_skills(self.modules["skills"])
self.web_server = Web(self)
self.web_server.setup_webhooks(self.skills)
await self.train_parsers(self.modules["skills"])
if self.modules["databases"] is not None:
await self.start_databases(self.modules["databases"])
await self.start_connectors(self.modules["connectors"])
self.cron_task = self.eventloop.create_task(parse_crontab(self))
self.eventloop.create_task(self.web_server.start())
self.eventloop.create_task(self.parse(events.OpsdroidStarted()))
@register_event(events.NewRoom)
@ensure_room_id_and_send
async def _send_room_creation(self, creation_event):
params = creation_event.room_params
params = params.get("matrix", params)
response = await self.connection.create_room(**params)
room_id = response["room_id"]
if creation_event.name is not None:
await self._send_room_name_set(
events.RoomName(creation_event.name, target=room_id)
)
return room_id
@register_event(events.UserInvite)
@ensure_room_id_and_send
async def _send_user_invitation(self, invite_event):
try:
return await self.connection.invite_user(
invite_event.target, invite_event.user_id
)
except MatrixRequestError as err:
content = json.loads(err.content)
if err.code == 403 and "is already in the room" in content["error"]:
_LOGGER.info(
f"{invite_event.user_id} is already in the room, ignoring."
)
# pylint: disable=broad-except
# We want to catch all exceptions coming from a skill module and not
# halt the application. If a skill throws an exception it just doesn't
# give a response to the user, so an error response should be given.
try:
if len(inspect.signature(skill).parameters.keys()) > 1:
return await skill(self, config, event)
else:
return await skill(event)
except Exception:
_LOGGER.exception(
_("Exception when running skill '%s'."), str(config["name"])
)
if event:
await event.respond(
events.Message(_("Whoops there has been an error."))
)
await event.respond(events.Message(_("Check the log for details.")))
async def _send_room_creation(self, creation_event):
params = creation_event.room_params
params = params.get("matrix", params)
response = await self.connection.create_room(**params)
room_id = response["room_id"]
if creation_event.name is not None:
await self._send_room_name_set(
events.RoomName(creation_event.name, target=room_id)
)
return room_id