Skip to content

Commit

Permalink
Merge pull request #280 from Philip-21/fabric
Browse files Browse the repository at this point in the history
test environment setups
  • Loading branch information
nguyer authored Jan 5, 2024
2 parents de9ad2f + 923a518 commit f6b6b9a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/google/go-containerregistry v0.8.0
github.com/hyperledger/firefly-common v1.1.2
github.com/hyperledger/firefly-signer v0.9.6
github.com/jarcoal/httpmock v1.3.1 // indirect
github.com/mattn/go-isatty v0.0.19
github.com/miracl/conflate v1.2.1
github.com/mitchellh/go-homedir v1.1.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/invopop/yaml v0.1.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
github.com/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -640,6 +642,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down
87 changes: 87 additions & 0 deletions internal/blockchain/fabric/fabconnect/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fabconnect

import (
"testing"

"github.com/hyperledger/firefly-cli/internal/utils"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

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

testContext := utils.NewTestEndPoint(t)
tests := []struct {
Name string
FabconnectURL string
Signer string
ApiResponse string
Method string
ExpectedResponse *CreateIdentityResponse
}{
{
Name: "TestIdentity-1",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
Signer: "user-1",
Method: "POST",
ApiResponse: `
{
"Name": "fabric_user-1",
"Secret": "9876543210987654321098765432109876543210987654321098765432109876"
}
`,
ExpectedResponse: &CreateIdentityResponse{
Name: "fabric_user-1",
Secret: "9876543210987654321098765432109876543210987654321098765432109876",
},
},
{
Name: "TestIdentity-2",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
Signer: "user-2",
Method: "POST",
ApiResponse: `
{
"Name": "fabric_user-2",
"Secret": "aabbccddeeff0011223344556677889900112233445566778899aabbccddeeff"
}
`,
ExpectedResponse: &CreateIdentityResponse{
Name: "fabric_user-2",
Secret: "aabbccddeeff0011223344556677889900112233445566778899aabbccddeeff",
},
},
{
Name: "TestIdentity-3",
FabconnectURL: testContext.FabricURL + "/fabconnect/identities",
Signer: "user-3",
Method: "POST",
ApiResponse: `
{
"Name": "fabric_user-3",
"Secret": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
}
`,
ExpectedResponse: &CreateIdentityResponse{
Name: "fabric_user-3",
Secret: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
//mockResponse
httpmock.RegisterResponder(tc.Method, tc.FabconnectURL,
httpmock.NewStringResponder(200, tc.ApiResponse))

identityResp, err := CreateIdentity(tc.FabconnectURL, tc.Signer)
if err != nil {
t.Fatalf("unable to create identity: %v", err)
}
assert.NotNil(t, identityResp)
assert.Equal(t, tc.ExpectedResponse, identityResp)
})
}
utils.StartMockServer(t)
}
37 changes: 37 additions & 0 deletions internal/utils/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file contains different Setup and tools, for the FireFly-CLI testing Environment
package utils

import (
"testing"

"github.com/jarcoal/httpmock"
)

type TestHelper struct {
FabricURL string
EthConnectURL string
EvmConnectURL string
}

var (
FabricEndpoint = "http://localhost:7054"
EthConnectEndpoint = "http://localhost:8080"
EvmConnectEndpoint = "http://localhost:5008"
)

func StartMockServer(t *testing.T) {
httpmock.Activate()
}

// mockprotocol endpoints for testing
func NewTestEndPoint(t *testing.T) *TestHelper {
return &TestHelper{
FabricURL: FabricEndpoint,
EthConnectURL: EthConnectEndpoint,
EvmConnectURL: EvmConnectEndpoint,
}
}

func StopMockServer(_ *testing.T) {
httpmock.DeactivateAndReset()
}

0 comments on commit f6b6b9a

Please sign in to comment.