github.com/ethersphere/bee/v2@v2.2.0/pkg/api/envelope_test.go (about) 1 // Copyright 2024 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 "fmt" 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 mockbatchstore "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock" 16 mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock" 17 ) 18 19 func TestPostEnvelope(t *testing.T) { 20 t.Parallel() 21 22 zeroHex := "0000000000000000000000000000000000000000000000000000000000000000" 23 envelopeEndpoint := func(chunkAddress string) string { return fmt.Sprintf("/envelope/%s", chunkAddress) } 24 client, _, _, _ := newTestServer(t, testServerOptions{ 25 Post: mockpost.New(mockpost.WithAcceptAll()), 26 }) 27 28 t.Run("ok", func(t *testing.T) { 29 t.Parallel() 30 31 jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusCreated, 32 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), 33 ) 34 }) 35 36 t.Run("wrong chunk address", func(t *testing.T) { 37 t.Parallel() 38 39 jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint("invalid"), http.StatusBadRequest, 40 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), 41 ) 42 }) 43 44 t.Run("postage does not exist", func(t *testing.T) { 45 t.Parallel() 46 client, _, _, _ := newTestServer(t, testServerOptions{}) 47 48 jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusNotFound, 49 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, zeroHex), 50 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{Message: "batch with id not found", Code: http.StatusNotFound}), 51 ) 52 }) 53 54 t.Run("batch unusable", func(t *testing.T) { 55 t.Parallel() 56 client, _, _, _ := newTestServer(t, testServerOptions{ 57 Post: mockpost.New(mockpost.WithAcceptAll()), 58 BatchStore: mockbatchstore.New(), 59 }) 60 61 jsonhttptest.Request(t, client, http.MethodPost, envelopeEndpoint(zeroHex), http.StatusUnprocessableEntity, 62 jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), 63 jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{Message: "batch not usable yet or does not exist", Code: http.StatusUnprocessableEntity}), 64 ) 65 }) 66 }