Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection record is_ready refactor #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aries_cloudagent/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def dispatch(
context = RequestContext(base_context=self.context)
context.message = message
context.message_delivery = delivery
context.connection_active = connection and connection.is_active
context.connection_ready = connection and connection.is_ready
context.connection_record = connection

responder = DispatcherResponder(
Expand Down
6 changes: 3 additions & 3 deletions aries_cloudagent/messaging/actionmenu/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def actionmenu_perform(request: web.BaseRequest):
except StorageNotFoundError:
raise web.HTTPNotFound()

if connection.is_active:
if connection.is_ready:
msg = Perform(name=params["name"], params=params.get("params"))
await outbound_handler(msg, connection_id=connection_id)
return web.json_response({})
Expand All @@ -126,7 +126,7 @@ async def actionmenu_request(request: web.BaseRequest):
LOGGER.debug("Connection not found for action menu request: %s", connection_id)
raise web.HTTPNotFound()

if connection.is_active:
if connection.is_ready:
msg = MenuRequest()
await outbound_handler(msg, connection_id=connection_id)
return web.json_response({})
Expand Down Expand Up @@ -163,7 +163,7 @@ async def actionmenu_send(request: web.BaseRequest):
)
raise web.HTTPNotFound()

if connection.is_active:
if connection.is_ready:
await outbound_handler(msg, connection_id=connection_id)
return web.json_response({})

Expand Down
Empty file.
235 changes: 235 additions & 0 deletions aries_cloudagent/messaging/actionmenu/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from .. import routes as test_module

from ....storage.error import StorageNotFoundError


class TestActionMenuRoutes(AsyncTestCase):
async def test_actionmenu_perform(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, "Perform", autospec=True
) as mock_perform:

mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()

test_module.web.json_response = async_mock.CoroutineMock()

res = await test_module.actionmenu_perform(mock_request)
test_module.web.json_response.assert_called_once_with({})
mock_request.app["outbound_message_router"].assert_called_once_with(
mock_perform.return_value, connection_id=mock_request.match_info["id"]
)

async def test_actionmenu_perform_no_conn_record(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, "Perform", autospec=True
) as mock_perform:

# Emulate storage not found (bad connection id)
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock(
side_effect=StorageNotFoundError
)

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.actionmenu_perform(mock_request)

async def test_actionmenu_perform_conn_not_ready(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, "Perform", autospec=True
) as mock_perform:

# Emulate connection not ready
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
mock_connection_record.retrieve_by_id.return_value.is_ready = False

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.actionmenu_perform(mock_request)

async def test_actionmenu_request(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, "MenuRequest", autospec=True
) as menu_request:

mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()

test_module.web.json_response = async_mock.CoroutineMock()

res = await test_module.actionmenu_request(mock_request)
test_module.web.json_response.assert_called_once_with({})
mock_request.app["outbound_message_router"].assert_called_once_with(
menu_request.return_value, connection_id=mock_request.match_info["id"]
)

async def test_actionmenu_request_no_conn_record(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, "Perform", autospec=True
) as mock_perform:

# Emulate storage not found (bad connection id)
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock(
side_effect=StorageNotFoundError
)

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.actionmenu_request(mock_request)

async def test_actionmenu_request_conn_not_ready(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, "Perform", autospec=True
) as mock_perform:

# Emulate connection not ready
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
mock_connection_record.retrieve_by_id.return_value.is_ready = False

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.actionmenu_request(mock_request)

async def test_actionmenu_send(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()

res = await test_module.actionmenu_send(mock_request)
test_module.web.json_response.assert_called_once_with({})
mock_request.app["outbound_message_router"].assert_called_once_with(
mock_menu.deserialize.return_value, connection_id=mock_request.match_info["id"]
)

async def test_actionmenu_send_no_conn_record(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_menu.deserialize = async_mock.MagicMock()

# Emulate storage not found (bad connection id)
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock(
side_effect=StorageNotFoundError
)

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.actionmenu_send(mock_request)

async def test_actionmenu_send_conn_not_ready(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_menu.deserialize = async_mock.MagicMock()

# Emulate connection not ready
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
mock_connection_record.retrieve_by_id.return_value.is_ready = False

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.actionmenu_send(mock_request)

2 changes: 1 addition & 1 deletion aries_cloudagent/messaging/basicmessage/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def connections_send_message(request: web.BaseRequest):
except StorageNotFoundError:
raise web.HTTPNotFound()

if connection.is_active:
if connection.is_ready:
msg = BasicMessage(content=params["content"])
await outbound_handler(msg, connection_id=connection_id)

Expand Down
Empty file.
98 changes: 98 additions & 0 deletions aries_cloudagent/messaging/basicmessage/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from .. import routes as test_module

from ....storage.error import StorageNotFoundError


class TestBasicMessageRoutes(AsyncTestCase):
async def test_connections_send_message(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, "BasicMessage", autospec=True
) as mock_basic_message, async_mock.patch.object(
test_module, "ConnectionManager", autospec=True
) as mock_conn_manager:

mock_conn_manager.return_value.log_activity = async_mock.CoroutineMock()

mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()

test_module.web.json_response = async_mock.CoroutineMock()

res = await test_module.connections_send_message(mock_request)
test_module.web.json_response.assert_called_once_with({})
mock_conn_manager.return_value.log_activity.assert_called_once_with(
mock_connection_record.retrieve_by_id.return_value,
"message",
mock_connection_record.retrieve_by_id.return_value.DIRECTION_SENT,
{"content": mock_request.json.return_value["content"]},
)
mock_basic_message.assert_called_once()

async def test_connections_send_message_no_conn_record(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, "BasicMessage", autospec=True
) as mock_basic_message, async_mock.patch.object(
test_module, "ConnectionManager", autospec=True
) as mock_conn_manager:

# Emulate storage not found (bad connection id)
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock(
side_effect=StorageNotFoundError
)

mock_conn_manager.return_value.log_activity = async_mock.CoroutineMock()

test_module.web.json_response = async_mock.CoroutineMock()

with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.connections_send_message(mock_request)

async def test_connections_send_message_not_ready(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, "BasicMessage", autospec=True
) as mock_basic_message, async_mock.patch.object(
test_module, "ConnectionManager", autospec=True
) as mock_conn_manager:

# Emulate connection not ready
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
mock_connection_record.retrieve_by_id.return_value.is_ready = False

mock_conn_manager.return_value.log_activity = async_mock.CoroutineMock()

test_module.web.json_response = async_mock.CoroutineMock()

await test_module.connections_send_message(mock_request)
mock_basic_message.assert_not_called()
4 changes: 2 additions & 2 deletions aries_cloudagent/messaging/connections/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@ async def establish_inbound(
raise ConnectionManagerError(
f"Routing connection not found: {inbound_connection_id}"
)
if not router.is_active:
if not router.is_ready:
raise ConnectionManagerError(
f"Routing connection is not active: {inbound_connection_id}"
f"Routing connection is not ready: {inbound_connection_id}"
)
connection.inbound_connection_id = inbound_connection_id

Expand Down
Loading