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

     1  // Copyright 2023 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  	"context"
     9  	"math/big"
    10  	"net/http"
    11  	"testing"
    12  
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethersphere/bee/v2/pkg/api"
    15  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    16  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    17  	statestore "github.com/ethersphere/bee/v2/pkg/statestore/mock"
    18  	"github.com/ethersphere/bee/v2/pkg/storageincentives"
    19  	"github.com/ethersphere/bee/v2/pkg/transaction/backendmock"
    20  	"github.com/ethersphere/bee/v2/pkg/transaction/mock"
    21  )
    22  
    23  func TestRedistributionStatus(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	t.Run("success", func(t *testing.T) {
    27  		t.Parallel()
    28  
    29  		store := statestore.NewStateStore()
    30  		err := store.Put("redistribution_state", storageincentives.Status{
    31  			Phase: storageincentives.PhaseType(1),
    32  			Round: 1,
    33  			Block: 12,
    34  		})
    35  		if err != nil {
    36  			t.Errorf("redistribution put state: %v", err)
    37  		}
    38  		srv, _, _, _ := newTestServer(t, testServerOptions{
    39  			StateStorer: store,
    40  			TransactionOpts: []mock.Option{
    41  				mock.WithTransactionFeeFunc(func(ctx context.Context, txHash common.Hash) (*big.Int, error) {
    42  					return big.NewInt(1000), nil
    43  				}),
    44  			},
    45  			BackendOpts: []backendmock.Option{
    46  				backendmock.WithBalanceAt(func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
    47  					return big.NewInt(100000000), nil
    48  				}),
    49  				backendmock.WithSuggestGasPriceFunc(func(ctx context.Context) (*big.Int, error) {
    50  					return big.NewInt(1), nil
    51  				}),
    52  			},
    53  		})
    54  		jsonhttptest.Request(t, srv, http.MethodGet, "/redistributionstate", http.StatusOK,
    55  			jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "application/json; charset=utf-8"),
    56  		)
    57  	})
    58  
    59  	t.Run("bad request", func(t *testing.T) {
    60  		t.Parallel()
    61  
    62  		srv, _, _, _ := newTestServer(t, testServerOptions{
    63  			BeeMode:     api.LightMode,
    64  			StateStorer: statestore.NewStateStore(),
    65  			TransactionOpts: []mock.Option{
    66  				mock.WithTransactionFeeFunc(func(ctx context.Context, txHash common.Hash) (*big.Int, error) {
    67  					return big.NewInt(1000), nil
    68  				}),
    69  			},
    70  		})
    71  		jsonhttptest.Request(t, srv, http.MethodGet, "/redistributionstate", http.StatusBadRequest,
    72  			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
    73  				Message: api.ErrOperationSupportedOnlyInFullMode.Error(),
    74  				Code:    http.StatusBadRequest,
    75  			}),
    76  		)
    77  	})
    78  }