Skip to content

Commit

Permalink
trying to get the session mocks working when not injecting the AnonCr…
Browse files Browse the repository at this point in the history
…edIssuer

Signed-off-by: Golda Velez <[email protected]>
  • Loading branch information
gvelez17 committed Mar 14, 2024
1 parent abe6adc commit 4705f1c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
12 changes: 10 additions & 2 deletions aries_cloudagent/core/in_memory/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ def test_profile(

@classmethod
def test_session(
cls, settings: Mapping[str, Any] = None, bind: Mapping[Type, Any] = None
cls,
settings: Mapping[str, Any] = None,
bind: Mapping[Type, Any] = None,
profile_class: Any = None
) -> "InMemoryProfileSession":
"""Used in tests to quickly create InMemoryProfileSession."""
session = InMemoryProfileSession(cls.test_profile(), settings=settings)
if profile_class is not None:
test_profile = cls.test_profile(profile_class=profile_class)
else:
test_profile = cls.test_profile()

session = InMemoryProfileSession(test_profile, settings=settings)
session._active = True
session._init_context()
if bind:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy
from time import time
from pprint import pprint
import json
import datetime
from unittest import IsolatedAsyncioTestCase
Expand All @@ -13,11 +14,17 @@
from .......ledger.multiple_ledger.ledger_requests_executor import (
IndyLedgerRequestsExecutor,
)
from anoncreds import CredentialDefinition
from aries_cloudagent.core.in_memory.profile import (
InMemoryProfile,
InMemoryProfileSession,
)
from aries_cloudagent.anoncreds.models.anoncreds_schema import AnonCredsSchema
from aries_cloudagent.wallet.did_info import DIDInfo
from aries_cloudagent.wallet.did_method import DIDMethod
from aries_cloudagent.wallet.key_type import KeyType
from aries_cloudagent.wallet.base import BaseWallet
from aries_cloudagent.storage.askar import AskarProfile
from aries_cloudagent.multitenant.askar_profile_manager import AskarAnoncredsProfile

from .......multitenant.base import BaseMultitenantManager
from .......multitenant.manager import MultitenantManager
Expand Down Expand Up @@ -118,13 +125,27 @@
class TestV20VCDICredFormatHandler(IsolatedAsyncioTestCase):
async def asyncSetUp(self):
# any required setup, see "formats/indy/tests/test_handler.py"
self.session = InMemoryProfile.test_session()
self.session = InMemoryProfile.test_session(profile_class=AskarAnoncredsProfile)
self.profile = self.session.profile
self.context = self.profile.context
self.askar_profile = mock.create_autospec(AskarProfile, instance=True)
self.context = self.session.profile.context

# self.profile = mock.MagicMock(spec=AskarAnoncredsProfile)
# self.profile.context = self.context
# self.session.profile = self.profile

setattr(self.profile, "session", mock.MagicMock(return_value=self.session))

# Issuer
self.patcher = mock.patch('aries_cloudagent.anoncreds.issuer.AnonCredsIssuer', autospec=True)
self.MockAnonCredsIssuer = self.patcher.start()
self.addCleanup(self.patcher.stop)

self.issuer = mock.create_autospec(AnonCredsIssuer, instance=True)
self.MockAnonCredsIssuer.return_value = self.issuer

self.issuer.profile = self.profile


# Wallet
self.public_did_info = mock.MagicMock()
self.public_did_info.did = 'mockedDID'
Expand Down Expand Up @@ -158,10 +179,6 @@ async def asyncSetUp(self):
self.cache = InMemoryCache()
self.context.injector.bind_instance(BaseCache, self.cache)

# Issuer
self.issuer = mock.MagicMock(AnonCredsIssuer, autospec=True)
self.issuer.profile = self.askar_profile
self.context.injector.bind_instance(AnonCredsIssuer, self.issuer)

# Holder
self.holder = mock.MagicMock(IndyHolder, autospec=True)
Expand Down Expand Up @@ -235,7 +252,12 @@ async def test_check_uniqueness(self):
# any required tests, see "formats/indy/tests/test_handler.py"
assert False

async def test_create_offer(self):
@mock.patch.object(InMemoryProfileSession, "handle")
async def test_create_offer(self, mock_session_handle):

mock_session_handle.fetch = mock.CoroutineMock(return_value=None)
#mock.MagicMock(spec=CredentialDefinition))

age = 24
d = datetime.date.today()
birth_date = datetime.date(d.year - age, d.month, d.day)
Expand Down Expand Up @@ -274,6 +296,14 @@ async def test_create_offer(self):
return_value=json.dumps(INDY_OFFER)
)


self.session._handle = mock.MagicMock()
self.session._handle.fetch = mock.CoroutineMock(return_value="Your mock fetch result")
print("In the test, session is:")
pprint(self.session)
print("In the test, session handle is:")
pprint(self.session.handle)

(cred_format, attachment) = await self.handler.create_offer(cred_proposal)

# this enforces the data format needed for alice-faber demo
Expand Down

0 comments on commit 4705f1c

Please sign in to comment.