From d5bcc71c0b62b77f72d4738b5e48674841eace82 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:08:55 +0800 Subject: [PATCH] fix(auth): restore the sidebar version badge --- internal/handler/auth.go | 6 +- .../handler/auth_login_validation_test.go | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/internal/handler/auth.go b/internal/handler/auth.go index c996af78..fcc31a0b 100644 --- a/internal/handler/auth.go +++ b/internal/handler/auth.go @@ -96,6 +96,7 @@ func (h *AuthHandler) Login(c *gin.Context) { "token": token, "expires_at": expiresAt.UTC().Format(time.RFC3339), "session_duration_hr": h.manager.SessionDurationHours(), + "app_version": h.config.Version, "user": gin.H{ "id": session.UserID, "username": session.Username, @@ -218,8 +219,9 @@ func (h *AuthHandler) Validate(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "token": session.Token, - "expires_at": session.ExpiresAt.UTC().Format(time.RFC3339), + "token": session.Token, + "expires_at": session.ExpiresAt.UTC().Format(time.RFC3339), + "app_version": h.config.Version, "user": gin.H{ "id": session.UserID, "username": session.Username, diff --git a/internal/handler/auth_login_validation_test.go b/internal/handler/auth_login_validation_test.go index 72b3ece4..a29d3881 100644 --- a/internal/handler/auth_login_validation_test.go +++ b/internal/handler/auth_login_validation_test.go @@ -5,10 +5,16 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "path/filepath" "strings" "testing" + "cyberstrike-ai/internal/config" + "cyberstrike-ai/internal/database" + "cyberstrike-ai/internal/security" + "github.com/gin-gonic/gin" + "go.uber.org/zap" ) func TestLoginRejectsMissingAndLongUsernameBeforeAuthentication(t *testing.T) { @@ -26,3 +32,55 @@ func TestLoginRejectsMissingAndLongUsernameBeforeAuthentication(t *testing.T) { } } } + +func TestAuthSessionResponsesIncludeAppVersion(t *testing.T) { + const version = "v9.8.7-test" + logger := zap.NewNop() + db, err := database.NewDB(filepath.Join(t.TempDir(), "auth.db"), logger) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + manager := security.NewAuthManager(12) + password, err := manager.AttachRBACStore(db) + if err != nil { + t.Fatal(err) + } + + h := NewAuthHandler(manager, &config.Config{Version: version}, "", logger) + r := gin.New() + r.POST("/api/auth/login", h.Login) + r.GET("/api/auth/validate", security.AuthMiddleware(manager), h.Validate) + + checkVersion := func(req *http.Request) string { + t.Helper() + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Fatalf("%s returned status %d", req.URL.Path, w.Code) + } + var session struct { + Token string `json:"token"` + AppVersion string `json:"app_version"` + } + if err := json.NewDecoder(w.Body).Decode(&session); err != nil { + t.Fatal(err) + } + if session.AppVersion != version { + t.Errorf("%s app_version = %q, want %q", req.URL.Path, session.AppVersion, version) + } + return session.Token + } + + body, err := json.Marshal(loginRequest{Username: "admin", Password: password}) + if err != nil { + t.Fatal(err) + } + login := httptest.NewRequest(http.MethodPost, "/api/auth/login", bytes.NewReader(body)) + login.Header.Set("Content-Type", "application/json") + token := checkVersion(login) + + validate := httptest.NewRequest(http.MethodGet, "/api/auth/validate", nil) + validate.Header.Set("Authorization", "Bearer "+token) + checkVersion(validate) +}