Skip to content

Commit

Permalink
Merge branch 'master' into refactor/transport
Browse files Browse the repository at this point in the history
  • Loading branch information
swcurran authored Nov 29, 2019
2 parents e3121b4 + 456280e commit ef49f83
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 10 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from asynctest import (
mock as async_mock,
TestCase as AsyncTestCase,
)

from .....messaging.request_context import RequestContext
from .....messaging.responder import MockResponder

from .. import menu_handler as handler


class TestHandler(AsyncTestCase):
async def test_called(self):
request_context = RequestContext()
request_context.connection_record = async_mock.MagicMock()
request_context.connection_record.connection_id = "dummy"

handler.save_connection_menu = async_mock.CoroutineMock()
responder = MockResponder()

request_context.message = handler.Menu()
handler_inst = handler.MenuHandler()
await handler_inst.handle(request_context, responder)

handler.save_connection_menu.assert_called_once_with(
request_context.message,
request_context.connection_record.connection_id,
request_context
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
from asynctest import (
mock as async_mock,
TestCase as AsyncTestCase,
)

from .....config.injection_context import InjectionContext
from .....messaging.request_context import RequestContext
from .....messaging.responder import MockResponder

from .. import menu_request_handler as handler


class TestHandler(AsyncTestCase):
async def test_called(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)
MenuService = async_mock.MagicMock(handler.BaseMenuService, autospec=True)
self.menu_service = MenuService()
self.context.injector.bind_instance(handler.BaseMenuService, self.menu_service)
self.context.inject = async_mock.CoroutineMock(
return_value=self.menu_service
)

self.context.connection_record = async_mock.MagicMock()
self.context.connection_record.connection_id = "dummy"

responder = MockResponder()
self.context.message = handler.MenuRequest()
self.menu_service.get_active_menu = async_mock.CoroutineMock(
return_value="menu"
)

handler_inst = handler.MenuRequestHandler()
await handler_inst.handle(self.context, responder)

messages = responder.messages
assert len(messages) == 1
(result, target) = messages[0]
assert result == "menu"
assert target == {}

async def test_called_no_active_menu(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)
MenuService = async_mock.MagicMock(handler.BaseMenuService, autospec=True)
self.menu_service = MenuService()
self.context.injector.bind_instance(handler.BaseMenuService, self.menu_service)
self.context.inject = async_mock.CoroutineMock(
return_value=self.menu_service
)

self.context.connection_record = async_mock.MagicMock()
self.context.connection_record.connection_id = "dummy"

responder = MockResponder()
self.context.message = handler.MenuRequest()
self.menu_service.get_active_menu = async_mock.CoroutineMock(
return_value=None
)

handler_inst = handler.MenuRequestHandler()
await handler_inst.handle(self.context, responder)

messages = responder.messages
assert not messages
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
from asynctest import (
mock as async_mock,
TestCase as AsyncTestCase,
)

from .....config.injection_context import InjectionContext
from .....messaging.request_context import RequestContext
from .....messaging.responder import MockResponder

from .. import perform_handler as handler


class TestHandler(AsyncTestCase):
async def test_called(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)
MenuService = async_mock.MagicMock(handler.BaseMenuService, autospec=True)
self.menu_service = MenuService()
self.context.injector.bind_instance(handler.BaseMenuService, self.menu_service)
self.context.inject = async_mock.CoroutineMock(
return_value=self.menu_service
)

self.context.connection_record = async_mock.MagicMock()
self.context.connection_record.connection_id = "dummy"

responder = MockResponder()
self.context.message = handler.Perform()
self.menu_service.perform_menu_action = async_mock.CoroutineMock(
return_value="perform"
)

handler_inst = handler.PerformHandler()
await handler_inst.handle(self.context, responder)

messages = responder.messages
assert len(messages) == 1
(result, target) = messages[0]
assert result == "perform"
assert target == {}

async def test_called_no_active_menu(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)
MenuService = async_mock.MagicMock(handler.BaseMenuService, autospec=True)
self.menu_service = MenuService()
self.context.injector.bind_instance(handler.BaseMenuService, self.menu_service)
self.context.inject = async_mock.CoroutineMock(
return_value=self.menu_service
)

self.context.connection_record = async_mock.MagicMock()
self.context.connection_record.connection_id = "dummy"

responder = MockResponder()
self.context.message = handler.Perform()
self.menu_service.perform_menu_action = async_mock.CoroutineMock(
return_value=None
)

handler_inst = handler.PerformHandler()
await handler_inst.handle(self.context, responder)

messages = responder.messages
assert not messages
26 changes: 26 additions & 0 deletions aries_cloudagent/protocols/actionmenu/tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from ....config.injection_context import InjectionContext
from ....messaging.request_context import RequestContext
from .. import controller as test_module


class TestActionMenuController(AsyncTestCase):
async def test_controller(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)
MenuService = async_mock.MagicMock(test_module.BaseMenuService, autospec=True)
self.menu_service = MenuService()
self.context.injector.bind_instance(
test_module.BaseMenuService,
self.menu_service
)
self.context.inject = async_mock.CoroutineMock(
return_value=self.menu_service
)

controller = test_module.Controller("protocol")

assert await controller.determine_roles(self.context) == ["provider"]
83 changes: 83 additions & 0 deletions aries_cloudagent/protocols/actionmenu/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,58 @@


class TestActionMenuRoutes(AsyncTestCase):
async def test_actionmenu_close(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()

mock_request.app = {
"outbound_message_router": async_mock.CoroutineMock(),
"request_context": "context",
}

test_module.retrieve_connection_menu = async_mock.CoroutineMock()
test_module.save_connection_menu = async_mock.CoroutineMock()
test_module.web.json_response = async_mock.CoroutineMock()

res = await test_module.actionmenu_close(mock_request)
test_module.web.json_response.assert_called_once_with({})

async def test_actionmenu_close_not_found(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()

mock_request.app = {
"outbound_message_router": async_mock.CoroutineMock(),
"request_context": "context",
}

test_module.retrieve_connection_menu = async_mock.CoroutineMock(
return_value=None
)
with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.actionmenu_close(mock_request)

async def test_actionmenu_fetch(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()

mock_request.app = {
"outbound_message_router": async_mock.CoroutineMock(),
"request_context": "context",
}

test_module.retrieve_connection_menu = async_mock.CoroutineMock(
return_value=None
)
test_module.web.json_response = async_mock.CoroutineMock()

res = await test_module.actionmenu_fetch(mock_request)
test_module.web.json_response.assert_called_once_with(
{
"result": None
}
)

async def test_actionmenu_perform(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()
Expand Down Expand Up @@ -181,6 +233,30 @@ async def test_actionmenu_send(self):
connection_id=mock_request.match_info["id"],
)

async def test_actionmenu_send_deserialize_x(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()

mock_request.app = {
"outbound_message_router": async_mock.CoroutineMock(),
"request_context": "context",
}

with async_mock.patch.object(
test_module, "ConnectionRecord", autospec=True
) as mock_connection_record, async_mock.patch.object(
test_module, "Menu", autospec=True
) as mock_menu:

mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
test_module.web.json_response = async_mock.CoroutineMock()
mock_menu.deserialize = async_mock.MagicMock(
side_effect=ValueError("cannot deserialize")
)

with self.assertRaises(ValueError):
await test_module.actionmenu_send(mock_request)

async def test_actionmenu_send_no_conn_record(self):
mock_request = async_mock.MagicMock()
mock_request.json = async_mock.CoroutineMock()
Expand Down Expand Up @@ -234,3 +310,10 @@ async def test_actionmenu_send_conn_not_ready(self):
with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.actionmenu_send(mock_request)

async def test_register(self):
mock_app = async_mock.MagicMock()
mock_app.add_routes = async_mock.MagicMock()

await test_module.register(mock_app)
mock_app.add_routes.assert_called_once()

72 changes: 72 additions & 0 deletions aries_cloudagent/protocols/actionmenu/tests/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from ....config.injection_context import InjectionContext
from ....messaging.request_context import RequestContext
from ....messaging.responder import MockResponder
from .. import driver_service as test_module


class TestActionMenuService(AsyncTestCase):
async def test_get_active_menu(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)

self.responder = MockResponder()
self.context.injector.bind_instance(test_module.BaseResponder, self.responder)

self.menu_service = await (
test_module.DriverMenuService.service_handler()(self.context)
)

connection = async_mock.MagicMock()
connection.connection_id = "connid"
thread_id = "thid"

await self.menu_service.get_active_menu(connection, thread_id)

webhooks = self.responder.webhooks
assert len(webhooks) == 1
(result, target) = webhooks[0]
assert result == "get-active-menu"
assert target == {
"connection_id": connection.connection_id,
"thread_id": thread_id
}

async def test_perform_menu_action(self):
self.context = RequestContext(
base_context=InjectionContext(enforce_typing=False)
)

self.responder = MockResponder()
self.context.injector.bind_instance(test_module.BaseResponder, self.responder)

self.menu_service = await (
test_module.DriverMenuService.service_handler()(self.context)
)

action_name = "action"
action_params = {'a': 1, 'b': 2}
connection = async_mock.MagicMock()
connection.connection_id = "connid"
thread_id = "thid"

await self.menu_service.perform_menu_action(
action_name,
action_params,
connection,
thread_id
)

webhooks = self.responder.webhooks
assert len(webhooks) == 1
(result, target) = webhooks[0]
assert result == "perform-menu-action"
assert target == {
"connection_id": connection.connection_id,
"thread_id": thread_id,
"action_name": action_name,
"action_params": action_params
}
Loading

0 comments on commit ef49f83

Please sign in to comment.