test: Add edge case for handleValidateBootstrapToken invalid JSON
Tests invalid JSON body triggers json.Decode error and returns 400. Coverage: 90.5% to 100%.
This commit is contained in:
parent
890178acb0
commit
d0c2524353
1 changed files with 23 additions and 0 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
||||||
|
|
@ -388,6 +391,26 @@ func TestBootstrapTokenValid(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleValidateBootstrapToken_InvalidJSON(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
cfg := &config.Config{DataPath: tmpDir}
|
||||||
|
r := &Router{config: cfg}
|
||||||
|
r.initializeBootstrapToken()
|
||||||
|
|
||||||
|
// Test invalid JSON body triggers json.Decode error
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/security/validate-bootstrap-token", strings.NewReader("not valid json"))
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
r.handleValidateBootstrapToken(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusBadRequest {
|
||||||
|
t.Errorf("expected 400 for invalid JSON, got %d", rr.Code)
|
||||||
|
}
|
||||||
|
if !strings.Contains(rr.Body.String(), "Invalid request payload") {
|
||||||
|
t.Errorf("expected 'Invalid request payload' error, got %q", rr.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClearBootstrapToken(t *testing.T) {
|
func TestClearBootstrapToken(t *testing.T) {
|
||||||
t.Run("nil router does not panic", func(t *testing.T) {
|
t.Run("nil router does not panic", func(t *testing.T) {
|
||||||
var r *Router = nil
|
var r *Router = nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue