func TestReenroll(t *testing.T) {
	fabricCAClient, err := NewFabricCAClient(org1, configImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient returned error: %v", err)
	}
	user := mocks.NewMockUser("")
	_, _, err = fabricCAClient.Reenroll(nil)
	if err == nil {
		t.Fatalf("Expected error with nil user")
	}
	if err.Error() != "user required" {
		t.Fatalf("Expected error user required. Got: %s", err.Error())
	}
	_, _, err = fabricCAClient.Reenroll(user)
	if err == nil {
		t.Fatalf("Expected error with user.Name is empty")
	}
	if err.Error() != "user name missing" {
		t.Fatalf("Expected error user name missing. Got: %s", err.Error())
	}
	user = mocks.NewMockUser("testUser")
	_, _, err = fabricCAClient.Reenroll(user)
	if err == nil {
		t.Fatalf("Expected error with user.EnrollmentCertificate is empty")
	}
	if !strings.Contains(err.Error(), "createSigningIdentity failed") {
		t.Fatalf("Expected error createSigningIdentity failed. Got: %s", err.Error())
	}
	user.SetEnrollmentCertificate(readCert(t))
	key, err := cryptosuite.GetDefault().KeyGen(cryptosuite.GetECDSAP256KeyGenOpts(true))
	if err != nil {
		t.Fatalf("KeyGen return error %v", err)
	}
	user.SetPrivateKey(key)
	_, _, err = fabricCAClient.Reenroll(user)
	if err != nil {
		t.Fatalf("Reenroll return error %v", err)
	}
	wrongConfigImp := mocks.NewMockConfig(wrongCAServerURL)
	fabricCAClient, err = NewFabricCAClient(org1, wrongConfigImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient return error: %v", err)
	}
	_, _, err = fabricCAClient.Reenroll(user)
	if err == nil {
		t.Fatalf("Expected error with wrong fabric-ca server url")
	}
	if !strings.Contains(err.Error(), "reenroll failed") {
		t.Fatalf("Expected error with wrong fabric-ca server url. Got: %s", err.Error())
	}
}
TestReenroll will test multiple scenarios of re enrolling a user
package fabricca
import (
	"io/ioutil"
	"os"
	"strings"
	"testing"
	"time"
	"github.com/golang/mock/gomock"
	"github.com/hyperledger/fabric-sdk-go/api/apiconfig/mocks"
	"github.com/pkg/errors"
	config "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
	ca "github.com/hyperledger/fabric-sdk-go/api/apifabca"
	"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
	"github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite"
	cryptosuiteimpl "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/sw"
	bccspwrapper "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/wrapper"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabric-ca-client/mocks"
)
var configImp config.Config
var cryptoSuiteProvider apicryptosuite.CryptoSuite
var org1 = "peerorg1"
var caServerURL = "http://localhost:8090"
var wrongCAServerURL = "http://localhost:8091"
func TestMain(m *testing.M) {
	configImp = mocks.NewMockConfig(caServerURL)
	cryptoSuiteProvider, _ = cryptosuiteimpl.GetSuiteByConfig(configImp)
	if cryptoSuiteProvider == nil {
		panic("Failed initialize cryptoSuiteProvider")
	}
	go mocks.StartFabricCAMockServer(strings.TrimPrefix(caServerURL, "http://"))
	time.Sleep(1 * time.Second)
	os.Exit(m.Run())
}
func TestEnroll(t *testing.T) {
	fabricCAClient, err := NewFabricCAClient(org1, configImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient return error: %v", err)
	}
	_, _, err = fabricCAClient.Enroll("", "user1")
	if err == nil {
		t.Fatalf("Enroll didn't return error")
	}
	if err.Error() != "enrollmentID required" {
		t.Fatalf("Enroll didn't return right error")
	}
	_, _, err = fabricCAClient.Enroll("test", "")
	if err == nil {
		t.Fatalf("Enroll didn't return error")
	}
	if err.Error() != "enrollmentSecret required" {
		t.Fatalf("Enroll didn't return right error")
	}
	_, _, err = fabricCAClient.Enroll("enrollmentID", "enrollmentSecret")
	if err != nil {
		t.Fatalf("fabricCAClient Enroll return error %v", err)
	}
	wrongConfigImp := mocks.NewMockConfig(wrongCAServerURL)
	fabricCAClient, err = NewFabricCAClient(org1, wrongConfigImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient return error: %v", err)
	}
	_, _, err = fabricCAClient.Enroll("enrollmentID", "enrollmentSecret")
	if err == nil {
		t.Fatalf("Enroll didn't return error")
	}
	if !strings.Contains(err.Error(), "enroll failed") {
		t.Fatalf("Expected error enroll failed. Got: %s", err)
	}
}
func TestRegister(t *testing.T) {
	fabricCAClient, err := NewFabricCAClient(org1, configImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient returned error: %v", err)
	}
	user := mocks.NewMockUser("test")
	_, err = fabricCAClient.Register(user, nil)
	if err == nil {
		t.Fatalf("Expected error with nil request")
	}
	if err.Error() != "registration request required" {
		t.Fatalf("Expected error registration request required. Got: %s", err.Error())
	}
	_, err = fabricCAClient.Register(nil, &ca.RegistrationRequest{})
	if err == nil {
		t.Fatalf("Expected error with nil user")
	}
	if !strings.Contains(err.Error(), "failed to create request for signing identity") {
		t.Fatalf("Expected error failed to create request for signing identity. Got: %s", err.Error())
	}
	_, err = fabricCAClient.Register(user, &ca.RegistrationRequest{})
	if err == nil {
		t.Fatalf("Expected error without user enrolment information")
	}
	if !strings.Contains(err.Error(), "failed to create request for signing identity") {
		t.Fatalf("Expected error failed to create request for signing identity. Got: %s", err.Error())
	}
	user.SetEnrollmentCertificate(readCert(t))
	key, err := cryptosuite.GetDefault().KeyGen(cryptosuite.GetECDSAP256KeyGenOpts(true))
	if err != nil {
		t.Fatalf("KeyGen return error %v", err)
	}
	user.SetPrivateKey(key)
	_, err = fabricCAClient.Register(user, &ca.RegistrationRequest{})
	if !strings.Contains(err.Error(), "failed to register user") {
		t.Fatalf("Expected error failed to register user. Got: %s", err.Error())
	}
	var attributes []ca.Attribute
	attributes = append(attributes, ca.Attribute{Key: "test1", Value: "test2"})
	attributes = append(attributes, ca.Attribute{Key: "test2", Value: "test3"})
	secret, err := fabricCAClient.Register(user, &ca.RegistrationRequest{Name: "test",
		Affiliation: "test", Attributes: attributes})
	if err != nil {
		t.Fatalf("fabricCAClient Register return error %v", err)
	}
	if secret != "mockSecretValue" {
		t.Fatalf("fabricCAClient Register return wrong value %s", secret)
	}
}
func TestRevoke(t *testing.T) {
	cryptoSuiteProvider, err := cryptosuiteimpl.GetSuiteByConfig(configImp)
	if err != nil {
		t.Fatalf("cryptosuite.GetSuiteByConfig returned error: %v", err)
	}
	fabricCAClient, err := NewFabricCAClient(org1, configImp, cryptoSuiteProvider)
	if err != nil {
		t.Fatalf("NewFabricCAClient returned error: %v", err)
	}
	mockKey := bccspwrapper.GetKey(&mocks.MockKey{})
	user := mocks.NewMockUser("test")
	_, err = fabricCAClient.Revoke(user, nil)
	if err == nil {
		t.Fatalf("Expected error with nil request")
	}
	if err.Error() != "revocation request required" {
		t.Fatalf("Expected error revocation request required. Got: %s", err.Error())
	}
	_, err = fabricCAClient.Revoke(nil, &ca.RevocationRequest{})
	if err == nil {
		t.Fatalf("Expected error with nil user")
	}
	if !strings.Contains(err.Error(), "failed to create request for signing identity") {
		t.Fatalf("Expected error failed to create request for signing identity. Got: %s", err.Error())
	}
	user.SetEnrollmentCertificate(readCert(t))
	user.SetPrivateKey(mockKey)
	_, err = fabricCAClient.Revoke(user, &ca.RevocationRequest{})
	if err == nil {
		t.Fatalf("Expected decoding error with test cert")
	}
}