2026-03-14 20:34:37 +01:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/api"
|
|
|
|
|
"github.com/J3vb/OwnCord/Server/config"
|
|
|
|
|
"github.com/J3vb/OwnCord/Server/db"
|
2026-03-14 20:34:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// setupRouter creates a test router with an in-memory database.
|
|
|
|
|
func setupRouter(t *testing.T) http.Handler {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
database, err := db.Open(":memory:")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("db.Open error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := db.Migrate(database); err != nil {
|
|
|
|
|
t.Fatalf("db.Migrate error: %v", err)
|
|
|
|
|
}
|
2026-03-17 08:09:52 +01:00
|
|
|
t.Cleanup(func() { _ = database.Close() })
|
2026-03-14 20:34:37 +01:00
|
|
|
|
|
|
|
|
cfg := &config.Config{
|
|
|
|
|
Server: config.ServerConfig{
|
|
|
|
|
Name: "Test Server",
|
|
|
|
|
Port: 8443,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:29:29 +00:00
|
|
|
handler, _, cleanup := api.NewRouter(cfg, database, "test", nil, nil)
|
2026-03-29 19:39:22 +02:00
|
|
|
t.Cleanup(cleanup)
|
2026-03-19 03:53:40 +01:00
|
|
|
return handler
|
2026-03-14 20:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHealthEndpointReturns200(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("GET /health status = %d, want 200", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHealthEndpointReturnsJSON(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
contentType := rec.Header().Get("Content-Type")
|
|
|
|
|
if !strings.Contains(contentType, "application/json") {
|
|
|
|
|
t.Errorf("Content-Type = %q, want application/json", contentType)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 04:11:04 +01:00
|
|
|
var body map[string]any
|
2026-03-14 20:34:37 +01:00
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("response body is not valid JSON: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHealthEndpointStatusOK(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
2026-03-17 04:11:04 +01:00
|
|
|
var body map[string]any
|
2026-03-14 20:34:37 +01:00
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("JSON decode error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if body["status"] != "ok" {
|
|
|
|
|
t.Errorf("status = %v, want 'ok'", body["status"])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 18:02:26 +00:00
|
|
|
func TestHealthEndpointOmitsVersion(t *testing.T) {
|
2026-03-14 20:34:37 +01:00
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
2026-03-17 04:11:04 +01:00
|
|
|
var body map[string]any
|
2026-03-14 20:34:37 +01:00
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("JSON decode error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 18:02:26 +00:00
|
|
|
// C-2: Version must NOT be exposed on unauthenticated endpoints.
|
|
|
|
|
if _, exists := body["version"]; exists {
|
|
|
|
|
t.Error("health response must not contain 'version' field (prevents fingerprinting)")
|
2026-03-14 20:34:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAPIV1InfoEndpoint(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/info", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("GET /api/v1/info status = %d, want 200", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAPIV1InfoReturnsServerName(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/info", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
2026-03-17 04:11:04 +01:00
|
|
|
var body map[string]any
|
2026-03-14 20:34:37 +01:00
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("JSON decode error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if body["name"] != "Test Server" {
|
|
|
|
|
t.Errorf("name = %v, want 'Test Server'", body["name"])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 18:02:26 +00:00
|
|
|
func TestAPIV1InfoOmitsVersion(t *testing.T) {
|
2026-03-14 20:34:37 +01:00
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/info", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
2026-03-17 04:11:04 +01:00
|
|
|
var body map[string]any
|
2026-03-14 20:34:37 +01:00
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("JSON decode error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 18:02:26 +00:00
|
|
|
// C-2: Version must NOT be exposed to prevent fingerprinting.
|
|
|
|
|
if _, exists := body["version"]; exists {
|
|
|
|
|
t.Error("info response must not contain 'version' field (prevents fingerprinting)")
|
2026-03-14 20:34:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUnknownRouteReturns404(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/nonexistent", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
|
|
|
t.Errorf("GET /api/v1/nonexistent status = %d, want 404", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRequestIDMiddleware(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
// Request ID header should be set by middleware.
|
|
|
|
|
requestID := rec.Header().Get("X-Request-Id")
|
|
|
|
|
if requestID == "" {
|
|
|
|
|
t.Error("X-Request-Id header not set by middleware")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHealthMethodNotAllowed(t *testing.T) {
|
|
|
|
|
router := setupRouter(t)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/health", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusMethodNotAllowed {
|
|
|
|
|
t.Errorf("POST /health status = %d, want 405", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|