From b43b20df675706e1df541778689aa3b4b44fa9de Mon Sep 17 00:00:00 2001 From: Christian Carlsson Date: Thu, 19 Dec 2024 11:23:18 +0000 Subject: [PATCH] test: server: cookie generation and deletion (#259) --- server/cookies/cookies.go | 6 ++-- server/cookies/cookies_test.go | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 server/cookies/cookies_test.go diff --git a/server/cookies/cookies.go b/server/cookies/cookies.go index 46937592..82b37c0c 100644 --- a/server/cookies/cookies.go +++ b/server/cookies/cookies.go @@ -17,11 +17,11 @@ func New(c *config.Config) *Cookies { return &Cookies{c} } -const cookieNameRefreshToken = "refreshToken" +const CookieNameRefreshToken = "refreshToken" func (c *Cookies) RefreshToken(value string) *http.Cookie { return &http.Cookie{ - Name: cookieNameRefreshToken, + Name: CookieNameRefreshToken, Value: value, Path: fmt.Sprintf("/%s", apiv1connect.AuthServiceName), Domain: c.config.Server.CookieDomain, @@ -34,7 +34,7 @@ func (c *Cookies) RefreshToken(value string) *http.Cookie { func (c *Cookies) ExpiredRefreshToken() *http.Cookie { return &http.Cookie{ - Name: cookieNameRefreshToken, + Name: CookieNameRefreshToken, Value: "", Path: fmt.Sprintf("/%s", apiv1connect.AuthServiceName), Domain: c.config.Server.CookieDomain, diff --git a/server/cookies/cookies_test.go b/server/cookies/cookies_test.go new file mode 100644 index 00000000..75712ef2 --- /dev/null +++ b/server/cookies/cookies_test.go @@ -0,0 +1,52 @@ +package cookies_test + +import ( + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/config" + "github.com/crlssn/getstronger/server/cookies" + "github.com/crlssn/getstronger/server/gen/proto/api/v1/apiv1connect" + "github.com/crlssn/getstronger/server/jwt" +) + +func TestCookies_RefreshToken(t *testing.T) { + t.Parallel() + + cfg := new(config.Config) + cfg.Server.CookieDomain = "cookie_domain" + cookie := cookies.New(cfg) + + require.Equal(t, &http.Cookie{ + Name: cookies.CookieNameRefreshToken, + Value: "value", + Path: fmt.Sprintf("/%s", apiv1connect.AuthServiceName), + Domain: cfg.Server.CookieDomain, + MaxAge: int(jwt.ExpiryTimeRefresh), + Secure: true, + HttpOnly: true, + SameSite: http.SameSiteNoneMode, + }, cookie.RefreshToken("value")) +} + +func TestCookies_ExpiredRefreshToken(t *testing.T) { + t.Parallel() + + cfg := new(config.Config) + cfg.Server.CookieDomain = "cookie_domain" + cookie := cookies.New(cfg) + + require.Equal(t, &http.Cookie{ + Name: cookies.CookieNameRefreshToken, + Value: "", + Path: fmt.Sprintf("/%s", apiv1connect.AuthServiceName), + Domain: cfg.Server.CookieDomain, + MaxAge: -1, + Secure: true, + HttpOnly: true, + SameSite: http.SameSiteNoneMode, + }, cookie.ExpiredRefreshToken()) +}