github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/validation_test.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/mysteriumnetwork/node/config"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func Test_EthEndpoints(t *testing.T) {
    34  	// given:
    35  	configFileName := NewTempFileName(t)
    36  	defer os.Remove(configFileName)
    37  	err := config.Current.LoadUserConfig(configFileName)
    38  	assert.NoError(t, err)
    39  
    40  	g := summonTestGin()
    41  	err = AddRoutesForValidator(g)
    42  	assert.NoError(t, err)
    43  
    44  	// expect:
    45  	for _, data := range []struct {
    46  		payload          string
    47  		expectedResponse int
    48  		configChainId    int64
    49  	}{
    50  		{
    51  			payload:          `["https://invalid"]`,
    52  			configChainId:    80001,
    53  			expectedResponse: 500,
    54  		},
    55  		{
    56  			payload:          `["https://polygon-amoy1.mysterium.network"]`,
    57  			configChainId:    80002,
    58  			expectedResponse: 200,
    59  		},
    60  		{
    61  			payload:          `["https://polygon-amoy1.mysterium.network"]`,
    62  			configChainId:    1,
    63  			expectedResponse: 400,
    64  		},
    65  	} {
    66  		t.Run(fmt.Sprintf("Validate: %s", data.payload), func(t *testing.T) {
    67  			config.Current.SetUser(config.FlagChain2ChainID.Name, data.configChainId)
    68  			req := httptest.NewRequest(
    69  				http.MethodPost,
    70  				"/validation/validate-rpc-chain2-urls",
    71  				strings.NewReader(data.payload))
    72  
    73  			resp := httptest.NewRecorder()
    74  			g.ServeHTTP(resp, req)
    75  
    76  			assert.Equal(t, data.expectedResponse, resp.Code)
    77  		})
    78  	}
    79  }
    80  
    81  func NewTempFileName(t *testing.T) string {
    82  	file, err := os.CreateTemp("", "*")
    83  	assert.NoError(t, err)
    84  	return file.Name()
    85  }