package handlers import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" "github.com/labstack/echo/v4" pscookie "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie" ) func TestCartActionFromRequestDefaultsToAdd(t *testing.T) { e := echo.New() req := httptest.NewRequest(http.MethodPost, "/cart", strings.NewReader(url.Values{ "id_product": []string{"42"}, }.Encode())) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) rec := httptest.NewRecorder() c := e.NewContext(req, rec) if got := cartActionFromRequest(c); got != cartActionAdd { t.Fatalf("cartActionFromRequest() = %q, want %q", got, cartActionAdd) } } func TestCartActionFromRequestHonorsDeleteFlag(t *testing.T) { e := echo.New() req := httptest.NewRequest(http.MethodPost, "/cart", strings.NewReader(url.Values{ "delete": []string{"1"}, "id_product": []string{"42"}, }.Encode())) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) rec := httptest.NewRecorder() c := e.NewContext(req, rec) if got := cartActionFromRequest(c); got != cartActionDelete { t.Fatalf("cartActionFromRequest() = %q, want %q", got, cartActionDelete) } } func TestSyncSessionCartIDPreservesChecksumOrder(t *testing.T) { session := &pscookie.SessionContext{ Values: map[string]string{ "id_lang": "5", "checksum": "123", "id_guest": "11", "id_cart": "", "id_shop": "1", "id_guest2": "ignored", }, OrderedKeys: []string{"id_lang", "id_guest", "checksum"}, RawCookie: "raw", Plaintext: "plaintext", } syncSessionCartID(session, 99) if session.CartID == nil || *session.CartID != 99 { t.Fatalf("CartID = %v, want 99", session.CartID) } if got := session.Values["id_cart"]; got != "99" { t.Fatalf("Values[id_cart] = %q, want %q", got, "99") } wantOrder := []string{"id_lang", "id_guest", "id_cart", "checksum"} if len(session.OrderedKeys) != len(wantOrder) { t.Fatalf("OrderedKeys length = %d, want %d", len(session.OrderedKeys), len(wantOrder)) } for i, want := range wantOrder { if got := session.OrderedKeys[i]; got != want { t.Fatalf("OrderedKeys[%d] = %q, want %q", i, got, want) } } if session.RawCookie != "" { t.Fatalf("RawCookie = %q, want empty", session.RawCookie) } if session.Plaintext != "" { t.Fatalf("Plaintext = %q, want empty", session.Plaintext) } }