cosmos-sdk/x/nft/handler_test.go
Federico Kunze eeb847c845 Merge #4209: NFT Module
* in sync with @okwme/cosmos-nft

* remove tmp tx

* structuring and minor changes

* supply and client files

* adding cli client

* complete cli/tx and rest.go

* cleanup and restructuring

* restructure rest folder

* minor updates on clients

* update querier

* encoding for clients and other changes

* genesis, invariants, and keeper updates

* update types

* make golangcibot happy

* renamed and removed bank keeper

* remove handlers for editmetadata, mint, burn, buy

* nft interface

* minor cleanup

* sort collections and nfts

* balance and find

* nft query and tx

* touch ups

* uint in place of int

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* little fixes:
- fix error to err to avoid collision
- error handling

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* module generalization changes

* fixes

* query with data

* minor updates and TODOs

* fix CLI tx

* golang bot fixes

* handlers and txs done

* update module generalization

* Added very basic tests which for some reason do not work

* fix test

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* fixed test, now we should fix implementation, seems to fail

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* fix test, create new struct instead of changing the old one

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* fix handler with new logic

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* let's make it compile

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* single failing test example, need to be fixed and extended

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* single failing test example, need to be fixed and extended

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* reverting work, still problems unmarshalling inside iterator from test

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* Setter in nft.go should return NFT instead of BaseNFT

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* remove TODOS

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* comment out broken tests, we want at least a green mark here

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* little fixes

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* hopefully no conflict

* minor changes for tests

* change nft id to string, refactors

* messy pause

* Changes Balances to OWners add all necessary functions, updated Keeper with UpdateNFT as as well as MintNFT and made sure they all update Owners

* pause dev to merge sdk master

* go.mod changes

* getting closer still need module.go

* builds!!!

* fix lint begin handler tests

* stableish

* re-order nft attributes, add back mint and burn msgs and handlers

* add errors to minting the same NFT and burning an NFT that doesnt exist

* first querier test

* add simulations for nft msgs

* handler tests check tags now (fixed a bug!)

* update simulation

* generic handler

* need to check if it compiles on another machine

* fix weird interface error

* add back cli

* wtfff

* codec error fixed, logs removed. still returning empty arrays of IDs

* Take empty input as yes answer

Closes: #4564

* Add pending log entry

* merged in master

* marshall errors

* build commands

* working!!!

* linting errors

* remove unused func

* pause

* fix burn error

* fix burn error

* tests for querier

* typo

* tests for NFT types

* module spec standard

* tests for Collection and Collections types

* merge w Fede

* tests for Owner Type

* added genesis tests and beefed up keeper, querier, handler & types tests

* linting errors deadcode

* DONT COVER test_common.go

* add msg type tests

* Update x/nft/internal/keeper/key.go

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update x/nft/genesis.go

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update x/nft/client/cli/query.go

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Apply suggestions from code review

* typo

* cleanup events

* split events

* more cleanup

* remove restrictions from default handlers

* not sure where these go mod changes came from

* sim generated changes

* make format

* add mint and burn sims

* move NFT interface to nft/exported

* make format

* NFT spec

* Updates

* more updates

* update specs readme

* fix sims

* rest additions

* rest additions

* fix invariant

* minimal nft without name, description or image

* sim

* fix sim

* fix sim

* fix Update methods

* nothing

* simplify update and remove

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* remove test on memory location

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* TEST to get logs, need to be removed

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* fix simulator editMetadata Msg type

* owner not found start with empty collection

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* artifacts on errors in case of failure, else, no artifacts

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* add more invariant checks to handler_tests

* never forget to overwrite

* merge and update spec

* colins feedback

* code coverage test

* code coverage test

* code coverage test

* spelling

* clean up client

* testing code coverage

* testing code coverage

* testing code coverage

* testing code coverage

* testing code coverage

* Update docs/spec/nft/README.md

Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com>

* Apply suggestions from code review

Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com>

* minor changes

* integration tests and fixes

* minor golangCI fixes

* Update simapp/app.go

Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>
2019-08-26 12:54:45 -04:00

253 lines
6.9 KiB
Go

package nft_test
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/internal/types"
)
const (
module = "module"
denom = "denom"
nftID = "nft-id"
sender = "sender"
recipient = "recipient"
tokenURI = "token-uri"
)
func TestInvalidMsg(t *testing.T) {
app, ctx := createTestApp(false)
h := nft.GenericHandler(app.NFTKeeper)
res := h(ctx, sdk.NewTestMsg())
require.False(t, res.IsOK())
require.True(t, strings.Contains(res.Log, "unrecognized nft message type"))
}
func TestTransferNFTMsg(t *testing.T) {
app, ctx := createTestApp(false)
h := nft.GenericHandler(app.NFTKeeper)
// An NFT to be transferred
nft := types.NewBaseNFT(id, address, "TokenURI")
// Define MsgTransferNft
transferNftMsg := types.NewMsgTransferNFT(address, address2, denom, id)
// handle should fail trying to transfer NFT that doesn't exist
res := h(ctx, transferNftMsg)
require.False(t, res.IsOK(), "%v", res)
// Create token (collection and owner)
app.NFTKeeper.MintNFT(ctx, denom, &nft)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
// handle should succeed when nft exists and is transferred by owner
res = h(ctx, transferNftMsg)
require.True(t, res.IsOK(), "%v", res)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
// event events should be emitted correctly
for _, event := range res.Events {
for _, attribute := range event.Attributes {
value := string(attribute.Value)
switch key := string(attribute.Key); key {
case module:
require.Equal(t, value, types.ModuleName)
case denom:
require.Equal(t, value, denom)
case nftID:
require.Equal(t, value, id)
case sender:
require.Equal(t, value, address.String())
case recipient:
require.Equal(t, value, address2.String())
default:
require.Fail(t, fmt.Sprintf("unrecognized event %s", key))
}
}
}
// nft should have been transferred as a result of the message
nftAfterwards, err := app.NFTKeeper.GetNFT(ctx, denom, id)
require.NoError(t, err)
require.True(t, nftAfterwards.GetOwner().Equals(address2))
transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom, id)
// handle should succeed when nft exists and is transferred by owner
res = h(ctx, transferNftMsg)
require.True(t, res.IsOK(), "%v", res)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
// Create token (collection and owner)
app.NFTKeeper.MintNFT(ctx, denom2, &nft)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom2, id)
// handle should succeed when nft exists and is transferred by owner
res = h(ctx, transferNftMsg)
require.True(t, res.IsOK(), "%v", res)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
}
func TestEditNFTMetadataMsg(t *testing.T) {
app, ctx := createTestApp(false)
h := nft.GenericHandler(app.NFTKeeper)
// An NFT to be edited
nft := types.NewBaseNFT(id, address, tokenURI)
// Create token (collection and address)
app.NFTKeeper.MintNFT(ctx, denom, &nft)
// Define MsgTransferNft
failingEditNFTMetadata := types.NewMsgEditNFTMetadata(address, id, denom2, tokenURI2)
res := h(ctx, failingEditNFTMetadata)
require.False(t, res.IsOK(), "%v", res)
// Define MsgTransferNft
editNFTMetadata := types.NewMsgEditNFTMetadata(address, id, denom, tokenURI2)
res = h(ctx, editNFTMetadata)
require.True(t, res.IsOK(), "%v", res)
// event events should be emitted correctly
for _, event := range res.Events {
for _, attribute := range event.Attributes {
value := string(attribute.Value)
switch key := string(attribute.Key); key {
case module:
require.Equal(t, value, types.ModuleName)
case denom:
require.Equal(t, value, denom)
case nftID:
require.Equal(t, value, id)
case sender:
require.Equal(t, value, address.String())
case tokenURI:
require.Equal(t, value, tokenURI2)
default:
require.Fail(t, fmt.Sprintf("unrecognized event %s", key))
}
}
}
nftAfterwards, err := app.NFTKeeper.GetNFT(ctx, denom, id)
require.NoError(t, err)
require.Equal(t, tokenURI2, nftAfterwards.GetTokenURI())
}
func TestMintNFTMsg(t *testing.T) {
app, ctx := createTestApp(false)
h := nft.GenericHandler(app.NFTKeeper)
// Define MsgMintNFT
mintNFT := types.NewMsgMintNFT(address, address, id, denom, tokenURI)
// minting a token should succeed
res := h(ctx, mintNFT)
require.True(t, res.IsOK(), "%v", res)
// event events should be emitted correctly
for _, event := range res.Events {
for _, attribute := range event.Attributes {
value := string(attribute.Value)
switch key := string(attribute.Key); key {
case module:
require.Equal(t, value, types.ModuleName)
case denom:
require.Equal(t, value, denom)
case nftID:
require.Equal(t, value, id)
case sender:
require.Equal(t, value, address.String())
case recipient:
require.Equal(t, value, address.String())
case tokenURI:
require.Equal(t, value, tokenURI)
default:
require.Fail(t, fmt.Sprintf("unrecognized event %s", key))
}
}
}
nftAfterwards, err := app.NFTKeeper.GetNFT(ctx, denom, id)
require.NoError(t, err)
require.Equal(t, tokenURI, nftAfterwards.GetTokenURI())
// minting the same token should fail
res = h(ctx, mintNFT)
require.False(t, res.IsOK(), "%v", res)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
}
func TestBurnNFTMsg(t *testing.T) {
app, ctx := createTestApp(false)
h := nft.GenericHandler(app.NFTKeeper)
// An NFT to be burned
nft := types.NewBaseNFT(id, address, tokenURI)
// Create token (collection and address)
app.NFTKeeper.MintNFT(ctx, denom, &nft)
exists := app.NFTKeeper.IsNFT(ctx, denom, id)
require.True(t, exists)
// burning a non-existent NFT should fail
failBurnNFT := types.NewMsgBurnNFT(address, id2, denom)
res := h(ctx, failBurnNFT)
require.False(t, res.IsOK(), "%s", res.Log)
// NFT should still exist
exists = app.NFTKeeper.IsNFT(ctx, denom, id)
require.True(t, exists)
// burning the NFt should succeed
burnNFT := types.NewMsgBurnNFT(address, id, denom)
res = h(ctx, burnNFT)
require.True(t, res.IsOK(), "%v", res)
// event events should be emitted correctly
for _, event := range res.Events {
for _, attribute := range event.Attributes {
value := string(attribute.Value)
switch key := string(attribute.Key); key {
case module:
require.Equal(t, value, types.ModuleName)
case denom:
require.Equal(t, value, denom)
case nftID:
require.Equal(t, value, id)
case sender:
require.Equal(t, value, address.String())
default:
require.Fail(t, fmt.Sprintf("unrecognized event %s", key))
}
}
}
// the NFT should not exist after burn
exists = app.NFTKeeper.IsNFT(ctx, denom, id)
require.False(t, exists)
ownerReturned := app.NFTKeeper.GetOwner(ctx, address)
require.Equal(t, 0, ownerReturned.Supply())
require.True(t, CheckInvariants(app.NFTKeeper, ctx))
}