This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathchaincode_owner_test.go
167 lines (137 loc) · 5.28 KB
/
chaincode_owner_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package owner_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/identity/testdata"
testcc "github.com/s7techlab/cckit/testing"
)
var _ = Describe(`Chaincode owner`, func() {
var (
ownerIdentity1 = testdata.Certificates[0].MustIdentity(`SOME_MSP`)
ownerIdentity2 = testdata.Certificates[1].MustIdentity(`SOME_MSP`) // same msp
nonOwnerIdentity = testdata.Certificates[2].MustIdentity(`SOME_MSP`)
ownerSvc = owner.NewService()
cc, ctx = testcc.NewTxHandler(`Chaincode owner`)
)
Context(`Register`, func() {
It("Allow to register self as owner on empty state", func() {
cc.From(ownerIdentity1).Tx(func() {
owner, err := ownerSvc.CreateOwnerTxCreator(ctx, &emptypb.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(owner.MspId).To(Equal(ownerIdentity1.GetMSPIdentifier()))
Expect(owner.Cert).To(Equal(ownerIdentity1.GetPEM()))
Expect(owner.Subject).To(Equal(ownerIdentity1.GetSubject()))
Expect(owner.Issuer).To(Equal(ownerIdentity1.GetIssuer()))
Expect(owner.ExpiresAt.String()).To(Equal(timestamppb.New(ownerIdentity1.ExpiresAt()).String()))
Expect(owner.UpdatedByMspId).To(Equal(ownerIdentity1.GetMSPIdentifier()))
Expect(owner.UpdatedByCert).To(Equal(ownerIdentity1.GetPEM()))
Expect(owner.UpdatedAt.String()).To(Equal(cc.TxTimestamp().String()))
})
})
It("Disallow to register same owner once more", func() {
cc.From(ownerIdentity1).Tx(func() {
cc.Expect(ownerSvc.CreateOwner(ctx, &owner.CreateOwnerRequest{
MspId: ownerIdentity1.GetMSPIdentifier(),
Cert: ownerIdentity1.GetPEM(),
})).HasError(`state key already exists: ChaincodeOwner`)
})
})
It("Disallow to register new owner from non registered identity", func() {
cc.From(ownerIdentity2).Tx(func() {
cc.Expect(ownerSvc.CreateOwner(ctx, &owner.CreateOwnerRequest{
MspId: ownerIdentity2.GetMSPIdentifier(),
Cert: ownerIdentity2.GetPEM(),
})).HasError(`tx invoker is not owner`)
})
})
It("Allow to register new owner by current owner", func() {
cc.From(ownerIdentity1).Tx(func() {
owner, err := ownerSvc.CreateOwner(ctx, &owner.CreateOwnerRequest{
MspId: ownerIdentity2.GetMSPIdentifier(),
Cert: ownerIdentity2.GetPEM(),
})
Expect(err).NotTo(HaveOccurred())
Expect(owner.MspId).To(Equal(ownerIdentity2.GetMSPIdentifier()))
Expect(owner.Cert).To(Equal(ownerIdentity2.GetPEM()))
})
})
It("Allow to get owner list", func() {
cc.From(nonOwnerIdentity).Tx(func() {
owners, err := ownerSvc.ListOwners(ctx, &emptypb.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(owners.Items).To(HaveLen(2))
})
})
})
Context(`Check`, func() {
It("Non owner receives error", func() {
cc.From(nonOwnerIdentity).Tx(func() {
cc.Expect(ownerSvc.GetOwnerByTxCreator(ctx, &emptypb.Empty{})).
HasError(`find owner by tx creator's msp_id and cert subject: state entry not found: ChaincodeOwner`)
})
})
It("Owner receives owner info", func() {
cc.From(ownerIdentity1).Tx(func() {
owner, err := ownerSvc.GetOwnerByTxCreator(ctx, &emptypb.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(owner.MspId).To(Equal(ownerIdentity1.GetMSPIdentifier()))
Expect(owner.Cert).To(Equal(ownerIdentity1.GetPEM()))
})
})
})
Context(`Update`, func() {
It("Disallow non owner to update owner", func() {
cc.From(nonOwnerIdentity).Tx(func() {
cc.Expect(ownerSvc.UpdateOwner(ctx, &owner.UpdateOwnerRequest{
MspId: ownerIdentity2.GetMSPIdentifier(),
Cert: ownerIdentity2.GetPEM(),
})).HasError(`tx invoker is not owner`)
})
})
It("Disallow to update owner with same cert", func() {
cc.From(ownerIdentity2).Tx(func() {
cc.Expect(ownerSvc.UpdateOwner(ctx, &owner.UpdateOwnerRequest{
MspId: ownerIdentity2.GetMSPIdentifier(),
Cert: ownerIdentity2.GetPEM(),
})).HasError(`new cert same as old cert`)
})
})
})
Context(`Delete`, func() {
It("Disallow non owner to delete owner", func() {
cc.From(nonOwnerIdentity).Tx(func() {
cc.Expect(ownerSvc.DeleteOwner(ctx, &owner.OwnerId{
MspId: ownerIdentity2.GetMSPIdentifier(),
Subject: ownerIdentity2.GetSubject(),
})).HasError(`tx invoker is not owner`)
})
})
It("Allow owner to delete owner, including self", func() {
cc.From(ownerIdentity1).Tx(func() {
deletedOwner, err := ownerSvc.DeleteOwner(ctx, &owner.OwnerId{
MspId: ownerIdentity1.GetMSPIdentifier(),
Subject: ownerIdentity1.GetSubject(),
})
Expect(err).NotTo(HaveOccurred())
Expect(deletedOwner.MspId).To(Equal(ownerIdentity1.GetMSPIdentifier()))
Expect(deletedOwner.Cert).To(Equal(ownerIdentity1.GetPEM()))
})
cc.From(nonOwnerIdentity).Tx(func() {
owners, err := ownerSvc.ListOwners(ctx, &emptypb.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(owners.Items).To(HaveLen(1))
})
})
It("Disallow to delete last owner", func() {
cc.From(ownerIdentity2).Tx(func() {
cc.Expect(ownerSvc.DeleteOwner(ctx, &owner.OwnerId{
MspId: ownerIdentity2.GetMSPIdentifier(),
Subject: ownerIdentity2.GetSubject(),
})).HasError(`delete last owner is not allowed`)
})
})
})
})