44from unittest import IsolatedAsyncioTestCase
55
66import pytest
7+ from anoncreds import CredentialDefinition
78from marshmallow import ValidationError
89
910from .......anoncreds .holder import AnonCredsHolder
1011from .......anoncreds .issuer import AnonCredsIssuer
12+ from .......anoncreds .models .credential_definition import (
13+ CredDef ,
14+ CredDefValue ,
15+ CredDefValuePrimary ,
16+ )
17+ from .......anoncreds .registry import AnonCredsRegistry
1118from .......anoncreds .revocation import AnonCredsRevocationRegistryFullError
1219from .......cache .base import BaseCache
1320from .......cache .in_memory import InMemoryCache
21+ from .......config .provider import ClassProvider
22+ from .......indy .credx .issuer import CATEGORY_CRED_DEF
1423from .......ledger .base import BaseLedger
1524from .......ledger .multiple_ledger .ledger_requests_executor import (
1625 IndyLedgerRequestsExecutor ,
193202
194203class TestV20AnonCredsCredFormatHandler (IsolatedAsyncioTestCase ):
195204 async def asyncSetUp (self ):
196- self .profile = await create_test_profile ()
205+ self .profile = await create_test_profile (
206+ {
207+ "wallet.type" : "askar-anoncreds" ,
208+ }
209+ )
197210 self .context = self .profile .context
198211
212+ # Context
213+ self .cache = InMemoryCache ()
214+ self .profile .context .injector .bind_instance (BaseCache , self .cache )
215+
216+ # Issuer
217+ self .issuer = mock .MagicMock (AnonCredsIssuer , autospec = True )
218+ self .profile .context .injector .bind_instance (AnonCredsIssuer , self .issuer )
219+
220+ # Holder
221+ self .holder = mock .MagicMock (AnonCredsHolder , autospec = True )
222+ self .profile .context .injector .bind_instance (AnonCredsHolder , self .holder )
223+
224+ # Anoncreds registry
225+ self .profile .context .injector .bind_instance (
226+ AnonCredsRegistry , AnonCredsRegistry ()
227+ )
228+ registry = self .profile .context .inject_or (AnonCredsRegistry )
229+ legacy_indy_registry = ClassProvider (
230+ "acapy_agent.anoncreds.default.legacy_indy.registry.LegacyIndyRegistry" ,
231+ # supported_identifiers=[],
232+ # method_name="",
233+ ).provide (self .profile .context .settings , self .profile .context .injector )
234+ await legacy_indy_registry .setup (self .profile .context )
235+ registry .register (legacy_indy_registry )
236+
199237 # Ledger
200238 self .ledger = mock .MagicMock (BaseLedger , autospec = True )
201239 self .ledger .get_schema = mock .CoroutineMock (return_value = SCHEMA )
@@ -214,18 +252,6 @@ async def asyncSetUp(self):
214252 )
215253 ),
216254 )
217- # Context
218- self .cache = InMemoryCache ()
219- self .profile .context .injector .bind_instance (BaseCache , self .cache )
220-
221- # Issuer
222- self .issuer = mock .MagicMock (AnonCredsIssuer , autospec = True )
223- self .profile .context .injector .bind_instance (AnonCredsIssuer , self .issuer )
224-
225- # Holder
226- self .holder = mock .MagicMock (AnonCredsHolder , autospec = True )
227- self .profile .context .injector .bind_instance (AnonCredsHolder , self .holder )
228-
229255 self .handler = AnonCredsCredFormatHandler (self .profile )
230256 assert self .handler .profile
231257
@@ -338,68 +364,123 @@ async def test_receive_proposal(self):
338364 # Not much to assert. Receive proposal doesn't do anything
339365 await self .handler .receive_proposal (cred_ex_record , cred_proposal_message )
340366
341- @pytest .mark .skip (reason = "Anoncreds-break" )
342- async def test_create_offer (self ):
343- schema_id_parts = SCHEMA_ID .split (":" )
344-
345- cred_preview = V20CredPreview (
346- attributes = (
347- V20CredAttrSpec (name = "legalName" , value = "value" ),
348- V20CredAttrSpec (name = "jurisdictionId" , value = "value" ),
349- V20CredAttrSpec (name = "incorporationDate" , value = "value" ),
350- )
351- )
352-
353- cred_proposal = V20CredProposal (
354- credential_preview = cred_preview ,
355- formats = [
356- V20CredFormat (
357- attach_id = "0" ,
358- format_ = ATTACHMENT_FORMAT [CRED_20_PROPOSAL ][
359- V20CredFormat .Format .ANONCREDS .api
367+ async def test_create_offer_cant_find_schema_in_wallet_or_data_registry (self ):
368+ with self .assertRaises (V20CredFormatError ):
369+ await self .handler .create_offer (
370+ V20CredProposal (
371+ formats = [
372+ V20CredFormat (
373+ attach_id = "0" ,
374+ format_ = ATTACHMENT_FORMAT [CRED_20_PROPOSAL ][
375+ V20CredFormat .Format .ANONCREDS .api
376+ ],
377+ )
378+ ],
379+ filters_attach = [
380+ AttachDecorator .data_base64 (
381+ {"cred_def_id" : CRED_DEF_ID }, ident = "0"
382+ )
360383 ],
361384 )
362- ],
363- filters_attach = [
364- AttachDecorator .data_base64 ({"cred_def_id" : CRED_DEF_ID }, ident = "0" )
365- ],
366- )
385+ )
367386
368- cred_def_record = StorageRecord (
369- CRED_DEF_SENT_RECORD_TYPE ,
370- CRED_DEF_ID ,
371- {
372- "schema_id" : SCHEMA_ID ,
373- "schema_issuer_did" : schema_id_parts [0 ],
374- "schema_name" : schema_id_parts [- 2 ],
375- "schema_version" : schema_id_parts [- 1 ],
376- "issuer_did" : TEST_DID ,
377- "cred_def_id" : CRED_DEF_ID ,
378- "epoch" : str (int (time ())),
379- },
387+ @mock .patch .object (
388+ AnonCredsRegistry ,
389+ "get_schema" ,
390+ mock .CoroutineMock (
391+ return_value = mock .MagicMock (schema = mock .MagicMock (attr_names = ["score" ]))
392+ ),
393+ )
394+ @mock .patch .object (
395+ AnonCredsIssuer ,
396+ "create_credential_offer" ,
397+ mock .CoroutineMock (return_value = json .dumps (ANONCREDS_OFFER )),
398+ )
399+ @mock .patch .object (
400+ CredentialDefinition ,
401+ "load" ,
402+ mock .MagicMock (to_dict = mock .MagicMock (return_value = {"schemaId" : SCHEMA_ID })),
403+ )
404+ async def test_create_offer (self ):
405+ self .issuer .create_credential_offer = mock .CoroutineMock ({})
406+ # With a schema_id
407+ await self .handler .create_offer (
408+ V20CredProposal (
409+ credential_preview = V20CredPreview (
410+ attributes = (V20CredAttrSpec (name = "score" , value = "0" ),)
411+ ),
412+ formats = [
413+ V20CredFormat (
414+ attach_id = "0" ,
415+ format_ = ATTACHMENT_FORMAT [CRED_20_PROPOSAL ][
416+ V20CredFormat .Format .ANONCREDS .api
417+ ],
418+ )
419+ ],
420+ filters_attach = [
421+ AttachDecorator .data_base64 (
422+ {"cred_def_id" : CRED_DEF_ID , "schema_id" : SCHEMA_ID }, ident = "0"
423+ )
424+ ],
425+ )
380426 )
381- await self .session .storage .add_record (cred_def_record )
382-
383- self .issuer .create_credential_offer = mock .CoroutineMock (
384- return_value = json .dumps (ANONCREDS_OFFER )
427+ # Only with cred_def_id
428+ async with self .profile .session () as session :
429+ await session .handle .insert (
430+ CATEGORY_CRED_DEF ,
431+ CRED_DEF_ID ,
432+ CredDef (
433+ issuer_id = TEST_DID ,
434+ schema_id = SCHEMA_ID ,
435+ tag = "tag" ,
436+ type = "CL" ,
437+ value = CredDefValue (
438+ primary = CredDefValuePrimary ("n" , "s" , {}, "rctxt" , "z" )
439+ ),
440+ ).to_json (),
441+ tags = {},
442+ )
443+ await self .handler .create_offer (
444+ V20CredProposal (
445+ credential_preview = V20CredPreview (
446+ attributes = (V20CredAttrSpec (name = "score" , value = "0" ),)
447+ ),
448+ formats = [
449+ V20CredFormat (
450+ attach_id = "0" ,
451+ format_ = ATTACHMENT_FORMAT [CRED_20_PROPOSAL ][
452+ V20CredFormat .Format .ANONCREDS .api
453+ ],
454+ )
455+ ],
456+ filters_attach = [
457+ AttachDecorator .data_base64 ({"cred_def_id" : CRED_DEF_ID }, ident = "0" )
458+ ],
459+ )
385460 )
386-
387- (cred_format , attachment ) = await self .handler .create_offer (cred_proposal )
388-
389- self .issuer .create_credential_offer .assert_called_once_with (CRED_DEF_ID )
390-
391- # assert identifier match
392- assert cred_format .attach_id == self .handler .format .api == attachment .ident
393-
394- # assert content of attachment is proposal data
395- assert attachment .content == ANONCREDS_OFFER
396-
397- # assert data is encoded as base64
398- assert attachment .data .base64
399-
400- self .issuer .create_credential_offer .reset_mock ()
401- await self .handler .create_offer (cred_proposal )
402- self .issuer .create_credential_offer .assert_not_called ()
461+ # Wrong attribute name
462+ with self .assertRaises (V20CredFormatError ):
463+ await self .handler .create_offer (
464+ V20CredProposal (
465+ credential_preview = V20CredPreview (
466+ attributes = (V20CredAttrSpec (name = "wrong" , value = "0" ),)
467+ ),
468+ formats = [
469+ V20CredFormat (
470+ attach_id = "0" ,
471+ format_ = ATTACHMENT_FORMAT [CRED_20_PROPOSAL ][
472+ V20CredFormat .Format .ANONCREDS .api
473+ ],
474+ )
475+ ],
476+ filters_attach = [
477+ AttachDecorator .data_base64 (
478+ {"cred_def_id" : CRED_DEF_ID , "schema_id" : SCHEMA_ID },
479+ ident = "0" ,
480+ )
481+ ],
482+ )
483+ )
403484
404485 @pytest .mark .skip (reason = "Anoncreds-break" )
405486 async def test_create_offer_no_cache (self ):
0 commit comments