Skip to content

Commit

Permalink
reject quantity less than 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nodestory committed Oct 15, 2019
1 parent 5509690 commit f075de2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 63 deletions.
9 changes: 7 additions & 2 deletions bitmark/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package bitmark

import (
"encoding/hex"
"errors"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -40,7 +41,11 @@ type IssueRequest struct {
Signature string `json:"signature"`
}

func NewIssuanceParams(assetID string, quantity int) *IssuanceParams {
func NewIssuanceParams(assetID string, quantity int) (*IssuanceParams, error) {
if quantity < 1 {
return nil, errors.New("quantity must be greater than or equal to 1")
}

ip := &IssuanceParams{
Issuances: make([]*IssueRequest, 0),
}
Expand All @@ -67,7 +72,7 @@ func NewIssuanceParams(assetID string, quantity int) *IssuanceParams {
ip.Issuances = append(ip.Issuances, issuance)
}

return ip
return ip, nil
}

// Sign all issunaces in a batch
Expand Down
9 changes: 9 additions & 0 deletions bitmark/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

sdk "github.com/bitmark-inc/bitmark-sdk-go"
"github.com/bitmark-inc/bitmark-sdk-go/account"
"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -62,6 +63,14 @@ func init() {
// }
// }

func TestInvalidQuantity(t *testing.T) {
_, err := NewIssuanceParams("03023b0a5fbf78f40eb97027e782ee7fbc2098fa54cb5abf5d5c97c6e7ffa4ceebd067e5b16648179771ec749f4fa3dde7085f441d41f0ba22bbc833a5b21c7e", -1)
assert.EqualError(t, err, "quantity must be greater than or equal to 1")

_, err = NewIssuanceParams("03023b0a5fbf78f40eb97027e782ee7fbc2098fa54cb5abf5d5c97c6e7ffa4ceebd067e5b16648179771ec749f4fa3dde7085f441d41f0ba22bbc833a5b21c7e", 0)
assert.EqualError(t, err, "quantity must be greater than or equal to 1")
}

func TestTransferParams(t *testing.T) {
params, _ := NewTransferParams(receiver.AccountNumber())
params.FromLatestTx("67ef8bfee0ef7b8c33eda34ba21c8b2b0fbff601a7021984b2e27985251a0a80")
Expand Down
2 changes: 1 addition & 1 deletion sample/bitmark_issuance_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func issueBitmarks(issuer account.Account, assetID string, quantity int) ([]string, error) {
issuanceParams := bitmark.NewIssuanceParams(assetID, quantity)
issuanceParams, _ := bitmark.NewIssuanceParams(assetID, quantity)
issuanceParams.Sign(issuer)

bitmarkIDs, err := bitmark.Issue(issuanceParams)
Expand Down
36 changes: 0 additions & 36 deletions test/asset_test.go

This file was deleted.

18 changes: 4 additions & 14 deletions test/base_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ func (s *BaseTestSuite) SetupSuite() {
Network: sdk.Network(network),
APIToken: token,
})

var err error
s.sender, err = account.FromSeed(os.Getenv("SENDER_SEED"))
if err != nil {
s.Fail(err.Error())
}
s.receiver, err = account.FromSeed(os.Getenv("RECEIVER_SEED"))
if err != nil {
s.Fail(err.Error())
}

assetID := s.mustRegisterAsset("", []byte(time.Now().String()))
s.bitmarkIDs = s.mustIssueBitmarks(assetID, s.bitmarkCount)
}

func (s *BaseTestSuite) TearDownTest() {
Expand All @@ -66,7 +53,10 @@ func (s *BaseTestSuite) mustRegisterAsset(name string, content []byte) string {
}

func (s *BaseTestSuite) mustIssueBitmarks(assetID string, quantity int) []string {
params := bitmark.NewIssuanceParams(assetID, quantity)
params, err := bitmark.NewIssuanceParams(assetID, quantity)
if !s.NoError(err) {
s.T().FailNow()
}
params.Sign(s.sender)
bitmarkIDs, err := bitmark.Issue(params)
if !s.NoError(err) {
Expand Down
24 changes: 22 additions & 2 deletions test/giveaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
package test

import (
"github.com/bitmark-inc/bitmark-sdk-go/bitmark"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"

"github.com/bitmark-inc/bitmark-sdk-go/account"
"github.com/bitmark-inc/bitmark-sdk-go/bitmark"
"github.com/stretchr/testify/suite"
)

type GiveawayTestSuite struct {
Expand All @@ -21,6 +24,23 @@ func NewGiveawayTestSuite(bitmarkCount int) *GiveawayTestSuite {
return s
}

func (s *GiveawayTestSuite) SetupSuite() {
s.BaseTestSuite.SetupSuite()

var err error
s.sender, err = account.FromSeed(os.Getenv("SENDER_SEED"))
if err != nil {
s.Fail(err.Error())
}
s.receiver, err = account.FromSeed(os.Getenv("RECEIVER_SEED"))
if err != nil {
s.Fail(err.Error())
}

assetID := s.mustRegisterAsset("", []byte(time.Now().String()))
s.bitmarkIDs = s.mustIssueBitmarks(assetID, s.bitmarkCount)
}

func (s *GiveawayTestSuite) TestDirectTransfer() {
bitmarkID := s.bitmarkIDs[s.bitmarkIndex]
s.T().Logf("bitmark_id=%s", bitmarkID)
Expand Down
37 changes: 35 additions & 2 deletions test/ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
package test

import (
"github.com/bitmark-inc/bitmark-sdk-go/bitmark"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"

"github.com/bitmark-inc/bitmark-sdk-go/account"
"github.com/bitmark-inc/bitmark-sdk-go/asset"
"github.com/bitmark-inc/bitmark-sdk-go/bitmark"
"github.com/stretchr/testify/suite"
)

type OwnershipTestSuite struct {
Expand All @@ -24,6 +28,19 @@ func NewOwnershipTestSuite(bitmarkCount int) *OwnershipTestSuite {
func (s *OwnershipTestSuite) SetupSuite() {
s.BaseTestSuite.SetupSuite()

var err error
s.sender, err = account.FromSeed(os.Getenv("SENDER_SEED"))
if err != nil {
s.Fail(err.Error())
}
s.receiver, err = account.FromSeed(os.Getenv("RECEIVER_SEED"))
if err != nil {
s.Fail(err.Error())
}

assetID := s.mustRegisterAsset("", []byte(time.Now().String()))
s.bitmarkIDs = s.mustIssueBitmarks(assetID, s.bitmarkCount)

for {
if txsAreReady(s.bitmarkIDs) {
break
Expand Down Expand Up @@ -115,6 +132,22 @@ func (s *OwnershipTestSuite) TestCreateAndAcceptCountersignedTransfer() {
s.verifyBitmark(bitmarkID, s.receiver.AccountNumber(), "transferring", 10*time.Second)
}

func (s *OwnershipTestSuite) TestRegisterDuplicateAsset() {
params, _ := asset.NewRegistrationParams("another name", nil)
params.SetFingerprintFromData([]byte("Fri May 10 14:01:41 CST 2019"))
params.Sign(s.sender)
_, err := asset.Register(params)
s.Error(err)
}

func (s *OwnershipTestSuite) TestIssueForNotExistingAsset() {
notExistingAssetID := "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
params, _ := bitmark.NewIssuanceParams(notExistingAssetID, 1)
params.Sign(s.sender)
_, err := bitmark.Issue(params)
s.Error(err)
}

func TestOwnershipTestSuite(t *testing.T) {
suite.Run(t, NewOwnershipTestSuite(4))
}
7 changes: 1 addition & 6 deletions test/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ type QueryTestSuite struct {
BaseTestSuite
}

func NewQueryTestSuite() *QueryTestSuite {
s := new(QueryTestSuite)
s.bitmarkCount = 0
return s
}
func TestQueryTestSuite(t *testing.T) {
suite.Run(t, NewQueryTestSuite())
suite.Run(t, new(QueryTestSuite))
}

func (q *QueryTestSuite) TestGetAsset() {
Expand Down

0 comments on commit f075de2

Please sign in to comment.