85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
pscookie "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"
|
|
)
|
|
|
|
func TestShouldBootstrapAnonymousSession(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
rawCookie string
|
|
session *pscookie.SessionContext
|
|
want bool
|
|
}{
|
|
{
|
|
name: "missing cookie bootstraps",
|
|
rawCookie: "",
|
|
session: &pscookie.SessionContext{},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "nil session bootstraps",
|
|
rawCookie: "cookie",
|
|
session: nil,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "incomplete anonymous cookie does not bootstrap",
|
|
rawCookie: "cookie",
|
|
session: &pscookie.SessionContext{
|
|
Values: map[string]string{
|
|
"id_lang": "5",
|
|
},
|
|
},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "logged in cookie does not bootstrap",
|
|
rawCookie: "cookie",
|
|
session: &pscookie.SessionContext{
|
|
IsLoggedIn: true,
|
|
},
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := shouldBootstrapAnonymousSession(tc.rawCookie, tc.session); got != tc.want {
|
|
t.Fatalf("shouldBootstrapAnonymousSession() = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetPrestaShopCookiePersistsExpiry(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodGet, "https://shop.example.com/product/test", nil)
|
|
rec := httptest.NewRecorder()
|
|
res := e.NewContext(req, rec).Response()
|
|
expiresAt := time.Now().UTC().Add(4 * time.Hour).Truncate(time.Second)
|
|
|
|
setPrestaShopCookie(req, res, &pscookie.SessionContext{
|
|
ExpiresAt: &expiresAt,
|
|
}, "PrestaShop-test", "value", "/")
|
|
|
|
setCookie := rec.Header().Get("Set-Cookie")
|
|
if !strings.Contains(setCookie, "max-age=") {
|
|
t.Fatalf("Set-Cookie missing max-age: %q", setCookie)
|
|
}
|
|
if strings.Contains(setCookie, "Expires=") {
|
|
t.Fatalf("Set-Cookie should not include Expires: %q", setCookie)
|
|
}
|
|
if !strings.Contains(setCookie, "path=/") {
|
|
t.Fatalf("Set-Cookie missing path=/: %q", setCookie)
|
|
}
|
|
}
|