Skip to content

Commit

Permalink
Merge pull request #106 from eqlabs/latenssi/test-deposits-handler
Browse files Browse the repository at this point in the history
Add deposit handler tests
  • Loading branch information
latenssi authored Jun 22, 2021
2 parents 981bb37 + 1555cba commit 9add4a4
Showing 1 changed file with 114 additions and 28 deletions.
142 changes: 114 additions & 28 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func handleStepRequest(s httpTestStep, r *mux.Router, t *testing.T) *httptest.Re
re := regexp.MustCompile(s.expected)
match := re.FindString(rr.Body.String())
if match == "" {
t.Errorf("handler returned unexpected body: got %q want %v", rr.Body.String(), re)
t.Errorf("handler returned unexpected body: got %s want %v", rr.Body.String(), re)
}

return rr
Expand Down Expand Up @@ -270,21 +270,21 @@ func TestAccountHandlers(t *testing.T) {
name: "list db empty",
method: http.MethodGet,
url: "/",
expected: `\[\]`,
expected: `(?m)^\[\]$`,
status: http.StatusOK,
},
{
name: "create",
method: http.MethodPost,
url: "/",
expected: `"jobId":".+"`,
expected: `(?m)^{"jobId":".+"}$`,
status: http.StatusCreated,
},
{
name: "list db not empty",
method: http.MethodGet,
url: "/",
expected: `\[.*"address":".+".*\]\n`,
expected: `(?m)^\[{"address":".+".*}\]$`,
status: http.StatusOK,
},
{
Expand All @@ -305,7 +305,7 @@ func TestAccountHandlers(t *testing.T) {
name: "details known address",
method: http.MethodGet,
url: "/<address>",
expected: `"address":".+"`,
expected: `(?m)^{"address":".+"}$`,
status: http.StatusOK,
},
}
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestTransactionHandlers(t *testing.T) {
name: "list db empty",
method: http.MethodGet,
url: fmt.Sprintf("/%s/transactions", cfg.AdminAddress),
expected: `\[\]`,
expected: `(?m)^\[\]$`,
status: http.StatusOK,
},
{
Expand All @@ -454,7 +454,7 @@ func TestTransactionHandlers(t *testing.T) {
contentType: "application/json",
body: strings.NewReader(validHello),
url: fmt.Sprintf("/%s/transactions", cfg.AdminAddress),
expected: `"jobId":".+"`,
expected: `(?m)^{"jobId":".+"}$`,
status: http.StatusCreated,
},
{
Expand All @@ -463,7 +463,7 @@ func TestTransactionHandlers(t *testing.T) {
contentType: "application/json",
body: strings.NewReader(validHello),
url: fmt.Sprintf("/%s/transactions", cfg.AdminAddress),
expected: `"transactionId":".+"`,
expected: `(?m)^{"transactionId":".+"}$`,
status: http.StatusCreated,
sync: true,
},
Expand All @@ -483,7 +483,7 @@ func TestTransactionHandlers(t *testing.T) {
contentType: "application/json",
body: strings.NewReader(validTransferFlow),
url: fmt.Sprintf("/%s/transactions", cfg.AdminAddress),
expected: `"transactionId":".+"`,
expected: `(?m)^{"transactionId":".+"}$`,
status: http.StatusCreated,
sync: true,
},
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestTransactionHandlers(t *testing.T) {
name: "list db not empty",
method: http.MethodGet,
url: fmt.Sprintf("/%s/transactions", cfg.AdminAddress),
expected: `\[.*"transactionId":".+".*\]\n`,
expected: `(?m)^\[{"transactionId":".+".*}\]$`,
status: http.StatusOK,
},
{
Expand All @@ -562,7 +562,7 @@ func TestTransactionHandlers(t *testing.T) {
name: "details known id",
method: http.MethodGet,
url: fmt.Sprintf("/%s/transactions/<id>", cfg.AdminAddress),
expected: `"transactionId":".+"`,
expected: `(?m)^{"transactionId":".+"}$`,
status: http.StatusOK,
},
{
Expand Down Expand Up @@ -654,7 +654,7 @@ func TestScriptsHandlers(t *testing.T) {
"arguments":[]
}`),
contentType: "application/json",
expected: "{\"Value\":1}",
expected: `(?m)^{"Value":1}$`,
status: http.StatusOK,
},
{
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestScriptsHandlers(t *testing.T) {
re := regexp.MustCompile(step.expected)
match := re.FindString(rr.Body.String())
if match == "" {
t.Errorf("handler returned unexpected body: got %q want %v", rr.Body.String(), re)
t.Errorf("handler returned unexpected body: got %s want %v", rr.Body.String(), re)
}
})
}
Expand Down Expand Up @@ -918,6 +918,8 @@ func TestTokenHandlers(t *testing.T) {
router.Handle("/{address}/fungible-tokens/{tokenName}/withdrawals", tokenHandlers.CreateFtWithdrawal()).Methods(http.MethodPost)
router.Handle("/{address}/fungible-tokens/{tokenName}/withdrawals", tokenHandlers.ListFtWithdrawals()).Methods(http.MethodGet)
router.Handle("/{address}/fungible-tokens/{tokenName}/withdrawals/{transactionId}", tokenHandlers.GetFtWithdrawal()).Methods(http.MethodGet)
router.Handle("/{address}/fungible-tokens/{tokenName}/deposits", tokenHandlers.ListFtDeposits()).Methods(http.MethodGet)
router.Handle("/{address}/fungible-tokens/{tokenName}/deposits/{transactionId}", tokenHandlers.GetFtDeposit()).Methods(http.MethodGet)

// Setup tokens

Expand Down Expand Up @@ -945,7 +947,7 @@ func TestTokenHandlers(t *testing.T) {
method: http.MethodPost,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", aa[0].Address, "fusd"),
expected: `"jobId":".+"`,
expected: `(?m)^{"jobId":".+".*}$`,
status: http.StatusCreated,
},
{
Expand All @@ -954,7 +956,7 @@ func TestTokenHandlers(t *testing.T) {
method: http.MethodPost,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", aa[1].Address, "fusd"),
expected: `"transactionId":".+"`,
expected: `(?m)^{"transactionId":".+".*}$`,
status: http.StatusCreated,
},
}
Expand All @@ -973,15 +975,15 @@ func TestTokenHandlers(t *testing.T) {
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", aa[1].Address, "flow-token"),
expected: `"name":"FlowToken","balance":".+"`,
expected: `(?m)^{"name":"FlowToken","balance":".+"}$`,
status: http.StatusOK,
},
{
name: "FUSD details",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", aa[1].Address, "fusd"),
expected: `"name":"FUSD\","balance":".+"`,
expected: `(?m)^{"name":"FUSD\","balance":".+"}$`,
status: http.StatusOK,
},
}
Expand All @@ -999,7 +1001,7 @@ func TestTokenHandlers(t *testing.T) {
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens", aa[1].Address),
expected: `\[.*"name":"FUSD".*"name":"FlowToken".*\]`,
expected: `(?m)^\[{"name":"FUSD".*"name":"FlowToken".*}\]$`,
status: http.StatusOK,
},
}
Expand All @@ -1021,11 +1023,6 @@ func TestTokenHandlers(t *testing.T) {
t.Fatal(err)
}

_, transfer, err := service.CreateFtWithdrawal(context.Background(), true, token.CanonName(), cfg.AdminAddress, account.Address, "1.0")
if err != nil {
t.Fatal(err)
}

// Create withdrawals
createWithdrawalSteps := []httpTestStep{
{
Expand All @@ -1034,7 +1031,7 @@ func TestTokenHandlers(t *testing.T) {
body: strings.NewReader(fmt.Sprintf(`{"recipient":"%s","amount":"1.0"}`, account.Address)),
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/withdrawals", cfg.AdminAddress, token.Name),
expected: `"jobId":".+"`,
expected: `(?m)^{"jobId":".+"}$`,
status: http.StatusCreated,
},
{
Expand All @@ -1044,7 +1041,7 @@ func TestTokenHandlers(t *testing.T) {
body: strings.NewReader(fmt.Sprintf(`{"recipient":"%s","amount":"1.0"}`, account.Address)),
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/withdrawals", cfg.AdminAddress, token.Name),
expected: `"transactionId":".+"`,
expected: `(?m)^{"transactionId":".+"}$`,
status: http.StatusCreated,
},
{
Expand Down Expand Up @@ -1073,30 +1070,35 @@ func TestTokenHandlers(t *testing.T) {
})
}

_, transfer, err := service.CreateFtWithdrawal(context.Background(), true, token.CanonName(), cfg.AdminAddress, account.Address, "1.0")
if err != nil {
t.Fatal(err)
}

// Get withdrawals
getWithdrawalSteps := []httpTestStep{
{
name: "list withdrawals valid",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/withdrawals", cfg.AdminAddress, token.Name),
expected: `\[.*"transactionId":".+".*\]`,
expected: `(?m)^\[{"transactionId":".+".*}\]$`,
status: http.StatusOK,
},
{
name: "list withdrawals empty",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/withdrawals", account.Address, token.Name),
expected: `\[\]`,
expected: `(?m)^\[\]$`,
status: http.StatusOK,
},
{
name: "get withdrawal details valid",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/withdrawals/%s", cfg.AdminAddress, token.Name, transfer.TransactionId),
expected: fmt.Sprintf(`"transactionId":"%s"`, transfer.TransactionId),
expected: fmt.Sprintf(`(?m)^{"transactionId":"%s".*}$`, transfer.TransactionId),
status: http.StatusOK,
},
{
Expand All @@ -1122,4 +1124,88 @@ func TestTokenHandlers(t *testing.T) {
handleStepRequest(s, router, t)
})
}

// List deposits
listDepositSteps := []httpTestStep{
{
name: "list deposits valid",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits", account.Address, token.Name),
expected: `(?m)^\[{"transactionId":".+".*}\]$`,
status: http.StatusOK,
},
{
name: "list deposits invalid token name",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits", account.Address, "some-invalid-token-name"),
expected: `token SomeInvalidTokenName not enabled`,
status: http.StatusBadRequest,
},
{
name: "list deposits invalid address",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits", "0x1", token.Name),
expected: `not a valid address`,
status: http.StatusBadRequest,
},
}

for _, s := range listDepositSteps {
t.Run(s.name, func(t *testing.T) {
handleStepRequest(s, router, t)
})
}

// Get deposits
getDepositSteps := []httpTestStep{
{
name: "get deposit details valid",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits/%s", account.Address, token.Name, transfer.TransactionId),
expected: `(?m)^{"transactionId":".+".*}$`,
status: http.StatusOK,
},
{
name: "get deposit details invalid token name",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits/%s", account.Address, "some-invalid-token-name", transfer.TransactionId),
expected: `token SomeInvalidTokenName not enabled`,
status: http.StatusBadRequest,
},
{
name: "get deposit details invalid address",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits/%s", "0x1", token.Name, transfer.TransactionId),
expected: `not a valid address`,
status: http.StatusBadRequest,
},
{
name: "get deposit details invalid transactionId",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits/%s", account.Address, token.Name, "0"),
expected: `not a valid transaction id`,
status: http.StatusBadRequest,
},
{
name: "get deposit details 404",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s/deposits/%s", aa[0].Address, token.Name, transfer.TransactionId),
expected: `record not found`,
status: http.StatusNotFound,
},
}

for _, s := range getDepositSteps {
t.Run(s.name, func(t *testing.T) {
handleStepRequest(s, router, t)
})
}
}

0 comments on commit 9add4a4

Please sign in to comment.