Skip to content

Commit 9c09336

Browse files
authored
Merge pull request #4 from didx-xyz/improvement/test_controllers
Improvement/test controllers
2 parents 9d4348d + 85fb3b4 commit 9c09336

13 files changed

+443
-11
lines changed

.coverage

172 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ dist
55
build
66
.env
77
.ipynb_checkpoints/
8-
.venv
8+
.venv
9+
.vscode/
10+
__pycache__/

aries_cloudcontroller/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from aries_cloudcontroller.aries_controller import AriesAgentController
1+
from aries_cloudcontroller.aries_controller import AriesAgentController
2+
from aries_cloudcontroller.aries_controller_base import AriesAgentControllerBase
3+
from aries_cloudcontroller.aries_tenant_controller import AriesTenantController
4+
from aries_cloudcontroller.aries_webhook_server import AriesWebhookServer

aries_cloudcontroller/aries_controller.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ async def listen_webhooks(self):
6363
await self.webhook_server.listen_webhooks()
6464
logger.info("Webhook server started.")
6565
except AttributeError:
66-
logger.warning("Webhook server not initialised.")
66+
warning = "Webhook server not initialised."
67+
logger.warning(warning)
68+
raise AttributeError(warning)
6769
except Exception as exc:
6870
logger.warning(
6971
f"Listening webhooks failed! {exc!r} occurred.")
72+
raise Exception(f"{exc!r}")

aries_cloudcontroller/aries_controller_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ async def listen_webhooks(self):
238238

239239
async def terminate(self):
240240
await self.client_session.close()
241+
logger.info("Client Session closed.")
241242
try:
242243
await self.webhook_server.terminate()
243244
except AttributeError:

aries_cloudcontroller/aries_tenant_controller.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,19 @@ def add_listener(self, listener):
7171
"topic":"topicname" key-value pairs
7272
"""
7373
try:
74+
assert (type(wallet_id) is str), "wallet_id must be a string"
75+
assert (wallet_id.__ne__("")), "Cannot add listener for empty wallet_id."
7476
pub_topic_path_base = listener['topic']
7577
pub_topic_path = f"{self.wallet_id}.{pub_topic_path_base}"
7678
pub.subscribe(listener["handler"], pub_topic_path)
7779
logger.debug("Lister added for topic : ", pub_topic_path)
78-
except self.wallet_id == "":
79-
logger.error(
80-
"Cannot add listener for empty wallet_id.")
80+
except AssertionError as err:
81+
logger.error(err)
82+
raise
8183
except Exception as exc:
8284
logger.warning(
8385
f"Adding webhooks listener failed! {exc!r} occurred.")
86+
raise
8487

8588
def update_wallet_id(self, wallet_id: str):
8689
"""This wallet_id is used to register for webhooks
@@ -92,9 +95,12 @@ def update_wallet_id(self, wallet_id: str):
9295
The tenant wallet identifier
9396
"""
9497
try:
98+
assert (type(wallet_id) is str), "wallet_id must be a string"
99+
assert (wallet_id.__ne__("")), "wallet_id must not be empty"
95100
self.wallet_id = wallet_id
96-
except wallet_id == "":
97-
raise Exception("wallet_id must not be empty")
101+
except AssertionError as err:
102+
logger.info(f"{err!r}")
103+
raise
98104

99105
def update_tenant_jwt(self, tenant_jwt: str, wallet_id: str):
100106
"""Update the tenant JW token attribute and the header
@@ -107,18 +113,22 @@ def update_tenant_jwt(self, tenant_jwt: str, wallet_id: str):
107113
The tenant wallet identifier
108114
"""
109115
try:
116+
assert (type(tenant_jwt) is str), "tenant_jwt must be a string"
117+
assert (tenant_jwt.__ne__("")), "tenant_jwt must not be empty"
110118
self.tenant_jwt = tenant_jwt
111119
self.update_wallet_id(wallet_id)
112120
self.headers.update(
113121
{'Authorization': 'Bearer ' + tenant_jwt,
114122
'content-type': "application/json"})
115123
self.client_session.headers.update(self.headers)
116-
except tenant_jwt == "":
117-
raise Exception("tenant_jwt must not be empty")
124+
except AssertionError as err:
125+
logger.info(f"{err!r}")
126+
raise
118127
except Exception as exc:
119128
logger.warning(
120129
(f"Updating tenant JW token"
121130
f" failed! {exc!r} occurred."))
131+
raise
122132

123133
def remove_tenant_jwt(self):
124134
"""Removes the tenant's JW Token attribute and corresponding
@@ -134,3 +144,4 @@ def remove_tenant_jwt(self):
134144
except Exception as exc:
135145
logger.warning(
136146
f"Removing JW token failed! {exc!r} occurred.")
147+
raise

aries_cloudcontroller/aries_webhook_server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ async def listen_webhooks(self):
5151
self.webhook_host,
5252
self.webhook_port)
5353
await self.webhook_site.start()
54+
logger.info(
55+
f"Listening Webhooks on {self.webhook_host}:{self.webhook_port}")
5456
except Exception as exc:
5557
logger.warning(f"Listening webhooks failed! {exc!r} occurred.")
58+
raise
5659

5760
async def _receive_webhook(self, request: ClientRequest):
5861
"""Helper to receive webhooks by requesting it
@@ -79,6 +82,7 @@ async def _receive_webhook(self, request: ClientRequest):
7982
return web.Response(status=200)
8083
except Exception as exc:
8184
logger.warning(f"Receiving webhooks failed! {exc!r} occurred.")
85+
raise
8286

8387
async def _handle_webhook(self, wallet_id, topic, payload):
8488
"""Helper handling a webhook
@@ -103,14 +107,17 @@ async def _handle_webhook(self, wallet_id, topic, payload):
103107
logger.warning(
104108
(f"Handling webhooks failed! {exc!r} occurred"
105109
f" when trying to handle this topic: {topic}"))
110+
raise
106111

107112
async def terminate(self):
108113
"""Terminate the controller client session and webhook listeners"""
109114
try:
110115
await self.webhook_site.stop()
116+
logger.info("Webhook server terminated.")
111117
except AttributeError:
112118
# Do nothing if no webhook site server is running
113119
return
114120
except Exception as exc:
115121
logger.warning(
116122
f"Terminating webhooks listener failed! {exc!r} occurred.")
123+
raise

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ python-dotenv
77
termcolor
88
pillow
99
qrcode
10-
beautifulsoup4
10+
beautifulsoup4
11+
pytest~=6.2.3
12+
pytest_asyncio

tests/__init__.py

Whitespace-only changes.

tests/test_aries_controller.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import logging
2+
import pytest
3+
4+
from aries_cloudcontroller import AriesAgentController
5+
from aries_cloudcontroller import AriesWebhookServer
6+
from aries_cloudcontroller.controllers.multitenant import MultitenancyController
7+
8+
LOGGER = logging.getLogger(__name__)
9+
10+
11+
class TestAriesAgentController():
12+
13+
admin_url = "0.0.0.0"
14+
webhook_host = ""
15+
webhook_port = 8000
16+
webhook_base = ""
17+
18+
@pytest.mark.asyncio
19+
async def test_init_args_missing(self):
20+
with pytest.raises(TypeError) as te:
21+
AriesAgentController()
22+
assert "__init__() missing 1 required positional argument: \
23+
'admin_url'" in str(te.value)
24+
25+
@pytest.mark.asyncio
26+
async def test_init_args_multi_default(self):
27+
ac = AriesAgentController(admin_url=self.admin_url)
28+
assert not ac.is_multitenant
29+
30+
@pytest.mark.asyncio
31+
async def test_init_args_multi_true(self):
32+
ac = AriesAgentController(
33+
admin_url=self.admin_url,
34+
is_multitenant=True)
35+
assert ac.is_multitenant
36+
assert type(ac.multitenant) == MultitenancyController
37+
assert ac.multitenant.admin_url == self.admin_url
38+
assert ac.multitenant.client_session.headers == {}
39+
await ac.terminate()
40+
41+
@pytest.mark.asyncio
42+
async def test_init_webhook_server(self):
43+
ac = AriesAgentController(
44+
admin_url=self.admin_url,
45+
is_multitenant=True)
46+
ac.init_webhook_server(
47+
self.webhook_host,
48+
self.webhook_port,
49+
self.webhook_base
50+
)
51+
assert type(ac.webhook_server) == AriesWebhookServer
52+
assert ac.webhook_server.webhook_base == self.webhook_base
53+
assert ac.webhook_server.webhook_port == self.webhook_port
54+
assert ac.webhook_server.webhook_host == self.webhook_host
55+
assert ac.webhook_server.is_multitenant
56+
await ac.terminate()
57+
58+
@pytest.mark.asyncio
59+
async def test_listen_webhooks_error(self, caplog):
60+
caplog.set_level(logging.WARNING)
61+
ac = AriesAgentController(
62+
admin_url=self.admin_url,
63+
is_multitenant=True)
64+
with pytest.raises(AttributeError) as ae:
65+
await ac.listen_webhooks()
66+
assert "Webhook server not initialised." in str(ae.value)
67+
assert "Webhook server not initialised." in caplog.text
68+
await ac.terminate()
69+
70+
@pytest.mark.asyncio
71+
async def test_init_webhook_server_terminate(self, caplog):
72+
caplog.set_level(logging.INFO)
73+
ac = AriesAgentController(
74+
admin_url=self.admin_url,
75+
is_multitenant=True)
76+
ac.init_webhook_server(
77+
self.webhook_host,
78+
self.webhook_port,
79+
self.webhook_base
80+
)
81+
await ac.listen_webhooks()
82+
assert "Webhook server started." in caplog.text
83+
res = await ac.webhook_server.terminate()
84+
assert "Webhook server terminated." in caplog.text
85+
assert res is None
86+
await ac.terminate()

0 commit comments

Comments
 (0)