Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package signer_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/piprate/json-gold/ld"
"github.com/stretchr/testify/assert"
"code.vereign.com/gaiax/tsa/infohub/internal/clients/signer"
)
const invalidPresentation = `{"invalid":"verifiable_presentation"}`
const validPresentation = `{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"id": "did:123",
"type": "VerifiablePresentation",
"verifiableCredential": [
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"credentialSubject": {
"allow": true,
"id": "example/example/1.0",
"taskID": "0123456789abcdef"
},
"issuanceDate": "2022-06-14T08:43:22.78309334Z",
"issuer": "https://example.com",
"type": "VerifiableCredential"
},
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"credentialSubject": {
"id": "example/example/2.0",
"result": {
"hello": "world"
}
},
"issuanceDate": "2022-06-14T08:43:22.783102173Z",
"issuer": "https://example.com",
"type": "VerifiableCredential"
}
]
}`
func TestClient_PresentationProof(t *testing.T) {
tests := []struct {
name string
ctx context.Context
vp *verifiable.Presentation
handler http.HandlerFunc
result *verifiable.Presentation
errtext string
}{
{
name: "error creating http request because of nil context",
errtext: "net/http: nil Context",
},
{
name: "signer server returns unexpected response code",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
},
errtext: "unexpected response from signer: 503 Service Unavailable",
},
{
name: "signer server returns empty response",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
errtext: "JSON unmarshalling of verifiable presentation: unexpected end of JSON input",
},
{
name: "signer server returns invalid verifiable presentation",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(invalidPresentation))
},
errtext: "verifiable presentation is not valid",
},
{
name: "signer server returns valid presentation",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(validPresentation))
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
signerSrv := httptest.NewServer(test.handler)
client := signer.New(signerSrv.URL, signer.WithHTTPClient(http.DefaultClient))
result, err := client.PresentationProof(test.ctx, test.vp)
if err != nil {
assert.Nil(t, result)
assert.NotEmpty(t, test.errtext)
assert.Contains(t, err.Error(), test.errtext)
} else {
assert.Empty(t, test.errtext)
assert.NotNil(t, result)
// parse the string representation to compare with the returned result
vp, err := verifiable.ParsePresentation(
[]byte(validPresentation),
verifiable.WithPresJSONLDDocumentLoader(ld.NewDefaultDocumentLoader(http.DefaultClient)),
verifiable.WithPresDisabledProofCheck(),
)
assert.NoError(t, err)
assert.Equal(t, vp, result)
}
})
}
}