From 38daba343f8b0b3d720e6eb1908316d0cab876b4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 20:31:06 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20test=20for=20clearAuthCook?= =?UTF-8?q?ies=20in=20auth=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎯 What: Add test coverage for the clearAuthCookies helper function which wasn't being tested. 📊 Coverage: Validates that access and refresh cookies are properly cleared by ensuring they are set with empty values, path '/', MaxAge of -1, and HttpOnly flag enabled. ✨ Result: Increased test reliability and coverage for the authentication flow's cookie clearance mechanism. --- .../internal/modules/auth/handler_test.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/apps/server/internal/modules/auth/handler_test.go b/apps/server/internal/modules/auth/handler_test.go index d250365..4452cfd 100644 --- a/apps/server/internal/modules/auth/handler_test.go +++ b/apps/server/internal/modules/auth/handler_test.go @@ -1,6 +1,11 @@ package auth import ( + "net/http" + "net/http/httptest" + + "github.com/labstack/echo/v5" + "testing" ) @@ -743,3 +748,61 @@ func TestRoutePrefix(t *testing.T) { }) } } + +// TestClearAuthCookies verifies that auth cookies are properly cleared +func TestClearAuthCookies(t *testing.T) { + t.Run("clears access and refresh cookies", func(t *testing.T) { + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + h := &Handler{} + h.clearAuthCookies(c) + + cookies := rec.Result().Cookies() + + var accessCookie, refreshCookie *http.Cookie + for _, cookie := range cookies { + if cookie.Name == "access_token" { + accessCookie = cookie + } else if cookie.Name == "refresh_token" { + refreshCookie = cookie + } + } + + if accessCookie == nil { + t.Error("Expected access_token cookie to be set") + } else { + if accessCookie.Value != "" { + t.Errorf("Expected access_token value to be empty, got %q", accessCookie.Value) + } + if accessCookie.MaxAge != -1 { + t.Errorf("Expected access_token MaxAge to be -1, got %d", accessCookie.MaxAge) + } + if accessCookie.Path != "/" { + t.Errorf("Expected access_token Path to be '/', got %q", accessCookie.Path) + } + if !accessCookie.HttpOnly { + t.Error("Expected access_token to be HttpOnly") + } + } + + if refreshCookie == nil { + t.Error("Expected refresh_token cookie to be set") + } else { + if refreshCookie.Value != "" { + t.Errorf("Expected refresh_token value to be empty, got %q", refreshCookie.Value) + } + if refreshCookie.MaxAge != -1 { + t.Errorf("Expected refresh_token MaxAge to be -1, got %d", refreshCookie.MaxAge) + } + if refreshCookie.Path != "/" { + t.Errorf("Expected refresh_token Path to be '/', got %q", refreshCookie.Path) + } + if !refreshCookie.HttpOnly { + t.Error("Expected refresh_token to be HttpOnly") + } + } + }) +}