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

fix: fix set admin for agent module #77

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion x/agent/keeper/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ func (k Keeper) GetNextNumber(ctx sdk.Context) uint64 {
return sdk.BigEndianToUint64(bz)
}

// SetAdmin sets the admin address in the Keeper's store.
//
// Parameters:
// - ctx: the SDK context.
// - admin: the admin address to be set.
//
// Returns:
// - error: an error if the admin address is already set.
func (k Keeper) SetAdmin(ctx sdk.Context, admin sdk.AccAddress) error {
// check if the sender is the current admin
if !k.GetAdmin(ctx).Equals(admin) {
if k.GetAdmin(ctx).Equals(admin) {
return types.ErrAdminExists
}
k.setAdmin(ctx, admin)
Expand Down
50 changes: 50 additions & 0 deletions x/agent/keeper/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package keeper_test

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func (suite *KeeperTestSuite) TestSetAdmin() {
testCases := []struct {
name string
args sdk.AccAddress
expectErr bool
validation func(sdk.AccAddress)
}{
{
name: "fail - invalid address, invalid bech32",
args: sdk.AccAddress("foobar"),
expectErr: true,
},
{
name: "fail - admin already set",
args: testAdmin,
expectErr: true,
},
{
name: "success - set admin",
args: sdk.AccAddress("lrz1xa40j022h2rcmnte47gyjg8688grln94pp84lc"),
expectErr: false,
validation: func(args sdk.AccAddress) {
admin := suite.keeper.GetAdmin(suite.ctx)
suite.Require().Equal(args, admin)
},
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("KeeperSetAdmin - %s", tc.name), func() {
suite.SetupTest()
err := suite.keeper.SetAdmin(suite.ctx, tc.args)
if tc.expectErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
}
if tc.validation != nil {
tc.validation(tc.args)
}
})
}
}
Loading