github.com/Finschia/finschia-sdk@v0.48.1/x/feegrant/keeper/msg_server_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
     5  	"github.com/Finschia/finschia-sdk/x/feegrant"
     6  )
     7  
     8  func (suite *KeeperTestSuite) TestGrantAllowance() {
     9  	oneYear := suite.sdkCtx.BlockTime().AddDate(1, 0, 0)
    10  
    11  	testCases := []struct {
    12  		name      string
    13  		req       func() *feegrant.MsgGrantAllowance
    14  		expectErr bool
    15  		errMsg    string
    16  	}{
    17  		{
    18  			"invalid granter address",
    19  			func() *feegrant.MsgGrantAllowance {
    20  				any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{})
    21  				suite.Require().NoError(err)
    22  				return &feegrant.MsgGrantAllowance{
    23  					Granter:   "invalid-granter",
    24  					Grantee:   suite.addrs[1].String(),
    25  					Allowance: any,
    26  				}
    27  			},
    28  			true,
    29  			"decoding bech32 failed",
    30  		},
    31  		{
    32  			"invalid grantee address",
    33  			func() *feegrant.MsgGrantAllowance {
    34  				any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{})
    35  				suite.Require().NoError(err)
    36  				return &feegrant.MsgGrantAllowance{
    37  					Granter:   suite.addrs[0].String(),
    38  					Grantee:   "invalid-grantee",
    39  					Allowance: any,
    40  				}
    41  			},
    42  			true,
    43  			"decoding bech32 failed",
    44  		},
    45  		{
    46  			"valid: basic fee allowance",
    47  			func() *feegrant.MsgGrantAllowance {
    48  				any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
    49  					SpendLimit: suite.atom,
    50  					Expiration: &oneYear,
    51  				})
    52  				suite.Require().NoError(err)
    53  				return &feegrant.MsgGrantAllowance{
    54  					Granter:   suite.addrs[0].String(),
    55  					Grantee:   suite.addrs[1].String(),
    56  					Allowance: any,
    57  				}
    58  			},
    59  			false,
    60  			"",
    61  		},
    62  		{
    63  			"fail: fee allowance exists",
    64  			func() *feegrant.MsgGrantAllowance {
    65  				any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
    66  					SpendLimit: suite.atom,
    67  					Expiration: &oneYear,
    68  				})
    69  				suite.Require().NoError(err)
    70  				return &feegrant.MsgGrantAllowance{
    71  					Granter:   suite.addrs[0].String(),
    72  					Grantee:   suite.addrs[1].String(),
    73  					Allowance: any,
    74  				}
    75  			},
    76  			true,
    77  			"fee allowance already exists",
    78  		},
    79  		{
    80  			"valid: periodic fee allowance",
    81  			func() *feegrant.MsgGrantAllowance {
    82  				any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
    83  					Basic: feegrant.BasicAllowance{
    84  						SpendLimit: suite.atom,
    85  						Expiration: &oneYear,
    86  					},
    87  				})
    88  				suite.Require().NoError(err)
    89  				return &feegrant.MsgGrantAllowance{
    90  					Granter:   suite.addrs[1].String(),
    91  					Grantee:   suite.addrs[2].String(),
    92  					Allowance: any,
    93  				}
    94  			},
    95  			false,
    96  			"",
    97  		},
    98  		{
    99  			"error: fee allowance exists",
   100  			func() *feegrant.MsgGrantAllowance {
   101  				any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
   102  					Basic: feegrant.BasicAllowance{
   103  						SpendLimit: suite.atom,
   104  						Expiration: &oneYear,
   105  					},
   106  				})
   107  				suite.Require().NoError(err)
   108  				return &feegrant.MsgGrantAllowance{
   109  					Granter:   suite.addrs[1].String(),
   110  					Grantee:   suite.addrs[2].String(),
   111  					Allowance: any,
   112  				}
   113  			},
   114  			true,
   115  			"fee allowance already exists",
   116  		},
   117  	}
   118  	for _, tc := range testCases {
   119  		suite.Run(tc.name, func() {
   120  			_, err := suite.msgSrvr.GrantAllowance(suite.ctx, tc.req())
   121  			if tc.expectErr {
   122  				suite.Require().Error(err)
   123  				suite.Require().Contains(err.Error(), tc.errMsg)
   124  			}
   125  		})
   126  	}
   127  }
   128  
   129  func (suite *KeeperTestSuite) TestRevokeAllowance() {
   130  	oneYear := suite.sdkCtx.BlockTime().AddDate(1, 0, 0)
   131  
   132  	testCases := []struct {
   133  		name      string
   134  		request   *feegrant.MsgRevokeAllowance
   135  		preRun    func()
   136  		expectErr bool
   137  		errMsg    string
   138  	}{
   139  		{
   140  			"error: invalid granter",
   141  			&feegrant.MsgRevokeAllowance{
   142  				Granter: "invalid-granter",
   143  				Grantee: suite.addrs[1].String(),
   144  			},
   145  			func() {},
   146  			true,
   147  			"decoding bech32 failed",
   148  		},
   149  		{
   150  			"error: invalid grantee",
   151  			&feegrant.MsgRevokeAllowance{
   152  				Granter: suite.addrs[0].String(),
   153  				Grantee: "invalid-grantee",
   154  			},
   155  			func() {},
   156  			true,
   157  			"decoding bech32 failed",
   158  		},
   159  		{
   160  			"error: fee allowance not found",
   161  			&feegrant.MsgRevokeAllowance{
   162  				Granter: suite.addrs[0].String(),
   163  				Grantee: suite.addrs[1].String(),
   164  			},
   165  			func() {},
   166  			true,
   167  			"fee-grant not found",
   168  		},
   169  		{
   170  			"success: revoke fee allowance",
   171  			&feegrant.MsgRevokeAllowance{
   172  				Granter: suite.addrs[0].String(),
   173  				Grantee: suite.addrs[1].String(),
   174  			},
   175  			func() {
   176  				// removing fee allowance from previous tests if exists
   177  				suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{
   178  					Granter: suite.addrs[0].String(),
   179  					Grantee: suite.addrs[1].String(),
   180  				})
   181  				any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
   182  					Basic: feegrant.BasicAllowance{
   183  						SpendLimit: suite.atom,
   184  						Expiration: &oneYear,
   185  					},
   186  				})
   187  				suite.Require().NoError(err)
   188  				req := &feegrant.MsgGrantAllowance{
   189  					Granter:   suite.addrs[0].String(),
   190  					Grantee:   suite.addrs[1].String(),
   191  					Allowance: any,
   192  				}
   193  				_, err = suite.msgSrvr.GrantAllowance(suite.ctx, req)
   194  				suite.Require().NoError(err)
   195  			},
   196  			false,
   197  			"",
   198  		},
   199  		{
   200  			"error: check fee allowance revoked",
   201  			&feegrant.MsgRevokeAllowance{
   202  				Granter: suite.addrs[0].String(),
   203  				Grantee: suite.addrs[1].String(),
   204  			},
   205  			func() {},
   206  			true,
   207  			"fee-grant not found",
   208  		},
   209  	}
   210  
   211  	for _, tc := range testCases {
   212  		suite.Run(tc.name, func() {
   213  			tc.preRun()
   214  			_, err := suite.msgSrvr.RevokeAllowance(suite.ctx, tc.request)
   215  			if tc.expectErr {
   216  				suite.Require().Error(err)
   217  				suite.Require().Contains(err.Error(), tc.errMsg)
   218  			}
   219  		})
   220  	}
   221  }