-
Notifications
You must be signed in to change notification settings - Fork 208
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
test(mstaking): implement StakeAuthorization tests #340
base: main
Are you sure you want to change the base?
Conversation
Implements comprehensive test suite for StakeAuthorization in the mstaking module. The tests cover: - Creation of new StakeAuthorization objects - Basic validation of authorization parameters - Message acceptance logic with allow/deny lists - Token limit validation and state updates - Message type URL handling for different authorization types This improves the overall test coverage and helps ensure the reliability of the staking authorization functionality.
📝 WalkthroughWalkthroughThe pull request introduces comprehensive unit tests for the Changes
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (3)
x/mstaking/types/authz_test.go (3)
15-75
: Add more test cases for comprehensive coverage.While the current test cases cover basic scenarios, consider adding the following cases to ensure thorough testing:
- Having both allow and deny lists (should error)
- Empty validator lists
- Nil amount
- Invalid authorization type
tests := []struct { name string allowedValidators []string deniedValidators []string authzType types.AuthorizationType amount sdk.Coins expectError bool }{ // ... existing test cases ... + { + name: "invalid - both allow and deny lists", + allowedValidators: []string{"val1"}, + deniedValidators: []string{"val2"}, + authzType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + expectError: true, + }, + { + name: "valid - empty validator lists", + allowedValidators: []string{}, + deniedValidators: nil, + authzType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + expectError: false, + }, + { + name: "valid - nil amount", + allowedValidators: []string{"val1"}, + deniedValidators: nil, + authzType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + amount: nil, + expectError: false, + }, + { + name: "invalid - unspecified authorization type", + allowedValidators: []string{"val1"}, + deniedValidators: nil, + authzType: types.AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED, + amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + expectError: true, + }, }
77-119
: Add test cases for additional validation scenarios.Consider adding the following test cases to improve validation coverage:
- Invalid coin denomination
- Zero amount coins
- Empty validator lists
tests := []struct { name string auth types.StakeAuthorization expectError bool }{ // ... existing test cases ... + { + name: "invalid - empty coin denomination", + auth: types.StakeAuthorization{ + MaxTokens: sdk.Coins{sdk.Coin{Denom: "", Amount: sdk.NewInt(1000)}}, + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + }, + expectError: true, + }, + { + name: "invalid - zero amount coins", + auth: types.StakeAuthorization{ + MaxTokens: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(0))), + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + }, + expectError: true, + }, + { + name: "valid - empty allow list", + auth: types.StakeAuthorization{ + Validators: &types.StakeAuthorization_AllowList{ + AllowList: &types.StakeAuthorization_Validators{ + Address: []string{}, + }, + }, + MaxTokens: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + }, + expectError: false, + }, }
124-202
: Add test cases for additional message types.Consider adding test cases for:
- Redelegate message type
- Cancel unbonding delegation message type
- Invalid message type
tests := []struct { // ... existing struct fields ... }{ // ... existing test cases ... + { + name: "valid redelegate with allow list", + auth: types.StakeAuthorization{ + Validators: &types.StakeAuthorization_AllowList{ + AllowList: &types.StakeAuthorization_Validators{ + Address: []string{"val1"}, + }, + }, + MaxTokens: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_REDELEGATE, + }, + msg: &types.MsgBeginRedelegate{ + DelegatorAddress: "delegator", + ValidatorSrcAddress: "val2", + ValidatorDstAddress: "val1", + Amount: sdk.NewCoin("stake", sdk.NewInt(500)), + }, + expectError: false, + }, + { + name: "valid cancel unbonding delegation", + auth: types.StakeAuthorization{ + Validators: &types.StakeAuthorization_AllowList{ + AllowList: &types.StakeAuthorization_Validators{ + Address: []string{"val1"}, + }, + }, + MaxTokens: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_CANCEL_UNBONDING_DELEGATION, + }, + msg: &types.MsgCancelUnbondingDelegation{ + DelegatorAddress: "delegator", + ValidatorAddress: "val1", + Amount: sdk.NewCoin("stake", sdk.NewInt(500)), + CreationHeight: 1, + }, + expectError: false, + }, + { + name: "invalid - wrong message type", + auth: types.StakeAuthorization{ + Validators: &types.StakeAuthorization_AllowList{ + AllowList: &types.StakeAuthorization_Validators{ + Address: []string{"val1"}, + }, + }, + MaxTokens: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))), + AuthorizationType: types.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, + }, + msg: &types.MsgUndelegate{ + DelegatorAddress: "delegator", + ValidatorAddress: "val1", + Amount: sdk.NewCoin("stake", sdk.NewInt(500)), + }, + expectError: true, + }, }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
x/mstaking/types/authz_test.go
(1 hunks)
🔇 Additional comments (1)
x/mstaking/types/authz_test.go (1)
226-274
: LGTM!The test cases are comprehensive, covering all authorization types and properly verifying the panic case for unspecified type.
} | ||
|
||
func TestStakeAuthorization_Accept(t *testing.T) { | ||
ctx := sdk.Context{}.WithGasMeter(sdk.NewInfiniteGasMeter()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use a proper context with block time and height.
The current test uses an empty context. Consider using a proper context with block time and height for more realistic testing.
-ctx := sdk.Context{}.WithGasMeter(sdk.NewInfiniteGasMeter())
+ctx := sdk.Context{}.
+ WithBlockTime(time.Now()).
+ WithBlockHeight(1).
+ WithGasMeter(sdk.NewInfiniteGasMeter())
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
ctx := sdk.Context{}.WithGasMeter(sdk.NewInfiniteGasMeter()) | |
ctx := sdk.Context{}. | |
WithBlockTime(time.Now()). | |
WithBlockHeight(1). | |
WithGasMeter(sdk.NewInfiniteGasMeter()) |
Implements comprehensive test suite for StakeAuthorization in the mstaking module. The tests cover:
This improves the overall test coverage and helps ensure the reliability of the staking authorization functionality.