136 lines
3.3 KiB
Go
136 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"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 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", "version": "test", "database": false})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("Expected %d, got %d", http.StatusOK, w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCORSHeaders(t *testing.T) {
|
|
r := gin.New()
|
|
r.Use(middleware.CORS())
|
|
r.GET("/test", func(c *gin.Context) { c.Status(200) })
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("OPTIONS", "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("Expected %d for OPTIONS, got %d", http.StatusNoContent, w.Code)
|
|
}
|
|
if w.Header().Get("Access-Control-Allow-Origin") != "*" {
|
|
t.Error("Missing CORS header")
|
|
}
|
|
}
|
|
|
|
func TestAllRoutesRegistered(t *testing.T) {
|
|
cfg := &config.Config{JWTSecret: "test"}
|
|
auth := handlers.NewAuthHandler(cfg)
|
|
chats := handlers.NewChatHandler()
|
|
msgs := handlers.NewMessageHandler()
|
|
|
|
r := gin.New()
|
|
api := r.Group("/api/v1")
|
|
|
|
authGroup := api.Group("/auth")
|
|
{
|
|
authGroup.POST("/register", auth.Register)
|
|
authGroup.POST("/login", auth.Login)
|
|
authGroup.POST("/refresh", auth.Refresh)
|
|
authGroup.POST("/logout", auth.Logout)
|
|
}
|
|
|
|
protected := api.Group("")
|
|
{
|
|
protected.GET("/chats", chats.ListChats)
|
|
protected.POST("/chats", chats.CreateChat)
|
|
protected.GET("/chats/:id", chats.GetChat)
|
|
protected.PUT("/chats/:id", chats.UpdateChat)
|
|
protected.DELETE("/chats/:id", chats.DeleteChat)
|
|
|
|
protected.GET("/chats/:id/messages", msgs.ListMessages)
|
|
protected.POST("/chats/:id/messages", msgs.CreateMessage)
|
|
protected.POST("/chats/:id/regenerate", msgs.Regenerate)
|
|
}
|
|
|
|
routes := r.Routes()
|
|
routePaths := make(map[string]bool)
|
|
for _, route := range routes {
|
|
routePaths[route.Method+" "+route.Path] = true
|
|
}
|
|
|
|
expected := []string{
|
|
"POST /api/v1/auth/register",
|
|
"POST /api/v1/auth/login",
|
|
"POST /api/v1/auth/refresh",
|
|
"POST /api/v1/auth/logout",
|
|
"GET /api/v1/chats",
|
|
"POST /api/v1/chats",
|
|
"GET /api/v1/chats/:id",
|
|
"PUT /api/v1/chats/:id",
|
|
"DELETE /api/v1/chats/:id",
|
|
"GET /api/v1/chats/:id/messages",
|
|
"POST /api/v1/chats/:id/messages",
|
|
"POST /api/v1/chats/:id/regenerate",
|
|
}
|
|
|
|
for _, e := range expected {
|
|
if !routePaths[e] {
|
|
t.Errorf("Missing route: %s", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStubHandler(t *testing.T) {
|
|
r := gin.New()
|
|
r.GET("/test", stubHandler("settings"))
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != 501 {
|
|
t.Errorf("Expected 501, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAuthMiddlewareNoDatabase(t *testing.T) {
|
|
cfg := &config.Config{JWTSecret: "test"}
|
|
r := gin.New()
|
|
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 %d", w.Code)
|
|
}
|
|
}
|