github.com/hyperledger/aries-framework-go@v0.3.2/pkg/wallet/invitation_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package wallet 8 9 import ( 10 "encoding/base64" 11 "encoding/json" 12 "fmt" 13 "strings" 14 "testing" 15 16 "github.com/stretchr/testify/require" 17 18 "github.com/hyperledger/aries-framework-go/pkg/client/outofband" 19 ) 20 21 const ( 22 testID = "abc123" 23 testTypeV1 = "https://didcomm.org/out-of-band/1.0/invitation" 24 testTypeV2 = "https://didcomm.org/out-of-band/2.0/invitation" 25 ) 26 27 func v1Message(id, typ string, attached []string) string { 28 template := `{ 29 "@id": "%s", 30 "@type": "%s", 31 "request~attach":[%s] 32 }` 33 34 attachments := make([]string, len(attached)) 35 36 for i := 0; i < len(attached); i++ { 37 attachments[i] = fmt.Sprintf(`{ 38 "data":{ 39 "base64":"%s" 40 } 41 }`, base64.StdEncoding.EncodeToString([]byte(attached[i]))) 42 } 43 44 reqs := strings.Join(attachments, ",") 45 46 return fmt.Sprintf(template, id, typ, reqs) 47 } 48 49 func v2Message(id, typ string, attached []string) string { 50 template := `{ 51 "id": "%s", 52 "type": "%s", 53 "attachments":[%s] 54 }` 55 56 attachments := make([]string, len(attached)) 57 58 for i := 0; i < len(attached); i++ { 59 attachments[i] = fmt.Sprintf(`{ 60 "data":{ 61 "base64":"%s" 62 } 63 }`, base64.StdEncoding.EncodeToString([]byte(attached[i]))) 64 } 65 66 reqs := strings.Join(attachments, ",") 67 68 return fmt.Sprintf(template, id, typ, reqs) 69 } 70 71 func TestGenericInvitation_UnmarshalJSON(t *testing.T) { 72 t.Run("success: from empty invitation", func(t *testing.T) { 73 rawV1 := "{}" 74 75 invitation := GenericInvitation{} 76 err := json.Unmarshal([]byte(rawV1), &invitation) 77 require.NoError(t, err) 78 }) 79 80 t.Run("success: from v1 invitation", func(t *testing.T) { 81 attachments := []string{"lorem", "ipsum"} 82 83 rawV1 := v1Message(testID, testTypeV1, attachments) 84 85 invitation := GenericInvitation{} 86 err := json.Unmarshal([]byte(rawV1), &invitation) 87 require.NoError(t, err) 88 require.Equal(t, testID, invitation.ID) 89 require.Equal(t, testTypeV1, invitation.Type) 90 require.Len(t, invitation.Requests, len(attachments)) 91 92 for i, expected := range attachments { 93 data, err := invitation.Requests[i].Data.Fetch() 94 require.NoError(t, err) 95 96 actual := string(data) 97 require.Equal(t, expected, actual) 98 } 99 }) 100 101 t.Run("success: from v2 invitation", func(t *testing.T) { 102 attachments := []string{"lorem", "ipsum"} 103 104 rawV2 := v2Message(testID, testTypeV1, attachments) 105 106 invitation := GenericInvitation{} 107 err := json.Unmarshal([]byte(rawV2), &invitation) 108 require.NoError(t, err) 109 require.Equal(t, testID, invitation.ID) 110 require.Equal(t, testTypeV1, invitation.Type) 111 require.Len(t, invitation.Requests, len(attachments)) 112 113 for i, expected := range attachments { 114 data, err := invitation.Requests[i].Data.Fetch() 115 require.NoError(t, err) 116 117 actual := string(data) 118 require.Equal(t, expected, actual) 119 } 120 }) 121 122 t.Run("failure: unmarshal error", func(t *testing.T) { 123 invitation := GenericInvitation{} 124 err := json.Unmarshal([]byte("uh oh"), &invitation) 125 require.Error(t, err) 126 127 err = json.Unmarshal([]byte(`{"id":["shouldn't'", "be", "a", "list"]}`), &invitation) 128 require.Error(t, err) 129 }) 130 } 131 132 func TestGenericInvitation_MarshalJSON(t *testing.T) { 133 t.Run("success: v1", func(t *testing.T) { 134 attachments := []string{"lorem", "ipsum"} 135 136 rawV1 := []byte(v1Message(testID, testTypeV1, attachments)) 137 138 v1Inv := outofband.Invitation{} 139 140 require.NoError(t, json.Unmarshal(rawV1, &v1Inv)) 141 142 var err error 143 rawV1, err = json.Marshal(&v1Inv) 144 require.NoError(t, err) 145 146 expected := map[string]interface{}{} 147 actual := map[string]interface{}{} 148 149 require.NoError(t, json.Unmarshal(rawV1, &expected)) 150 151 invitation := GenericInvitation{} 152 err = json.Unmarshal(rawV1, &invitation) 153 require.NoError(t, err) 154 155 invBytes, err := invitation.MarshalJSON() 156 require.NoError(t, err) 157 158 require.NoError(t, json.Unmarshal(invBytes, &actual)) 159 160 require.Equal(t, expected, actual) 161 }) 162 163 t.Run("success: from v2 invitation", func(t *testing.T) { 164 attachments := []string{"lorem", "ipsum"} 165 166 rawV2 := []byte(v2Message(testID, testTypeV2, attachments)) 167 168 invitation := GenericInvitation{} 169 err := json.Unmarshal(rawV2, &invitation) 170 require.NoError(t, err) 171 172 _, err = json.Marshal(&invitation) 173 require.NoError(t, err) 174 }) 175 } 176 177 func TestGenericInvitation_AsV1(t *testing.T) { 178 attachments := []string{"lorem", "ipsum"} 179 180 rawV1 := v1Message(testID, testTypeV1, attachments) 181 182 invitation := GenericInvitation{} 183 err := json.Unmarshal([]byte(rawV1), &invitation) 184 require.NoError(t, err) 185 186 invV1 := invitation.AsV1() 187 require.Equal(t, testID, invV1.ID) 188 require.Equal(t, testTypeV1, invV1.Type) 189 require.Len(t, invV1.Requests, len(attachments)) 190 191 for i, expected := range attachments { 192 data, err := invV1.Requests[i].Data.Fetch() 193 require.NoError(t, err) 194 195 actual := string(data) 196 require.Equal(t, expected, actual) 197 } 198 } 199 200 func TestGenericInvitation_AsV2(t *testing.T) { 201 attachments := []string{"lorem", "ipsum"} 202 203 rawV1 := v2Message(testID, testTypeV2, attachments) 204 205 invitation := GenericInvitation{} 206 err := json.Unmarshal([]byte(rawV1), &invitation) 207 require.NoError(t, err) 208 209 invV2 := invitation.AsV2() 210 require.Equal(t, testID, invV2.ID) 211 require.Equal(t, testTypeV2, invV2.Type) 212 require.Len(t, invV2.Requests, len(attachments)) 213 214 for i, expected := range attachments { 215 data, err := invV2.Requests[i].Data.Fetch() 216 require.NoError(t, err) 217 218 actual := string(data) 219 require.Equal(t, expected, actual) 220 } 221 }