github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/issuecredential/params_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package issuecredential 8 9 import ( 10 "encoding/json" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 16 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 17 ) 18 19 const ( 20 commentText = "this is a comment" 21 goalCodeText = "goal_code" 22 messageIDText = "message-id-123" 23 ) 24 25 func previewCredV2() PreviewCredential { 26 return PreviewCredential{ 27 Type: CredentialPreviewMsgTypeV2, 28 Attributes: []Attribute{ 29 { 30 Name: "attribute 1", 31 MimeType: "plaintext", 32 Value: "foo bar", 33 }, 34 { 35 Name: "attribute 2", 36 MimeType: "application/json", 37 Value: `{"foo":"bar"}`, 38 }, 39 }, 40 } 41 } 42 43 func previewCredV3(t *testing.T) map[string]interface{} { 44 t.Helper() 45 46 prev := PreviewCredentialV3{ 47 Type: CredentialPreviewMsgTypeV3, 48 ID: "bar-baz-qux", 49 Body: IssueCredentialV3Body{ 50 GoalCode: "goal_code", 51 ReplacementID: "blah-id", 52 Comment: commentText, 53 }, 54 } 55 56 prevBytes, err := json.Marshal(&prev) 57 require.NoError(t, err) 58 59 prevMap := map[string]interface{}{} 60 61 require.NoError(t, json.Unmarshal(prevBytes, &prevMap)) 62 63 return prevMap 64 } 65 66 func formatList() []Format { 67 return []Format{ 68 { 69 AttachID: "attach-1", 70 Format: "foo", 71 }, 72 } 73 } 74 75 func attachV1List() []decorator.Attachment { 76 return []decorator.Attachment{ 77 { 78 ID: "attach-1", 79 Data: decorator.AttachmentData{}, 80 }, 81 } 82 } 83 84 func attachV2List() []decorator.AttachmentV2 { 85 return []decorator.AttachmentV2{ 86 { 87 ID: "attachv2-1", 88 Data: decorator.AttachmentData{}, 89 }, 90 } 91 } 92 93 func TestProposeCredentialParams(t *testing.T) { 94 t.Run("from&to v2", func(t *testing.T) { 95 src := ProposeCredentialV2{ 96 Type: ProposeCredentialMsgTypeV2, 97 Comment: commentText, 98 CredentialProposal: previewCredV2(), 99 Formats: formatList(), 100 FiltersAttach: attachV1List(), 101 } 102 103 srcBytes, err := json.Marshal(src) 104 require.NoError(t, err) 105 106 params := ProposeCredentialParams{} 107 108 err = json.Unmarshal(srcBytes, ¶ms) 109 require.NoError(t, err) 110 111 dst := params.AsV2() 112 113 require.Equal(t, &src, dst) 114 115 srcMsg := service.NewDIDCommMsgMap(src) 116 117 params = ProposeCredentialParams{} 118 119 err = srcMsg.Decode(¶ms) 120 require.NoError(t, err) 121 122 dst = params.AsV2() 123 124 require.Equal(t, &src, dst) 125 }) 126 127 t.Run("from&to v3", func(t *testing.T) { 128 src := ProposeCredentialV3{ 129 Type: ProposeCredentialMsgTypeV3, 130 ID: messageIDText, 131 Body: ProposeCredentialV3Body{ 132 GoalCode: goalCodeText, 133 Comment: commentText, 134 CredentialPreview: previewCredV3(t), 135 }, 136 Attachments: attachV2List(), 137 } 138 139 srcBytes, err := json.Marshal(src) 140 require.NoError(t, err) 141 142 params := ProposeCredentialParams{} 143 144 err = json.Unmarshal(srcBytes, ¶ms) 145 require.NoError(t, err) 146 147 dst := params.AsV3() 148 149 require.Equal(t, &src, dst) 150 151 srcMsg := service.NewDIDCommMsgMap(src) 152 153 params = ProposeCredentialParams{} 154 155 err = srcMsg.Decode(¶ms) 156 require.NoError(t, err) 157 158 dst = params.AsV3() 159 160 require.Equal(t, &src, dst) 161 }) 162 } 163 164 func TestOfferCredentialParams(t *testing.T) { 165 t.Run("from&to v2", func(t *testing.T) { 166 src := OfferCredentialV2{ 167 Type: OfferCredentialMsgTypeV2, 168 Comment: commentText, 169 CredentialPreview: previewCredV2(), 170 Formats: formatList(), 171 OffersAttach: attachV1List(), 172 } 173 174 srcBytes, err := json.Marshal(src) 175 require.NoError(t, err) 176 177 params := OfferCredentialParams{} 178 179 err = json.Unmarshal(srcBytes, ¶ms) 180 require.NoError(t, err) 181 182 dst := params.AsV2() 183 184 require.Equal(t, &src, dst) 185 186 srcMsg := service.NewDIDCommMsgMap(src) 187 188 params = OfferCredentialParams{} 189 190 err = srcMsg.Decode(¶ms) 191 require.NoError(t, err) 192 193 dst = params.AsV2() 194 195 require.Equal(t, &src, dst) 196 }) 197 198 t.Run("from&to v3", func(t *testing.T) { 199 src := OfferCredentialV3{ 200 Type: OfferCredentialMsgTypeV3, 201 ID: messageIDText, 202 Body: OfferCredentialV3Body{ 203 GoalCode: goalCodeText, 204 Comment: commentText, 205 ReplacementID: "replace-me-1", 206 CredentialPreview: previewCredV3(t), 207 }, 208 Attachments: attachV2List(), 209 } 210 211 srcBytes, err := json.Marshal(src) 212 require.NoError(t, err) 213 214 params := OfferCredentialParams{} 215 216 err = json.Unmarshal(srcBytes, ¶ms) 217 require.NoError(t, err) 218 219 dst := params.AsV3() 220 221 require.Equal(t, &src, dst) 222 223 srcMsg := service.NewDIDCommMsgMap(src) 224 225 params = OfferCredentialParams{} 226 227 err = srcMsg.Decode(¶ms) 228 require.NoError(t, err) 229 230 dst = params.AsV3() 231 232 require.Equal(t, &src, dst) 233 }) 234 } 235 236 func TestRequestCredentialParams(t *testing.T) { 237 t.Run("from&to v2", func(t *testing.T) { 238 src := RequestCredentialV2{ 239 Type: RequestCredentialMsgTypeV2, 240 Comment: commentText, 241 Formats: formatList(), 242 RequestsAttach: attachV1List(), 243 } 244 245 srcBytes, err := json.Marshal(src) 246 require.NoError(t, err) 247 248 params := RequestCredentialParams{} 249 250 err = json.Unmarshal(srcBytes, ¶ms) 251 require.NoError(t, err) 252 253 dst := params.AsV2() 254 255 require.Equal(t, &src, dst) 256 257 srcMsg := service.NewDIDCommMsgMap(src) 258 259 params = RequestCredentialParams{} 260 261 err = srcMsg.Decode(¶ms) 262 require.NoError(t, err) 263 264 dst = params.AsV2() 265 266 require.Equal(t, &src, dst) 267 }) 268 269 t.Run("from&to v3", func(t *testing.T) { 270 src := RequestCredentialV3{ 271 Type: RequestCredentialMsgTypeV3, 272 ID: messageIDText, 273 Body: RequestCredentialV3Body{ 274 GoalCode: goalCodeText, 275 Comment: commentText, 276 }, 277 Attachments: attachV2List(), 278 } 279 280 srcBytes, err := json.Marshal(src) 281 require.NoError(t, err) 282 283 params := RequestCredentialParams{} 284 285 err = json.Unmarshal(srcBytes, ¶ms) 286 require.NoError(t, err) 287 288 dst := params.AsV3() 289 290 require.Equal(t, &src, dst) 291 292 srcMsg := service.NewDIDCommMsgMap(src) 293 294 params = RequestCredentialParams{} 295 296 err = srcMsg.Decode(¶ms) 297 require.NoError(t, err) 298 299 dst = params.AsV3() 300 301 require.Equal(t, &src, dst) 302 }) 303 } 304 305 func TestIssueCredentialParams(t *testing.T) { 306 t.Run("from&to v2", func(t *testing.T) { 307 src := IssueCredentialV2{ 308 Type: IssueCredentialMsgTypeV2, 309 Comment: commentText, 310 Formats: formatList(), 311 CredentialsAttach: attachV1List(), 312 } 313 314 srcBytes, err := json.Marshal(src) 315 require.NoError(t, err) 316 317 params := IssueCredentialParams{} 318 319 err = json.Unmarshal(srcBytes, ¶ms) 320 require.NoError(t, err) 321 322 dst := params.AsV2() 323 324 require.Equal(t, &src, dst) 325 326 srcMsg := service.NewDIDCommMsgMap(src) 327 328 params = IssueCredentialParams{} 329 330 err = srcMsg.Decode(¶ms) 331 require.NoError(t, err) 332 333 dst = params.AsV2() 334 335 require.Equal(t, &src, dst) 336 }) 337 338 t.Run("from&to v3", func(t *testing.T) { 339 src := IssueCredentialV3{ 340 Type: IssueCredentialMsgTypeV3, 341 ID: messageIDText, 342 Body: IssueCredentialV3Body{ 343 GoalCode: goalCodeText, 344 ReplacementID: "replace", 345 Comment: commentText, 346 }, 347 Attachments: attachV2List(), 348 } 349 350 srcBytes, err := json.Marshal(src) 351 require.NoError(t, err) 352 353 params := IssueCredentialParams{} 354 355 err = json.Unmarshal(srcBytes, ¶ms) 356 require.NoError(t, err) 357 358 dst := params.AsV3() 359 360 require.Equal(t, &src, dst) 361 362 srcMsg := service.NewDIDCommMsgMap(src) 363 364 params = IssueCredentialParams{} 365 366 err = srcMsg.Decode(¶ms) 367 require.NoError(t, err) 368 369 dst = params.AsV3() 370 371 require.Equal(t, &src, dst) 372 }) 373 }