Skip to content

Commit 22e63bc

Browse files
committed
itest: add a new itest to validate new meta json parsing
1 parent 10fd65d commit 22e63bc

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

itest/asset_meta_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package itest
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/lightninglabs/taproot-assets/taprpc"
8+
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// testAssetMeta tests the validation+parsing logic for asset meta data.
13+
func testAssetMeta(t *harnessTest) {
14+
// In this test, we'll attempt to issue the following assets with
15+
// distinct meta type. Within each test case, negative failure
16+
// scenarios may exist.
17+
jsonType := taprpc.AssetMetaType_META_TYPE_JSON
18+
testCases := []struct {
19+
asset *mintrpc.MintAssetRequest
20+
errString string
21+
}{
22+
// Existing opaque meta data option, should succeed.
23+
{
24+
asset: &mintrpc.MintAssetRequest{
25+
Asset: &mintrpc.MintAsset{
26+
AssetType: taprpc.AssetType_NORMAL,
27+
Name: "opaque asset",
28+
AssetMeta: &taprpc.AssetMeta{
29+
Data: []byte("some metadata"),
30+
},
31+
Amount: 5000,
32+
},
33+
},
34+
},
35+
36+
// Existing JSON meta data option, with valid JSON should
37+
// succeed.
38+
{
39+
asset: &mintrpc.MintAssetRequest{
40+
Asset: &mintrpc.MintAsset{
41+
AssetType: taprpc.AssetType_NORMAL,
42+
Name: "json asset",
43+
AssetMeta: &taprpc.AssetMeta{
44+
Data: []byte(
45+
`{"key": "value"}`,
46+
),
47+
Type: jsonType,
48+
},
49+
Amount: 5000,
50+
},
51+
},
52+
},
53+
54+
// Existing JSON meta data option, with invalid JSON should
55+
// fail.
56+
{
57+
asset: &mintrpc.MintAssetRequest{
58+
Asset: &mintrpc.MintAsset{
59+
AssetType: taprpc.AssetType_NORMAL,
60+
Name: "invalid json",
61+
AssetMeta: &taprpc.AssetMeta{
62+
Data: []byte("not json"),
63+
Type: jsonType,
64+
},
65+
Amount: 5000,
66+
},
67+
},
68+
errString: "invalid asset meta: invalid JSON",
69+
},
70+
71+
// Custom meta data type, with valid data should succeed.
72+
{
73+
asset: &mintrpc.MintAssetRequest{
74+
Asset: &mintrpc.MintAsset{
75+
AssetType: taprpc.AssetType_NORMAL,
76+
Name: "custom meta type",
77+
AssetMeta: &taprpc.AssetMeta{
78+
Data: []byte("custom stuff"),
79+
Type: 99,
80+
},
81+
Amount: 5000,
82+
},
83+
},
84+
},
85+
}
86+
87+
ctxb := context.Background()
88+
for _, tc := range testCases {
89+
t.t.Run(tc.asset.Asset.Name, func(tt *testing.T) {
90+
_, err := t.tapd.MintAsset(ctxb, tc.asset)
91+
if err != nil {
92+
if tc.errString == "" {
93+
tt.Fatalf("unexpected error: %v", err)
94+
}
95+
require.ErrorContains(tt, err, tc.errString)
96+
}
97+
})
98+
}
99+
100+
// We only test validation here, so we'll cancel the pending batch.
101+
_, err := t.tapd.CancelBatch(ctxb, &mintrpc.CancelBatchRequest{})
102+
require.NoError(t.t, err)
103+
}

itest/test_list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var testCases = []*testCase{
99
name: "mint assets",
1010
test: testMintAssets,
1111
},
12+
{
13+
name: "asset meta validation",
14+
test: testAssetMeta,
15+
},
1216
{
1317
name: "asset name collision raises mint error",
1418
test: testMintAssetNameCollisionError,

0 commit comments

Comments
 (0)