-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
122 lines (101 loc) · 2.53 KB
/
setup_test.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package rbac_test
import (
"testing"
"github.com/hyperledger/fabric-chaincode-go/pkg/cid"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-chaincode-go/shimtest"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/stretchr/testify/mock"
"github.com/stickypixel/hyperledger/rbac"
)
/*
*
* Setup all types for RBAC resources and contractNames
*
*/
const (
resourceAsset = "asset"
resourceTransfer = "transfer"
resourceWallet = "wallet"
contractCreateTransfer = "createTransfer"
contractCreateWallet = "createWallet"
contractQueryLedger = "queryLedger"
)
/*
*
* Setup all permission maps
*
*/
func getRolePerms() rbac.RolePermissions {
return rbac.RolePermissions{
"admin": {
ContractPermissions: rbac.ContractPermissions{
contractCreateTransfer: true,
contractCreateWallet: false,
contractQueryLedger: true,
},
QueryPermissions: rbac.QueryPermissions{
resourceAsset: filterFields,
resourceTransfer: allow,
resourceWallet: disallow,
},
},
"user": {
ContractPermissions: rbac.ContractPermissions{
contractCreateWallet: true,
contractQueryLedger: true,
},
QueryPermissions: rbac.QueryPermissions{
resourceTransfer: inTransfer,
resourceWallet: owner,
},
},
}
}
/*
*
* Create an empty chaincode interface
*
*/
type emptyChaincode struct {
}
func (t *emptyChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
func (t *emptyChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
func initEmptyStub() (stub *shimtest.MockStub) {
cc := new(emptyChaincode)
stub = shimtest.NewMockStub("__TEST__", cc)
stub.MockInit("__TEST_INIT__", nil)
return stub
}
/*
*
* Create a mockCID so we can mock calls to the CID service
*
*/
type mockCID struct {
cid.ClientIdentity
mock.Mock
}
func (mc *mockCID) GetID() (string, error) {
args := mc.Called()
return args.String(0), nil
}
func (mc *mockCID) GetAttributeValue(attrName string) (string, bool, error) {
args := mc.Called(attrName)
return args.String(0), args.Bool(1), args.Error(2)
}
func simpleSetup(t *testing.T, userRoles string) rbac.AuthServiceInterface {
stub := initEmptyStub()
cid := new(mockCID)
cid.On("GetAttributeValue", "roles").Return(userRoles, true, nil)
cid.On("GetID").Return("testuserID")
appAuth, err := rbac.New(stub, cid, getRolePerms(), mock.Anything)
if err != nil {
t.Fatalf("New appAuth failed unexpectedly")
}
return appAuth
}