github.com/s7techlab/cckit@v0.10.5/examples/private_cars/cars_test.go (about) 1 package cars 2 3 import ( 4 "testing" 5 6 "github.com/s7techlab/cckit/extensions/owner" 7 "github.com/s7techlab/cckit/identity/testdata" 8 "github.com/s7techlab/cckit/state" 9 testcc "github.com/s7techlab/cckit/testing" 10 expectcc "github.com/s7techlab/cckit/testing/expect" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 func TestCars(t *testing.T) { 17 RegisterFailHandler(Fail) 18 RunSpecs(t, "Cars Suite") 19 } 20 21 // load actor certificates 22 var ( 23 Authority = testdata.Certificates[0].MustIdentity(`SOME_MSP`) 24 Someone = testdata.Certificates[1].MustIdentity(`SOME_MSP`) 25 ) 26 27 var _ = Describe(`Cars`, func() { 28 29 //Create chaincode mock 30 cc := testcc.NewMockStub(`cars`, New()) 31 ccWithoutAC := testcc.NewMockStub(`cars`, NewWithoutAccessControl()) 32 33 BeforeSuite(func() { 34 // init chaincode 35 expectcc.ResponseOk(cc.From(Authority).Init()) // init chaincode from authority 36 }) 37 38 Describe("Car", func() { 39 40 It("Allow authority to add information about car", func() { 41 //invoke chaincode method from authority actor 42 expectcc.ResponseOk(cc.From(Authority).Invoke(`carRegister`, Payloads[0])) 43 }) 44 45 It("Disallow non authority to add information about car", func() { 46 //invoke chaincode method from non authority actor 47 expectcc.ResponseError( 48 cc.From(Someone).Invoke(`carRegister`, Payloads[0]), 49 owner.ErrOwnerOnly) // expect "only owner" error 50 }) 51 52 It("Allow non authority to add information about car to chaincode without access control", func() { 53 //invoke chaincode method from non authority actor 54 expectcc.ResponseOk( 55 ccWithoutAC.From(Someone).Invoke(`carRegister`, Payloads[0])) 56 }) 57 58 It("Disallow authority to add duplicate information about car", func() { 59 expectcc.ResponseError( 60 cc.From(Authority).Invoke(`carRegister`, Payloads[0]), 61 state.ErrKeyAlreadyExists) //expect car id already exists 62 }) 63 64 It("Allow everyone to retrieve car information", func() { 65 car := expectcc.PayloadIs(cc.Invoke(`carGet`, Payloads[0].Id), 66 &Car{}).(Car) 67 68 Expect(car.Title).To(Equal(Payloads[0].Title)) 69 Expect(car.Id).To(Equal(Payloads[0].Id)) 70 }) 71 72 It("Allow everyone to get car list", func() { 73 // &[]Car{} - declares target type for unmarshalling from []byte received from chaincode 74 cars := expectcc.PayloadIs(cc.Invoke(`carList`), &[]Car{}).([]Car) 75 76 Expect(len(cars)).To(Equal(1)) 77 Expect(cars[0].Id).To(Equal(Payloads[0].Id)) 78 }) 79 80 It("Allow authority to add more information about car", func() { 81 // register second car 82 expectcc.ResponseOk(cc.From(Authority).Invoke(`carRegister`, Payloads[1])) 83 cars := expectcc.PayloadIs( 84 cc.From(Authority).Invoke(`carList`), 85 &[]Car{}).([]Car) 86 87 Expect(len(cars)).To(Equal(2)) 88 }) 89 }) 90 })