github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/connection/connection_validator_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 connection
    19  
    20  import (
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/mysteriumnetwork/node/identity"
    25  	"github.com/mysteriumnetwork/node/market"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestValidator_Validate(t *testing.T) {
    30  	type fields struct {
    31  		consumerBalanceGetter consumerBalanceGetter
    32  		unlockChecker         unlockChecker
    33  	}
    34  	type args struct {
    35  		consumerID identity.Identity
    36  		price      market.Price
    37  		chainID    int64
    38  	}
    39  	tests := []struct {
    40  		name    string
    41  		fields  fields
    42  		args    args
    43  		wantErr error
    44  	}{
    45  		{
    46  			name:    "returns insufficient balance",
    47  			wantErr: ErrInsufficientBalance,
    48  			fields: fields{
    49  				unlockChecker: &mockUnlockChecker{
    50  					toReturn: true,
    51  				},
    52  				consumerBalanceGetter: &mockConsumerBalanceGetter{
    53  					toReturn:    big.NewInt(1),
    54  					forceReturn: big.NewInt(99),
    55  				},
    56  			},
    57  			args: args{
    58  				chainID:    1,
    59  				consumerID: identity.FromAddress("whatever"),
    60  				price: market.Price{
    61  					PricePerHour: big.NewInt(6000),
    62  					PricePerGiB:  big.NewInt(6000),
    63  				},
    64  			},
    65  		},
    66  		{
    67  			name:    "resync balance on insufficient balance",
    68  			wantErr: nil,
    69  			fields: fields{
    70  				unlockChecker: &mockUnlockChecker{
    71  					toReturn: true,
    72  				},
    73  				consumerBalanceGetter: &mockConsumerBalanceGetter{
    74  					toReturn:    big.NewInt(99),
    75  					forceReturn: big.NewInt(100),
    76  				},
    77  			},
    78  			args: args{
    79  				chainID:    1,
    80  				consumerID: identity.FromAddress("whatever"),
    81  				price: market.Price{
    82  					PricePerHour: big.NewInt(600),
    83  					PricePerGiB:  big.NewInt(600),
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name:    "returns unlock required",
    89  			wantErr: ErrUnlockRequired,
    90  			fields: fields{
    91  				unlockChecker: &mockUnlockChecker{
    92  					toReturn: false,
    93  				},
    94  			},
    95  			args: args{
    96  				chainID:    1,
    97  				consumerID: identity.FromAddress("whatever"),
    98  			},
    99  		},
   100  		{
   101  			name:    "returns no error if conditions are satisfied",
   102  			wantErr: nil,
   103  			fields: fields{
   104  				unlockChecker: &mockUnlockChecker{
   105  					toReturn: true,
   106  				},
   107  				consumerBalanceGetter: &mockConsumerBalanceGetter{
   108  					toReturn: big.NewInt(101),
   109  				},
   110  			},
   111  			args: args{
   112  				chainID:    1,
   113  				consumerID: identity.FromAddress("whatever"),
   114  				price: market.Price{
   115  					PricePerHour: big.NewInt(100),
   116  					PricePerGiB:  big.NewInt(100),
   117  				},
   118  			},
   119  		},
   120  	}
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			v := &Validator{
   124  				consumerBalanceGetter: tt.fields.consumerBalanceGetter,
   125  				unlockChecker:         tt.fields.unlockChecker,
   126  			}
   127  			err := v.Validate(tt.args.chainID, tt.args.consumerID, tt.args.price)
   128  			if tt.wantErr != nil {
   129  				assert.EqualError(t, err, tt.wantErr.Error(), tt.name)
   130  			} else {
   131  				assert.NoError(t, err, tt.name)
   132  			}
   133  		})
   134  	}
   135  }
   136  
   137  type mockUnlockChecker struct {
   138  	toReturn bool
   139  }
   140  
   141  func (muc *mockUnlockChecker) IsUnlocked(id string) bool {
   142  	return muc.toReturn
   143  }
   144  
   145  type mockConsumerBalanceGetter struct {
   146  	needSync    bool
   147  	toReturn    *big.Int
   148  	forceReturn *big.Int
   149  }
   150  
   151  func (mcbg *mockConsumerBalanceGetter) NeedsForceSync(chainID int64, id identity.Identity) bool {
   152  	return mcbg.needSync
   153  }
   154  
   155  func (mcbg *mockConsumerBalanceGetter) GetBalance(chainID int64, id identity.Identity) *big.Int {
   156  	return mcbg.toReturn
   157  }
   158  
   159  func (mcbg *mockConsumerBalanceGetter) ForceBalanceUpdate(chainID int64, id identity.Identity) *big.Int {
   160  	return mcbg.forceReturn
   161  }