[BACKEND] Initialize Go backend structure and dependencies (#23)
This commit is contained in:
@@ -6,13 +6,16 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/config"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/middleware"
|
||||
)
|
||||
|
||||
func TestHealthEndpoint(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
c.JSON(200, gin.H{"status": "ok", "database": false})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
@@ -22,29 +25,18 @@ func TestHealthEndpoint(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
expectedBody := `{"status":"ok"}`
|
||||
if w.Body.String() != expectedBody {
|
||||
t.Errorf("Expected body %s, got %s", expectedBody, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCORSHeaders(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
r.Use(middleware.CORS())
|
||||
r.GET("/test", func(c *gin.Context) {
|
||||
c.Status(200)
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("OPTIONS", "/api/v1/test", nil)
|
||||
req, _ := http.NewRequest("OPTIONS", "/test", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNoContent {
|
||||
@@ -60,7 +52,6 @@ func TestRouterSetup(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
|
||||
// Setup routes like in main.go
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
})
|
||||
@@ -73,7 +64,6 @@ func TestRouterSetup(t *testing.T) {
|
||||
api.POST("/chats", handleCreateChat)
|
||||
}
|
||||
|
||||
// Verify routes are registered
|
||||
routes := r.Routes()
|
||||
routePaths := make(map[string]bool)
|
||||
for _, route := range routes {
|
||||
@@ -93,4 +83,24 @@ func TestRouterSetup(t *testing.T) {
|
||||
t.Errorf("Expected route %s to be registered", expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthMiddlewareNoDatabase(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
|
||||
cfg := &config.Config{JWTSecret: "test-secret"}
|
||||
// No database connected — middleware should pass through
|
||||
r.Use(middleware.Auth(cfg))
|
||||
r.GET("/protected", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"ok": true})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/protected", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected pass-through with no DB, got status %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user