github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/legacyconnection/models_test.go (about) 1 /* 2 Copyright Avast Software. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package legacyconnection 8 9 import ( 10 _ "embed" 11 "encoding/json" 12 "testing" 13 14 "github.com/btcsuite/btcutil/base58" 15 "github.com/stretchr/testify/require" 16 17 "github.com/hyperledger/aries-framework-go/pkg/doc/did" 18 ) 19 20 //nolint:gochecknoglobals 21 var ( 22 //go:embed testdata/valid_conn_data.jsonld 23 validConnectionData string 24 ) 25 26 // nolint:lll 27 func TestJSONConversion(t *testing.T) { 28 con, err := parseLegacyJSONBytes([]byte(validConnectionData)) 29 require.NoError(t, err) 30 require.NotEmpty(t, con) 31 32 require.Equal(t, con.DID, "did:example:21tDAKCERh95uGgKbJNHYp") 33 require.Equal(t, con.DIDDoc.ID, "did:example:21tDAKCERh95uGgKbJNHYp") 34 require.Contains(t, con.DIDDoc.Context, "https://w3id.org/did/v0.11") 35 require.Equal(t, con.DIDDoc.AlsoKnownAs[0], "did:example:123") 36 require.Equal(t, con.DIDDoc.VerificationMethod[0].Type, "Secp256k1VerificationKey2018") 37 require.Equal(t, base58.Encode(con.DIDDoc.VerificationMethod[0].Value), "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV") 38 require.Equal(t, con.DIDDoc.Authentication[0].VerificationMethod.Type, "Secp256k1VerificationKey2018") 39 require.Equal(t, base58.Encode(con.DIDDoc.Authentication[0].VerificationMethod.Value), "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV") 40 require.Equal(t, con.DIDDoc.Service[0].Type, "IndyAgent") 41 uri, err := con.DIDDoc.Service[0].ServiceEndpoint.URI() 42 require.NoError(t, err) 43 require.Contains(t, uri, "https://agent.example.com/") 44 require.Equal(t, con.DIDDoc.Service[0].RecipientKeys, []string{"did:example:123456789abcdefghi#key2"}) 45 require.Equal(t, con.DIDDoc.Service[0].RoutingKeys, []string{"did:example:123456789abcdefghi#key2"}) 46 47 conBytes, err := con.toLegacyJSONBytes() 48 require.NoError(t, err) 49 require.NotEmpty(t, conBytes) 50 51 rawString := string(conBytes) 52 53 require.Contains(t, rawString, "\"DID\":\"did:example:21tDAKCERh95uGgKbJNHYp\"") 54 require.Contains(t, rawString, "\"@context\":\"https://w3id.org/did/v1\"") 55 require.Contains(t, rawString, "\"controller\":\"did:example:123456789abcdefghi\",\"id\":\"did:example:123456789abcdefghi#keys-1\",\"publicKeyBase58\":\"H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV\",\"type\":\"Secp256k1VerificationKey2018\"") 56 require.Contains(t, rawString, "\"authentication\":[{\"publicKey\":\"did:example:123456789abcdefghi#keys-1\",\"type\":\"Secp256k1VerificationKey2018\"},{\"publicKey\":\"did:example:123456789abcdefghs#key3\",\"type\":\"RsaVerificationKey2018\"}]") 57 require.Contains(t, rawString, "\"service\":[{\"id\":\"did:example:123456789abcdefghi#did-communication\",\"priority\":0,\"recipientKeys\":[\"did:example:123456789abcdefghi#key2\"],\"routingKeys\":[\"did:example:123456789abcdefghi#key2\"],\"serviceEndpoint\":\"https://agent.example.com/\",\"type\":\"IndyAgent\"}]") 58 } 59 60 func TestToLegacyJSONBytes(t *testing.T) { 61 con := &Connection{ 62 DID: "did:example:21tDAKCERh95uGgKbJNHYp", 63 } 64 65 // Empty DIDDoc data 66 legacyDoc, err := con.toLegacyJSONBytes() 67 require.ErrorContains(t, err, "DIDDoc field cannot be empty") 68 require.Empty(t, legacyDoc) 69 70 // Success 71 72 con.DIDDoc = &did.Doc{ 73 Context: "https://w3id.org/did/v0.11", 74 ID: "did:example:21tDAKCERh95uGgKbJNHYp", 75 } 76 legacyDoc, err = con.toLegacyJSONBytes() 77 78 require.NoError(t, err) 79 require.NotEmpty(t, legacyDoc) 80 require.Contains(t, string(legacyDoc), "\"DID\":\"did:example:21tDAKCERh95uGgKbJNHYp\"") 81 require.Contains(t, string(legacyDoc), "\"@context\":\"https://w3id.org/did/v1\"") 82 } 83 84 func TestParseJSONBytes(t *testing.T) { 85 // Nil payload 86 conRaw, err := parseLegacyJSONBytes(nil) 87 require.ErrorContains(t, err, "JSON umarshalling of connection data bytes failed") 88 require.Empty(t, conRaw) 89 90 // Empty payload 91 conRaw, err = parseLegacyJSONBytes([]byte{}) 92 require.ErrorContains(t, err, "JSON umarshalling of connection data bytes failed") 93 require.Empty(t, conRaw) 94 95 // Empty DIDDoc 96 docBytes, err := json.Marshal(Connection{ 97 DID: "did:example:21tDAKCERh95uGgKbJNHYp", 98 }) 99 require.NoError(t, err) 100 require.NotEmpty(t, docBytes) 101 102 conRaw, err = parseLegacyJSONBytes(docBytes) 103 require.ErrorContains(t, err, "connection DIDDoc field is missed") 104 require.Empty(t, conRaw) 105 106 // Success 107 con, err := parseLegacyJSONBytes([]byte(validConnectionData)) 108 require.NoError(t, err) 109 require.NotEmpty(t, con) 110 }