github.com/ethersphere/bee/v2@v2.2.0/pkg/api/accounting_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api_test
     6  
     7  import (
     8  	"errors"
     9  	"math/big"
    10  	"net/http"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/ethersphere/bee/v2/pkg/accounting"
    15  	"github.com/ethersphere/bee/v2/pkg/accounting/mock"
    16  	"github.com/ethersphere/bee/v2/pkg/api"
    17  	"github.com/ethersphere/bee/v2/pkg/bigint"
    18  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    19  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    20  )
    21  
    22  func TestAccountingInfo(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	accountingFunc := func() (map[string]accounting.PeerInfo, error) {
    26  		ret := make(map[string]accounting.PeerInfo)
    27  		ret["BEEF"] = accounting.PeerInfo{
    28  			Balance:               big.NewInt(25),
    29  			ThresholdReceived:     big.NewInt(37),
    30  			ThresholdGiven:        big.NewInt(49),
    31  			SurplusBalance:        big.NewInt(74),
    32  			ReservedBalance:       big.NewInt(85),
    33  			ShadowReservedBalance: big.NewInt(92),
    34  			GhostBalance:          big.NewInt(94),
    35  		}
    36  		ret["B33F"] = accounting.PeerInfo{
    37  			Balance:               big.NewInt(26),
    38  			ThresholdReceived:     big.NewInt(38),
    39  			ThresholdGiven:        big.NewInt(50),
    40  			SurplusBalance:        big.NewInt(75),
    41  			ReservedBalance:       big.NewInt(86),
    42  			ShadowReservedBalance: big.NewInt(93),
    43  			GhostBalance:          big.NewInt(95),
    44  		}
    45  		ret["BE3F"] = accounting.PeerInfo{
    46  			Balance:               big.NewInt(27),
    47  			ThresholdReceived:     big.NewInt(39),
    48  			ThresholdGiven:        big.NewInt(51),
    49  			SurplusBalance:        big.NewInt(76),
    50  			ReservedBalance:       big.NewInt(87),
    51  			ShadowReservedBalance: big.NewInt(94),
    52  			GhostBalance:          big.NewInt(96),
    53  		}
    54  
    55  		return ret, nil
    56  	}
    57  
    58  	testServer, _, _, _ := newTestServer(t, testServerOptions{
    59  		AccountingOpts: []mock.Option{mock.WithPeerAccountingFunc(accountingFunc)},
    60  	})
    61  
    62  	expected := &api.PeerData{
    63  		InfoResponse: map[string]api.PeerDataResponse{
    64  			"BEEF": {
    65  				Balance:               bigint.Wrap(big.NewInt(25)),
    66  				ThresholdReceived:     bigint.Wrap(big.NewInt(37)),
    67  				ThresholdGiven:        bigint.Wrap(big.NewInt(49)),
    68  				SurplusBalance:        bigint.Wrap(big.NewInt(74)),
    69  				ReservedBalance:       bigint.Wrap(big.NewInt(85)),
    70  				ShadowReservedBalance: bigint.Wrap(big.NewInt(92)),
    71  				GhostBalance:          bigint.Wrap(big.NewInt(94)),
    72  			},
    73  			"B33F": {
    74  				Balance:               bigint.Wrap(big.NewInt(26)),
    75  				ThresholdReceived:     bigint.Wrap(big.NewInt(38)),
    76  				ThresholdGiven:        bigint.Wrap(big.NewInt(50)),
    77  				SurplusBalance:        bigint.Wrap(big.NewInt(75)),
    78  				ReservedBalance:       bigint.Wrap(big.NewInt(86)),
    79  				ShadowReservedBalance: bigint.Wrap(big.NewInt(93)),
    80  				GhostBalance:          bigint.Wrap(big.NewInt(95)),
    81  			},
    82  			"BE3F": {
    83  				Balance:               bigint.Wrap(big.NewInt(27)),
    84  				ThresholdReceived:     bigint.Wrap(big.NewInt(39)),
    85  				ThresholdGiven:        bigint.Wrap(big.NewInt(51)),
    86  				SurplusBalance:        bigint.Wrap(big.NewInt(76)),
    87  				ReservedBalance:       bigint.Wrap(big.NewInt(87)),
    88  				ShadowReservedBalance: bigint.Wrap(big.NewInt(94)),
    89  				GhostBalance:          bigint.Wrap(big.NewInt(96)),
    90  			},
    91  		},
    92  	}
    93  
    94  	// We expect a list of items unordered by peer:
    95  	var got *api.PeerData
    96  	jsonhttptest.Request(t, testServer, http.MethodGet, "/accounting", http.StatusOK,
    97  		jsonhttptest.WithUnmarshalJSONResponse(&got),
    98  	)
    99  
   100  	if !reflect.DeepEqual(got, expected) {
   101  		t.Errorf("got accounting: %v, expected: %v", got, expected)
   102  	}
   103  
   104  }
   105  
   106  func TestAccountingInfoError(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	wantErr := errors.New("ASDF")
   110  	accountingFunc := func() (map[string]accounting.PeerInfo, error) {
   111  		return nil, wantErr
   112  	}
   113  	testServer, _, _, _ := newTestServer(t, testServerOptions{
   114  		AccountingOpts: []mock.Option{mock.WithPeerAccountingFunc(accountingFunc)},
   115  	})
   116  
   117  	jsonhttptest.Request(t, testServer, http.MethodGet, "/accounting", http.StatusInternalServerError,
   118  		jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
   119  			Message: api.HttpErrGetAccountingInfo,
   120  			Code:    http.StatusInternalServerError,
   121  		}),
   122  	)
   123  }