github.com/blockchain-gm/fabric-ca@v0.0.0-20200423072702-b2c40c7ac69c/lib/identity_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package lib 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-ca/api" 13 clientcred "github.com/hyperledger/fabric-ca/lib/client/credential" 14 "github.com/hyperledger/fabric-ca/lib/client/credential/x509" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func getIdentity(t *testing.T) *Identity { 19 cred := x509.NewCredential("../testdata/ec.pem", "../testdata/ec-key.pem", nil) 20 err := cred.Load() 21 if err != nil { 22 t.Fatalf("Failed to load credential from non existant file ../tesdata/ec.pem: %s", err.Error()) 23 } 24 id := NewIdentity(nil, "test", []clientcred.Credential{cred}) 25 return id 26 } 27 28 func TestIdentity(t *testing.T) { 29 id := getIdentity(t) 30 testGetName(id, t) 31 testGetECert(id, t) 32 } 33 34 func TestBadStoreIdentity(t *testing.T) { 35 id := &Identity{} 36 err := id.Store() 37 if err == nil { 38 t.Error("TestBadStoreIdentity passed but should have failed") 39 } 40 } 41 42 func TestBadRegistration(t *testing.T) { 43 id := &Identity{} 44 req := &api.RegistrationRequest{} 45 _, err := id.Register(req) 46 if err == nil { 47 t.Error("Empty registration request should have failed") 48 } 49 } 50 51 func testGetName(id *Identity, t *testing.T) { 52 name := id.GetName() 53 if name != "test" { 54 t.Error("Incorrect name retrieved") 55 } 56 } 57 58 func testGetECert(id *Identity, t *testing.T) { 59 ecert := id.GetECert() 60 if ecert == nil { 61 t.Error("No ECert was returned") 62 } 63 } 64 65 func TestGetCertificatesErr(t *testing.T) { 66 id := getIdentity(t) 67 id.client = &Client{ 68 Config: &ClientConfig{}, 69 } 70 err := id.GetCertificates(&api.GetCertificatesRequest{}, nil) 71 assert.Error(t, err, "Should fail, no server to contact") 72 }