github.com/ethersphere/bee/v2@v2.2.0/pkg/api/stewardship_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 "encoding/hex" 9 "net/http" 10 "testing" 11 12 "github.com/ethersphere/bee/v2/pkg/api" 13 "github.com/ethersphere/bee/v2/pkg/jsonhttp" 14 "github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" 15 "github.com/ethersphere/bee/v2/pkg/log" 16 mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock" 17 "github.com/ethersphere/bee/v2/pkg/steward/mock" 18 mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock" 19 "github.com/ethersphere/bee/v2/pkg/swarm" 20 ) 21 22 // nolint:paralleltest 23 func TestStewardship(t *testing.T) { 24 var ( 25 logger = log.Noop 26 stewardMock = &mock.Steward{} 27 storer = mockstorer.New() 28 addr = swarm.NewAddress([]byte{31: 128}) 29 ) 30 client, _, _, _ := newTestServer(t, testServerOptions{ 31 Storer: storer, 32 Logger: logger, 33 Steward: stewardMock, 34 Post: mockpost.New(mockpost.WithAcceptAll()), 35 }) 36 37 t.Run("re-upload", func(t *testing.T) { 38 jsonhttptest.Request(t, client, http.MethodPut, "/v1/stewardship/"+addr.String(), http.StatusOK, 39 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ 40 Message: http.StatusText(http.StatusOK), 41 Code: http.StatusOK, 42 }), 43 jsonhttptest.WithRequestHeader("Swarm-Postage-Batch-Id", "aa"), 44 ) 45 if !stewardMock.LastAddress().Equal(addr) { 46 t.Fatalf("\nhave address: %q\nwant address: %q", stewardMock.LastAddress().String(), addr.String()) 47 } 48 }) 49 50 t.Run("is-retrievable", func(t *testing.T) { 51 jsonhttptest.Request(t, client, http.MethodGet, "/v1/stewardship/"+addr.String(), http.StatusOK, 52 jsonhttptest.WithExpectedJSONResponse(api.IsRetrievableResponse{IsRetrievable: true}), 53 ) 54 jsonhttptest.Request(t, client, http.MethodGet, "/v1/stewardship/"+hex.EncodeToString([]byte{}), http.StatusNotFound, 55 jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{ 56 Code: http.StatusNotFound, 57 Message: http.StatusText(http.StatusNotFound), 58 }), 59 ) 60 }) 61 } 62 63 func TestStewardshipInvalidInputs(t *testing.T) { 64 t.Parallel() 65 66 client, _, _, _ := newTestServer(t, testServerOptions{ 67 Storer: mockstorer.New(), 68 }) 69 70 tests := []struct { 71 name string 72 address string 73 want jsonhttp.StatusResponse 74 }{{ 75 name: "address - odd hex string", 76 address: "123", 77 want: jsonhttp.StatusResponse{ 78 Code: http.StatusBadRequest, 79 Message: "invalid path params", 80 Reasons: []jsonhttp.Reason{ 81 { 82 Field: "address", 83 Error: api.ErrHexLength.Error(), 84 }, 85 }, 86 }, 87 }, { 88 name: "address - invalid hex character", 89 address: "123G", 90 want: jsonhttp.StatusResponse{ 91 Code: http.StatusBadRequest, 92 Message: "invalid path params", 93 Reasons: []jsonhttp.Reason{ 94 { 95 Field: "address", 96 Error: api.HexInvalidByteError('G').Error(), 97 }, 98 }, 99 }, 100 }} 101 102 for _, method := range []string{http.MethodGet, http.MethodPut} { 103 method := method 104 for _, tc := range tests { 105 tc := tc 106 t.Run(method+" "+tc.name, func(t *testing.T) { 107 t.Parallel() 108 109 jsonhttptest.Request(t, client, method, "/stewardship/"+tc.address, tc.want.Code, 110 jsonhttptest.WithExpectedJSONResponse(tc.want), 111 ) 112 }) 113 } 114 } 115 116 t.Run("batch with id not found", func(t *testing.T) { 117 t.Parallel() 118 119 jsonhttptest.Request(t, client, http.MethodPut, "/stewardship/1234", http.StatusNotFound, 120 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, "1234"), 121 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ 122 Code: http.StatusNotFound, 123 Message: "batch with id not found", 124 }), 125 ) 126 }) 127 t.Run("invalid batch id", func(t *testing.T) { 128 t.Parallel() 129 130 jsonhttptest.Request(t, client, http.MethodPut, "/stewardship/1234", http.StatusBadRequest, 131 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, "1234G"), 132 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ 133 Code: http.StatusBadRequest, 134 Message: "invalid header params", 135 Reasons: []jsonhttp.Reason{ 136 { 137 Field: api.SwarmPostageBatchIdHeader, 138 Error: api.HexInvalidByteError('G').Error(), 139 }, 140 }, 141 }), 142 ) 143 }) 144 }