forked from flow-hydraulics/flow-pds
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
165 lines (129 loc) · 3.9 KB
/
main_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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/bjartek/overflow/overflow"
"github.com/flow-hydraulics/flow-pds/go-contracts/util"
"github.com/flow-hydraulics/flow-pds/service/common"
pds_http "github.com/flow-hydraulics/flow-pds/service/http"
"github.com/google/uuid"
flow "github.com/onflow/flow-go-sdk"
)
func TestCreate(t *testing.T) {
t.Skip("focusing on e2e test now")
cfg := getTestCfg(t, nil)
server, cleanup := getTestServer(cfg, false)
defer func() {
cleanup()
}()
packs := 10
slotsPerBucket := 5
addr := common.FlowAddress(flow.HexToAddress("0x1"))
collection := makeTestCollection(packs * slotsPerBucket)
dReq := pds_http.ReqCreateDistribution{
FlowID: common.FlowID{Int64: int64(1), Valid: true},
Issuer: addr,
PackTemplate: pds_http.ReqPackTemplate{
PackReference: pds_http.AddressLocation{
Name: "TestPackNFT",
Address: addr,
},
CollectibleReference: pds_http.AddressLocation{
Name: "TestCollectibleNFT",
Address: addr,
},
PackCount: uint(packs),
Buckets: []pds_http.ReqBucket{
{
CollectibleCount: uint(slotsPerBucket),
CollectibleCollection: collection,
},
},
},
}
jReq, err := json.Marshal(dReq)
if err != nil {
t.Fatal(err)
}
// Create
rr1 := httptest.NewRecorder()
createReq, err := http.NewRequest("POST", "/v1/distributions", bytes.NewBuffer(jReq))
if err != nil {
t.Fatal(err)
}
createReq.Header.Set("Content-Type", "application/json")
server.Server.Handler.ServeHTTP(rr1, createReq)
// Check the status code is what we expect.
if status := rr1.Code; status != http.StatusCreated {
t.Fatalf("handler returned wrong status code: got %v want %v, error: %s", status, http.StatusCreated, rr1.Body)
}
createRes := pds_http.ResCreateDistribution{}
if err := json.NewDecoder(rr1.Body).Decode(&createRes); err != nil {
t.Fatal(err)
}
AssertNotEqual(t, createRes.ID, uuid.Nil)
// Get
rr2 := httptest.NewRecorder()
getReq, err := http.NewRequest("GET", fmt.Sprintf("/v1/distributions/%s", createRes.ID), nil)
if err != nil {
t.Fatal(err)
}
server.Server.Handler.ServeHTTP(rr2, getReq)
// Check the status code is what we expect.
if status := rr2.Code; status != http.StatusOK {
t.Fatalf("handler returned wrong status code: got %v want %v, error: %s", status, http.StatusOK, rr2.Body)
}
getRes := pds_http.ResGetDistribution{}
if err := json.NewDecoder(rr2.Body).Decode(&getRes); err != nil {
t.Fatal(err)
}
AssertEqual(t, getRes.ID, createRes.ID)
AssertEqual(t, getRes.Issuer, addr)
}
func TestSetDistCap(t *testing.T) {
cfg := getTestCfg(t, nil)
server, cleanup := getTestServer(cfg, false)
defer func() {
cleanup()
}()
g, err := overflow.NewOverflowEmulator().Config("./flow.json").ExistingEmulator().StartE()
if err != nil {
t.Fatal(err)
}
t.Log("Issuer create PackIssuer resource to store DistCap")
createPackIssuer := "./cadence-transactions/pds/create_new_pack_issuer.cdc"
createPackIssuerCode := util.ParseCadenceTemplate(createPackIssuer)
_, err = g.
Transaction(string(createPackIssuerCode)).
SignProposeAndPayAs("issuer").
RunE()
if err != nil {
t.Fatal(err)
}
// Wait a bit more as the blocktime might be 1s if run from the test script
time.Sleep(time.Second * 2)
issuer := common.FlowAddress(g.Account("issuer").Address())
dReq := pds_http.ReqCreateDistribution{
Issuer: issuer,
}
jReq, err := json.Marshal(dReq)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/v1/set-dist-cap", bytes.NewBuffer(jReq))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
server.Server.Handler.ServeHTTP(r, req)
// Check the status code is what we expect.
if status := r.Code; status != http.StatusOK {
t.Fatalf("handler returned wrong status code: got %v want %v, error: %s", status, http.StatusOK, r.Body)
}
}