From 3551a798f5e5ec7c5f187d87872adcf5f6b71875 Mon Sep 17 00:00:00 2001 From: notanatol Date: Thu, 8 Feb 2024 11:36:54 -0600 Subject: [PATCH] fix: add test --- pkg/api/api.go | 8 +++- pkg/api/api_test.go | 2 + pkg/api/integritycheck.go | 1 + pkg/api/integritycheck_test.go | 67 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 pkg/api/integritycheck_test.go diff --git a/pkg/api/api.go b/pkg/api/api.go index 59bab0c9b61..0d9cdaab415 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -131,6 +131,10 @@ type Storer interface { storer.Debugger } +type PinIntegrity interface { + Check(ctx context.Context, logger log.Logger, pin string, out chan storer.PinStat) +} + type Service struct { auth auth.Authenticator storer Storer @@ -174,7 +178,7 @@ type Service struct { batchStore postage.Storer stamperStore storage.Store - pinIntegrity *storer.PinIntegrity + pinIntegrity PinIntegrity syncStatus func() (bool, error) @@ -244,7 +248,7 @@ type ExtraOptions struct { Steward steward.Interface SyncStatus func() (bool, error) NodeStatus *status.Service - PinIntegrity *storer.PinIntegrity + PinIntegrity PinIntegrity } func New( diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index b06e5808479..cfa6794257d 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -131,6 +131,7 @@ type testServerOptions struct { BeeMode api.BeeNodeMode RedistributionAgent *storageincentives.Agent NodeStatus *status.Service + PinIntegrity api.PinIntegrity } func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.Conn, string, *chanStorer) { @@ -201,6 +202,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket. SyncStatus: o.SyncStatus, Staking: o.StakingContract, NodeStatus: o.NodeStatus, + PinIntegrity: o.PinIntegrity, } // By default bee mode is set to full mode. diff --git a/pkg/api/integritycheck.go b/pkg/api/integritycheck.go index 75b90b5222d..5b133d38183 100644 --- a/pkg/api/integritycheck.go +++ b/pkg/api/integritycheck.go @@ -41,6 +41,7 @@ func (s *Service) pinIntegrityHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Transfer-Encoding", "chunked") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) flusher.Flush() diff --git a/pkg/api/integritycheck_test.go b/pkg/api/integritycheck_test.go new file mode 100644 index 00000000000..8c6d9c99d44 --- /dev/null +++ b/pkg/api/integritycheck_test.go @@ -0,0 +1,67 @@ +// Copyright 2022 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package api_test + +import ( + "context" + "net/http" + "testing" + + "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" + "github.com/ethersphere/bee/pkg/log" + storage "github.com/ethersphere/bee/pkg/storage" + "github.com/ethersphere/bee/pkg/storage/inmemstore" + storer "github.com/ethersphere/bee/pkg/storer" +) + +const pinRef = "620fcd78c7ce54da2d1b7cc2274a02e190cbe8fecbc3bd244690ab6517ce8f39" + +func TestIntegrityHandler(t *testing.T) { + t.Parallel() + + t.Run("ok", func(t *testing.T) { + t.Parallel() + testServer, _, _, _ := newTestServer(t, testServerOptions{ + DebugAPI: true, + PinIntegrity: &mockPinIntegrity{ + Store: inmemstore.New(), + tester: t, + }, + }) + + endp := "/check/pin?ref=" + pinRef + + // When probe is not set health endpoint should indicate that node is not healthy + jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusOK, jsonhttptest.WithExpectedResponse(nil)) + }) + + t.Run("wrong hash format", func(t *testing.T) { + t.Parallel() + testServer, _, _, _ := newTestServer(t, testServerOptions{ + DebugAPI: true, + PinIntegrity: &mockPinIntegrity{ + Store: inmemstore.New(), + tester: t, + }, + }) + + endp := "/check/pin?ref=0xbadhash" + + // When probe is not set health endpoint should indicate that node is not healthy + jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusBadRequest, jsonhttptest.WithExpectedResponse(nil)) + }) +} + +type mockPinIntegrity struct { + tester *testing.T + Store storage.Store +} + +func (p *mockPinIntegrity) Check(ctx context.Context, logger log.Logger, pin string, out chan storer.PinStat) { + if pin != pinRef { + p.tester.Fatal("bad pin", pin) + } + close(out) +}