code.vegaprotocol.io/vega@v0.79.0/commands/delegate_submission_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package commands_test 17 18 import ( 19 "errors" 20 "testing" 21 22 "code.vegaprotocol.io/vega/commands" 23 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 // DELEGATION 29 30 func TestSubmittingNoDelegateCommandFails(t *testing.T) { 31 err := checkDelegateSubmission(nil) 32 33 assert.Contains(t, err.Get("delegate_submission"), commands.ErrIsRequired) 34 } 35 36 func TestSubmittingNoDelegateNodeIdFails(t *testing.T) { 37 cmd := &commandspb.DelegateSubmission{ 38 Amount: "1000", 39 } 40 err := checkDelegateSubmission(cmd) 41 42 assert.Contains(t, err.Get("delegate_submission.node_id"), commands.ErrIsRequired) 43 } 44 45 func TestSubmittingNoDelegateAmountFails(t *testing.T) { 46 cmd := &commandspb.DelegateSubmission{ 47 NodeId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741", 48 } 49 err := checkDelegateSubmission(cmd) 50 51 assert.Contains(t, err.Get("delegate_submission.amount"), commands.ErrIsRequired) 52 } 53 54 func checkDelegateSubmission(cmd *commandspb.DelegateSubmission) commands.Errors { 55 err := commands.CheckDelegateSubmission(cmd) 56 57 var e commands.Errors 58 if ok := errors.As(err, &e); !ok { 59 return commands.NewErrors() 60 } 61 return e 62 } 63 64 // UNDELEGATION 65 66 func TestSubmittingNoUndelegateCommandFails(t *testing.T) { 67 err := checkUndelegateSubmission(nil) 68 69 assert.Contains(t, err.Get("undelegate_submission"), commands.ErrIsRequired) 70 } 71 72 func TestSubmittingNoUndelegateNodeIdFails(t *testing.T) { 73 cmd := &commandspb.UndelegateSubmission{ 74 Amount: "1000", 75 } 76 err := checkUndelegateSubmission(cmd) 77 78 assert.Contains(t, err.Get("undelegate_submission.node_id"), commands.ErrIsRequired) 79 } 80 81 func TestSubmittingInvalidUndelegateMethod(t *testing.T) { 82 invalidMethod := len(commandspb.UndelegateSubmission_Method_value) 83 cmd := &commandspb.UndelegateSubmission{ 84 NodeId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741", 85 Method: commandspb.UndelegateSubmission_Method(invalidMethod), 86 } 87 err := checkUndelegateSubmission(cmd) 88 89 assert.Contains(t, err.Get("undelegate_submission.method"), commands.ErrIsRequired) 90 91 cmd = &commandspb.UndelegateSubmission{ 92 NodeId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741", 93 } 94 err = checkUndelegateSubmission(cmd) 95 96 assert.Contains(t, err.Get("undelegate_submission.method"), commands.ErrIsRequired) 97 } 98 99 func TestSubmittingNoUndelegateAmountSucceeds(t *testing.T) { 100 cmd := &commandspb.UndelegateSubmission{ 101 NodeId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741", 102 Method: 1, 103 } 104 err := checkUndelegateSubmission(cmd) 105 106 assert.Equal(t, 0, len(err)) 107 } 108 109 func checkUndelegateSubmission(cmd *commandspb.UndelegateSubmission) commands.Errors { 110 err := commands.CheckUndelegateSubmission(cmd) 111 112 var e commands.Errors 113 if ok := errors.As(err, &e); !ok { 114 return commands.NewErrors() 115 } 116 return e 117 }