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
/
cars_test.go
90 lines (71 loc) · 2.64 KB
/
cars_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
package cars
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/identity/testdata"
"github.com/s7techlab/cckit/state"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"
)
func TestCars(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cars Suite")
}
// load actor certificates
var (
Authority = testdata.Certificates[0].MustIdentity(`SOME_MSP`)
Someone = testdata.Certificates[1].MustIdentity(`SOME_MSP`)
)
var _ = Describe(`Cars`, func() {
//Create chaincode mock
cc := testcc.NewMockStub(`cars`, New())
ccWithoutAC := testcc.NewMockStub(`cars`, NewWithoutAccessControl())
BeforeSuite(func() {
// init chaincode
expectcc.ResponseOk(cc.From(Authority).Init()) // init chaincode from authority
})
Describe("Car", func() {
It("Allow authority to add information about car", func() {
//invoke chaincode method from authority actor
expectcc.ResponseOk(cc.From(Authority).Invoke(`carRegister`, Payloads[0]))
})
It("Disallow non authority to add information about car", func() {
//invoke chaincode method from non authority actor
expectcc.ResponseError(
cc.From(Someone).Invoke(`carRegister`, Payloads[0]),
owner.ErrOwnerOnly) // expect "only owner" error
})
It("Allow non authority to add information about car to chaincode without access control", func() {
//invoke chaincode method from non authority actor
expectcc.ResponseOk(
ccWithoutAC.From(Someone).Invoke(`carRegister`, Payloads[0]))
})
It("Disallow authority to add duplicate information about car", func() {
expectcc.ResponseError(
cc.From(Authority).Invoke(`carRegister`, Payloads[0]),
state.ErrKeyAlreadyExists) //expect car id already exists
})
It("Allow everyone to retrieve car information", func() {
car := expectcc.PayloadIs(cc.Invoke(`carGet`, Payloads[0].Id),
&Car{}).(Car)
Expect(car.Title).To(Equal(Payloads[0].Title))
Expect(car.Id).To(Equal(Payloads[0].Id))
})
It("Allow everyone to get car list", func() {
// &[]Car{} - declares target type for unmarshalling from []byte received from chaincode
cars := expectcc.PayloadIs(cc.Invoke(`carList`), &[]Car{}).([]Car)
Expect(len(cars)).To(Equal(1))
Expect(cars[0].Id).To(Equal(Payloads[0].Id))
})
It("Allow authority to add more information about car", func() {
// register second car
expectcc.ResponseOk(cc.From(Authority).Invoke(`carRegister`, Payloads[1]))
cars := expectcc.PayloadIs(
cc.From(Authority).Invoke(`carList`),
&[]Car{}).([]Car)
Expect(len(cars)).To(Equal(2))
})
})
})