github.com/s7techlab/cckit@v0.10.5/examples/cars/cars_test.go (about) 1 package cars_test 2 3 import ( 4 "testing" 5 6 "github.com/s7techlab/cckit/examples/cars" 7 "github.com/s7techlab/cckit/extensions/owner" 8 "github.com/s7techlab/cckit/identity/testdata" 9 "github.com/s7techlab/cckit/state" 10 testcc "github.com/s7techlab/cckit/testing" 11 expectcc "github.com/s7techlab/cckit/testing/expect" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 func TestCars(t *testing.T) { 18 RegisterFailHandler(Fail) 19 RunSpecs(t, "Cars Suite") 20 } 21 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`, cars.New()) 31 ccWithoutAC := testcc.NewMockStub(`cars`, 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`, cars.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`, cars.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(Authority).Invoke(`carRegister`, cars.Payloads[0])) 56 }) 57 58 It("Disallow authority to add duplicate information about car", func() { 59 expectcc.ResponseError( 60 cc.From(Authority).Invoke(`carRegister`, cars.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`, cars.Payloads[0].Id), 66 &cars.Car{}).(cars.Car) 67 68 Expect(car.Title).To(Equal(cars.Payloads[0].Title)) 69 Expect(car.Id).To(Equal(cars.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 cc := expectcc.PayloadIs(cc.Invoke(`carList`), &[]cars.Car{}).([]cars.Car) 75 76 Expect(cc).To(HaveLen(1)) 77 Expect(cc[0].Id).To(Equal(cars.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`, cars.Payloads[1])) 83 cc := expectcc.PayloadIs( 84 cc.From(Authority).Invoke(`carList`), 85 &[]cars.Car{}).([]cars.Car) 86 87 Expect(cc).To(HaveLen(2)) 88 }) 89 }) 90 })