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

adding editors test done #199

Merged
merged 6 commits into from
Jan 6, 2023
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
117 changes: 117 additions & 0 deletions x/filetree/keeper/msg_server_add_editors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package keeper_test

import (
"fmt"
"strings"

sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/x/filetree/keeper"
"github.com/jackalLabs/canine-chain/x/filetree/types"
)

func (suite *KeeperTestSuite) TestMsgAddEditors() {
suite.SetupSuite()
msgSrvr, _, context := setupMsgServer(suite)

alice, err := sdkTypes.AccAddressFromBech32("cosmos1ytwr7x4av05ek0tf8z9s4zmvr6w569zsm27dpg")
suite.Require().NoError(err)

bob, err := sdkTypes.AccAddressFromBech32("cosmos17j2hkm7n9fz9dpntyj2kxgxy5pthzd289nvlfl")
suite.Require().NoError(err)

// set root folder for alice
aliceRootFolder, err := types.CreateRootFolder(alice.String())
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceRootFolder)

aliceViewerID := strings.Split(alice.String(), ",")
aliceEditorID := aliceViewerID

// set home folder for alice
aliceHomeFolder, err := types.CreateFolderOrFile(alice.String(), aliceEditorID, aliceViewerID, "s/home/")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceHomeFolder)

aliceAccountHash := types.HashThenHex(alice.String())
aliceHomeMerklePath := types.MerklePath("s/home/")

ownerAddress := types.MakeOwnerAddress(aliceHomeMerklePath, aliceAccountHash)
bobEditorAddress := keeper.MakeEditorAddress(aliceHomeFolder.TrackingNumber, bob.String())

cases := []struct {
preRun func() *types.MsgAddEditors
expErr bool
expErrMsg string
name string
}{
{ // alice adds an editor
preRun: func() *types.MsgAddEditors {
return types.NewMsgAddEditors(
alice.String(),
bobEditorAddress,
fmt.Sprintf("%x", "encryptedHomeFolderAESKeyAndIV"),
aliceHomeMerklePath,
ownerAddress,
)
},
expErr: false,
name: "alice adds an editor",
},
{ // alice cannot add an editor to a non existent file
preRun: func() *types.MsgAddEditors {
return types.NewMsgAddEditors(
alice.String(),
bobEditorAddress,
fmt.Sprintf("%x", "encryptedAESKeyAndIV"),
types.MerklePath("s/DNE/"),
ownerAddress,
)
},
expErr: true,
name: "alice fails to add editor",
expErrMsg: "file not found",
},
{ // bob can't add himself as an editor to alice's home folder
preRun: func() *types.MsgAddEditors {
return types.NewMsgAddEditors(
bob.String(),
bobEditorAddress,
fmt.Sprintf("%x", "encryptedHomeFolderAESKeyAndIV"),
aliceHomeMerklePath,
ownerAddress,
)
},
expErr: true,
name: "bob cannot add himself as editor",
expErrMsg: "Unathorized. Only the owner can add an editor.",
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
msg := tc.preRun()
suite.Require().NoError(err)
res, err := msgSrvr.AddEditors(context, msg)
if tc.expErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {
suite.Require().NoError(err)
suite.Require().EqualValues(types.MsgAddEditorsResponse{}, *res)

fileReq := types.QueryFileRequest{
Address: aliceHomeMerklePath,
OwnerAddress: ownerAddress,
}

res, err := suite.queryClient.Files(suite.ctx.Context(), &fileReq)
suite.Require().NoError(err)

validEditor, err := keeper.HasEditAccess(res.Files, bob.String())
suite.Require().NoError(err)
suite.Require().Equal(validEditor, true)

}
})
}
}
154 changes: 154 additions & 0 deletions x/filetree/keeper/msg_server_remove_editors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package keeper_test

import (
"encoding/json"
"strings"

sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/x/filetree/keeper"
"github.com/jackalLabs/canine-chain/x/filetree/types"
)

func (suite *KeeperTestSuite) TestMsgRemoveEditors() {
suite.SetupSuite()
msgSrvr, _, context := setupMsgServer(suite)

alice, err := sdkTypes.AccAddressFromBech32("cosmos1ytwr7x4av05ek0tf8z9s4zmvr6w569zsm27dpg")
suite.Require().NoError(err)

bob, err := sdkTypes.AccAddressFromBech32("cosmos17j2hkm7n9fz9dpntyj2kxgxy5pthzd289nvlfl")
suite.Require().NoError(err)

charlie, err := sdkTypes.AccAddressFromBech32("cosmos1xetrp5dwjplsn4lev5r2cu8en5qsq824vza9nu")
suite.Require().NoError(err)

// set root folder for alice
aliceRootFolder, err := types.CreateRootFolder(alice.String())
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceRootFolder)

// set home folder for alice
aliceHomeFolder, err := types.CreateFolderOrFile(alice.String(), strings.Split(alice.String(), ","), strings.Split(alice.String(), ","), "s/home/")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceHomeFolder)

// add bob as a editor for pepe

EditorIds := strings.Split(alice.String(), ",")
EditorIds = append(EditorIds, bob.String())

// put pepe in home
pepejpg, err := types.CreateFolderOrFile(alice.String(), EditorIds, strings.Split(alice.String(), ","), "s/home/pepe.jpg")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *pepejpg)

pepeMerklePath := types.MerklePath("s/home/pepe.jpg")
aliceAccountHash := types.HashThenHex(alice.String())
aliceOwnerAddress := types.MakeOwnerAddress(pepeMerklePath, aliceAccountHash)

// Let's query the file after it was set to confirm that alice and bob are editors

fileReq := types.QueryFileRequest{
Address: pepeMerklePath,
OwnerAddress: aliceOwnerAddress,
}

res, err := suite.queryClient.Files(suite.ctx.Context(), &fileReq)
suite.Require().NoError(err)

bobIsEditor, err := keeper.HasEditAccess(res.Files, bob.String())
suite.Require().NoError(err)
suite.Require().Equal(bobIsEditor, true)

aliceIsEditor, err := keeper.HasEditAccess(res.Files, alice.String())
suite.Require().NoError(err)
suite.Require().Equal(aliceIsEditor, true)

bobEditorAddress := keeper.MakeEditorAddress(res.Files.TrackingNumber, bob.String())

cases := []struct {
preRun func() *types.MsgRemoveEditors
expErr bool
expErrMsg string
name string
}{
{ // charlie fails to remove bob from alice's editing access
preRun: func() *types.MsgRemoveEditors {
return types.NewMsgRemoveEditors(
charlie.String(),
bobEditorAddress,
pepeMerklePath,
aliceOwnerAddress,
)
},
expErr: true,
expErrMsg: "Not permitted to remove or reset edit/view access. You are not the owner of this file",
name: "charlie fails to remove bob from alice's viewing permissions",
},
{ // alice removes bob from editing access for a non existent file
preRun: func() *types.MsgRemoveEditors {
return types.NewMsgRemoveEditors(
alice.String(),
bobEditorAddress,
types.MerklePath("s/home/ghost.jpg"),
aliceOwnerAddress,
)
},
expErr: true,
expErrMsg: "file not found",
name: "alice can't edit editing access for a file that doesn't exist",
},
{ // alice removes bob from viewing access
preRun: func() *types.MsgRemoveEditors {
return types.NewMsgRemoveEditors(
alice.String(),
bobEditorAddress,
pepeMerklePath,
aliceOwnerAddress,
)
},
expErr: false,
name: "alice removes bob from editing permissions",
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
msg := tc.preRun()
suite.Require().NoError(err)
res, err := msgSrvr.RemoveEditors(context, msg)
if tc.expErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {

suite.Require().NoError(err)
suite.Require().EqualValues(types.MsgRemoveEditorsResponse{}, *res)
// Let's confirm that bob is no longer an editor

fileReq := types.QueryFileRequest{
Address: pepeMerklePath,
OwnerAddress: aliceOwnerAddress,
}
res, err := suite.queryClient.Files(suite.ctx.Context(), &fileReq)
suite.Require().NoError(err)

bobIsEditor, err := keeper.HasEditAccess(res.Files, bob.String())
suite.Require().NoError(err)
suite.Require().EqualValues(bobIsEditor, false)

aliceIsEditor, err := keeper.HasEditAccess(res.Files, alice.String())
suite.Require().NoError(err)
suite.Require().EqualValues(aliceIsEditor, true)

peacc := res.Files.EditAccess
jeacc := make(map[string]string)

error := json.Unmarshal([]byte(peacc), &jeacc)
suite.Require().NoError(error)
suite.Require().EqualValues(len(jeacc), 1)

}
})
}
}
Loading