forked from hyperledger/fabric-sdk-go
/
mockidentitymgr.go
87 lines (71 loc) · 2.19 KB
/
mockidentitymgr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"fmt"
"github.com/akorosmezey/fabric-sdk-go/pkg/common/providers/msp"
mspmocks "github.com/akorosmezey/fabric-sdk-go/pkg/msp/test/mockmsp"
"github.com/pkg/errors"
)
// MockIdentityManager is a mock IdentityManager
type MockIdentityManager struct {
users map[string]msp.SigningIdentity
}
// UsersOptions holds optional users
type UsersOptions struct {
users map[string]msp.SigningIdentity
}
// UsersOption describes a functional parameter for the New constructor
type UsersOption func(*UsersOptions) error
// WithUsers option
func WithUsers(users map[string]msp.SigningIdentity) UsersOption {
return func(mgr *UsersOptions) error {
if mgr.users != nil {
return errors.New("already initialized")
}
mgr.users = users
return nil
}
}
// WithUser option
func WithUser(username string, org string) UsersOption {
return func(mgr *UsersOptions) error {
if mgr.users != nil {
return errors.New("already initialized")
}
mgr.users = make(map[string]msp.SigningIdentity)
mgr.users[username] = mspmocks.NewMockSigningIdentity(username, org)
return nil
}
}
// NewMockIdentityManager Constructor for a identity manager.
func NewMockIdentityManager(opts ...UsersOption) msp.IdentityManager {
manager := MockIdentityManager{}
usersOptions := UsersOptions{}
for _, param := range opts {
err := param(&usersOptions)
if err != nil {
panic(fmt.Errorf("failed to create IdentityManager: %s", err))
}
}
if usersOptions.users != nil {
manager.users = usersOptions.users
} else {
manager.users = make(map[string]msp.SigningIdentity)
}
return &manager
}
// GetSigningIdentity will return an identity that can be used to cryptographically sign an object
func (mgr *MockIdentityManager) GetSigningIdentity(id string) (msp.SigningIdentity, error) {
si, ok := mgr.users[id]
if !ok {
return nil, msp.ErrUserNotFound
}
return si, nil
}
// CreateSigningIdentity creates a signing identity with the given options
func (mgr *MockIdentityManager) CreateSigningIdentity(opts ...msp.SigningIdentityOption) (msp.SigningIdentity, error) {
return nil, errors.New("not implemented")
}