Skip to content

Instantly share code, notes, and snippets.

@jesusjavierdediego
Created February 19, 2021 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusjavierdediego/21fa9557ac276a1e5333ac29c3a895db to your computer and use it in GitHub Desktop.
Save jesusjavierdediego/21fa9557ac276a1e5333ac29c3a895db to your computer and use it in GitHub Desktop.
package grpc
import (
gomock "github.com/golang/mock/gomock"
"golang.org/x/net/context"
"time"
"testing"
. "github.com/smartystreets/goconvey/convey"
utils "me/project/utils"
mock "me/project/grpc/grpcmock"
pb "me/project/protobuf"
)
var req = utils.GetIDR4Test()
var res = utils.GetIDRes4Test()
var errorRes pb.Empty
func TestGRPCDigitalIdentityService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockClient := mock.NewMockDigitalIdentityServiceClient(ctrl)
mockClient.EXPECT().RegisterNewDID(
gomock.Any(),
req,
).Return(&errorRes, nil)
mockClient.EXPECT().GetDID(
gomock.Any(),
req,
).Return(res, nil)
mockClient.EXPECT().PartialRevokeDID(
gomock.Any(),
req,
).Return(&errorRes, nil)
mockClient.EXPECT().CompleteRevokeDID(
gomock.Any(),
req,
).Return(&errorRes, nil)
testRegisterNewDID(t, mockClient)
testGetDID(t, mockClient)
testPartialRevokeDID(t, mockClient)
testCompleteRevokeDID(t, mockClient)
}
func testRegisterNewDID(t *testing.T, client pb.DigitalIdentityServiceClient) {
Convey("Should register a new DID", t, func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := client.RegisterNewDID(ctx, req)
So(err, ShouldBeNil)
})
}
func testGetDID(t *testing.T, client pb.DigitalIdentityServiceClient) {
Convey("Should retrieve a DID", t, func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r0, r1 := client.GetDID(ctx, req)
if r0.UserId.Piiid != "973e0e91-1120-4acd-8135-aad21be70d26"{
t.Errorf("mocking failed")
}
So(r1, ShouldBeNil)
So(r0.UserId.Piiid, ShouldEqual, req.UserId.Piiid)
})
}
func testCompleteRevokeDID(t *testing.T, client pb.DigitalIdentityServiceClient) {
Convey("Should revoke a DID", t, func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := client.CompleteRevokeDID(ctx, req)
if err != nil {
t.Errorf("mocking failed")
}
So(err, ShouldBeNil)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment