Skip to content

Commit

Permalink
test: server: cookie generation and deletion (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 19, 2024
1 parent f8254f8 commit b43b20d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
6 changes: 3 additions & 3 deletions server/cookies/cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions server/cookies/cookies_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit b43b20d

Please sign in to comment.