Skip to content

Commit 06db386

Browse files
test(kanon): add or update test_store_kanon_unit.py
Signed-off-by: Vinay Singh vinay@verid.id Signed-off-by: Vinay Singh <vinay@verid.id>
1 parent b8aa246 commit 06db386

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import json
2+
import pytest
3+
4+
5+
def _base_config(sqlite=True):
6+
return {
7+
"name": "test",
8+
"storage_type": "sqlite" if sqlite else "postgres_storage",
9+
"dbstore_storage_type": "sqlite" if sqlite else "postgres_storage",
10+
"test": True,
11+
}
12+
13+
14+
@pytest.mark.asyncio
15+
async def test_sqlite_uris_and_open_remove(monkeypatch, tmp_path):
16+
from acapy_agent.kanon.store_kanon import KanonStoreConfig, KanonOpenStore
17+
18+
cfg = KanonStoreConfig(_base_config(sqlite=True))
19+
20+
class _DB:
21+
def __init__(self):
22+
self.closed = False
23+
async def close(self, remove=False):
24+
self.closed = True
25+
class _KMS:
26+
def __init__(self):
27+
self.closed = False
28+
async def close(self, remove=False):
29+
self.closed = True
30+
31+
async def _db_open(uri, *a, **k):
32+
return _DB()
33+
async def _kms_open(uri, *a, **k):
34+
return _KMS()
35+
36+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.open", _db_open)
37+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.open", _kms_open)
38+
39+
opened = await cfg.open_store(provision=False, in_memory=True)
40+
assert isinstance(opened, KanonOpenStore)
41+
assert opened.name == "test"
42+
await opened.close()
43+
44+
45+
@pytest.mark.asyncio
46+
async def test_postgres_missing_config_errors(monkeypatch):
47+
from acapy_agent.kanon.store_kanon import KanonStoreConfig, ProfileError
48+
49+
cfg = {
50+
**_base_config(sqlite=False),
51+
"storage_config": json.dumps({"url": "localhost:5432/db"}),
52+
"storage_creds": json.dumps({"account": "a", "password": "p"}),
53+
"dbstore_storage_config": json.dumps({"url": "localhost:5432/db"}),
54+
"dbstore_storage_creds": json.dumps({"account": "a", "password": "p"}),
55+
}
56+
57+
sc = KanonStoreConfig(cfg)
58+
assert "postgres" in sc.get_dbstore_uri(create=False)
59+
assert "postgres" in sc.get_askar_uri(create=False)
60+
61+
bad_cfg = {**cfg}
62+
bad_cfg["dbstore_storage_config"] = json.dumps({})
63+
with pytest.raises(ProfileError):
64+
KanonStoreConfig(bad_cfg).get_dbstore_uri()
65+
66+
bad_cfg2 = {**cfg}
67+
bad_cfg2["storage_config"] = json.dumps({})
68+
with pytest.raises(ProfileError):
69+
KanonStoreConfig(bad_cfg2).get_askar_uri()
70+
71+
bad_tls = {**cfg}
72+
bad_tls["dbstore_storage_config"] = json.dumps({"url": "host/db", "tls": {"sslmode": "bad"}})
73+
with pytest.raises(ProfileError):
74+
KanonStoreConfig(bad_tls).get_dbstore_uri()
75+
76+
bad_creds = {**cfg}
77+
bad_creds["dbstore_storage_creds"] = "{" # invalid JSON
78+
with pytest.raises(ProfileError):
79+
KanonStoreConfig(bad_creds)
80+
81+
82+
@pytest.mark.asyncio
83+
async def test_open_error_translation_and_rekey(monkeypatch):
84+
from acapy_agent.kanon.store_kanon import KanonStoreConfig, ProfileError
85+
from acapy_agent.database_manager.dbstore import DBStoreError, DBStoreErrorCode
86+
from aries_askar import AskarError, AskarErrorCode
87+
88+
cfg = KanonStoreConfig({"name": "t", "rekey": "rk", "dbstore_key": "dk"})
89+
90+
class _DB:
91+
async def rekey(self, *a, **k):
92+
pass
93+
class _KMS:
94+
async def rekey(self, *a, **k):
95+
pass
96+
97+
async def _db_open_fail(uri, *a, **k):
98+
raise DBStoreError(code=DBStoreErrorCode.NOT_FOUND, message="x")
99+
100+
async def _kms_open_fail(uri, *a, **k):
101+
raise AskarError(AskarErrorCode.NOT_FOUND, "x")
102+
103+
async def _kms_open_retry(uri, *a, **k):
104+
return _KMS()
105+
106+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.open", _db_open_fail)
107+
with pytest.raises(ProfileError):
108+
await cfg.open_store(provision=False, in_memory=True)
109+
110+
async def _db_open_dup(uri, *a, **k):
111+
raise DBStoreError(code=DBStoreErrorCode.DUPLICATE, message="dup")
112+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.open", _db_open_dup)
113+
with pytest.raises(ProfileError):
114+
await cfg.open_store(provision=False, in_memory=True)
115+
116+
async def _db_open_ok(uri, *a, **k):
117+
return _DB()
118+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.open", _db_open_ok)
119+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.open", _kms_open_fail)
120+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.open", _kms_open_retry)
121+
opened = await cfg.open_store(provision=False, in_memory=True)
122+
assert opened is not None
123+
124+
async def _kms_dup(uri, *a, **k):
125+
from aries_askar import AskarError
126+
raise AskarError(AskarErrorCode.DUPLICATE, "x")
127+
async def _kms_nf(uri, *a, **k):
128+
from aries_askar import AskarError
129+
raise AskarError(AskarErrorCode.NOT_FOUND, "x")
130+
cfg2 = KanonStoreConfig({"name": "t2"})
131+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.open", _db_open_ok)
132+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.open", _kms_dup)
133+
with pytest.raises(ProfileError):
134+
await cfg2.open_store(provision=False, in_memory=True)
135+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.open", _kms_nf)
136+
with pytest.raises(ProfileError):
137+
await cfg2.open_store(provision=False, in_memory=True)
138+
139+
140+
@pytest.mark.asyncio
141+
async def test_remove_store_mappings(monkeypatch):
142+
from acapy_agent.kanon.store_kanon import KanonStoreConfig, ProfileNotFoundError
143+
from acapy_agent.database_manager.dbstore import DBStoreError, DBStoreErrorCode
144+
from aries_askar import AskarError, AskarErrorCode
145+
146+
cfg = KanonStoreConfig({"name": "t"})
147+
148+
async def _kms_remove(uri):
149+
raise AskarError(AskarErrorCode.NOT_FOUND, "x")
150+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.Store.remove", _kms_remove)
151+
cfg.store_class = "askar"
152+
with pytest.raises(ProfileNotFoundError):
153+
await cfg.remove_store()
154+
155+
async def _db_remove(uri, *a, **k):
156+
raise DBStoreError(code=DBStoreErrorCode.NOT_FOUND, message="x")
157+
monkeypatch.setattr("acapy_agent.kanon.store_kanon.DBStore.remove", _db_remove)
158+
cfg.store_class = "dbstore"
159+
with pytest.raises(ProfileNotFoundError):
160+
await cfg.remove_store()
161+
162+

0 commit comments

Comments
 (0)