github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/presentproof/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 presentproof 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 17 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 18 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 19 ) 20 21 const foo = "foo" 22 23 func formatList() []Format { 24 return []Format{ 25 { 26 AttachID: "attach-1", 27 Format: "foo", 28 }, 29 } 30 } 31 32 func attachV1List() []decorator.Attachment { 33 return []decorator.Attachment{ 34 { 35 ID: "attach-1", 36 Data: decorator.AttachmentData{}, 37 }, 38 } 39 } 40 41 func attachV2List() []decorator.AttachmentV2 { 42 return []decorator.AttachmentV2{ 43 { 44 ID: "attachv2-1", 45 Data: decorator.AttachmentData{}, 46 }, 47 } 48 } 49 50 func TestProposePresentationParams(t *testing.T) { 51 t.Run("from/to v2", func(t *testing.T) { 52 src := &ProposePresentationV2{ 53 Type: ProposePresentationMsgTypeV2, 54 Comment: foo, 55 Formats: formatList(), 56 ProposalsAttach: attachV1List(), 57 } 58 59 param := ProposePresentationParams{} 60 61 param.FromV2(src) 62 63 dest := param.AsV2() 64 65 require.Equal(t, src, dest) 66 }) 67 68 t.Run("from/to v3", func(t *testing.T) { 69 src := &ProposePresentationV3{ 70 Type: ProposePresentationMsgTypeV3, 71 Body: ProposePresentationV3Body{ 72 Comment: foo, 73 }, 74 Attachments: attachV2List(), 75 } 76 77 param := ProposePresentationParams{} 78 79 param.FromV3(src) 80 81 dest := param.AsV3() 82 83 require.Equal(t, src, dest) 84 }) 85 } 86 87 func TestRequestPresentationParams(t *testing.T) { 88 t.Run("from/to v2", func(t *testing.T) { 89 src := &RequestPresentationV2{ 90 Type: RequestPresentationMsgTypeV2, 91 Comment: foo, 92 WillConfirm: true, 93 Formats: formatList(), 94 RequestPresentationsAttach: attachV1List(), 95 } 96 97 param := RequestPresentationParams{} 98 99 param.FromV2(src) 100 101 dest := param.AsV2() 102 103 require.Equal(t, src, dest) 104 }) 105 106 t.Run("from/to v3", func(t *testing.T) { 107 src := &RequestPresentationV3{ 108 Type: RequestPresentationMsgTypeV3, 109 Body: RequestPresentationV3Body{ 110 Comment: foo, 111 WillConfirm: true, 112 }, 113 Attachments: attachV2List(), 114 } 115 116 param := RequestPresentationParams{} 117 118 param.FromV3(src) 119 120 dest := param.AsV3() 121 122 require.Equal(t, src, dest) 123 }) 124 } 125 126 func TestPresentationParams(t *testing.T) { 127 t.Run("from/to v2", func(t *testing.T) { 128 src := &PresentationV2{ 129 Type: PresentationMsgTypeV2, 130 Comment: foo, 131 Formats: formatList(), 132 PresentationsAttach: attachV1List(), 133 } 134 135 param := PresentationParams{} 136 137 param.FromV2(src) 138 139 dest := param.AsV2() 140 141 require.Equal(t, src, dest) 142 }) 143 144 t.Run("from/to v3", func(t *testing.T) { 145 src := &PresentationV3{ 146 Type: PresentationMsgTypeV3, 147 Body: PresentationV3Body{ 148 Comment: foo, 149 }, 150 Attachments: attachV2List(), 151 } 152 153 param := PresentationParams{} 154 155 param.FromV3(src) 156 157 dest := param.AsV3() 158 159 require.Equal(t, src, dest) 160 }) 161 } 162 163 func TestProposePresentationParams_UnmarshalJSON(t *testing.T) { 164 tests := testTable( 165 &ProposePresentationParams{}, 166 &ProposePresentationParams{Comment: foo}, 167 ) 168 169 handleTestSet(t, func() interface{} { 170 return &ProposePresentationParams{} 171 }, tests, jsonUnmarshalTest) 172 173 t.Run("fail: parse error", func(t *testing.T) { 174 unMarshalTo := &ProposePresentationParams{} 175 dataBytes := []byte("{{{{bad json") 176 177 require.Error(t, json.Unmarshal(dataBytes, unMarshalTo)) 178 }) 179 } 180 181 func TestProposePresentationParams_FromDIDCommMsgMap(t *testing.T) { 182 tests := testTable( 183 &ProposePresentationParams{}, 184 &ProposePresentationParams{Comment: foo}, 185 ) 186 187 handleTestSet(t, func() interface{} { 188 return &ProposePresentationParams{} 189 }, tests, msgMapDecodeTest) 190 } 191 192 func TestRequestPresentationParams_UnmarshalJSON(t *testing.T) { 193 tests := testTable( 194 &RequestPresentationParams{}, 195 &RequestPresentationParams{Comment: foo}, 196 ) 197 198 handleTestSet(t, func() interface{} { 199 // this function returns an object for the test to unmarshal into 200 return &RequestPresentationParams{} 201 }, tests, jsonUnmarshalTest) 202 203 t.Run("fail: parse error", func(t *testing.T) { 204 unMarshalTo := &RequestPresentationParams{} 205 dataBytes := []byte("{{{{bad json") 206 207 require.Error(t, json.Unmarshal(dataBytes, unMarshalTo)) 208 }) 209 } 210 211 func TestRequestPresentationParams_FromDIDCommMsgMap(t *testing.T) { 212 tests := testTable( 213 &RequestPresentationParams{}, 214 &RequestPresentationParams{Comment: foo}, 215 ) 216 217 handleTestSet(t, func() interface{} { 218 // this function returns an object for the test to unmarshal into 219 return &RequestPresentationParams{} 220 }, tests, msgMapDecodeTest) 221 } 222 223 func TestPresentationParams_UnmarshalJSON(t *testing.T) { 224 tests := testTable( 225 &PresentationParams{}, 226 &PresentationParams{Comment: foo}, 227 ) 228 229 handleTestSet(t, func() interface{} { 230 // this function returns an object for the test to unmarshal into 231 return &PresentationParams{} 232 }, tests, jsonUnmarshalTest) 233 234 t.Run("fail: parse error", func(t *testing.T) { 235 unMarshalTo := &PresentationParams{} 236 dataBytes := []byte("{{{{bad json") 237 238 require.Error(t, json.Unmarshal(dataBytes, unMarshalTo)) 239 }) 240 } 241 242 func TestPresentationParams_FromDIDCommMsgMap(t *testing.T) { 243 tests := testTable( 244 &PresentationParams{}, 245 &PresentationParams{Comment: foo}, 246 ) 247 248 handleTestSet(t, func() interface{} { 249 // this function returns an object for the test to unmarshal into 250 return &PresentationParams{} 251 }, tests, msgMapDecodeTest) 252 } 253 254 func testTable(expectedEmpty, expectedWithComment interface{}) []testCase { 255 return []testCase{ 256 { 257 name: "empty", 258 srcBytes: []byte(`{}`), 259 expect: expectedEmpty, 260 }, 261 { 262 name: "id v2", 263 srcBytes: []byte(`{"@id":"foo"}`), 264 expect: expectedEmpty, 265 }, 266 { 267 name: "type v2", 268 srcBytes: []byte(`{"@type":"foo"}`), 269 expect: expectedEmpty, 270 }, 271 { 272 name: "comment v2", 273 srcBytes: []byte(`{"comment":"foo"}`), 274 expect: expectedWithComment, 275 }, 276 { 277 name: "id v3", 278 srcBytes: []byte(`{"id":"foo"}`), 279 expect: expectedEmpty, 280 }, 281 { 282 name: "type v3", 283 srcBytes: []byte(`{"type":"foo"}`), 284 expect: expectedEmpty, 285 }, 286 { 287 name: "comment v3", 288 srcBytes: []byte(`{"id":"foo","type":"foo","body":{"comment":"foo"}}`), 289 expect: expectedWithComment, 290 }, 291 } 292 } 293 294 type testCase struct { 295 name string 296 srcBytes []byte 297 expect interface{} 298 } 299 300 type freshObjGetter func() interface{} 301 302 type unmarshalTester func(t *testing.T, srcBytes []byte, unMarshalTo, expected interface{}) error 303 304 func handleTestSet(t *testing.T, getEmptyObject freshObjGetter, tests []testCase, testFunc unmarshalTester) { 305 t.Parallel() 306 307 for _, tc := range tests { 308 t.Run(tc.name, func(t *testing.T) { 309 result := getEmptyObject() 310 err := testFunc(t, tc.srcBytes, result, tc.expect) 311 if err != nil { 312 t.FailNow() 313 } 314 }) 315 } 316 } 317 318 func jsonUnmarshalTest(t *testing.T, srcBytes []byte, unMarshalTo, expected interface{}) error { 319 t.Helper() 320 321 require.NoError(t, json.Unmarshal(srcBytes, unMarshalTo)) 322 323 if !assert.Equal(t, expected, unMarshalTo) { 324 return fmt.Errorf("") 325 } 326 327 return nil 328 } 329 330 func msgMapDecodeTest(t *testing.T, srcBytes []byte, unMarshalTo, expected interface{}) error { 331 t.Helper() 332 333 msg, err := service.ParseDIDCommMsgMap(srcBytes) 334 require.NoError(t, err) 335 336 require.NoError(t, msg.Decode(unMarshalTo)) 337 338 if !assert.Equal(t, expected, unMarshalTo) { 339 return fmt.Errorf("") 340 } 341 342 return nil 343 }