|
4 | 4 | "testing"
|
5 | 5 | "time"
|
6 | 6 |
|
| 7 | + proto "github.com/gogo/protobuf/proto" |
7 | 8 | "github.com/stretchr/testify/assert"
|
8 | 9 | "github.com/stretchr/testify/require"
|
9 | 10 |
|
@@ -188,3 +189,73 @@ func TestFilteredFeeValidAllow(t *testing.T) {
|
188 | 189 | })
|
189 | 190 | }
|
190 | 191 | }
|
| 192 | + |
| 193 | +// invalidInterfaceAllowance does not implement proto.Message |
| 194 | +type invalidInterfaceAllowance struct { |
| 195 | +} |
| 196 | + |
| 197 | +// compilation time interface implementation check |
| 198 | +var _ feegrant.FeeAllowanceI = (*invalidInterfaceAllowance)(nil) |
| 199 | + |
| 200 | +func (i invalidInterfaceAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (remove bool, err error) { |
| 201 | + return false, nil |
| 202 | +} |
| 203 | + |
| 204 | +func (i invalidInterfaceAllowance) ValidateBasic() error { |
| 205 | + return nil |
| 206 | +} |
| 207 | + |
| 208 | +// invalidProtoAllowance can not run proto.Marshal |
| 209 | +type invalidProtoAllowance struct { |
| 210 | + invalidInterfaceAllowance |
| 211 | +} |
| 212 | + |
| 213 | +// compilation time interface implementation check |
| 214 | +var _ feegrant.FeeAllowanceI = (*invalidProtoAllowance)(nil) |
| 215 | +var _ proto.Message = (*invalidProtoAllowance)(nil) |
| 216 | + |
| 217 | +func (i invalidProtoAllowance) Reset() { |
| 218 | +} |
| 219 | + |
| 220 | +func (i invalidProtoAllowance) String() string { |
| 221 | + return "" |
| 222 | +} |
| 223 | + |
| 224 | +func (i invalidProtoAllowance) ProtoMessage() { |
| 225 | +} |
| 226 | + |
| 227 | +func TestSetAllowance(t *testing.T) { |
| 228 | + cases := map[string]struct { |
| 229 | + allowance feegrant.FeeAllowanceI |
| 230 | + valid bool |
| 231 | + }{ |
| 232 | + "valid allowance": { |
| 233 | + allowance: &feegrant.BasicAllowance{}, |
| 234 | + valid: true, |
| 235 | + }, |
| 236 | + "invalid interface allowance": { |
| 237 | + allowance: &invalidInterfaceAllowance{}, |
| 238 | + valid: false, |
| 239 | + }, |
| 240 | + "empty allowance": { |
| 241 | + allowance: (*invalidProtoAllowance)(nil), |
| 242 | + valid: false, |
| 243 | + }, |
| 244 | + } |
| 245 | + |
| 246 | + for name, tc := range cases { |
| 247 | + t.Run(name, func(t *testing.T) { |
| 248 | + allowance := &feegrant.BasicAllowance{} |
| 249 | + msgs := []string{sdk.MsgTypeURL(&banktypes.MsgSend{})} |
| 250 | + allowed, err := feegrant.NewAllowedMsgAllowance(allowance, msgs) |
| 251 | + require.NoError(t, err) |
| 252 | + require.NotNil(t, allowed) |
| 253 | + err = allowed.SetAllowance(tc.allowance) |
| 254 | + if tc.valid { |
| 255 | + require.NoError(t, err) |
| 256 | + } else { |
| 257 | + require.Error(t, err) |
| 258 | + } |
| 259 | + }) |
| 260 | + } |
| 261 | +} |
0 commit comments