github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/policy/requested/provider_test.go (about)

     1  /*
     2   * Copyright (C) 2023 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 requested_test
    19  
    20  import (
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/core/policy/requested"
    27  	"github.com/mysteriumnetwork/node/identity"
    28  	"github.com/mysteriumnetwork/node/requests"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func Test_RequestedProvider_GetIdentityAllowed(t *testing.T) {
    33  	policyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    34  		switch r.URL.Query().Get("identity-value") {
    35  		case "0x1":
    36  			w.WriteHeader(http.StatusOK)
    37  			w.Write([]byte(`{
    38  				"id": "1",
    39  				"title": "One",
    40  				"description": "",
    41  				"allow": [
    42  					{"type": "identity", "value": "0x1"}
    43  				]
    44  			}`))
    45  		case "0x2":
    46  			w.WriteHeader(http.StatusOK)
    47  			w.Write([]byte(`{
    48  				"id": "2",
    49  				"title": "Two",
    50  				"description": "",
    51  				"allow": [
    52  					{"type": "identity", "value": "0x2"}
    53  				]
    54  			}`))
    55  		default:
    56  			w.WriteHeader(http.StatusNotFound)
    57  		}
    58  	}))
    59  	defer policyServer.Close()
    60  
    61  	oracle := requested.NewRequestedProvider(requests.NewHTTPClient("0.0.0.0", 100*time.Millisecond), policyServer.URL+"/")
    62  
    63  	assert.True(t, oracle.IsIdentityAllowed(identity.Identity{Address: "0x1"}))
    64  	assert.True(t, oracle.IsIdentityAllowed(identity.Identity{Address: "0x2"}))
    65  	assert.False(t, oracle.IsIdentityAllowed(identity.Identity{Address: "0x3"}))
    66  }