github.com/hyperledger/aries-framework-go@v0.3.2/pkg/store/did/didconnection_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package did 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 15 vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr" 16 mockdiddoc "github.com/hyperledger/aries-framework-go/pkg/mock/diddoc" 17 mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage" 18 mockvdr "github.com/hyperledger/aries-framework-go/pkg/mock/vdr" 19 "github.com/hyperledger/aries-framework-go/spi/storage" 20 ) 21 22 type ctx struct { 23 store storage.Provider 24 vdr vdrapi.Registry 25 } 26 27 func (c *ctx) StorageProvider() storage.Provider { 28 return c.store 29 } 30 31 func (c *ctx) VDRegistry() vdrapi.Registry { 32 return c.vdr 33 } 34 35 func TestBaseConnectionStore(t *testing.T) { 36 prov := ctx{ 37 store: mockstorage.NewMockStoreProvider(), 38 vdr: &mockvdr.MockVDRegistry{ 39 CreateValue: mockdiddoc.GetMockDIDDoc(t, false), 40 ResolveValue: mockdiddoc.GetMockDIDDoc(t, false), 41 }, 42 } 43 44 t.Run("New", func(t *testing.T) { 45 _, err := NewConnectionStore(&prov) 46 require.NoError(t, err) 47 48 _, err = NewConnectionStore(&ctx{ 49 store: &mockstorage.MockStoreProvider{ 50 ErrOpenStoreHandle: fmt.Errorf("store error"), 51 }, 52 }) 53 require.Error(t, err) 54 require.Contains(t, err.Error(), "store error") 55 }) 56 57 t.Run("SaveDID error", func(t *testing.T) { 58 cs, err := NewConnectionStore(&ctx{ 59 store: &mockstorage.MockStoreProvider{ 60 Store: &mockstorage.MockStore{ 61 Store: map[string]mockstorage.DBEntry{}, 62 ErrPut: fmt.Errorf("put error"), 63 }, 64 }, 65 vdr: &mockvdr.MockVDRegistry{ 66 CreateValue: mockdiddoc.GetMockDIDDoc(t, false), 67 ResolveValue: mockdiddoc.GetMockDIDDoc(t, false), 68 }, 69 }) 70 require.NoError(t, err) 71 72 err = cs.SaveDID("did", "key") 73 require.Error(t, err) 74 require.Contains(t, err.Error(), "put error") 75 }) 76 77 t.Run("SaveDID + GetDID", func(t *testing.T) { 78 connStore, err := NewConnectionStore(&prov) 79 require.NoError(t, err) 80 81 err = connStore.SaveDID("did:abcde", "abcde") 82 require.NoError(t, err) 83 84 didVal, err := connStore.GetDID("abcde") 85 require.NoError(t, err) 86 require.Equal(t, "did:abcde", didVal) 87 88 wrong, err := connStore.GetDID("fhtagn") 89 require.EqualError(t, err, ErrNotFound.Error()) 90 require.Equal(t, "", wrong) 91 92 err = connStore.store.Put("bad-data", []byte("aaooga")) 93 require.NoError(t, err) 94 95 _, err = connStore.GetDID("bad-data") 96 require.Error(t, err) 97 require.Contains(t, err.Error(), "invalid character") 98 }) 99 100 t.Run("SaveDIDFromDoc", func(t *testing.T) { 101 connStore, err := NewConnectionStore(&prov) 102 require.NoError(t, err) 103 104 err = connStore.SaveDIDFromDoc(mockdiddoc.GetMockDIDDocWithKeyAgreements(t)) 105 require.NoError(t, err) 106 }) 107 108 t.Run("SaveDIDFromDoc with invalid DIDCommService type does not link keys to the DID", func(t *testing.T) { 109 connStore, err := NewConnectionStore(&prov) 110 require.NoError(t, err) 111 112 mDIDDoc := mockdiddoc.GetMockDIDDocWithKeyAgreements(t) 113 mDIDDoc.Service[0].Type = "invalid" 114 115 err = connStore.SaveDIDFromDoc(mDIDDoc) 116 require.NoError(t, err) 117 118 _, err = connStore.GetDID(mDIDDoc.KeyAgreement[0].VerificationMethod.ID) 119 require.EqualError(t, err, "did not found under given key") 120 }) 121 122 t.Run("SaveDIDByResolving success", func(t *testing.T) { 123 cs, err := NewConnectionStore(&prov) 124 require.NoError(t, err) 125 126 err = cs.SaveDIDByResolving(mockdiddoc.GetMockDIDDoc(t, false).ID) 127 require.NoError(t, err) 128 }) 129 130 t.Run("SaveDIDByResolving error", func(t *testing.T) { 131 prov := ctx{ 132 store: mockstorage.NewMockStoreProvider(), 133 vdr: &mockvdr.MockVDRegistry{ResolveErr: fmt.Errorf("resolve error")}, 134 } 135 136 cs, err := NewConnectionStore(&prov) 137 require.NoError(t, err) 138 139 err = cs.SaveDIDByResolving("did") 140 require.Error(t, err) 141 require.Contains(t, err.Error(), "resolve error") 142 }) 143 }