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

     1  /*
     2   * Copyright (C) 2019 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  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/mysteriumnetwork/node/identity/registry"
    28  	"github.com/mysteriumnetwork/node/requests"
    29  )
    30  
    31  func Test_TokenRewardAmount(t *testing.T) {
    32  	mockResponse := `{"reward":"1000000000000000000","campaign_type":"consumer"}`
    33  	server := newTestAffiliatorServer(http.StatusOK, mockResponse)
    34  
    35  	router := summonTestGin()
    36  
    37  	a := registry.NewAffiliator(requests.NewHTTPClient(server.URL, requests.DefaultTimeout), server.URL)
    38  	err := AddRoutesForAffiliator(a)(router)
    39  	assert.NoError(t, err)
    40  	//
    41  	req, err := http.NewRequest(
    42  		http.MethodGet,
    43  		"/affiliator/token/mytoken/reward",
    44  		nil,
    45  	)
    46  	assert.Nil(t, err)
    47  
    48  	resp := httptest.NewRecorder()
    49  	router.ServeHTTP(resp, req)
    50  
    51  	expectedResponse := `{"amount":1000000000000000000}`
    52  	assert.Equal(t, http.StatusOK, resp.Code)
    53  	assert.Equal(t, expectedResponse, resp.Body.String())
    54  }
    55  
    56  func newTestAffiliatorServer(mockStatus int, mockResponse string) *httptest.Server {
    57  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    58  		w.WriteHeader(mockStatus)
    59  		w.Write([]byte(mockResponse))
    60  	}))
    61  }