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

     1  /*
     2   * Copyright (C) 2020 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  	"encoding/json"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"testing"
    25  
    26  	"github.com/gin-gonic/gin"
    27  
    28  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func Test_ExchangeMyst(t *testing.T) {
    33  	me := &mechangeMock{
    34  		vals: map[string]float64{
    35  			"BTC": 1.0,
    36  		},
    37  	}
    38  
    39  	g := gin.Default()
    40  	err := AddRoutesForCurrencyExchange(me)(g)
    41  	assert.NoError(t, err)
    42  
    43  	// Exchange to BTC green path
    44  	resp := httptest.NewRecorder()
    45  	req, err := http.NewRequest("GET", "/exchange/myst/btc", nil)
    46  	assert.NoError(t, err)
    47  
    48  	g.ServeHTTP(resp, req)
    49  
    50  	assert.Equal(t, http.StatusOK, resp.Result().StatusCode)
    51  	parsedResponse := contract.CurrencyExchangeDTO{}
    52  	err = json.Unmarshal(resp.Body.Bytes(), &parsedResponse)
    53  	assert.Nil(t, err)
    54  
    55  	assert.Equal(t, me.vals["BTC"], parsedResponse.Amount)
    56  	assert.Equal(t, "BTC", parsedResponse.Currency)
    57  
    58  	// No such currency returns 404
    59  	resp = httptest.NewRecorder()
    60  	req, err = http.NewRequest("GET", "/myst/notACurrency", nil)
    61  	g.ServeHTTP(resp, req)
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, http.StatusNotFound, resp.Result().StatusCode)
    64  }
    65  
    66  type mechangeMock struct {
    67  	vals map[string]float64
    68  }
    69  
    70  func (m *mechangeMock) GetMystExchangeRate() (map[string]float64, error) {
    71  	return m.vals, nil
    72  }