Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success TX Unittest for Register Credential Status RPC #190

Merged
merged 1 commit into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.8 // indirect
github.com/multiformats/go-multibase v0.0.3
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,9 @@ golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down
43 changes: 43 additions & 0 deletions x/ssi/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,49 @@ var SchemaValidSignInfo []*types.SignInfo = getSchemaSigningInfo(ValidSchemaDocu

/**********************************************/

/**************** Register Cred Status Test *****************/

var ValidCredentialStatus *types.CredentialStatus = &types.CredentialStatus{
Claim: &types.Claim{
Id: "vc_1",
CurrentStatus: "Live",
StatusReason: "Valid",
},
Issuer: didId,
IssuanceDate: "2022-04-10T04:07:12Z",
ExpirationDate: "2023-02-22T13:45:55Z",
CredentialHash: "Hash234",
}

var credentialStatusSignature string = base64.StdEncoding.EncodeToString(
ed25519.Sign(keyPair.privateKeyBase64, ValidCredentialStatus.GetSignBytes()),
)

var ValidCredentialProof *types.CredentialProof = &types.CredentialProof{
Type: "Ed25519VerificationKey2020",
Created: "2022-04-10T04:07:12Z",
Updated: "2022-04-10T04:07:12Z",
VerificationMethod: verificationMethodId,
ProofValue: credentialStatusSignature,
ProofPurpose: "assertion",
}

/************************************************************/

func updateCredStatus(newStatus string, credStatus types.CredentialStatus, credProof types.CredentialProof) (types.CredentialStatus, types.CredentialProof) {
credStatus.Claim.CurrentStatus = newStatus
credStatus.Claim.StatusReason = "Status changed for Testing"
credProof.Updated = "2022-05-12T00:00:00Z"

updatedSignature := base64.StdEncoding.EncodeToString(
ed25519.Sign(keyPair.privateKeyBase64, credStatus.GetSignBytes()),
)

credProof.ProofValue = updatedSignature

return credStatus, credProof
}

func getDidSigningInfo(didDoc *types.Did, keyPair ed25519KeyPair, vm *types.VerificationMethod) []*types.SignInfo {
signature := ed25519.Sign(keyPair.privateKeyBase64, didDoc.GetSignBytes())
signInfo := &types.SignInfo{
Expand Down
7 changes: 3 additions & 4 deletions x/ssi/tests/create_did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package tests
import (
"testing"

testkeeper "github.com/hypersign-protocol/hid-node/testutil/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestCreateDID(t *testing.T) {
t.Log("Running test for Valid Create DID Tx")
k, ctx := testkeeper.SsiKeeper(t)
msgServ := keeper.NewMsgServerImpl(*k)
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

t.Logf("Registering DID with DID Id: %s", ValidDidDocument.GetId())
Expand All @@ -22,7 +21,7 @@ func TestCreateDID(t *testing.T) {
Creator: Creator,
}

_, err := msgServ.CreateDID(goCtx, msgCreateDID)
_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
Expand Down
9 changes: 4 additions & 5 deletions x/ssi/tests/create_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package tests
import (
"testing"

testkeeper "github.com/hypersign-protocol/hid-node/testutil/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestCreateSchema(t *testing.T) {
t.Log("Running test for Valid Create Schmea Tx")
k, ctx := testkeeper.SsiKeeper(t)
msgServ := keeper.NewMsgServerImpl(*k)
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

t.Logf("Registering DID with Id: %s", ValidDidDocument.GetId())
Expand All @@ -22,7 +21,7 @@ func TestCreateSchema(t *testing.T) {
Creator: Creator,
}

_, err := msgServ.CreateDID(goCtx, msgCreateDID)
_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
Expand All @@ -37,7 +36,7 @@ func TestCreateSchema(t *testing.T) {
Creator: Creator,
}

_, errCreateSchema := msgServ.CreateSchema(goCtx, msgCreateSchema)
_, errCreateSchema := msgServer.CreateSchema(goCtx, msgCreateSchema)
if errCreateSchema != nil {
t.Error("Schema Registeration Failed")
t.Error(errCreateSchema)
Expand Down
9 changes: 4 additions & 5 deletions x/ssi/tests/deactivate_did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/hypersign-protocol/hid-node/testutil/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)

func TestDeactivateDID(t *testing.T) {
t.Log("Running test for Valid Deactivate DID Tx")
k, ctx := testkeeper.SsiKeeper(t)
msgServ := keeper.NewMsgServerImpl(*k)
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

t.Logf("Registering DID with DID Id: %s", ValidDidDocument.GetId())
Expand All @@ -22,7 +21,7 @@ func TestDeactivateDID(t *testing.T) {
Creator: Creator,
}

_, err := msgServ.CreateDID(goCtx, msgCreateDID)
_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
Expand All @@ -48,7 +47,7 @@ func TestDeactivateDID(t *testing.T) {
Creator: Creator,
}

_, errDeactivateDID := msgServ.DeactivateDID(goCtx, msgDeactivateDID)
_, errDeactivateDID := msgServer.DeactivateDID(goCtx, msgDeactivateDID)
if errDeactivateDID != nil {
t.Error("DID Deactivation Failed")
t.Error(errDeactivateDID)
Expand Down
108 changes: 108 additions & 0 deletions x/ssi/tests/register_credential_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package tests

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)

func TestRegisterCredentialStatus(t *testing.T) {
t.Log("Running test for Valid Register Cred Tx")
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

t.Logf("Registering DID with DID Id: %s", ValidDidDocument.GetId())
msgCreateDID := &types.MsgCreateDID{
DidDocString: ValidDidDocument,
Signatures: DidDocumentValidSignInfo,
Creator: Creator,
}

_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
t.FailNow()
}
t.Log("Did Registeration Successful")

t.Logf("Registering Credential Status with Id: %s", ValidCredentialStatus.GetClaim().GetId())
msgRegisterCredentialStatus := &types.MsgRegisterCredentialStatus{
CredentialStatus: ValidCredentialStatus,
Proof: ValidCredentialProof,
Creator: Creator,
}

t.Logf("Block Time: %s", ctx.BlockTime().Format(time.RFC3339))

_, errCredStatus := msgServer.RegisterCredentialStatus(goCtx, msgRegisterCredentialStatus)
if errCredStatus != nil {
t.Error("Credential Status Registeration Failed")
t.Error(errCredStatus)
t.FailNow()
}
t.Log("Credential Status Registeration Successful")

t.Log("Valid Register Cred Tx Test Completed")
}

func TestUpdateCredentialStatus(t *testing.T) {
t.Log("Running test for updating status of registered credential status")
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

t.Logf("Registering DID with DID Id: %s", ValidDidDocument.GetId())
msgCreateDID := &types.MsgCreateDID{
DidDocString: ValidDidDocument,
Signatures: DidDocumentValidSignInfo,
Creator: Creator,
}

_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
t.FailNow()
}
t.Log("Did Registeration Successful")

t.Logf("Registering Credential Status with Id: %s", ValidCredentialStatus.GetClaim().GetId())
msgRegisterCredentialStatus := &types.MsgRegisterCredentialStatus{
CredentialStatus: ValidCredentialStatus,
Proof: ValidCredentialProof,
Creator: Creator,
}

t.Logf("Block Time: %s", ctx.BlockTime().Format(time.RFC3339))

_, errCredStatus := msgServer.RegisterCredentialStatus(goCtx, msgRegisterCredentialStatus)
if errCredStatus != nil {
t.Error("Credential Status Registeration Failed")
t.Error(errCredStatus)
t.FailNow()
}
t.Log("Credential Status Registeration Successful")

t.Logf("Updating Credential Status (Id: %s) from Live to Revoked", ValidCredentialStatus.GetClaim().GetId())

updatedCredentialStatus, updatedCredentialProof := updateCredStatus("Revoked", *ValidCredentialStatus, *ValidCredentialProof)

msgUpdateCredentialStatus := &types.MsgRegisterCredentialStatus{
CredentialStatus: &updatedCredentialStatus,
Proof: &updatedCredentialProof,
Creator: Creator,
}

_, errUpdatedCredStatus := msgServer.RegisterCredentialStatus(goCtx, msgUpdateCredentialStatus)
if errUpdatedCredStatus != nil {
t.Error("Credential Status Update Failed")
t.Error(errUpdatedCredStatus)
t.FailNow()
}
t.Log("Credential Status Update Successful")
}
58 changes: 58 additions & 0 deletions x/ssi/tests/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tests

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

func TestKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"SsiParams",
)
k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
paramsSubspace,
)

blockTime, _ := time.Parse(time.RFC3339, "2022-05-10T00:00:00Z")
ctx := sdk.NewContext(stateStore,
tmproto.Header{
ChainID: "hidnode",
Time: blockTime,
}, false, log.NewNopLogger())

// Initialize params
k.SetParams(ctx, types.DefaultParams())

return k, ctx
}