fabric-samples/asset-transfer-private-data/chaincode-go/chaincode/asset_transfer_test.go
2023-03-01 11:17:33 -08:00

101 lines
3.5 KiB
Go

/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode_test
import (
"encoding/json"
"os"
"testing"
"github.com/hyperledger/fabric-chaincode-go/pkg/cid"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"github.com/hyperledger/fabric-samples/asset-transfer-private-data/chaincode-go/chaincode"
"github.com/hyperledger/fabric-samples/asset-transfer-private-data/chaincode-go/chaincode/mocks"
"github.com/stretchr/testify/require"
)
/*
These unit tests use mocks to simulate chaincode-api & fabric interactions
The mocks are generated using counterfeiter directives in the comments (starting with "go:generate counterfeiter")
All files in mocks/* are generated by running following, in the directory with your directive:
`go generate`
*/
//go:generate counterfeiter -o mocks/transaction.go -fake-name TransactionContext . transactionContext
type transactionContext interface {
contractapi.TransactionContextInterface
}
//go:generate counterfeiter -o mocks/chaincodestub.go -fake-name ChaincodeStub . chaincodeStub
type chaincodeStub interface {
shim.ChaincodeStubInterface
}
//go:generate counterfeiter -o mocks/statequeryiterator.go -fake-name StateQueryIterator . stateQueryIterator
type stateQueryIterator interface {
shim.StateQueryIteratorInterface
}
//go:generate counterfeiter -o mocks/clientIdentity.go -fake-name ClientIdentity . clientIdentity
type clientIdentity interface {
cid.ClientIdentity
}
const assetCollectionName = "assetCollection"
const transferAgreementObjectType = "transferAgreement"
const myOrg1Msp = "Org1Testmsp"
const myOrg1Clientid = "myOrg1Userid"
const myOrg1PrivCollection = "Org1TestmspPrivateCollection"
const myOrg2Msp = "Org2Testmsp"
const myOrg2Clientid = "myOrg2Userid"
const myOrg2PrivCollection = "Org2TestmspPrivateCollection"
func prepMocksAsOrg1() (*mocks.TransactionContext, *mocks.ChaincodeStub) {
return prepMocks(myOrg1Msp, myOrg1Clientid)
}
func prepMocksAsOrg2() (*mocks.TransactionContext, *mocks.ChaincodeStub) {
return prepMocks(myOrg2Msp, myOrg2Clientid)
}
func prepMocks(orgMSP, clientId string) (*mocks.TransactionContext, *mocks.ChaincodeStub) {
chaincodeStub := &mocks.ChaincodeStub{}
transactionContext := &mocks.TransactionContext{}
transactionContext.GetStubReturns(chaincodeStub)
clientIdentity := &mocks.ClientIdentity{}
clientIdentity.GetMSPIDReturns(orgMSP, nil)
clientIdentity.GetIDReturns(clientId, nil)
// set matching msp ID using peer shim env variable
os.Setenv("CORE_PEER_LOCALMSPID", orgMSP)
transactionContext.GetClientIdentityReturns(clientIdentity)
return transactionContext, chaincodeStub
}
func TestCreateUserID(t *testing.T) {
chaincodeStub := &mocks.ChaincodeStub{}
transactionContext := &mocks.TransactionContext{}
transactionContext.GetStubReturns(chaincodeStub)
contract := chaincode.SmartContract{}
err := contract.CreateUserID(transactionContext, "testID", "testOrg")
require.NoError(t, err)
}
func TestCreateUserIDDuplicate(t *testing.T) {
chaincodeStub := &mocks.ChaincodeStub{}
transactionContext := &mocks.TransactionContext{}
transactionContext.GetStubReturns(chaincodeStub)
existingUser := &chaincode.User{UUID: "uuid1", APIUserId: []string{"testID"}, Org: "testOrg"}
bytes, err := json.Marshal(existingUser)
require.NoError(t, err)
chaincodeStub.GetStateReturns(bytes, nil)
contract := chaincode.SmartContract{}
err = contract.CreateUserID(transactionContext, "testID", "testOrg")
require.EqualError(t, err, "the user with APIId testID already exists")
}