Skip to content

Commit

Permalink
Merge pull request #282 from Philip-21/tests
Browse files Browse the repository at this point in the history
firefly-cli pkg and internal tests
  • Loading branch information
nguyer authored Jan 12, 2024
2 parents 68497ce + 1997809 commit 07b26cb
Show file tree
Hide file tree
Showing 9 changed files with 829 additions and 0 deletions.
183 changes: 183 additions & 0 deletions internal/blockchain/ethereum/remoterpc/remoterpc_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package remoterpc

import (
"context"
"testing"

"github.com/hyperledger/firefly-cli/internal/blockchain/ethereum"
"github.com/hyperledger/firefly-cli/pkg/types"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/stretchr/testify/assert"
)

func TestNewRemoteRPCProvider(t *testing.T) {
var ctx context.Context
testCases := []struct {
Name string
Ctx context.Context
Stack *types.Stack
}{
{
Name: "testcase-1",
Ctx: ctx,
Stack: &types.Stack{
Name: "TestRPCwithEvmConnect",
Members: []*types.Organization{
{
OrgName: "Org1",
NodeName: "rpc",
Account: &ethereum.Account{
Address: "0x1234567890abcdef0123456789abcdef6789abcd",
PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
},
},
BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "Ethereum"),
BlockchainConnector: fftypes.FFEnumValue("BlockchainConnector", "Evmconnect"),
BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "rpc"),
},
},
{
Name: "testcase2",
Ctx: ctx,
Stack: &types.Stack{
Name: "TestRPCWithEthConnect",
Members: []*types.Organization{
{
OrgName: "Org7",
NodeName: "rpc",
Account: &ethereum.Account{
Address: "0x1f2a000000000000000000000000000000000000",
PrivateKey: "aabbccddeeff0011223344556677889900112233445566778899aabbccddeeff",
},
},
},
BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "Ethereum"),
BlockchainConnector: fftypes.FFEnumValue("BlockchainConnector", "Ethconnect"),
BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "rpc"),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
rpcProvider := NewRemoteRPCProvider(tc.Ctx, tc.Stack)
assert.NotNil(t, rpcProvider)
})
}
}

func TestParseAccount(t *testing.T) {
tests := []struct {
Name string
ExpectedAccount *ethereum.Account
Account map[string]interface{}
}{
{
Name: "Account 1",
Account: map[string]interface{}{
"address": "0x1234567890abcdef0123456789abcdef6789abcd",
"privateKey": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
ExpectedAccount: &ethereum.Account{
Address: "0x1234567890abcdef0123456789abcdef6789abcd",
PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
},
{
Name: "Account 2",
Account: map[string]interface{}{
"address": "0x549b5f43a40e1a0522864a004cfff2b0ca473a65",
"privateKey": "112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00",
},
ExpectedAccount: &ethereum.Account{
Address: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65",
PrivateKey: "112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00",
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
p := &RemoteRPCProvider{}

account := p.ParseAccount(tc.Account)
_, ok := account.(*ethereum.Account)
if !ok {
t.Errorf("Expected result to be of type *ethereum.Account, but got %T", account)
}
assert.Equal(t, tc.ExpectedAccount, account, "Generated account unmatched")
})
}
}

func TestGetOrgConfig(t *testing.T) {
testCases := []struct {
Name string
Org *types.Organization
Stack *types.Stack
OrgConfig *types.OrgConfig
}{
{
Name: "Testcase1",
Stack: &types.Stack{
Name: "Org-1",
},
Org: &types.Organization{
OrgName: "Org-1",
NodeName: "rpc",
Account: &ethereum.Account{
Address: "0x1234567890abcdef0123456789abcdef6789abcd",
PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
},
OrgConfig: &types.OrgConfig{
Name: "Org-1",
Key: "0x1234567890abcdef0123456789abcdef6789abcd",
},
},
{
Name: "Testcase2",
Stack: &types.Stack{
Name: "Org-2",
},
Org: &types.Organization{
OrgName: "Org-2",
NodeName: "rpc",
Account: &ethereum.Account{
Address: "0x1f2a000000000000000000000000000000000000",
PrivateKey: "9876543210987654321098765432109876543210987654321098765432109876",
},
},
OrgConfig: &types.OrgConfig{
Name: "Org-2",
Key: "0x1f2a000000000000000000000000000000000000",
},
},
{
Name: "Testcase3",
Stack: &types.Stack{
Name: "Org-3",
},
Org: &types.Organization{
OrgName: "Org-3",
NodeName: "rpc",
Account: &ethereum.Account{
Address: "0xabcdeffedcba9876543210abcdeffedc00000000",
PrivateKey: "aabbccddeeff0011223344556677889900112233445566778899aabbccddeeff",
},
},
OrgConfig: &types.OrgConfig{
Name: "Org-3",
Key: "0xabcdeffedcba9876543210abcdeffedc00000000",
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
p := &RemoteRPCProvider{}

Orgconfig := p.GetOrgConfig(tc.Stack, tc.Org)
assert.NotNil(t, Orgconfig)
assert.Equal(t, tc.OrgConfig, Orgconfig)
})
}
}
96 changes: 96 additions & 0 deletions internal/blockchain/fabric/fabconnect/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fabconnect

import (
"fmt"
"testing"

"github.com/hyperledger/firefly-cli/internal/utils"
Expand Down Expand Up @@ -83,5 +84,100 @@ func TestCreateIdentity(t *testing.T) {
assert.Equal(t, tc.ExpectedResponse, identityResp)
})
}
utils.StopMockServer(t)
}

func TestEnrollIdentity(t *testing.T) {
utils.StartMockServer(t)

testContext := utils.NewTestEndPoint(t)

testCases := []struct {
Name string
FabconnectURL string
Secret string
Signer string
Method string
ApiResponse string
ExpectedResponse *EnrollIdentityResponse
}{
{
Name: "TestIdentity-1",
Secret: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
Signer: "user-1",
Method: "POST",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
ApiResponse: `
{
"Name": "fabric_user-1",
"Success": "success"
}`,
ExpectedResponse: &EnrollIdentityResponse{
Name: "fabric_user-1",
Success: "success",
},
},
{
Name: "TestIdentity-2",
Secret: "9876543210987654321098765432109876543210987654321098765432109876",
Method: "POST",
Signer: "user-2",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
ApiResponse: `
{
"Name": "fabric_user-2",
"Success": "success"
}`,
ExpectedResponse: &EnrollIdentityResponse{
Name: "fabric_user-2",
Success: "success",
},
},
{
Name: "TestIdentity-3",
Secret: "5011213210987654321098765432109876543210987654321098765432109876",
Method: "POST",
Signer: "user-3",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
ApiResponse: `
{
"Name": "fabric_user-3",
"Success": "success"
}`,
ExpectedResponse: &EnrollIdentityResponse{
Name: "fabric_user-3",
Success: "success",
},
},
{
Name: "TestIdentity-4",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
Signer: "user-4",
Method: "POST",
ApiResponse: `
{
"Name": "fabric_user-4",
"Success": "success"
}
`,
ExpectedResponse: &EnrollIdentityResponse{
Name: "fabric_user-4",
Success: "success",
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
//mockResponse
httpmock.RegisterResponder(tc.Method, fmt.Sprintf("%s/%s/enroll", tc.FabconnectURL, tc.Signer),
httpmock.NewStringResponder(200, tc.ApiResponse))
enrolledIdentity, err := EnrollIdentity(tc.FabconnectURL, tc.Signer, tc.Secret)
if err != nil {
t.Log("enroll identity failed:", err)
}
assert.NotNil(t, enrolledIdentity)
assert.Equal(t, tc.ExpectedResponse, enrolledIdentity)
})
}
utils.StopMockServer(t)
}
30 changes: 30 additions & 0 deletions internal/blockchain/fabric/fabconnect/fabconnect_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fabconnect

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWriteFabconnectConfig(t *testing.T) {
directory := "testdata"

testCases := []struct {
Name string
filePath string
}{
{Name: "TestPath-1", filePath: directory + "/fabconfig1.yaml"},
{Name: "TestPath-2", filePath: directory + "/fabconfig2.yaml"},
{Name: "TestPath-3", filePath: directory + "/fabconfig3.yaml"},
{Name: "TestPath-4", filePath: directory + "/fabconfig4.yaml"},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := WriteFabconnectConfig(tc.filePath)
if err != nil {
t.Log("cannot write config:", err)
}
})
assert.NotNil(t, tc.filePath)
}
}
18 changes: 18 additions & 0 deletions internal/blockchain/fabric/fabconnect/testdata/fabconfig1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
maxinflight: 10
maxtxwaittime: 60
sendconcurrency: 25
receipts:
maxdocs: 1000
querylimit: 100
retryinitialdelay: 5
retrytimeout: 30
leveldb:
path: /fabconnect/receipts
events:
webhooksAllowPrivateIPs: true
leveldb:
path: /fabconnect/events
http:
port: 3000
rpc:
configpath: /fabconnect/ccp.yaml
18 changes: 18 additions & 0 deletions internal/blockchain/fabric/fabconnect/testdata/fabconfig2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
maxinflight: 10
maxtxwaittime: 60
sendconcurrency: 25
receipts:
maxdocs: 1000
querylimit: 100
retryinitialdelay: 5
retrytimeout: 30
leveldb:
path: /fabconnect/receipts
events:
webhooksAllowPrivateIPs: true
leveldb:
path: /fabconnect/events
http:
port: 3000
rpc:
configpath: /fabconnect/ccp.yaml
18 changes: 18 additions & 0 deletions internal/blockchain/fabric/fabconnect/testdata/fabconfig3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
maxinflight: 10
maxtxwaittime: 60
sendconcurrency: 25
receipts:
maxdocs: 1000
querylimit: 100
retryinitialdelay: 5
retrytimeout: 30
leveldb:
path: /fabconnect/receipts
events:
webhooksAllowPrivateIPs: true
leveldb:
path: /fabconnect/events
http:
port: 3000
rpc:
configpath: /fabconnect/ccp.yaml
18 changes: 18 additions & 0 deletions internal/blockchain/fabric/fabconnect/testdata/fabconfig4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
maxinflight: 10
maxtxwaittime: 60
sendconcurrency: 25
receipts:
maxdocs: 1000
querylimit: 100
retryinitialdelay: 5
retrytimeout: 30
leveldb:
path: /fabconnect/receipts
events:
webhooksAllowPrivateIPs: true
leveldb:
path: /fabconnect/events
http:
port: 3000
rpc:
configpath: /fabconnect/ccp.yaml
Loading

0 comments on commit 07b26cb

Please sign in to comment.