Skip to content

Instantly share code, notes, and snippets.

@vitiko
Created May 20, 2018 14:53
Show Gist options
  • Save vitiko/187dc363d68d339bb292b05bc3b93217 to your computer and use it in GitHub Desktop.
Save vitiko/187dc363d68d339bb292b05bc3b93217 to your computer and use it in GitHub Desktop.
Cars chaincode test example
package main
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
examplecert "github.com/s7techlab/cckit/examples/cert"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/identity"
"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")
}
var _ = Describe(`Cars`, func() {
//Create chaincode mock
cc := testcc.NewMockStub(`cars`, New())
// load actor certificates
actors, err := identity.ActorsFromPemFile(`SOME_MSP`, map[string]string{
`authority`: `s7techlab.pem`,
`someone`: `victor-nosov.pem`}, examplecert.Content)
if err != nil {
panic(err)
}
// cars fixtures
car1 := &Car{
Id: `A777MP77`,
Title: `BMW`,
Owner: `victor-nosov`,
}
car2 := &Car{
Id: `O888OO77`,
Title: `TOYOTA`,
Owner: `alexander`,
}
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`, car1))
})
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`, car1),
owner.ErrOwnerOnly) // expect "only owner" error
})
It("Disallow authority to add duplicate information about car", func() {
expectcc.ResponseError(
cc.From(actors[`authority`]).Invoke(`carRegister`, car1),
state.ErrKeyAlreadyExists) //expect car id already exists
})
It("Allow everyone to retrieve car information", func() {
car := expectcc.PayloadIs(cc.Invoke(`carGet`, car1.Id),
&Car{}).(Car)
Expect(car.Title).To(Equal(car1.Title))
Expect(car.Id).To(Equal(car1.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(car1.Id))
})
It("Allow authority to add more information about car", func() {
// register second car
expectcc.ResponseOk(cc.From(actors[`authority`]).Invoke(`carRegister`, car2))
cars := expectcc.PayloadIs(
cc.From(actors[`authority`]).Invoke(`carList`),
&[]Car{}).([]Car)
Expect(len(cars)).To(Equal(2))
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment