github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/pilvytis_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 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "testing" 25 26 "github.com/gin-gonic/gin" 27 "github.com/mysteriumnetwork/payments/exchange" 28 29 "github.com/mysteriumnetwork/node/core/location/locationstate" 30 "github.com/mysteriumnetwork/node/identity" 31 "github.com/mysteriumnetwork/node/pilvytis" 32 "github.com/pkg/errors" 33 "github.com/stretchr/testify/assert" 34 ) 35 36 type mockPilvytis struct { 37 currencies []string 38 identity string 39 respgw pilvytis.GatewayOrderResponse 40 } 41 42 type mockPilvytisIssuer struct { 43 identity string 44 respgw pilvytis.GatewayOrderResponse 45 } 46 47 func (mock *mockPilvytisIssuer) CreatePaymentGatewayOrder(cgo pilvytis.GatewayOrderRequest) (*pilvytis.GatewayOrderResponse, error) { 48 if cgo.Identity.Address != mock.identity { 49 return nil, errors.New("wrong identity") 50 } 51 52 return &mock.respgw, nil 53 } 54 55 func (mock *mockPilvytis) GetPaymentOrderCurrencies() ([]string, error) { 56 return mock.currencies, nil 57 } 58 59 func (mock *mockPilvytis) GetPaymentOrderOptions() (*pilvytis.PaymentOrderOptions, error) { 60 return &pilvytis.PaymentOrderOptions{ 61 Minimum: 16.7, 62 Suggested: []float64{ 63 20, 64 40, 65 100, 66 }, 67 }, nil 68 } 69 70 func (mock *mockPilvytis) GetPaymentGatewayOrder(id identity.Identity, oid string) (*pilvytis.GatewayOrderResponse, error) { 71 return nil, nil 72 } 73 74 func (mock *mockPilvytis) GetPaymentGatewayOrders(id identity.Identity) ([]pilvytis.GatewayOrderResponse, error) { 75 return nil, nil 76 } 77 78 func (mock *mockPilvytis) GetPaymentGatewayOrderInvoice(id identity.Identity, oid string) ([]byte, error) { 79 return nil, nil 80 } 81 82 func (mock *mockPilvytis) GetPaymentGateways(_ exchange.Currency) ([]pilvytis.GatewaysResponse, error) { 83 return nil, nil 84 } 85 86 func (mock *mockPilvytis) GetRegistrationPaymentStatus(id identity.Identity) (*pilvytis.RegistrationPaymentResponse, error) { 87 if id.Address != mock.identity { 88 return &pilvytis.RegistrationPaymentResponse{ 89 Paid: false, 90 }, nil 91 } 92 93 return &pilvytis.RegistrationPaymentResponse{ 94 Paid: true, 95 }, nil 96 } 97 98 func (mock *mockPilvytis) GatewayClientCallback(id identity.Identity, gateway string, payload any) error { 99 return nil 100 } 101 102 type mockPilvytisLocation struct{} 103 104 func (mock *mockPilvytisLocation) GetOrigin() locationstate.Location { 105 return locationstate.Location{ 106 Country: "LT", 107 } 108 } 109 110 func TestGetRegistrationPaymentStatus(t *testing.T) { 111 identity := "0x000000000000000000000000000000000000000b" 112 113 mock := &mockPilvytis{ 114 identity: identity, 115 } 116 handler := NewPilvytisEndpoint(mock, &mockPilvytisIssuer{}, &mockPilvytisLocation{}).GetRegistrationPaymentStatus 117 118 resp := httptest.NewRecorder() 119 req, err := http.NewRequest( 120 http.MethodGet, 121 fmt.Sprintf("/identities/%s/registration-payment", identity), 122 nil, 123 ) 124 assert.NoError(t, err) 125 126 g := gin.Default() 127 g.GET("/identities/:id/registration-payment", handler) 128 g.ServeHTTP(resp, req) 129 130 assert.Equal(t, 200, resp.Code) 131 assert.JSONEq(t, 132 `{ 133 "paid":true 134 }`, 135 resp.Body.String(), 136 ) 137 }