From 0a714f2ac9894606c6881f3a753f860423185a41 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Wed, 10 Jan 2024 12:27:01 +0100 Subject: [PATCH 1/5] fabric tests completed, new rpc tests Signed-off-by: Philip-21 --- .../remoterpc/remoterpc_provider_test.go | 183 ++++++++++++++++++ .../fabric/fabconnect/client_test.go | 96 +++++++++ .../fabconnect/fabconnect_config_test.go | 30 +++ .../fabconnect/testdata/fabconfig1.yaml | 18 ++ .../fabconnect/testdata/fabconfig2.yaml | 18 ++ .../fabconnect/testdata/fabconfig3.yaml | 18 ++ .../fabconnect/testdata/fabconfig4.yaml | 18 ++ 7 files changed, 381 insertions(+) create mode 100644 internal/blockchain/ethereum/remoterpc/remoterpc_provider_test.go create mode 100644 internal/blockchain/fabric/fabconnect/fabconnect_config_test.go create mode 100644 internal/blockchain/fabric/fabconnect/testdata/fabconfig1.yaml create mode 100644 internal/blockchain/fabric/fabconnect/testdata/fabconfig2.yaml create mode 100644 internal/blockchain/fabric/fabconnect/testdata/fabconfig3.yaml create mode 100644 internal/blockchain/fabric/fabconnect/testdata/fabconfig4.yaml diff --git a/internal/blockchain/ethereum/remoterpc/remoterpc_provider_test.go b/internal/blockchain/ethereum/remoterpc/remoterpc_provider_test.go new file mode 100644 index 00000000..e91d0b62 --- /dev/null +++ b/internal/blockchain/ethereum/remoterpc/remoterpc_provider_test.go @@ -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: ðereum.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: ðereum.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: ðereum.Account{ + Address: "0x1234567890abcdef0123456789abcdef6789abcd", + PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", + }, + }, + { + Name: "Account 2", + Account: map[string]interface{}{ + "address": "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", + "privateKey": "112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00", + }, + ExpectedAccount: ðereum.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: ðereum.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: ðereum.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: ðereum.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) + }) + } +} diff --git a/internal/blockchain/fabric/fabconnect/client_test.go b/internal/blockchain/fabric/fabconnect/client_test.go index 85a6b8a4..ad749ceb 100644 --- a/internal/blockchain/fabric/fabconnect/client_test.go +++ b/internal/blockchain/fabric/fabconnect/client_test.go @@ -1,6 +1,7 @@ package fabconnect import ( + "fmt" "testing" "github.com/hyperledger/firefly-cli/internal/utils" @@ -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) } diff --git a/internal/blockchain/fabric/fabconnect/fabconnect_config_test.go b/internal/blockchain/fabric/fabconnect/fabconnect_config_test.go new file mode 100644 index 00000000..a1dd637e --- /dev/null +++ b/internal/blockchain/fabric/fabconnect/fabconnect_config_test.go @@ -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) + } +} diff --git a/internal/blockchain/fabric/fabconnect/testdata/fabconfig1.yaml b/internal/blockchain/fabric/fabconnect/testdata/fabconfig1.yaml new file mode 100644 index 00000000..917aaf96 --- /dev/null +++ b/internal/blockchain/fabric/fabconnect/testdata/fabconfig1.yaml @@ -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 diff --git a/internal/blockchain/fabric/fabconnect/testdata/fabconfig2.yaml b/internal/blockchain/fabric/fabconnect/testdata/fabconfig2.yaml new file mode 100644 index 00000000..917aaf96 --- /dev/null +++ b/internal/blockchain/fabric/fabconnect/testdata/fabconfig2.yaml @@ -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 diff --git a/internal/blockchain/fabric/fabconnect/testdata/fabconfig3.yaml b/internal/blockchain/fabric/fabconnect/testdata/fabconfig3.yaml new file mode 100644 index 00000000..917aaf96 --- /dev/null +++ b/internal/blockchain/fabric/fabconnect/testdata/fabconfig3.yaml @@ -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 diff --git a/internal/blockchain/fabric/fabconnect/testdata/fabconfig4.yaml b/internal/blockchain/fabric/fabconnect/testdata/fabconfig4.yaml new file mode 100644 index 00000000..917aaf96 --- /dev/null +++ b/internal/blockchain/fabric/fabconnect/testdata/fabconfig4.yaml @@ -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 From 18e0d8e071569d18fb95cf2e9ae98ff749677582 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Thu, 11 Jan 2024 17:45:43 +0100 Subject: [PATCH 2/5] new tests for fabric provider Signed-off-by: Philip-21 --- .../blockchain/fabric/fabric_provider_test.go | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 internal/blockchain/fabric/fabric_provider_test.go diff --git a/internal/blockchain/fabric/fabric_provider_test.go b/internal/blockchain/fabric/fabric_provider_test.go new file mode 100644 index 00000000..9f7cecc2 --- /dev/null +++ b/internal/blockchain/fabric/fabric_provider_test.go @@ -0,0 +1,223 @@ +package fabric + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-cli/internal/blockchain/ethereum" + "github.com/hyperledger/firefly-cli/internal/log" + "github.com/hyperledger/firefly-cli/pkg/types" + "github.com/hyperledger/firefly-common/pkg/fftypes" + "github.com/stretchr/testify/assert" +) + +func TestNewFabricProvider(t *testing.T) { + Stack := &types.Stack{ + Name: "FabricUser", + Members: []*types.Organization{{OrgName: "Hyperledger-fabric"}}, + BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "fabric"), + BlockchainConnector: fftypes.FFEnumValue("BlockchainConnector", "fabonnect"), + BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "fabric"), + } + ctx := log.WithLogger(context.Background(), &log.StdoutLogger{}) + fabricProvider := NewFabricProvider(ctx, Stack) + assert.NotNil(t, fabricProvider) + assert.NotNil(t, fabricProvider.ctx) + assert.NotNil(t, fabricProvider.stack) + assert.NotNil(t, fabricProvider.log) +} + +func TestGetOrgConfig(t *testing.T) { + testcases := []struct { + Name string + Stack *types.Stack + OrgConfig *types.OrgConfig + Member *types.Organization + }{ + { + Name: "TestFabric-1", + Stack: &types.Stack{Name: "fabric_user-1"}, + Member: &types.Organization{ + OrgName: "Org-1", + NodeName: "fabric", + Account: ðereum.Account{ + Address: "0x1234567890abcdef0123456789abcdef6789abcd", + PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", + }, + }, + OrgConfig: &types.OrgConfig{ + Name: "Org-1", + Key: "Org-1", + }, + }, + { + Name: "TestFabric-2", + Stack: &types.Stack{Name: "fabri_user-2"}, + Member: &types.Organization{ + OrgName: "Org-2", + NodeName: "besu", + Account: ðereum.Account{ + Address: "0x1f2a000000000000000000000000000000000000", + PrivateKey: "9876543210987654321098765432109876543210987654321098765432109876", + }, + }, + OrgConfig: &types.OrgConfig{ + Name: "Org-2", + Key: "Org-2", + }, + }, + } + for _, tc := range testcases { + t.Run(tc.Name, func(t *testing.T) { + p := &FabricProvider{} + orgConfig := p.GetOrgConfig(tc.Stack, tc.Member) + assert.NotNil(t, orgConfig) + assert.Equal(t, tc.OrgConfig, orgConfig) + }) + + } +} + +func TestGetFabconnetServiceDefinitions(t *testing.T) { + stack := &types.Stack{ + Name: "fabric_user-1", + RuntimeDir: "mock_runtime_dir", + VersionManifest: &types.VersionManifest{ + Fabconnect: &types.ManifestEntry{ + Image: "fabric-apline", + Local: true, + }, + }, + RemoteFabricNetwork: true, + } + p := &FabricProvider{ + stack: stack, + } + // Create mock organizations + members := []*types.Organization{ + { + ID: "org1", + ExposedConnectorPort: 4000, + }, + { + ID: "org2", + ExposedConnectorPort: 4001, + }, + { + ID: "org3", + ExposedConnectorPort: 4002, + }, + { + ID: "org4", + ExposedConnectorPort: 4003, + }, + { + ID: "org5", + ExposedConnectorPort: 4004, + }, + } + serviceDefinitions := p.getFabconnectServiceDefinitions(members) + assert.NotNil(t, serviceDefinitions) +} + +func TestParseAccount(t *testing.T) { + input := map[string]interface{}{ + "name": "user-1", + "orgName": "hyperledger", + } + + besuProvider := &FabricProvider{} + result := besuProvider.ParseAccount(input) + + if _, ok := result.(*Account); !ok { + t.Errorf("Expected result to be of type *ethereum.Account, but got %T", result) + } + expectedAccount := &Account{ + Name: "user-1", + OrgName: "hyperledger", + } + assert.Equal(t, expectedAccount, result, "Generated result unmatched") + +} + +func TestGetConnectorName(t *testing.T) { + testString := "fabconnect" + p := &FabricProvider{} + connector := p.GetConnectorName() + assert.NotNil(t, connector) + assert.Equal(t, testString, connector) +} + +func TestGetConnectorURL(t *testing.T) { + testCases := []struct { + Name string + Org *types.Organization + ExpectedURL string + }{ + { + Name: "testcase-1", + Org: &types.Organization{ + ID: "user-1", + NodeName: "fabric", + Account: &Account{ + Name: "Nicko", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-1:3000", + }, + { + Name: "testcase-2", + Org: &types.Organization{ + ID: "user-2", + NodeName: "fabric", + Account: &Account{ + Name: "Richardson", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-2:3000", + }, + { + Name: "testcase-3", + Org: &types.Organization{ + ID: "user-3", + NodeName: "fabric", + Account: &Account{ + Name: "Philip", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-3:3000", + }, + } + for _, tc := range testCases { + t.Run(tc.Name, func(t *testing.T) { + p := &FabricProvider{} + URL := p.GetConnectorURL(tc.Org) + assert.Equal(t, tc.ExpectedURL, URL) + }) + } + +} + +// func TestCreateChannel(t *testing.T) { +// ctx := log.WithLogger(context.Background(), &log.StdoutLogger{ +// LogLevel: log.Info, +// }) + +// Stack := &types.Stack{ +// Name: "fabric_user-1", +// StackDir: "mock_stack_dir", +// } + +// p := &FabricProvider{ +// stack: Stack, +// ctx: ctx, +// log: Log, +// } +// err := p.createChannel() +// if err != nil { +// t.Log("unable to create channel:", err) +// } +// } From 4c767b98da9c98de35a8dbca2e6395a6b8987763 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Thu, 11 Jan 2024 20:44:00 +0100 Subject: [PATCH 3/5] new tests for fabric provider Signed-off-by: Philip-21 --- .../blockchain/fabric/fabric_provider_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/blockchain/fabric/fabric_provider_test.go b/internal/blockchain/fabric/fabric_provider_test.go index 9f7cecc2..837becc8 100644 --- a/internal/blockchain/fabric/fabric_provider_test.go +++ b/internal/blockchain/fabric/fabric_provider_test.go @@ -201,6 +201,24 @@ func TestGetConnectorURL(t *testing.T) { } +func TestGetConnectorExternalURL(t*testing.T){ + testCases := []struct { + Name string + Org *types.Organization + ExpectedURL string + }{ + { + + }, + } +} + +func TestGetDockerPlatform(t *testing.T) { + expectedString := "linux/amd64" + String := getDockerPlatform() + assert.Equal(t, expectedString, String) +} + // func TestCreateChannel(t *testing.T) { // ctx := log.WithLogger(context.Background(), &log.StdoutLogger{ // LogLevel: log.Info, From 2b3e8699daab4531c80605dff400a63d18f2d94d Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Thu, 11 Jan 2024 21:07:32 +0100 Subject: [PATCH 4/5] new tests for fabric provider Signed-off-by: Philip-21 --- .../blockchain/fabric/fabric_provider_test.go | 99 ++++++++++++++++--- .../fabric/testdata/test_contracts.json | 14 +++ 2 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 internal/blockchain/fabric/testdata/test_contracts.json diff --git a/internal/blockchain/fabric/fabric_provider_test.go b/internal/blockchain/fabric/fabric_provider_test.go index 837becc8..66e58e45 100644 --- a/internal/blockchain/fabric/fabric_provider_test.go +++ b/internal/blockchain/fabric/fabric_provider_test.go @@ -2,6 +2,8 @@ package fabric import ( "context" + "os" + "path/filepath" "testing" "github.com/hyperledger/firefly-cli/internal/blockchain/ethereum" @@ -148,7 +150,7 @@ func TestGetConnectorName(t *testing.T) { assert.Equal(t, testString, connector) } -func TestGetConnectorURL(t *testing.T) { +func TestGetConnectorExternalURL(t *testing.T) { testCases := []struct { Name string Org *types.Organization @@ -163,8 +165,9 @@ func TestGetConnectorURL(t *testing.T) { Name: "Nicko", OrgName: "hyperledger", }, + ExposedConnectorPort: 8900, }, - ExpectedURL: "http://fabconnect_user-1:3000", + ExpectedURL: "http://127.0.0.1:8900", }, { Name: "testcase-2", @@ -175,8 +178,9 @@ func TestGetConnectorURL(t *testing.T) { Name: "Richardson", OrgName: "hyperledger", }, + ExposedConnectorPort: 3000, }, - ExpectedURL: "http://fabconnect_user-2:3000", + ExpectedURL: "http://127.0.0.1:3000", }, { Name: "testcase-3", @@ -187,30 +191,69 @@ func TestGetConnectorURL(t *testing.T) { Name: "Philip", OrgName: "hyperledger", }, + ExposedConnectorPort: 4005, }, - ExpectedURL: "http://fabconnect_user-3:3000", + ExpectedURL: "http://127.0.0.1:4005", }, } for _, tc := range testCases { - t.Run(tc.Name, func(t *testing.T) { - p := &FabricProvider{} - URL := p.GetConnectorURL(tc.Org) - assert.Equal(t, tc.ExpectedURL, URL) - }) + p := &FabricProvider{} + ExternalURL := p.GetConnectorExternalURL(tc.Org) + assert.Equal(t, tc.ExpectedURL, ExternalURL) } - } -func TestGetConnectorExternalURL(t*testing.T){ +func TestGetConnectorURL(t *testing.T) { testCases := []struct { Name string Org *types.Organization ExpectedURL string }{ { - + Name: "testcase-1", + Org: &types.Organization{ + ID: "user-1", + NodeName: "fabric", + Account: &Account{ + Name: "Nicko", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-1:3000", + }, + { + Name: "testcase-2", + Org: &types.Organization{ + ID: "user-2", + NodeName: "fabric", + Account: &Account{ + Name: "Richardson", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-2:3000", + }, + { + Name: "testcase-3", + Org: &types.Organization{ + ID: "user-3", + NodeName: "fabric", + Account: &Account{ + Name: "Philip", + OrgName: "hyperledger", + }, + }, + ExpectedURL: "http://fabconnect_user-3:3000", }, } + for _, tc := range testCases { + t.Run(tc.Name, func(t *testing.T) { + p := &FabricProvider{} + URL := p.GetConnectorURL(tc.Org) + assert.Equal(t, tc.ExpectedURL, URL) + }) + } + } func TestGetDockerPlatform(t *testing.T) { @@ -219,6 +262,38 @@ func TestGetDockerPlatform(t *testing.T) { assert.Equal(t, expectedString, String) } +func TestGetContracts(t *testing.T) { + FilePath := "testdata" + testContractFile := filepath.Join(FilePath, "/test_contracts.json") + // Sample contract JSON content for testing + const testContractJSON = `{ + "contracts": { + "Contract1": { + "name": "fabric_1", + "abi": "fabric_abi_1", + "bin": "sample_bin_1" + }, + "Contract2": { + "name": "fabric_2", + "abi": "fabric_abi_2", + "bin": "fabric_bin_2" + } + } + }` + p := &FabricProvider{} + + err := os.WriteFile(testContractFile, []byte(testContractJSON), 0755) + if err != nil { + t.Log("unable to write file:", err) + } + contracts, err := p.GetContracts(testContractFile, nil) + if err != nil { + t.Log("unable to get contract", err) + } + assert.NotNil(t, contracts) +} + +//Implement a Mock logger to make this work // func TestCreateChannel(t *testing.T) { // ctx := log.WithLogger(context.Background(), &log.StdoutLogger{ // LogLevel: log.Info, diff --git a/internal/blockchain/fabric/testdata/test_contracts.json b/internal/blockchain/fabric/testdata/test_contracts.json new file mode 100644 index 00000000..65f073c3 --- /dev/null +++ b/internal/blockchain/fabric/testdata/test_contracts.json @@ -0,0 +1,14 @@ +{ + "contracts": { + "Contract1": { + "name": "fabric_1", + "abi": "fabric_abi_1", + "bin": "sample_bin_1" + }, + "Contract2": { + "name": "fabric_2", + "abi": "fabric_abi_2", + "bin": "fabric_bin_2" + } + } + } \ No newline at end of file From 1997809a83e10d1ff95eeafcab39fe713337dbd2 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Fri, 12 Jan 2024 02:14:33 +0100 Subject: [PATCH 5/5] additional tests for fabric provider Signed-off-by: Philip-21 --- .../blockchain/fabric/fabric_provider_test.go | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/internal/blockchain/fabric/fabric_provider_test.go b/internal/blockchain/fabric/fabric_provider_test.go index 66e58e45..a8a6116c 100644 --- a/internal/blockchain/fabric/fabric_provider_test.go +++ b/internal/blockchain/fabric/fabric_provider_test.go @@ -2,14 +2,17 @@ package fabric import ( "context" + "fmt" "os" "path/filepath" "testing" "github.com/hyperledger/firefly-cli/internal/blockchain/ethereum" "github.com/hyperledger/firefly-cli/internal/log" + "github.com/hyperledger/firefly-cli/internal/utils" "github.com/hyperledger/firefly-cli/pkg/types" "github.com/hyperledger/firefly-common/pkg/fftypes" + "github.com/jarcoal/httpmock" "github.com/stretchr/testify/assert" ) @@ -293,6 +296,121 @@ func TestGetContracts(t *testing.T) { assert.NotNil(t, contracts) } +func TestCreateAccount(t *testing.T) { + testAccounts := []struct { + Name string + Stack *types.Stack + Args []string + }{ + { + Name: "TestAccount-1", + Args: []string{}, + Stack: &types.Stack{ + Name: "user-1", + BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "fabric"), + BlockchainConnector: fftypes.FFEnumValue("BlockChainConnector", "fabric"), + BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "fabric"), + Members: []*types.Organization{ + { + ID: "org1", + OrgName: "hyperledger", + ExposedConnectorPort: 4000, + }, + }, + }, + }, + { + Name: "TestAccount-2", + Args: []string{}, + Stack: &types.Stack{ + Name: "user-2", + BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "fabric"), + BlockchainConnector: fftypes.FFEnumValue("BlockChainConnector", "fabric"), + BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "fabric"), + Members: []*types.Organization{ + { + ID: "org1", + OrgName: "solana", + ExposedConnectorPort: 4001, + }, + }, + }, + }, + + { + Name: "TestAccount-3", + Args: []string{}, + Stack: &types.Stack{ + Name: "user-3", + BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "fabric"), + BlockchainConnector: fftypes.FFEnumValue("BlockChainConnector", "fabric"), + BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "fabric"), + Members: []*types.Organization{ + { + ID: "org1", + OrgName: "ethereum", + ExposedConnectorPort: 4002, + }, + }, + }, + }, + } + for _, tc := range testAccounts { + p := &FabricProvider{ + stack: tc.Stack, + } + Account, err := p.CreateAccount(tc.Args) + if err != nil { + t.Log("unable to create account", err) + } + assert.NotNil(t, Account) + } +} + +func TestRegisterIdentity(t *testing.T) { + t.Run("register", func(t *testing.T) { + utils.StartMockServer(t) + + Member := &types.Organization{ + ID: "fabric_user-1", + NodeName: "fabric", + Account: &Account{Name: "Nicko", OrgName: "hyperledger"}, + ExposedConnectorPort: 3000, + } + createIdentityURL := fmt.Sprintf("http://127.0.0.1:%v/identities", Member.ExposedConnectorPort) + enrollIdentityURL := fmt.Sprintf("http://127.0.0.1:%v/identities/Nicko/enroll", Member.ExposedConnectorPort) + + IdentityName := "Nicko" + createdApiResponse := ` + { + "Name": "Nicko", + "Secret": "9876543210987654321098765432109876543210987654321098765432109876" + }` + enrolledApiResponse := ` + { + "Name": "Nicko", + "OrgName": "hyperledger" + }` + + httpmock.RegisterResponder("POST", createIdentityURL, + httpmock.NewStringResponder(200, createdApiResponse)) + + httpmock.RegisterResponder("POST", enrollIdentityURL, + httpmock.NewStringResponder(200, enrolledApiResponse)) + + p := &FabricProvider{} + + account, err := p.registerIdentity(Member, IdentityName) + if err != nil { + t.Log("cannot register identity:", err) + } + assert.NotNil(t, account) + assert.NotNil(t, account.Name) + assert.NotNil(t, account.OrgName) + }) + +} + //Implement a Mock logger to make this work // func TestCreateChannel(t *testing.T) { // ctx := log.WithLogger(context.Background(), &log.StdoutLogger{