-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdefinitions_test.go
87 lines (83 loc) · 3.06 KB
/
definitions_test.go
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
package minercraft
import (
"encoding/json"
"testing"
"github.com/libsv/go-bk/envelope"
"github.com/stretchr/testify/assert"
)
func Test_JSONEnvelope_process(t *testing.T) {
t.Parallel()
tests := map[string]struct {
env envelope.JSONEnvelope
exp *JSONEnvelope
err error
}{
"JSONEnvelope with no sig should map correctly": {
env: envelope.JSONEnvelope{
Payload: "{\"index\":7\"}",
MimeType: "application/json",
},
exp: &JSONEnvelope{
Miner: nil,
Validated: false,
JSONEnvelope: envelope.JSONEnvelope{
Payload: "{\"index\":7\"}",
MimeType: "application/json",
},
},
err: nil,
}, "JSONEnvelope with sig should map correctly and set validated to true": {
env: envelope.JSONEnvelope{
Payload: `{\"Test\":\"abc123\",\"Name\":\"4567890\",\"Thing\":\"%$oddchars££$-\"}`,
Signature: strToPtr("3045022100b2b3000353b1acaf6e0190a44fc26b0b43830e5aa8d1232813c928d003697c010220294796e63da19d238b29f9cb17e2f31f728ef77a41bfd0f5e355f99f347ff4bf"),
PublicKey: strToPtr("0394890eeb9888e68cb953d56c598ab0aaa6789e20522cc8b937353694799d7ab1"),
Encoding: "UTF-8",
MimeType: "application/json",
},
exp: &JSONEnvelope{
Miner: nil,
Validated: false,
JSONEnvelope: envelope.JSONEnvelope{
Payload: `{\"Test\":\"abc123\",\"Name\":\"4567890\",\"Thing\":\"%$oddchars££$-\"}`,
Signature: strToPtr("3045022100b2b3000353b1acaf6e0190a44fc26b0b43830e5aa8d1232813c928d003697c010220294796e63da19d238b29f9cb17e2f31f728ef77a41bfd0f5e355f99f347ff4bf"),
PublicKey: strToPtr("0394890eeb9888e68cb953d56c598ab0aaa6789e20522cc8b937353694799d7ab1"),
Encoding: "UTF-8",
MimeType: "application/json",
},
},
err: nil,
}, "JSONEnvelope with modified payload should map correctly and set validated to false": {
env: envelope.JSONEnvelope{
Payload: `{\"Test\":\"abc124\",\"Name\":\"4567890\",\"Thing\":\"%$oddchars££$-\"}`,
Signature: strToPtr("3045022100b2b3000353b1acaf6e0190a44fc26b0b43830e5aa8d1232813c928d003697c010220294796e63da19d238b29f9cb17e2f31f728ef77a41bfd0f5e355f99f347ff4bf"),
PublicKey: strToPtr("0394890eeb9888e68cb953d56c598ab0aaa6789e20522cc8b937353694799d7ab1"),
Encoding: "UTF-8",
MimeType: "application/json",
},
exp: &JSONEnvelope{
Miner: nil,
Validated: false,
JSONEnvelope: envelope.JSONEnvelope{
Payload: `{\"Test\":\"abc124\",\"Name\":\"4567890\",\"Thing\":\"%$oddchars££$-\"}`,
Signature: strToPtr("3045022100b2b3000353b1acaf6e0190a44fc26b0b43830e5aa8d1232813c928d003697c010220294796e63da19d238b29f9cb17e2f31f728ef77a41bfd0f5e355f99f347ff4bf"),
PublicKey: strToPtr("0394890eeb9888e68cb953d56c598ab0aaa6789e20522cc8b937353694799d7ab1"),
Encoding: "UTF-8",
MimeType: "application/json",
},
},
err: nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
bb, err := json.Marshal(test.env)
assert.NoError(t, err)
env := &JSONEnvelope{}
assert.NoError(t, env.process(nil, bb))
assert.Equal(t, test.exp, env)
})
}
}
func strToPtr(s string) *string {
return &s
}