Skip to content

Commit

Permalink
Merge pull request #1700 from shaangill025/issue#1691
Browse files Browse the repository at this point in the history
Fix: update IndyLedgerRequestsExecutor logic - multitenancy and basic base wallet type
  • Loading branch information
swcurran authored Apr 1, 2022
2 parents 9a69a59 + 5d761f8 commit 97192ec
Show file tree
Hide file tree
Showing 20 changed files with 427 additions and 54 deletions.
13 changes: 11 additions & 2 deletions aries_cloudagent/indy/models/pres_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ...messaging.models.base import BaseModel, BaseModelSchema
from ...messaging.util import canon
from ...messaging.valid import INDY_CRED_DEF_ID, INDY_PREDICATE
from ...multitenant.base import BaseMultitenantManager
from ...protocols.didcomm_prefix import DIDCommPrefix
from ...wallet.util import b64_to_str

Expand Down Expand Up @@ -351,7 +352,11 @@ def non_revoc(cred_def_id: str) -> IndyNonRevocationInterval:
revoc_support = False
if cd_id:
if profile:
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(profile)
else:
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
cd_id,
Expand Down Expand Up @@ -410,7 +415,11 @@ def non_revoc(cred_def_id: str) -> IndyNonRevocationInterval:
revoc_support = False
if cd_id:
if profile:
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(profile)
else:
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
cd_id,
Expand Down
6 changes: 6 additions & 0 deletions aries_cloudagent/indy/models/tests/test_pres_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
IndyLedgerRequestsExecutor,
)
from ....messaging.util import canon
from ....multitenant.base import BaseMultitenantManager
from ....multitenant.manager import MultitenantManager
from ....protocols.didcomm_prefix import DIDCommPrefix


Expand Down Expand Up @@ -443,6 +445,10 @@ async def test_to_indy_proof_request_revo(self):
context.injector.bind_instance(
IndyLedgerRequestsExecutor, IndyLedgerRequestsExecutor(mock_profile)
)
context.injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
with async_mock.patch.object(
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
Expand Down
24 changes: 24 additions & 0 deletions aries_cloudagent/indy/tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from ...ledger.multiple_ledger.ledger_requests_executor import (
IndyLedgerRequestsExecutor,
)
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager

from .. import verifier as test_module
from ..verifier import IndyVerifier
Expand Down Expand Up @@ -332,6 +334,28 @@ def setUp(self):
self.verifier = MockVerifier()

async def test_check_timestamps(self):
# multitenant
mock_profile = InMemoryProfile.test_profile()
context = mock_profile.context
context.injector.bind_instance(
IndyLedgerRequestsExecutor,
IndyLedgerRequestsExecutor(mock_profile),
)
context.injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
with async_mock.patch.object(
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
await self.verifier.check_timestamps(
mock_profile,
INDY_PROOF_REQ_NAME,
INDY_PROOF_NAME,
REV_REG_DEFS,
)

# all clear, with timestamps
mock_profile = InMemoryProfile.test_profile()
context = mock_profile.context
Expand Down
7 changes: 6 additions & 1 deletion aries_cloudagent/indy/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
IndyLedgerRequestsExecutor,
)
from ..messaging.util import canon, encode
from ..multitenant.base import BaseMultitenantManager

from .models.xform import indy_proof_req2non_revoc_intervals

Expand Down Expand Up @@ -111,7 +112,11 @@ async def check_timestamps(
for (index, ident) in enumerate(pres["identifiers"]):
if ident.get("timestamp"):
cred_def_id = ident["cred_def_id"]
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(profile)
else:
ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
cred_def_id,
Expand Down
19 changes: 16 additions & 3 deletions aries_cloudagent/ledger/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
INT_EPOCH,
UUIDFour,
)
from ..multitenant.base import BaseMultitenantManager

from ..protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
Expand Down Expand Up @@ -369,7 +370,11 @@ async def get_nym_role(request: web.BaseRequest):
raise web.HTTPBadRequest(reason="Request query must include DID")

async with context.profile.session() as session:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
did,
txn_record_type=GET_NYM_ROLE,
Expand Down Expand Up @@ -442,7 +447,11 @@ async def get_did_verkey(request: web.BaseRequest):
raise web.HTTPBadRequest(reason="Request query must include DID")

async with context.profile.session() as session:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
did,
txn_record_type=GET_KEY_FOR_DID,
Expand Down Expand Up @@ -487,7 +496,11 @@ async def get_did_endpoint(request: web.BaseRequest):
raise web.HTTPBadRequest(reason="Request query must include DID")

async with context.profile.session() as session:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
did,
txn_record_type=GET_ENDPOINT_FOR_DID,
Expand Down
69 changes: 69 additions & 0 deletions aries_cloudagent/ledger/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
BaseMultipleLedgerManager,
MultipleLedgerManagerError,
)
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager

from .. import routes as test_module
from ..indy import Role
Expand Down Expand Up @@ -121,6 +123,29 @@ async def test_get_verkey_b(self):
)
assert result is json_response.return_value

async def test_get_verkey_multitenant(self):
self.context.injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
self.request.query = {"did": self.test_did}
with async_mock.patch.object(
IndyLedgerRequestsExecutor,
"get_ledger_for_identifier",
async_mock.CoroutineMock(return_value=("test_ledger_id", self.ledger)),
), async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as json_response:
self.ledger.get_key_for_did.return_value = self.test_verkey
result = await test_module.get_did_verkey(self.request)
json_response.assert_called_once_with(
{
"ledger_id": "test_ledger_id",
"verkey": self.ledger.get_key_for_did.return_value,
}
)
assert result is json_response.return_value

async def test_get_verkey_no_did(self):
self.request.query = {"no": "did"}
with self.assertRaises(test_module.web.HTTPBadRequest):
Expand Down Expand Up @@ -174,6 +199,29 @@ async def test_get_endpoint(self):
)
assert result is json_response.return_value

async def test_get_endpoint_multitenant(self):
self.context.injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
self.request.query = {"did": self.test_did}
with async_mock.patch.object(
IndyLedgerRequestsExecutor,
"get_ledger_for_identifier",
async_mock.CoroutineMock(return_value=("test_ledger_id", self.ledger)),
), async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as json_response:
self.ledger.get_endpoint_for_did.return_value = self.test_endpoint
result = await test_module.get_did_endpoint(self.request)
json_response.assert_called_once_with(
{
"ledger_id": "test_ledger_id",
"endpoint": self.ledger.get_endpoint_for_did.return_value,
}
)
assert result is json_response.return_value

async def test_get_endpoint_of_type_profile(self):
self.profile.context.injector.bind_instance(
IndyLedgerRequestsExecutor,
Expand Down Expand Up @@ -486,6 +534,27 @@ async def test_get_nym_role_b(self):
)
assert result is json_response.return_value

async def test_get_nym_role_multitenant(self):
self.context.injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
self.request.query = {"did": self.test_did}

with async_mock.patch.object(
IndyLedgerRequestsExecutor,
"get_ledger_for_identifier",
async_mock.CoroutineMock(return_value=("test_ledger_id", self.ledger)),
), async_mock.patch.object(
test_module.web, "json_response", async_mock.Mock()
) as json_response:
self.ledger.get_nym_role.return_value = Role.USER
result = await test_module.get_nym_role(self.request)
json_response.assert_called_once_with(
{"ledger_id": "test_ledger_id", "role": "USER"}
)
assert result is json_response.return_value

async def test_get_nym_role_bad_request(self):
self.request.query = {"no": "did"}
with self.assertRaises(test_module.web.HTTPBadRequest):
Expand Down
13 changes: 11 additions & 2 deletions aries_cloudagent/messaging/credential_definitions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
GET_CRED_DEF,
IndyLedgerRequestsExecutor,
)
from ...multitenant.base import BaseMultitenantManager
from ...protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
TransactionManagerError,
Expand Down Expand Up @@ -371,7 +372,11 @@ async def credential_definitions_get_credential_definition(request: web.BaseRequ
cred_def_id = request.match_info["cred_def_id"]

async with context.profile.session() as session:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
cred_def_id,
txn_record_type=GET_CRED_DEF,
Expand Down Expand Up @@ -416,7 +421,11 @@ async def credential_definitions_fix_cred_def_wallet_record(request: web.BaseReq

async with context.profile.session() as session:
storage = session.inject(BaseStorage)
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
cred_def_id,
txn_record_type=GET_CRED_DEF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ....ledger.multiple_ledger.ledger_requests_executor import (
IndyLedgerRequestsExecutor,
)
from ....multitenant.base import BaseMultitenantManager
from ....multitenant.manager import MultitenantManager
from ....storage.base import BaseStorage
from ....tails.base import BaseTailsServer

Expand Down Expand Up @@ -354,6 +356,28 @@ async def test_get_credential_definition(self):
}
)

async def test_get_credential_definition_multitenant(self):
self.profile_injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
self.request.match_info = {"cred_def_id": CRED_DEF_ID}
with async_mock.patch.object(
IndyLedgerRequestsExecutor,
"get_ledger_for_identifier",
async_mock.CoroutineMock(return_value=("test_ledger_id", self.ledger)),
), async_mock.patch.object(test_module.web, "json_response") as mock_response:
result = await test_module.credential_definitions_get_credential_definition(
self.request
)
assert result == mock_response.return_value
mock_response.assert_called_once_with(
{
"ledger_id": "test_ledger_id",
"credential_definition": {"cred": "def", "signed_txn": "..."},
}
)

async def test_get_credential_definition_no_ledger(self):
self.profile_injector.bind_instance(
IndyLedgerRequestsExecutor,
Expand Down
13 changes: 11 additions & 2 deletions aries_cloudagent/messaging/schemas/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
GET_SCHEMA,
IndyLedgerRequestsExecutor,
)
from ...multitenant.base import BaseMultitenantManager
from ...protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
TransactionManagerError,
Expand Down Expand Up @@ -355,7 +356,11 @@ async def schemas_get_schema(request: web.BaseRequest):
schema_id = request.match_info["schema_id"]

async with context.profile.session() as session:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
schema_id,
txn_record_type=GET_SCHEMA,
Expand Down Expand Up @@ -400,7 +405,11 @@ async def schemas_fix_schema_wallet_record(request: web.BaseRequest):

async with profile.session() as session:
storage = session.inject(BaseStorage)
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
multitenant_mgr = session.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(context.profile)
else:
ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor)
ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier(
schema_id,
txn_record_type=GET_SCHEMA,
Expand Down
22 changes: 22 additions & 0 deletions aries_cloudagent/messaging/schemas/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ....ledger.multiple_ledger.ledger_requests_executor import (
IndyLedgerRequestsExecutor,
)
from ....multitenant.base import BaseMultitenantManager
from ....multitenant.manager import MultitenantManager
from ....storage.base import BaseStorage

from .. import routes as test_module
Expand Down Expand Up @@ -324,6 +326,26 @@ async def test_get_schema(self):
}
)

async def test_get_schema_multitenant(self):
self.profile_injector.bind_instance(
BaseMultitenantManager,
async_mock.MagicMock(MultitenantManager, autospec=True),
)
self.request.match_info = {"schema_id": SCHEMA_ID}
with async_mock.patch.object(
IndyLedgerRequestsExecutor,
"get_ledger_for_identifier",
async_mock.CoroutineMock(return_value=("test_ledger_id", self.ledger)),
), async_mock.patch.object(test_module.web, "json_response") as mock_response:
result = await test_module.schemas_get_schema(self.request)
assert result == mock_response.return_value
mock_response.assert_called_once_with(
{
"ledger_id": "test_ledger_id",
"schema": {"schema": "def", "signed_txn": "..."},
}
)

async def test_get_schema_on_seq_no(self):
self.profile_injector.bind_instance(
IndyLedgerRequestsExecutor,
Expand Down
Loading

0 comments on commit 97192ec

Please sign in to comment.