Skip to content

Commit e8c9a5c

Browse files
committed
Tests for AriesTenantController
* Slight refactor of the actual class to catch inputs better and * raise exceptions accordingly
1 parent 906e488 commit e8c9a5c

File tree

4 files changed

+156
-8
lines changed

4 files changed

+156
-8
lines changed

libs/aries-basic-controller/aries_basic_controller/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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
wallet_id = "1234"
2+
empty_id = ""
3+
4+
def checker(wallet_id):
5+
try:
6+
if not wallet_id:
7+
raise TypeError("Empty string")
8+
print(wallet_id)
9+
except TypeError as err:
10+
print(err)
11+
12+
def other(wallet_id):
13+
if not wallet_id:
14+
raise TypeError("Empty string")
15+
print(wallet_id)
16+
17+
other(wallet_id)
18+
other(empty_id)

libs/aries-basic-controller/aries_basic_controller/tests/test_aries_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def test_init_args_missing(self):
2626
with pytest.raises(TypeError) as te:
2727
AriesAgentController()
2828
assert "__init__() missing 1 required positional argument: 'admin_url'" \
29-
in str(tf.value)
29+
in str(te.value)
3030

3131
@pytest.mark.asyncio
3232
async def test_init_args_multi_default(self):
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import logging
2+
import re
3+
import pytest
4+
import pytest_asyncio
5+
from aiohttp import (
6+
web,
7+
ClientSession,
8+
)
9+
10+
from ..aries_webhook_server import AriesWebhookServer
11+
from ..controllers.multitenant import MultitenancyController
12+
13+
from ..aries_tenant_controller import AriesTenantController
14+
15+
LOGGER = logging.getLogger(__name__)
16+
17+
18+
class TestAriesAgentControllerBase():
19+
20+
admin_url = "0.0.0.0"
21+
webhook_host = ""
22+
webhook_port = 8000
23+
webhook_base = ""
24+
wallet_id = "123456789"
25+
tenant_jwt = "987654321"
26+
27+
@pytest.mark.asyncio
28+
async def test_init_args_missing_wallet_id(self):
29+
with pytest.raises(
30+
TypeError,
31+
match=re.escape("__init__ missing required wallet_id (str)")):
32+
AriesTenantController(admin_url=self.admin_url)
33+
34+
@pytest.mark.asyncio
35+
async def test_init_args_missing_tenant_jwt(self):
36+
with pytest.raises(
37+
TypeError,
38+
match=re.escape("__init__ missing required tenant_jwt (str)")):
39+
AriesTenantController(admin_url=self.admin_url, wallet_id=self.wallet_id)
40+
41+
@pytest.mark.asyncio
42+
async def test_init_webhook_server(self):
43+
ac = AriesTenantController(
44+
admin_url=self.admin_url,
45+
wallet_id=self.wallet_id,
46+
tenant_jwt=self.tenant_jwt)
47+
with pytest.raises(
48+
NotImplementedError,
49+
match=("Please, use an AriesAgentController to start a webhook server\n"
50+
"Webhook server fct is disallowed for tenant controllers.")):
51+
ac.init_webhook_server()
52+
await ac.terminate()
53+
54+
@pytest.mark.asyncio
55+
async def test_listen_webhooks(self):
56+
ac = AriesTenantController(
57+
admin_url=self.admin_url,
58+
wallet_id=self.wallet_id,
59+
tenant_jwt=self.tenant_jwt)
60+
with pytest.raises(
61+
NotImplementedError,
62+
match=("Please, use an AriesAgentController to start a webhook server\n"
63+
"Webhook server fct is disallowed for tenant controllers.")):
64+
ac.listen_webhooks()
65+
await ac.terminate()
66+
67+
@pytest.mark.asyncio
68+
async def test_update_wallet_id(self):
69+
new_wallet_id = "567438291"
70+
ac = AriesTenantController(
71+
admin_url=self.admin_url,
72+
wallet_id=self.wallet_id,
73+
tenant_jwt=self.tenant_jwt)
74+
assert ac.wallet_id != new_wallet_id
75+
ac.update_wallet_id(wallet_id=new_wallet_id)
76+
assert ac.wallet_id == new_wallet_id
77+
with pytest.raises(
78+
AssertionError,
79+
match="wallet_id must not be empty"):
80+
ac.update_wallet_id(wallet_id="")
81+
with pytest.raises(
82+
AssertionError,
83+
match="wallet_id must be a string"):
84+
ac.update_wallet_id(wallet_id=23456)
85+
await ac.terminate()
86+
87+
@pytest.mark.asyncio
88+
async def test_update_tenant_jwt(self):
89+
new_tenant_jwt = "567438291"
90+
ac = AriesTenantController(
91+
admin_url=self.admin_url,
92+
wallet_id=self.wallet_id,
93+
tenant_jwt=self.tenant_jwt)
94+
assert ac.tenant_jwt != new_tenant_jwt
95+
ac.update_tenant_jwt(wallet_id=self.wallet_id, tenant_jwt=new_tenant_jwt)
96+
assert ac.tenant_jwt == new_tenant_jwt
97+
with pytest.raises(
98+
AssertionError,
99+
match="tenant_jwt must not be empty"):
100+
ac.update_tenant_jwt(wallet_id=self.wallet_id, tenant_jwt="")
101+
with pytest.raises(
102+
AssertionError,
103+
match="tenant_jwt must be a string"):
104+
ac.update_tenant_jwt(wallet_id=self.wallet_id, tenant_jwt=23456)
105+
await ac.terminate()
106+
107+
@pytest.mark.asyncio
108+
async def test_remove_tenant_jwt(self):
109+
ac = AriesTenantController(
110+
admin_url=self.admin_url,
111+
wallet_id=self.wallet_id,
112+
tenant_jwt=self.tenant_jwt)
113+
ac.remove_tenant_jwt()
114+
assert not ac.tenant_jwt
115+
assert 'Authorization' not in ac.headers
116+
assert 'Authorization' not in ac.client_session.headers
117+
assert 'content-type' not in ac.client_session.headers
118+
assert 'content-type' not in ac.headers
119+
await ac.terminate()

0 commit comments

Comments
 (0)