cosmos-sdk/x/nft/client/cli/query_test.go
Likhita Polavarapu ac6b19df21
refactor: x/nft audit changes (#14055)
## Description

ref: #13991 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-11-30 07:23:30 +00:00

308 lines
8.1 KiB
Go

package cli_test
import (
"context"
"fmt"
"io"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/client/cli"
)
func (s *CLITestSuite) TestQueryClass() {
testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{testClassID, fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[kitty --output=json]`,
},
{
name: "text output",
args: []string{testClassID, fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[kitty --output=text]`,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryClass()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "class [class-id] [] [] query an NFT class based on its id")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}
func (s *CLITestSuite) TestQueryClasses() {
testCases := []struct {
name string
flagArgs []string
expCmdOutput string
}{
{
name: "json output",
flagArgs: []string{fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[--output=json]`,
},
{
name: "text output",
flagArgs: []string{fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[--output=text]`,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryClasses()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.flagArgs)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "classes [] [] query all NFT classes")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.flagArgs)
s.Require().NoError(err)
})
}
}
func (s *CLITestSuite) TestQueryNFT() {
testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{testClassID, testID, fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[kitty kitty1 --output=json]`,
},
{
name: "text output",
args: []string{testClassID, testID, fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[kitty kitty1 --output=text]`,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryNFT()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "nft [class-id] [nft-id] [] [] query an NFT based on its class and id")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}
func (s *CLITestSuite) TestQueryNFTs() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
testCases := []struct {
name string
args struct {
ClassID string
Owner string
}
expectErr bool
expErrMsg string
}{
{
name: "empty class id and owner",
args: struct {
ClassID string
Owner string
}{},
expectErr: true,
expErrMsg: "must provide at least one of classID or owner",
},
{
name: "valid case",
args: struct {
ClassID string
Owner string
}{
ClassID: testClassID,
Owner: accounts[0].Address.String(),
},
expectErr: false,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryNFTs()
var args []string
args = append(args, fmt.Sprintf("--%s=%s", cli.FlagClassID, tc.args.ClassID))
args = append(args, fmt.Sprintf("--%s=%s", cli.FlagOwner, tc.args.Owner))
args = append(args, fmt.Sprintf("--%s=json", flags.FlagOutput))
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, args)
if tc.expectErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
var result nft.QueryNFTsResponse
err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &result)
s.Require().NoError(err)
}
})
}
}
func (s *CLITestSuite) TestQueryOwner() {
testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{testClassID, testID, fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[kitty kitty1 --output=json]`,
},
{
name: "text output",
args: []string{testClassID, testID, fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[kitty kitty1 --output=text]`,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryOwner()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "owner [class-id] [nft-id] [] [] query the owner of the NFT based on its class and id")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}
func (s *CLITestSuite) TestQueryBalance() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{accounts[0].Address.String(), testClassID, fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: fmt.Sprintf("%s kitty --output=json", accounts[0].Address.String()),
},
{
name: "text output",
args: []string{accounts[0].Address.String(), testClassID, fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: fmt.Sprintf("%s kitty --output=text", accounts[0].Address.String()),
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryBalance()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "balance [owner] [class-id] [] [] query the number of NFTs of a given class owned by the owner")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}
func (s *CLITestSuite) TestQuerySupply() {
testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "valid case",
args: []string{testClassID, fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[kitty --output=json]`,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetCmdQuerySupply()
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
s.Require().NotNil(cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd))
s.Require().Contains(fmt.Sprint(cmd), "supply [class-id] [] [] query the number of nft based on the class")
s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}