cosmos-sdk/x/nft/keeper/msg_server_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

111 lines
2.2 KiB
Go

package keeper_test
import (
"fmt"
"github.com/cosmos/cosmos-sdk/x/nft"
)
var (
ExpClass = nft.Class{
Id: testClassID,
Name: testClassName,
Symbol: testClassSymbol,
Description: testClassDescription,
Uri: testClassURI,
UriHash: testClassURIHash,
}
ExpNFT = nft.NFT{
ClassId: testClassID,
Id: testID,
Uri: testURI,
}
)
func (s *TestSuite) TestSend() {
err := s.nftKeeper.SaveClass(s.ctx, ExpClass)
s.Require().NoError(err)
actual, has := s.nftKeeper.GetClass(s.ctx, testClassID)
s.Require().True(has)
s.Require().EqualValues(ExpClass, actual)
err = s.nftKeeper.Mint(s.ctx, ExpNFT, s.addrs[0])
s.Require().NoError(err)
expGenesis := &nft.GenesisState{
Classes: []*nft.Class{&ExpClass},
Entries: []*nft.Entry{{
Owner: s.addrs[0].String(),
Nfts: []*nft.NFT{&ExpNFT},
}},
}
genesis := s.nftKeeper.ExportGenesis(s.ctx)
s.Require().Equal(expGenesis, genesis)
testCases := []struct {
name string
req *nft.MsgSend
expErr bool
errMsg string
}{
{
name: "invalid class id",
req: &nft.MsgSend{
ClassId: "invalid ClassId",
Id: testID,
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
},
expErr: true,
errMsg: "unauthorized",
},
{
name: "invalid nft id",
req: &nft.MsgSend{
ClassId: testClassID,
Id: "invalid Id",
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
},
expErr: true,
errMsg: "unauthorized",
},
{
name: "unauthorized sender",
req: &nft.MsgSend{
ClassId: testClassID,
Id: testID,
Sender: s.addrs[1].String(),
Receiver: s.addrs[2].String(),
},
expErr: true,
errMsg: fmt.Sprintf("%s is not the owner of nft %s", s.addrs[1].String(), testID),
},
{
name: "valid transaction",
req: &nft.MsgSend{
ClassId: testClassID,
Id: testID,
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
},
expErr: false,
errMsg: "",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
_, err := s.nftKeeper.Send(s.ctx, tc.req)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.errMsg)
} else {
s.Require().NoError(err)
}
})
}
}