Complete lint remediation addressing errcheck, gosec, and staticcheck violations across backend test files. Tighten pre-commit configuration to prevent future blind spots. Key Changes: - Fix 61 Go linting issues (errcheck, gosec G115/G301/G304/G306, bodyclose) - Add proper error handling for json.Unmarshal, os.Setenv, db.Close(), w.Write() - Fix gosec G115 integer overflow with strconv.FormatUint - Add #nosec annotations with justifications for test fixtures - Fix SecurityService goroutine leaks (add Close() calls) - Fix CrowdSec tar.gz non-deterministic ordering with sorted keys Pre-commit Hardening: - Remove test file exclusion from golangci-lint hook - Add gosec to .golangci-fast.yml with critical checks (G101, G110, G305) - Replace broad .golangci.yml exclusions with targeted path-specific rules - Test files now linted on every commit Test Fixes: - Fix emergency route count assertions (1→2 for dual-port setup) - Fix DNS provider service tests with proper mock setup - Fix certificate service tests with deterministic behavior Backend: 27 packages pass, 83.5% coverage Frontend: 0 lint warnings, 0 TypeScript errors Pre-commit: All 14 hooks pass (~37s)
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewRouter(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
// Create a dummy frontend dir
|
|
tempDir := t.TempDir()
|
|
// #nosec G306 -- Test fixture HTML file needs to be world-readable for HTTP serving test
|
|
err := os.WriteFile(filepath.Join(tempDir, "index.html"), []byte("<html></html>"), 0o644)
|
|
assert.NoError(t, err)
|
|
|
|
router := NewRouter(tempDir)
|
|
assert.NotNil(t, router)
|
|
|
|
// Test static file serving
|
|
req, _ := http.NewRequest("GET", "/", http.NoBody)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "<html></html>")
|
|
|
|
// Test /api NoRoute special-case: do not serve SPA HTML
|
|
apiReq, _ := http.NewRequest("GET", "/api/this-route-does-not-exist", http.NoBody)
|
|
apiW := httptest.NewRecorder()
|
|
router.ServeHTTP(apiW, apiReq)
|
|
assert.Equal(t, http.StatusNotFound, apiW.Code)
|
|
assert.NotContains(t, apiW.Body.String(), "<html></html>")
|
|
assert.Contains(t, apiW.Body.String(), "not found")
|
|
}
|