Repository: Zapix/mtpylon (language: python)

Issue:
Implement view to return public keys
Implement view to return public keys from rsa-manager


Discussion / hints:

Fix patch:
diff --git a/mtpylon/aiohandlers/__init__.py b/mtpylon/aiohandlers/__init__.py
index 40a96af..a49fb3b 100644
--- a/mtpylon/aiohandlers/__init__.py
+++ b/mtpylon/aiohandlers/__init__.py
@@ -1,1 +1,10 @@
 # -*- coding: utf-8 -*-
+from .websockets import create_websocket_handler
+from .schema import schema_view_factory
+from .pub_keys import pub_keys_view
+
+__all__ = [
+    'create_websocket_handler',
+    'schema_view_factory',
+    'pub_keys_view',
+]
diff --git a/mtpylon/aiohandlers/pub_keys.py b/mtpylon/aiohandlers/pub_keys.py
new file mode 100644
index 0000000..18f0767
--- /dev/null
+++ b/mtpylon/aiohandlers/pub_keys.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+from aiohttp.web import Request, Response
+from aiohttp.web_response import json_response
+from mtpylon.contextvars import rsa_manager
+
+
+async def pub_keys_view(request: Request) -> Response:
+    try:
+        manager = rsa_manager.get()
+    except LookupError:
+        response = Response(
+            text='Public keys has not been set',
+            status=400
+        )
+    else:
+        response = json_response(
+            [key.decode() for key in manager.public_key_list],
+            status=200
+        )
+
+    return response


Test patch:
diff --git a/tests/test_aiohandlers/test_pub_keys.py b/tests/test_aiohandlers/test_pub_keys.py
new file mode 100644
index 0000000..13eab27
--- /dev/null
+++ b/tests/test_aiohandlers/test_pub_keys.py
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+import pytest
+from aiohttp.web import Application
+
+from mtpylon.contextvars import rsa_manager
+from mtpylon.aiohandlers.pub_keys import pub_keys_view
+
+from tests.simple_manager import manager
+
+
+@pytest.fixture
+def cli(loop, aiohttp_client):
+
+    app = Application()
+    app.router.add_get('/pub-keys', pub_keys_view)
+
+    return loop.run_until_complete(aiohttp_client(app))
+
+
+@pytest.fixture
+def cli_with_manager(loop, aiohttp_client):
+    rsa_manager.set(manager)
+
+    app = Application()
+    app.router.add_get('/pub-keys', pub_keys_view)
+
+    return loop.run_until_complete(aiohttp_client(app))
+
+
+async def test_400_no_keys(cli):
+    resp = await cli.get('/pub-keys')
+
+    assert resp.status == 400
+
+
+async def test_200_ok(cli_with_manager):
+
+    resp = await cli_with_manager.get('/pub-keys')
+
+    assert resp.status == 200