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

feat: auth, authz, bank e2e tests for fiattokenfactory #373

Merged
merged 2 commits into from
Oct 17, 2024

Conversation

boojamya
Copy link
Member

@boojamya boojamya commented May 27, 2024

Includes:

  • TestFiatTFAuth
  • TestFiatTFAuthzGrant
  • TestFiatTFAuthzSend
  • TestFiatTFBankSend

Copy link

coderabbitai bot commented May 27, 2024

Walkthrough

The changes in this pull request introduce multiple new test functions and modify existing tests within the e2e/fiat_tf_test.go file. These updates focus on testing the token factory's behavior under specific conditions, such as when the factory is paused or when accounts are blacklisted. The modifications enhance test coverage related to authorization and error handling for various token operations.

Changes

File Change Summary
e2e/fiat_tf_test.go - Added new test functions: TestFiatTFAuth, TestFiatTFAuthzGrant, TestFiatTFAuthzSend, TestFiatTFBankSend.
- Updated existing tests to include checks for new functionalities related to token factory pause and blacklisting.
- Included assertions for error messages during failure scenarios.

Possibly related PRs

  • feat: pause e2e tests for fiattokenfactory #369: The new tests in this PR enhance the coverage of the pauser functionality, which is directly related to the main PR's focus on testing behaviors when the token factory is paused or accounts are blacklisted.

Suggested reviewers

  • kaliweh-circle

Poem

In the garden of code, where rabbits play,
New tests hop in, brightening the day.
With pauses and blacklists, they frolic and run,
Ensuring our tokens are safe and have fun!
So let’s celebrate, with a twitch of our nose,
For the changes we’ve made, as our confidence grows! 🐇✨


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

bal, err := noble.GetBalance(ctx, alice.FormattedAddress(), "ustake")
require.NoError(t, err)
require.Equal(t, originalAmount, bal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the expected behaviour for the fees here? Are fees still charged (and so alice's USDC balance drops), or is the transaction dropped before fees are charged so the balance remains? Is this worth adding an assertion on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. The fee is not taken.
Added a check in: 93925c1

bal, err = noble.GetBalance(ctx, alice.FormattedAddress(), "ustake")
require.NoError(t, err)
require.Equal(t, originalAmount, bal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question about checking the usdc balance

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check in: 93925c1


blacklistAccount(t, ctx, val, nw.fiatTfRoles.Blacklister, granter)

preSendBal := bal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that balance stays zero for this entire test (all requests fail), do we need this preSendBal variable? Can we always check that it is zero instead? Or is there a reason you prefer to do the checks in this way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good call. Simplified:
133af8f (#373)

require.NoError(t, err)
// bal should not change
require.Equal(t, preSendBal, bal)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any value in adding a happy path test here (no accounts blacklisted, chain not paused)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, mainly to validate the test. Added:
b345e59 (#373)

@johnletey johnletey changed the base branch from feat/sdk-eden to main October 17, 2024 09:37
@johnletey johnletey requested a review from a team as a code owner October 17, 2024 09:37
@johnletey johnletey changed the title chore: auth + authz + bank E2E tests for fiat token factory feat: auth, authz, bank e2e tests for fiattokenfactory Oct 17, 2024
Copy link
Member

@johnletey johnletey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Passing on Cosmos SDK v0.50 ⚡

  • ✅ Green CI for TestFiatTFAuth (link)
  • ✅ Green CI for TestFiatTFAuthzGrant (link)
  • ✅ Green CI for TestFiatTFAuthzSend (link)
  • ✅ Green CI for TestFiatTFBankSend (link)

NOTE: An expected error message had to be adjusted slightly due to the most recent code changes to the FiatTokenFactory send restriction (fc8a898). This is due to the error message incorrectly using the toAddr instead of the grantee here. cc @circle-smartin

@johnletey johnletey merged commit 4d9b4f5 into main Oct 17, 2024
32 checks passed
@johnletey johnletey deleted the dan/fiat-tf-send branch October 17, 2024 10:06
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (2)
e2e/fiat_tf_test.go (2)

1795-1810: Verify Balance After Failed Transaction Due to Paused Chain

In TestFiatTFBankSend, when the chain is paused, and a send operation fails (lines 1795-1810), consider adding an assertion to verify that the sender's balance remains unchanged to ensure fees are not deducted.

Add the following checks after the failed send attempt:

aliceBal, err := noble.GetBalance(ctx, alice.FormattedAddress(), "uusdc")
require.NoError(t, err, "error getting balance")
require.Equal(t, math.NewInt(mintAmount), aliceBal, "Alice's balance should remain unchanged when the transaction fails")

This confirms that no funds are deducted from Alice's account when the transaction is rejected due to the paused chain.


1811-1824: Consistent Unblacklisting After Tests

In lines 1823-1824, after unblacklisting Alice, ensure similar cleanup is done for any other accounts blacklisted during the test to prevent side effects on subsequent tests.

Review the test to confirm all blacklisted accounts are unblacklisted at the end.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8cb4c9f and fc8a898.

📒 Files selected for processing (1)
  • e2e/fiat_tf_test.go (2 hunks)
🧰 Additional context used
🔇 Additional comments (2)
e2e/fiat_tf_test.go (2)

1627-1652: ⚠️ Potential issue

Clarify Expected Behavior When Granting Authz to Blacklisted Accounts

In the TestFiatTFAuthzGrant function, when granting authorization to a blacklisted grantee or from a blacklisted granter, the test expects success:

  • Lines 1627-1638: Granting authz to a blacklisted grantee (grantee2) succeeds.
  • Lines 1641-1652: Granting authz from a blacklisted granter (granter3) succeeds.

Typically, blacklisted accounts should be restricted from performing such operations. Please verify if this behavior aligns with the intended security policies.

If blacklisted accounts should not be able to grant or receive authorizations, consider updating the implementation to prevent this and adjusting the test expectations accordingly. Otherwise, if this is acceptable, adding comments to clarify the rationale would be helpful.


1717-1718: Ensure Consistent Error Handling for Blacklisted Granter

When the granter is blacklisted (line 1715), the error message correctly references the granter's address:

require.ErrorContains(t, err, fmt.Sprintf("an address (%s) is blacklisted and can not send tokens: unauthorized", granter.FormattedAddress()))

However, verify that the application's error handling consistently identifies and reports blacklisted entities across different scenarios.

Run the following script to search for error messages related to blacklisted accounts to ensure consistency:

This will help identify any discrepancies in error messages related to blacklisted accounts.

✅ Verification successful

Error Handling for Blacklisted Granter is Consistent

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent error messages for blacklisted accounts.

# Search for error messages involving blacklisted accounts.
rg --type go 'is blacklisted and can not' -A 2

Length of output: 1698

Comment on lines +1693 to +1702
// ACTION: Execute an authz SEND using a TF token from a grantee who is blacklisted
// EXPECTED: Request fails; Even though grantee is acting on behalf of the granter,
// the granter still cannot execute `send` due to being blacklisted
// Status:
// Granter1 has authorized Grantee1 to send 100usdc from their wallet

e2e.BlacklistAccount(t, ctx, val, nw.FiatTfRoles.Blacklister, grantee)

_, err = val.AuthzExec(ctx, grantee, nestedCmd)
require.ErrorContains(t, err, fmt.Sprintf("an address (%s) is blacklisted and can not authorize tokens: unauthorized", receiver.FormattedAddress()))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct Error Message Check for Blacklisted Grantee

In TestFiatTFAuthzSend, when the grantee is blacklisted (line 1699), the error check references the receiver's address instead of the grantee's:

require.ErrorContains(t, err, fmt.Sprintf("an address (%s) is blacklisted and can not authorize tokens: unauthorized", receiver.FormattedAddress()))

Since the grantee (grantee) is blacklisted, the error message should reference the grantee's address.

Apply this diff to correct the error message check:

- require.ErrorContains(t, err, fmt.Sprintf("an address (%s) is blacklisted and can not authorize tokens: unauthorized", receiver.FormattedAddress()))
+ require.ErrorContains(t, err, fmt.Sprintf("an address (%s) is blacklisted and can not authorize tokens: unauthorized", grantee.FormattedAddress()))

This ensures that the test accurately validates the error message corresponding to the blacklisted grantee.

Comment on lines +1639 to +1652

// ACTION: Grant an authz SEND using a TF token from a granter who is blacklisted
// EXPECTED: Success;

w = interchaintest.GetAndFundTestUsers(t, ctx, "default", math.OneInt(), noble, noble)
granter3 := w[0]
grantee3 := w[1]

e2e.BlacklistAccount(t, ctx, val, nw.FiatTfRoles.Blacklister, granter3)

res, err = val.AuthzGrant(ctx, granter3, grantee3.FormattedAddress(), "send", "--spend-limit=100uusdc")
require.NoError(t, err)
require.Zero(t, res.Code)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider Validating Authorization Grants

After granting authorization (lines 1636-1652), it's good practice to verify that the authorization has been correctly recorded.

Add assertions to confirm that the grantee has the expected authorization:

res, err := val.AuthzListGrants(ctx, granter2, grantee2.FormattedAddress())
require.NoError(t, err)
require.Contains(t, res.Grants, /* expected grant details */)

This ensures that the authorization grant is not only successful but also properly registered in the system.

Comment on lines +1747 to +1757
e2e.PauseFiatTF(t, ctx, val, nw.FiatTfRoles.Pauser)

_, err = val.AuthzExec(ctx, grantee, nestedCmd)
require.ErrorContains(t, err, "the chain is paused")

bal, err = noble.GetBalance(ctx, receiver.FormattedAddress(), "uusdc")
require.NoError(t, err)
require.True(t, bal.IsZero())

e2e.UnpauseFiatTF(t, ctx, val, nw.FiatTfRoles.Pauser)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure Proper Cleanup After Paused State

In TestFiatTFAuthzSend, after testing the paused state (lines 1747-1757), ensure that the TF is unpaused before proceeding to other tests to avoid side effects.

Add a call to e2e.UnpauseFiatTF if not already done, or confirm that the TF is unpaused at the end of the test.

// Ensure TF is unpaused after test
e2e.UnpauseFiatTF(t, ctx, val, nw.FiatTfRoles.Pauser)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants