/
cars_test.go
93 lines (74 loc) · 2.89 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
91
92
93
package cars_test
import (
"testing"
. "github.com/optherium/cckit/errors"
"github.com/optherium/cckit/examples/cars"
examplecert "github.com/optherium/cckit/examples/cert"
"github.com/optherium/cckit/extensions/owner"
testcc "github.com/optherium/cckit/testing"
expectcc "github.com/optherium/cckit/testing/expect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestCars(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cars Suite")
}
var _ = Describe(`Cars`, func() {
//Create chaincode mock
cc := testcc.NewMockStub(`cars`, cars.New())
ccWithoutAC := testcc.NewMockStub(`cars`, cars.NewWithoutAccessControl())
// load actor certificates
actors, err := testcc.IdentitiesFromFiles(`SOME_MSP`, map[string]string{
`authority`: `s7techlab.pem`,
`someone`: `victor-nosov.pem`}, examplecert.Content)
if err != nil {
panic(err)
}
BeforeSuite(func() {
// init chaincode
expectcc.ResponseOk(cc.From(actors[`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(actors[`authority`]).Invoke(`carRegister`, cars.Payloads[0]))
})
It("Disallow non authority to add information about car", func() {
//invoke chaincode method from non authority actor
expectcc.ResponseError(
cc.From(actors[`someone`]).Invoke(`carRegister`, cars.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(actors[`someone`]).Invoke(`carRegister`, cars.Payloads[0]))
})
It("Disallow authority to add duplicate information about car", func() {
expectcc.ResponseError(
cc.From(actors[`authority`]).Invoke(`carRegister`, cars.Payloads[0]),
ErrKeyAlreadyExists) //expect car id already exists
})
It("Allow everyone to retrieve car information", func() {
car := expectcc.PayloadIs(cc.Invoke(`carGet`, cars.Payloads[0].Id),
&cars.Car{}).(cars.Car)
Expect(car.Title).To(Equal(cars.Payloads[0].Title))
Expect(car.Id).To(Equal(cars.Payloads[0].Id))
})
It("Allow everyone to get car list", func() {
// &[]Car{} - declares target type for unmarshalling from []byte received from chaincode
cc := expectcc.PayloadIs(cc.Invoke(`carList`), &[]cars.Car{}).([]cars.Car)
Expect(cc).To(HaveLen(1))
Expect(cc[0].Id).To(Equal(cars.Payloads[0].Id))
})
It("Allow authority to add more information about car", func() {
// register second car
expectcc.ResponseOk(cc.From(actors[`authority`]).Invoke(`carRegister`, cars.Payloads[1]))
cc := expectcc.PayloadIs(
cc.From(actors[`authority`]).Invoke(`carList`),
&[]cars.Car{}).([]cars.Car)
Expect(cc).To(HaveLen(2))
})
})
})