Skip to content

Commit

Permalink
test: increase test coverage (#334)
Browse files Browse the repository at this point in the history
* test: add msg server test for delete controller

* chore: update codecov settings

* test: increase test coverage for types did

* test: add tests for events

* chore: small fixes all together

* test: improve coverage for message validation

* test: add tests for did add/delete controllers

* test: improve tests for did types

fix issues along the way

* test: more testing for did/types
  • Loading branch information
noandrea authored Jan 28, 2022
1 parent c93824c commit 7c52f4c
Show file tree
Hide file tree
Showing 16 changed files with 2,135 additions and 829 deletions.
26 changes: 2 additions & 24 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,16 @@ coverage:
# Learn more at https://docs.codecov.io/docs/commit-status
project:
default:
threshold: 1% # allow this much decrease on project
app:
target: 80%
flags:
- app
modules:
target: 80%
flags:
- modules
client:
flags:
- client
threshold: 1%
paths: "x/" # only include coverage in "x/" folder
changes: false

comment:
layout: "reach, diff, files"
behavior: default # update if exists else create new
require_changes: true

flags:
app:
paths:
- "app/"
modules:
paths:
- "x/"
- "!x/**/client/" # ignore client package
client:
paths:
- "client/"
- "x/**/client/" # ignore client package

ignore:
- "docs"
- "*.md"
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
branches:
- main
- dev
workflow_dispatch:
jobs:
tests:
runs-on: ubuntu-latest
Expand All @@ -17,7 +18,7 @@ jobs:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: "1.16"
go-version: "1.17"
- name: Run coverage
run: make test-ci
- name: Upload coverage to Codecov
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PACKAGES=$(shell go list ./...)
PACKAGES="./x/..."
# build paramters
BUILD_FOLDER = build
APP_VERSION = $(git describe --tags --always)
Expand Down
1 change: 1 addition & 0 deletions x/did/keeper/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (k Keeper) GetAllDidDocuments(ctx sdk.Context) []types.DidDocument {
}

// GetDidDocumentsByPubKey retrieve a did document using a pubkey associated to the DID
// TODO: this function is used only in the issuer module ante handler !
func (k Keeper) GetDidDocumentsByPubKey(ctx sdk.Context, pubkey cryptotypes.PubKey) (dids []types.DidDocument) {

dids = k.GetAllDidDocumentsWithCondition(
Expand Down
3 changes: 3 additions & 0 deletions x/did/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func newConstraints(relationships ...string) VerificationRelationships {
func executeOnDidWithRelationships(goCtx context.Context, k *Keeper, constraints VerificationRelationships, did, signer string, update func(document *types.DidDocument) error) (err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
k.Logger(ctx).Info("request to update a did document", "target did", did)
// TODO: fail if the input did is of type KEY (immutable)
// eg: ErrInvalidState, "did document key is immutable"

// get the did document
didDoc, found := k.GetDidDocument(ctx, []byte(did))
if !found {
Expand Down
188 changes: 176 additions & 12 deletions x/did/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,6 @@ func (suite *KeeperTestSuite) TestHandleMsgAddController() {

server := NewMsgServerImpl(suite.keeper)

// FAIL: cannot add controller, did doesn't exist
// FAIL: controller is not a valid did
// FAIL: signer not authorized to change controller
// FAIL: controller is not type key
// PASS: controller added
// PASS: controller already added (duplicated)
testCases := []struct {
name string
malleate func()
Expand Down Expand Up @@ -997,13 +991,11 @@ func (suite *KeeperTestSuite) TestHandleMsgAddController() {

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)


req = *types.NewMsgAddController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2",
"cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2", // does not match the pub key (it's the new controller)
)

)

errExp = sdkerrors.Wrapf(types.ErrUnauthorized, "signer account %s not authorized to update the target did document at %s", "cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2", didDoc.Id)
},
Expand All @@ -1029,19 +1021,17 @@ func (suite *KeeperTestSuite) TestHandleMsgAddController() {

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)


req = *types.NewMsgAddController(
"did:cosmos:cash:subject",
"did:cosmos:net:foochain:whatever",
"cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8", // does not match the pub key (it's the new controller)
)


errExp = sdkerrors.Wrapf(types.ErrInvalidInput, "did document controller 'did:cosmos:net:foochain:whatever' must be of type key")
},
},
{
"PASS: can add controller",
"PASS: can add controller (via authentication relationship)",
func() {
didDoc, err := types.NewDidDocument(
"did:cosmos:cash:subject",
Expand Down Expand Up @@ -1072,6 +1062,40 @@ func (suite *KeeperTestSuite) TestHandleMsgAddController() {
errExp = nil
},
},
{
"PASS: can add controller (via controller)",
func() {
didDoc, err := types.NewDidDocument(
"did:cosmos:cash:subject",
types.WithVerifications(
types.NewVerification(
types.NewVerificationMethod(
"did:cosmos:cash:subject#key-1",
types.DID("did:cosmos:cash:subject"),
types.NewPublicKeyMultibase([]byte{3, 223, 208, 164, 105, 128, 109, 102, 162, 60, 124, 148, 143, 85, 193, 41, 70, 125, 109, 9, 116, 162, 34, 239, 110, 36, 165, 56, 250, 104, 130, 243, 215}, types.DIDVMethodTypeEcdsaSecp256k1VerificationKey2019),
),
[]string{types.AssertionMethod},
nil,
),
),
types.WithControllers("did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2"),
)

if err != nil {
suite.FailNow("test setup failed: ", err)
}

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)
suite.keeper.SetDidMetadata(suite.ctx, []byte(didDoc.Id), types.NewDidMetadata([]byte{1}, time.Now()))

req = *types.NewMsgAddController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos17t8t3t6a6vpgk69perfyq930593sa8dn4kzsdf",
"cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2") // this is the controller

errExp = nil
},
},
{
"PASS: controller already added (duplicated)",
func() {
Expand Down Expand Up @@ -1121,3 +1145,143 @@ func (suite *KeeperTestSuite) TestHandleMsgAddController() {
}
}

func (suite *KeeperTestSuite) TestHandleMsgDeleteController() {
var (
req types.MsgDeleteController
errExp error
)

server := NewMsgServerImpl(suite.keeper)

// FAIL: cannot delete controller, did doesn't exist
// FAIL: signer not authorized to change controller
// PASS: controller removed
testCases := []struct {
name string
malleate func()
}{
{
"FAIL: cannot delete controller, did doesn't exist",
func() {
req = *types.NewMsgDeleteController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2",
"cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8")
errExp = sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", "did:cosmos:cash:subject")
},
},
{
"FAIL: signer not authorized to change controller",
func() {

didDoc, _ := types.NewDidDocument(
"did:cosmos:cash:subject",
types.WithVerifications(
types.NewVerification(
types.NewVerificationMethod(
"did:cosmos:cash:subject#key-1",
types.DID("did:cosmos:cash:subject"),
types.NewPublicKeyMultibase([]byte{3, 223, 208, 164, 105, 128, 109, 102, 162, 60, 124, 148, 143, 85, 193, 41, 70, 125, 109, 9, 116, 162, 34, 239, 110, 36, 165, 56, 250, 104, 130, 243, 215}, types.DIDVMethodTypeEcdsaSecp256k1VerificationKey2019),
),
[]string{types.CapabilityInvocation, types.CapabilityDelegation},
nil,
),
),
)

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)

req = *types.NewMsgDeleteController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2",
"cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2", // does not match the pub key (it's the new controller)
)

errExp = sdkerrors.Wrapf(types.ErrUnauthorized, "signer account %s not authorized to update the target did document at %s", "cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2", didDoc.Id)
},
},
{
"PASS: can delete controller (via authentication relationship)",
func() {
didDoc, err := types.NewDidDocument(
"did:cosmos:cash:subject",
types.WithVerifications(
types.NewVerification(
types.NewVerificationMethod(
"did:cosmos:cash:subject#key-1",
types.DID("did:cosmos:cash:subject"),
types.NewPublicKeyMultibase([]byte{3, 223, 208, 164, 105, 128, 109, 102, 162, 60, 124, 148, 143, 85, 193, 41, 70, 125, 109, 9, 116, 162, 34, 239, 110, 36, 165, 56, 250, 104, 130, 243, 215}, types.DIDVMethodTypeEcdsaSecp256k1VerificationKey2019),
),
[]string{types.Authentication},
nil,
),
),
)

if err != nil {
suite.FailNow("test setup failed: ", err)
}

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)
suite.keeper.SetDidMetadata(suite.ctx, []byte(didDoc.Id), types.NewDidMetadata([]byte{1}, time.Now()))

req = *types.NewMsgDeleteController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2",
"cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8")
errExp = nil
},
}, {
"PASS: can delete controller (via controller)",
func() {
didDoc, err := types.NewDidDocument(
"did:cosmos:cash:subject",
types.WithVerifications(
types.NewVerification(
types.NewVerificationMethod(
"did:cosmos:cash:subject#key-1",
types.DID("did:cosmos:cash:subject"),
types.NewPublicKeyMultibase([]byte{3, 223, 208, 164, 105, 128, 109, 102, 162, 60, 124, 148, 143, 85, 193, 41, 70, 125, 109, 9, 116, 162, 34, 239, 110, 36, 165, 56, 250, 104, 130, 243, 215}, types.DIDVMethodTypeEcdsaSecp256k1VerificationKey2019),
),
[]string{types.AssertionMethod},
nil,
),
),
types.WithControllers(
"did:cosmos:key:cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2",
"did:cosmos:key:cosmos17t8t3t6a6vpgk69perfyq930593sa8dn4kzsdf",
),
)

if err != nil {
suite.FailNow("test setup failed: ", err)
}

suite.keeper.SetDidDocument(suite.ctx, []byte(didDoc.Id), didDoc)
suite.keeper.SetDidMetadata(suite.ctx, []byte(didDoc.Id), types.NewDidMetadata([]byte{1}, time.Now()))

req = *types.NewMsgDeleteController(
"did:cosmos:cash:subject",
"did:cosmos:key:cosmos17t8t3t6a6vpgk69perfyq930593sa8dn4kzsdf",
"cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2", // this is the controller
)

errExp = nil
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.malleate()

_, err := server.DeleteController(sdk.WrapSDKContext(suite.ctx), &req)

if errExp == nil {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
suite.Require().Equal(errExp.Error(), err.Error())
}
})
}
}
16 changes: 12 additions & 4 deletions x/did/spec/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

For testing purposes the following accounts can be used:

| Account | `cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8` |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Hex Pub Key | `03dfd0a469806d66a23c7c948f55c129467d6d0974a222ef6e24a538fa6882f3d7` |
| Bech32 Pub Key | `cosmospub1addwnpepq00apfrfspkkdg3u0j2g74wp99r86mgfwj3z9mmwyjjn37ngsteawc5ajvj` |
| byte Pub Key | `[]byte{3, 223, 208, 164, 105, 128, 109, 102, 162, 60, 124, 148, 143, 85, 193, 41, 70, 125, 109, 9, 116, 162, 34, 239, 110, 36, 165, 56, 250, 104, 130, 243, 215}` |

| Account | `cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8` |
| -------------- | ------------------------------------------------------------------------------- |
| Hex Pub Key | `03dfd0a469806d66a23c7c948f55c129467d6d0974a222ef6e24a538fa6882f3d7` |
| Bech32 Pub Key | `cosmospub1addwnpepq00apfrfspkkdg3u0j2g74wp99r86mgfwj3z9mmwyjjn37ngsteawc5ajvj` |

| Account | `cosmos1lvl2s8x4pta5f96appxrwn3mypsvumukvk7ck2` |
| -------------- | ------------------------------------------------------------------------------- |
| Hex Pub Key | `03786095e15eb228f4e15692eda6e0607a313cc081ad54d69aadd15d515e304590` |
| Bech32 Pub Key | `cosmospub1addwnpepqduxp90pt6ez3a8p26fwmfhqvparz0xqsxk4f4564hg46527xpzeq82cerm` |

| Account | `cosmos17t8t3t6a6vpgk69perfyq930593sa8dn4kzsdf` |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Pub Key | `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Aslf+LuFzmGmRuXiWHwdK0YD9OETgCyEbg8PIyi97Uf1"}` |
| Hex Pub Key | `` |
| Bech32 Pub Key | `` |
| mnemonic | `coil animal waste sound canvas weekend struggle skirt donor boil around bounce grant right silent year subway boost banana unlock powder riot spawn nerve` |
2 changes: 2 additions & 0 deletions x/did/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&MsgRevokeVerification{},
&MsgAddService{},
&MsgDeleteService{},
&MsgAddController{},
&MsgDeleteController{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
Loading

0 comments on commit 7c52f4c

Please sign in to comment.