Skip to content

Commit

Permalink
Merge pull request #602 from sklump/sort-swagger-tags
Browse files Browse the repository at this point in the history
Sort swagger tags
  • Loading branch information
andrewwhitehead authored Jul 10, 2020
2 parents 659168d + cc853ee commit 1b7d209
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aries_cloudagent/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .base_server import BaseAdminServer
from .error import AdminSetupError


LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -270,6 +271,7 @@ async def start(self) -> None:
if plugin_registry:
plugin_registry.post_process_routes(self.app)

self.app._state["swagger_dict"].get("tags", []).sort(key=lambda t: t["name"])
self.site = web.TCPSite(runner, host=self.host, port=self.port)

try:
Expand Down
21 changes: 21 additions & 0 deletions aries_cloudagent/protocols/out_of_band/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,24 @@ async def register(app: web.Application):
web.post("/out-of-band/receive-invitation", invitation_receive),
]
)


def post_process_routes(app: web.Application):
"""Amend swagger API."""

# Add top-level tags description
if "tags" not in app._state["swagger_dict"]:
app._state["swagger_dict"]["tags"] = []
app._state["swagger_dict"]["tags"].append(
{
"name": "out-of-band",
"description": "Out-of-band connections",
"externalDocs": {
"description": "Design",
"url": (
"https://github.com/hyperledger/aries-rfcs/tree/"
"2da7fc4ee043effa3a9960150e7ba8c9a4628b68/features/0434-outofband"
),
},
}
)
91 changes: 91 additions & 0 deletions aries_cloudagent/protocols/out_of_band/v1_0/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock
import pytest

from aiohttp.web import HTTPBadRequest, HTTPForbidden, HTTPNotFound

from .. import routes as test_module


class TestOutOfBandRoutes(AsyncTestCase):
async def test_invitation_create(self):
request = async_mock.MagicMock()
request.app = {"request_context": async_mock.MagicMock()}
request.query = {"multi_use": "true"}
request.json = async_mock.CoroutineMock(
return_value={
"attachments": async_mock.MagicMock(),
"include_handshake": True,
"use_public_did": True,
}
)

with async_mock.patch.object(
test_module, "OutOfBandManager", autospec=True
) as mock_oob_mgr, async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as mock_json_response:
mock_oob_mgr.return_value.create_invitation = async_mock.CoroutineMock(
return_value=async_mock.MagicMock(
serialize=async_mock.MagicMock(return_value={"abc": "123"})
)
)

result = await test_module.invitation_create(request)
mock_json_response.assert_called_once_with({"abc": "123"})

async def test_invitation_create_x(self):
request = async_mock.MagicMock()
request.app = {"request_context": async_mock.MagicMock()}
request.query = {"multi_use": "true"}
request.json = async_mock.CoroutineMock(
return_value={
"attachments": async_mock.MagicMock(),
"include_handshake": True,
"use_public_did": True,
}
)

with async_mock.patch.object(
test_module, "OutOfBandManager", autospec=True
) as mock_oob_mgr, async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as mock_json_response:
mock_oob_mgr.return_value.create_invitation = async_mock.CoroutineMock(
side_effect=test_module.OutOfBandManagerError()
)

with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.invitation_create(request)
mock_json_response.assert_not_called()

async def test_invitation_receive(self):
request = async_mock.MagicMock()
request.app = {"request_context": async_mock.MagicMock()}
request.json = async_mock.CoroutineMock()

with async_mock.patch.object(
test_module, "OutOfBandManager", autospec=True
) as mock_oob_mgr, async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as mock_json_response:
mock_oob_mgr.return_value.receive_invitation = async_mock.CoroutineMock(
return_value=async_mock.MagicMock(
serialize=async_mock.MagicMock(return_value={"abc": "123"})
)
)

result = await test_module.invitation_receive(request)
mock_json_response.assert_called_once_with({"abc": "123"})

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()

async def test_post_process_routes(self):
mock_app = async_mock.MagicMock(_state={"swagger_dict": {}})
test_module.post_process_routes(mock_app)
assert "tags" in mock_app._state["swagger_dict"]

0 comments on commit 1b7d209

Please sign in to comment.