-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
postage: add create endpoint (#1142)
- Loading branch information
1 parent
891a756
commit d75b882
Showing
14 changed files
with
616 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package api | ||
|
||
import ( | ||
"math/big" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/ethersphere/bee/pkg/jsonhttp" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
type postageCreateResponse struct { | ||
BatchID []byte `json:"batchID"` | ||
} | ||
|
||
func (s *server) postageCreateHandler(w http.ResponseWriter, r *http.Request) { | ||
depthStr := mux.Vars(r)["depth"] | ||
|
||
amount, ok := big.NewInt(0).SetString(mux.Vars(r)["amount"], 10) | ||
if !ok { | ||
s.Logger.Error("create batch: invalid amount") | ||
jsonhttp.BadRequest(w, "invalid postage amount") | ||
return | ||
|
||
} | ||
|
||
depth, err := strconv.ParseUint(depthStr, 10, 8) | ||
if err != nil { | ||
s.Logger.Debugf("create batch: invalid depth: %v", err) | ||
s.Logger.Error("create batch: invalid depth") | ||
jsonhttp.BadRequest(w, "invalid depth") | ||
return | ||
} | ||
|
||
label := r.URL.Query().Get("label") | ||
|
||
batchID, err := s.postageContract.CreateBatch(r.Context(), amount, uint8(depth), label) | ||
if err != nil { | ||
s.Logger.Debugf("create batch: failed to create: %v", err) | ||
s.Logger.Error("create batch: failed to create") | ||
jsonhttp.InternalServerError(w, "cannot create batch") | ||
return | ||
} | ||
|
||
jsonhttp.OK(w, &postageCreateResponse{ | ||
BatchID: batchID, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2021 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package api_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/pkg/api" | ||
"github.com/ethersphere/bee/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" | ||
contractMock "github.com/ethersphere/bee/pkg/postage/postagecontract/mock" | ||
) | ||
|
||
func TestPostageCreateStamp(t *testing.T) { | ||
batchID := []byte{1, 2, 3, 4} | ||
initialBalance := int64(1000) | ||
depth := uint8(1) | ||
label := "label" | ||
createBatch := func(amount int64, depth uint8, label string) string { | ||
return fmt.Sprintf("/stamps/%d/%d?label=%s", amount, depth, label) | ||
} | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
contract := contractMock.New( | ||
contractMock.WithCreateBatchFunc(func(ctx context.Context, ib *big.Int, d uint8, l string) ([]byte, error) { | ||
if ib.Cmp(big.NewInt(initialBalance)) != 0 { | ||
return nil, fmt.Errorf("called with wrong initial balance. wanted %d, got %d", initialBalance, ib) | ||
} | ||
if d != depth { | ||
return nil, fmt.Errorf("called with wrong depth. wanted %d, got %d", depth, d) | ||
} | ||
if l != label { | ||
return nil, fmt.Errorf("called with wrong label. wanted %s, got %s", label, l) | ||
} | ||
return batchID, nil | ||
}), | ||
) | ||
client, _, _ := newTestServer(t, testServerOptions{ | ||
PostageContract: contract, | ||
}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, createBatch(initialBalance, depth, label), http.StatusOK, | ||
jsonhttptest.WithExpectedJSONResponse(&api.PostageCreateResponse{ | ||
BatchID: batchID, | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("with-error", func(t *testing.T) { | ||
contract := contractMock.New( | ||
contractMock.WithCreateBatchFunc(func(ctx context.Context, ib *big.Int, d uint8, l string) ([]byte, error) { | ||
return nil, errors.New("err") | ||
}), | ||
) | ||
client, _, _ := newTestServer(t, testServerOptions{ | ||
PostageContract: contract, | ||
}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, createBatch(initialBalance, depth, label), http.StatusInternalServerError, | ||
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{ | ||
Code: http.StatusInternalServerError, | ||
Message: "cannot create batch", | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("invalid depth", func(t *testing.T) { | ||
client, _, _ := newTestServer(t, testServerOptions{}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, "/stamps/1000/ab", http.StatusBadRequest, | ||
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{ | ||
Code: http.StatusBadRequest, | ||
Message: "invalid depth", | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("invalid balance", func(t *testing.T) { | ||
client, _, _ := newTestServer(t, testServerOptions{}) | ||
|
||
jsonhttptest.Request(t, client, http.MethodPost, "/stamps/abcd/2", http.StatusBadRequest, | ||
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{ | ||
Code: http.StatusBadRequest, | ||
Message: "invalid postage amount", | ||
}), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.