import jsigs from 'jsonld-signatures' ; import { CapabilityDelegation } from 'ocapld' ; import { getMocks } from '../test-utils/getMocks' ; const { SECURITY_CONTEXT_V2_URL , sign , suites : { Ed25519Signature2018 } , } = jsigs ; let fastifyServer ; let invocationSigner ; let keyResolver ; let createRecipient ; let EdvClient ; let mock ; beforeAll ( async ( ) => { ( { fastifyServer , invocationSigner , keyResolver , EdvClient , mock , createRecipient } = await getMocks ( ) ) ; } ) ; afterAll ( async ( ) => { fastifyServer . close ( ) ; } ) ; let capabilityToRead ; let edvClient ; describe ( 'EdvClient revokeCapability API' , ( ) => { beforeAll ( async ( ) => { const { keyAgreementKey } = mock . keys ; edvClient = await mock . createEdv ( ) ; const testId = await EdvClient . generateId ( ) ; const doc = { id : testId , content : { someKey : 'someValue' } } ; const didKeys = [ await mock . createKeyAgreementKey ( ) ] ; const recipients = didKeys . map ( createRecipient ) ; recipients . unshift ( { header : { kid : keyAgreementKey . id , alg : 'ECDH-ES+A256KW' } } ) ; const inserted = await edvClient . insert ( { keyResolver , invocationSigner , doc , recipients } ) ; capabilityToRead = { '@context' : SECURITY_CONTEXT_V2_URL , id : `urn:uuid:${ await EdvClient . generateId ( ) }` , invocationTarget : `${ edvClient . id }/documents/${ inserted . id }` , invoker : didKeys [ 0 ] . id , allowedAction : 'read' , parentCapability : `${ edvClient . id }/zcaps/documents/${ inserted . id }` } ; const signer = mock . invocationSigner ; const { documentLoader } = mock ; const suite = new Ed25519Signature2018 ( { signer , verificationMethod : signer . id } ) ; const purpose = new CapabilityDelegation ( { capabilityChain : [ capabilityToRead . parentCapability ] } ) ; await sign ( capabilityToRead , { documentLoader , suite , purpose } ) ; } ) ; it ( 'returns TypeError on missing capabilityToRevoke param' , async ( ) => { let err ; let result ; try { result = await edvClient . revokeCapability ( { } ) ; } catch ( e ) { err = e ; } expect ( err ) . toBeDefined ( ) ; expect ( result ) . toBeFalsy ( ) ; expect ( err . name ) . toBe ( 'TypeError' ) ; expect ( err . message ) . toEqual ( 'capabilityToRevoke' ) ; } ) ; it ( 'returns TypeError on missing invocationSigner param' , async ( ) => { let err ; let result ; try { result = await edvClient . revokeCapability ( { capabilityToRevoke : capabilityToRead } ) ; } catch ( e ) { err = e ; } expect ( err ) . toBeDefined ( ) ; expect ( result ) . toBeFalsy ( ) ; expect ( err . name ) . toBe ( 'TypeError' ) ; expect ( err . message ) . toEqual ( 'invocationSigner' ) ; } ) ; it ( 'should revoke a delegated capability' , async ( ) => { let err ; let result ; try { result = await edvClient . revokeCapability ( { capabilityToRevoke : capabilityToRead , invocationSigner : mock . invocationSigner } ) ; } catch ( e ) { err = e ; } expect ( err ) . toBeFalsy ( ) ; expect ( result ) . toBeFalsy ( ) ; } ) ; } ) ;