Issue 4 jwt (#24)
This commit is contained in:
@@ -8,14 +8,18 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/config"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/handlers"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/middleware"
|
||||
)
|
||||
|
||||
func TestHealthEndpoint(t *testing.T) {
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func TestHealthEndpoint(t *testing.T) {
|
||||
r := gin.New()
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok", "database": false})
|
||||
c.JSON(200, gin.H{"status": "ok", "version": "test", "database": false})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
@@ -28,7 +32,6 @@ func TestHealthEndpoint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCORSHeaders(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.CORS())
|
||||
r.GET("/test", func(c *gin.Context) {
|
||||
@@ -40,28 +43,25 @@ func TestCORSHeaders(t *testing.T) {
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Errorf("Expected status %d for OPTIONS, got %d", http.StatusNoContent, w.Code)
|
||||
t.Errorf("Expected %d for OPTIONS, got %d", http.StatusNoContent, w.Code)
|
||||
}
|
||||
|
||||
if w.Header().Get("Access-Control-Allow-Origin") != "*" {
|
||||
t.Error("Missing or incorrect CORS header")
|
||||
t.Error("Missing CORS header")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterSetup(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
func TestAuthRoutesRegistered(t *testing.T) {
|
||||
cfg := &config.Config{JWTSecret: "test-secret"}
|
||||
auth := handlers.NewAuthHandler(cfg)
|
||||
|
||||
r := gin.New()
|
||||
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
})
|
||||
|
||||
api := r.Group("/api/v1")
|
||||
authGroup := api.Group("/auth")
|
||||
{
|
||||
api.POST("/auth/register", handleRegister)
|
||||
api.POST("/auth/login", handleLogin)
|
||||
api.GET("/chats", handleListChats)
|
||||
api.POST("/chats", handleCreateChat)
|
||||
authGroup.POST("/register", auth.Register)
|
||||
authGroup.POST("/login", auth.Login)
|
||||
authGroup.POST("/refresh", auth.Refresh)
|
||||
authGroup.POST("/logout", auth.Logout)
|
||||
}
|
||||
|
||||
routes := r.Routes()
|
||||
@@ -70,27 +70,45 @@ func TestRouterSetup(t *testing.T) {
|
||||
routePaths[route.Method+" "+route.Path] = true
|
||||
}
|
||||
|
||||
expectedRoutes := []string{
|
||||
"GET /health",
|
||||
expected := []string{
|
||||
"POST /api/v1/auth/register",
|
||||
"POST /api/v1/auth/login",
|
||||
"GET /api/v1/chats",
|
||||
"POST /api/v1/chats",
|
||||
"POST /api/v1/auth/refresh",
|
||||
"POST /api/v1/auth/logout",
|
||||
}
|
||||
|
||||
for _, expected := range expectedRoutes {
|
||||
if !routePaths[expected] {
|
||||
t.Errorf("Expected route %s to be registered", expected)
|
||||
for _, e := range expected {
|
||||
if !routePaths[e] {
|
||||
t.Errorf("Expected route %s to be registered", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthMiddlewareNoDatabase(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
func TestProtectedRoutesRegistered(t *testing.T) {
|
||||
r := gin.New()
|
||||
api := r.Group("/api/v1")
|
||||
protected := api.Group("")
|
||||
{
|
||||
protected.GET("/chats", handleListChats)
|
||||
protected.POST("/chats", handleCreateChat)
|
||||
protected.GET("/chats/:id", handleGetChat)
|
||||
protected.DELETE("/chats/:id", handleDeleteChat)
|
||||
protected.GET("/chats/:id/messages", handleGetMessages)
|
||||
protected.POST("/chats/:id/messages", handleCreateMessage)
|
||||
protected.GET("/settings", handleGetSettings)
|
||||
protected.GET("/api-configs", handleListAPIConfigs)
|
||||
protected.GET("/models", handleListModels)
|
||||
}
|
||||
|
||||
routes := r.Routes()
|
||||
if len(routes) != 9 {
|
||||
t.Errorf("Expected 9 routes, got %d", len(routes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthMiddlewareNoDatabase(t *testing.T) {
|
||||
cfg := &config.Config{JWTSecret: "test-secret"}
|
||||
// No database connected — middleware should pass through
|
||||
r := gin.New()
|
||||
r.Use(middleware.Auth(cfg))
|
||||
r.GET("/protected", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"ok": true})
|
||||
@@ -100,7 +118,22 @@ func TestAuthMiddlewareNoDatabase(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/protected", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// No database connected → middleware should pass through
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected pass-through with no DB, got status %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaceholderHandlersReturn501(t *testing.T) {
|
||||
r := gin.New()
|
||||
r.GET("/chats", handleListChats)
|
||||
r.POST("/chats", handleCreateChat)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/chats", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 501 {
|
||||
t.Errorf("Expected 501 for unimplemented handler, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user