code.vegaprotocol.io/vega@v0.79.0/commands/oracle_data_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  func TestCheckOracleDataSubmission(t *testing.T) {
    29  	t.Run("Submitting a nil command fails", testNilOracleDataSubmissionFails)
    30  	t.Run("Submitting an oracle data without payload fails", testOracleDataSubmissionWithoutPayloadFails)
    31  	t.Run("Submitting an oracle data with payload succeeds", testOracleDataSubmissionWithPayloadSucceeds)
    32  	t.Run("Submitting an oracle data without source fails", testOracleDataSubmissionWithoutSourceFails)
    33  	t.Run("Submitting an oracle data with invalid source fails", testOracleDataSubmissionWithInvalidSourceFails)
    34  	t.Run("Submitting an oracle data with source succeeds", testOracleDataSubmissionWithSourceSucceeds)
    35  }
    36  
    37  func testNilOracleDataSubmissionFails(t *testing.T) {
    38  	err := checkOracleDataSubmission(nil)
    39  
    40  	assert.Contains(t, err.Get("oracle_data_submission"), commands.ErrIsRequired)
    41  }
    42  
    43  func testOracleDataSubmissionWithoutPayloadFails(t *testing.T) {
    44  	err := checkOracleDataSubmission(&commandspb.OracleDataSubmission{})
    45  	assert.Contains(t, err.Get("oracle_data_submission.payload"), commands.ErrIsRequired)
    46  }
    47  
    48  func testOracleDataSubmissionWithPayloadSucceeds(t *testing.T) {
    49  	err := checkOracleDataSubmission(&commandspb.OracleDataSubmission{
    50  		Payload: []byte("0xDEADBEEF"),
    51  	})
    52  	assert.NotContains(t, err.Get("oracle_data_submission.payload"), commands.ErrIsRequired)
    53  }
    54  
    55  func testOracleDataSubmissionWithoutSourceFails(t *testing.T) {
    56  	err := checkOracleDataSubmission(&commandspb.OracleDataSubmission{})
    57  	assert.Contains(t, err.Get("oracle_data_submission.source"), commands.ErrIsRequired)
    58  }
    59  
    60  func testOracleDataSubmissionWithInvalidSourceFails(t *testing.T) {
    61  	err := checkOracleDataSubmission(&commandspb.OracleDataSubmission{
    62  		Source: commandspb.OracleDataSubmission_OracleSource(-42),
    63  	})
    64  	assert.Contains(t, err.Get("oracle_data_submission.source"), commands.ErrIsNotValid)
    65  }
    66  
    67  func testOracleDataSubmissionWithSourceSucceeds(t *testing.T) {
    68  	testCases := []struct {
    69  		msg   string
    70  		value commandspb.OracleDataSubmission_OracleSource
    71  	}{
    72  		{
    73  			msg:   "with Open Oracle source",
    74  			value: commandspb.OracleDataSubmission_ORACLE_SOURCE_OPEN_ORACLE,
    75  		}, {
    76  			msg:   "with JSON source",
    77  			value: commandspb.OracleDataSubmission_ORACLE_SOURCE_JSON,
    78  		},
    79  	}
    80  	for _, tc := range testCases {
    81  		t.Run(tc.msg, func(t *testing.T) {
    82  			err := checkOracleDataSubmission(&commandspb.OracleDataSubmission{
    83  				Source: tc.value,
    84  			})
    85  			assert.NotContains(t, err.Get("oracle_data_submission.source"), commands.ErrIsRequired)
    86  			assert.NotContains(t, err.Get("oracle_data_submission.source"), commands.ErrIsNotValid)
    87  		})
    88  	}
    89  }
    90  
    91  func checkOracleDataSubmission(cmd *commandspb.OracleDataSubmission) commands.Errors {
    92  	err := commands.CheckOracleDataSubmission(cmd)
    93  
    94  	var e commands.Errors
    95  	if ok := errors.As(err, &e); !ok {
    96  		return commands.NewErrors()
    97  	}
    98  
    99  	return e
   100  }