Skip to content

Commit

Permalink
Make error message more informative when user tries to get keyless asset
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Jul 28, 2021
1 parent 240541a commit 91a406f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
6 changes: 6 additions & 0 deletions assets/existsInLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ func existsInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, key string) (

// ExistsInLedger checks if asset currently has a state on the ledger.
func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
if a.Key() == "" {
return false, errors.NewCCError("asset key is empty", 500)
}
return existsInLedger(stub, a.IsPrivate(), a.TypeTag(), a.Key())
}

// ExistsInLedger checks if asset referenced by a key object currently has a state on the ledger.
func (k *Key) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
if k.Key() == "" {
return false, errors.NewCCError("key is empty", 500)
}
return existsInLedger(stub, k.IsPrivate(), k.TypeTag(), k.Key())
}
5 changes: 5 additions & 0 deletions assets/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
func get(stub *sw.StubWrapper, pvtCollection, key string) (*Asset, errors.ICCError) {
var assetBytes []byte
var err error

if key == "" {
return nil, errors.NewCCError("key cannot be empty", 500)
}

if pvtCollection != "" {
assetBytes, err = stub.GetPrivateData(pvtCollection, key)
} else {
Expand Down
2 changes: 1 addition & 1 deletion assets/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (k Key) TypeTag() string {

// Key returns the asset's unique identifying key in the ledger.
func (k Key) Key() string {
assetKey := k["@key"].(string)
assetKey, _ := k["@key"].(string)
return assetKey
}

Expand Down
47 changes: 47 additions & 0 deletions test/assets_existsInLedger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package test

import (
"testing"

"github.com/goledgerdev/cc-tools/assets"
sw "github.com/goledgerdev/cc-tools/stubwrapper"
"github.com/hyperledger/fabric-chaincode-go/shimtest"
)

func TestExistsInLedgerNoKey(t *testing.T) {
key := assets.Key{
"@assetType": "person",
// "@key": "person:47061146-c642-51a1-844a-bf0b17cb5e19",
}

stub := shimtest.NewMockStub("org1MSP", new(testCC))
stub.MockTransactionStart("TestExistsInLedgerNoKey")
sw := &sw.StubWrapper{
Stub: stub,
}
_, err := key.ExistsInLedger(sw)
if err.Status() != 500 || err.Message() != "key is empty" {
t.FailNow()
}
stub.MockTransactionEnd("TestExistsInLedgerNoAssetKey")
}

func TestExistsInLedgerNoAssetKey(t *testing.T) {
asset := assets.Asset{
// "@key": "person:47061146-c642-51a1-844a-bf0b17cb5e19",
"@assetType": "person",
"name": "Maria",
"id": "31820792048",
}

stub := shimtest.NewMockStub("org1MSP", new(testCC))
stub.MockTransactionStart("TestExistsInLedgerNoAssetKey")
sw := &sw.StubWrapper{
Stub: stub,
}
_, err := asset.ExistsInLedger(sw)
if err.Status() != 500 || err.Message() != "asset key is empty" {
t.FailNow()
}
stub.MockTransactionEnd("TestExistsInLedgerNoAssetKey")
}
19 changes: 19 additions & 0 deletions test/assets_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,22 @@ func TestGetRecursiveWithPvtData(t *testing.T) {
}
stub.MockTransactionEnd("TestGetAsset")
}

func TestGetAssetNoKey(t *testing.T) {
stub := shimtest.NewMockStub("org1MSP", new(testCC))

personKey := assets.Key{
"@assetType": "person",
// "@key": "person:47061146-c642-51a1-844a-bf0b17cb5e19",
}

stub.MockTransactionStart("TestGetAssetNoKey")
sw := &sw.StubWrapper{
Stub: stub,
}
_, err := personKey.Get(sw)
if err.Status() != 500 || err.Message() != "key cannot be empty" {
t.FailNow()
}
stub.MockTransactionEnd("TestGetAssetNoKey")
}
1 change: 1 addition & 0 deletions test/assets_put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestPutAssetWithSubAsset(t *testing.T) {
}
book := assets.Asset{
"@assetType": "book",
"@key": "book:a36a2920-c405-51c3-b584-dcd758338cb5",
"title": "Meu Nome é Maria",
"author": "Maria Viana",
"currentTenant": map[string]interface{}{
Expand Down

0 comments on commit 91a406f

Please sign in to comment.