Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
new_callable=lambda: Mock(side_effect=Exception)) as process:
connect, receive, eof = self.loop.create_future(), self.loop.create_future(), self.loop.create_future()
def test_run(self):
with OpsDroid() as opsdroid:
opsdroid.is_running = amock.Mock(side_effect=[False, True, False])
opsdroid.eventloop = mock.MagicMock()
opsdroid.eventloop.run_until_complete = mock.Mock()
with mock.patch("sys.exit") as mock_sysexit:
opsdroid.run()
self.assertTrue(opsdroid.eventloop.run_until_complete.called)
self.assertTrue(mock_sysexit.called)
def _get_child_mock(self, *args, **kwargs):
# A FileMock returns a Mock by default, not a FileMock
return mock.Mock(**kwargs)
async def test_facebook_challenge_handler(self):
"""Test the facebook challenge handler."""
import aiohttp
opsdroid = amock.CoroutineMock()
connector = ConnectorFacebook({"verify-token": "token_123"}, opsdroid=opsdroid)
mock_request = amock.Mock()
mock_request.query = {
"hub.verify_token": "token_123",
"hub.challenge": "challenge_123",
}
response = await connector.facebook_challenge_handler(mock_request)
self.assertEqual(type(response), aiohttp.web.Response)
self.assertEqual(response.text, "challenge_123")
self.assertEqual(response.status, 200)
mock_request.query = {
"hub.verify_token": "token_abc",
"hub.challenge": "challenge_123",
}
response = await connector.facebook_challenge_handler(mock_request)
self.assertEqual(type(response), aiohttp.web.Response)
async def test_call_watson(self):
opsdroid = amock.CoroutineMock()
mock_connector = Connector({}, opsdroid=opsdroid)
message = Message("Hello", "user", "default", mock_connector)
config = {
"name": "watson",
"token": "test",
"gateway": "gateway",
"min-score": 0.3,
"assistant-id": "test",
"session-id": "12ndior2kld",
}
result = amock.Mock()
result.json = amock.CoroutineMock()
result.json.return_value = {
"output": {
"generic": [{"response_type": "text", "text": "Hey hows it going?"}],
"intents": [{"intent": "hello", "confidence": 1}],
"entities": [
{
"entity": "greetings",
"location": [0, 2],
"value": "hello",
"confidence": 1,
},
{
"entity": "greetings",
"location": [0, 2],
"value": "hi",
new_callable=lambda: Mock(side_effect=Exception)) as process:
connect, receive, eof = self.loop.create_future(),self.loop.create_future(),self.loop.create_future()
async def test_constrain_connectors_skips(self):
with OpsDroid() as opsdroid:
skill = await self.getMockSkill()
skill = match_regex(r".*")(skill)
skill = constraints.constrain_connectors(["slack"])(skill)
opsdroid.skills.append(skill)
connector = mock.Mock()
connector.configure_mock(name="slack")
tasks = await opsdroid.parse(
Message(
text="Hello", user="user", target="#general", connector=connector
)
)
self.assertEqual(len(tasks), 3) # match_always, match_event and the skill
async def test_respond_on_interactive_actions(self):
"""Test the respond method for interactive actions in Slack."""
result = amock.Mock()
result.json = amock.CoroutineMock()
result.json.return_value = {"success": "payload sent."}
payload = {
"type": "message_action",
"team": {"id": "TXXXXXX", "domain": "coverbands"},
"user": {"id": "UXXXXXX", "name": "dreamweaver"},
"response_url": "https://hooks.slack.com/app-actions/T0MJR11A4/21974584944/yk1S9ndf35Q1flupVG5JbpM6",
}
interactive_action = InteractiveAction(payload)
with amock.patch("aiohttp.ClientSession.post") as patched_request:
patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(result)
await interactive_action.respond("Respond called with response_url")
self.assertTrue(patched_request.called)
async def test_call_rasanlu_bad_response(self):
opsdroid = amock.CoroutineMock()
mock_connector = Connector({}, opsdroid=opsdroid)
message = Message(
text="how's the weather outside",
user="user",
target="default",
connector=mock_connector,
)
config = {"name": "rasanlu", "token": "test", "min-score": 0.3}
result = amock.Mock()
result.status = 403
result.text = amock.CoroutineMock()
result.text.return_value = "unauthorized"
with amock.patch("aiohttp.ClientSession.post") as patched_request:
patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(result)
response = await rasanlu.call_rasanlu(message.text, config)
self.assertTrue(patched_request.called)
self.assertEqual(response, result.text.return_value)