Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2cbc1a55c
commit
15a34e1f84
39 files changed
+3529
-163
No files matched your search
@@ -206,12 +206,23 @@ func TestJarShared(t *testing.T) {
|
||||
fresh.now = watcher.now
|
||||
if err := fresh.Load(path); err != nil { t.Fatal(err) }
|
||||
if got := cookieNames(fresh.Cookies(u)); got != "session=solved" { t.Errorf("file after both saved: %q", got) }
|
||||
|
||||
// Logging out in another process clears the watcher's copy as well.
|
||||
fresh.now = func() time.Time { return now.Add(3 * time.Minute) }
|
||||
fresh.Clear()
|
||||
if got := cookieNames(fresh.Cookies(u)); got != "" { t.Errorf("after Clear: %q", got) }
|
||||
if err := fresh.Save(path); err != nil { t.Fatal(err) }
|
||||
|
||||
watcher.now = func() time.Time { return now.Add(4 * time.Minute) }
|
||||
if err := watcher.Save(path); err != nil { t.Fatal(err) }
|
||||
if got := cookieNames(watcher.Cookies(u)); got != "" { t.Errorf("watcher after another process cleared the jar: %q", got) }
|
||||
}
|
||||
|
||||
// noLimit lets tests talk to their local server without the host floor.
|
||||
type noLimit struct{}
|
||||
|
||||
func (noLimit) WaitCheck(context.Context, string) error { return nil }
|
||||
func (noLimit) Wait(context.Context, string) error { return nil }
|
||||
|
||||
func newTestClient(t *testing.T, opts Options) *Client {
|
||||
t.Helper()
|
||||
@@ -279,6 +290,78 @@ func TestClientGet(t *testing.T) {
|
||||
if got := c.Jar().Export()["127.0.0.1"]; len(got) != 1 || got[0].Name != "sid" { t.Errorf("jar after requests: %+v", got) }
|
||||
}
|
||||
|
||||
// countingLimiter records which kind of wait each request made.
|
||||
type countingLimiter struct{ checks, waits []string }
|
||||
|
||||
func (l *countingLimiter) WaitCheck(_ context.Context, u string) error {
|
||||
l.checks = append(l.checks, u)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *countingLimiter) Wait(_ context.Context, host string) error {
|
||||
l.waits = append(l.waits, host)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestClientDo(t *testing.T) {
|
||||
var seen []*http.Request
|
||||
var forms []url.Values
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
seen = append(seen, r)
|
||||
forms = append(forms, r.PostForm)
|
||||
|
||||
if r.URL.Path == "/signin" { http.Redirect(w, r, "/home", http.StatusFound) }
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
limiter := &countingLimiter{}
|
||||
c := newTestClient(t, Options{Limiter: limiter})
|
||||
|
||||
resp, err := c.Do(context.Background(), Request{
|
||||
Method: "POST",
|
||||
URL: srv.URL + "/signin",
|
||||
Form: url.Values{"email": {"me@example.com"}, "password": {"p&ss word"}},
|
||||
Referer: srv.URL + "/ap/signin?x=1",
|
||||
})
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
if resp.FinalURL != srv.URL+"/home" { t.Errorf("final URL %q, want the redirect followed", resp.FinalURL) }
|
||||
if len(seen) != 2 { t.Fatalf("server saw %d requests, want 2", len(seen)) }
|
||||
|
||||
post := seen[0]
|
||||
if post.Method != "POST" || forms[0].Get("password") != "p&ss word" { t.Errorf("first request %s with form %v", post.Method, forms[0]) }
|
||||
|
||||
for name, want := range map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Origin": srv.URL,
|
||||
"Referer": srv.URL + "/ap/signin?x=1",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
"Cache-Control": "max-age=0",
|
||||
} {
|
||||
if got := post.Header.Get(name); got != want { t.Errorf("%s = %q, want %q", name, got, want) }
|
||||
}
|
||||
|
||||
if len(limiter.checks) != 0 || len(limiter.waits) != 1 { t.Errorf("limiter saw checks %v and waits %v, want one host wait", limiter.checks, limiter.waits) }
|
||||
|
||||
// A link from another site sends only that site's origin.
|
||||
if _, err := c.Do(context.Background(), Request{Method: "GET", URL: srv.URL + "/cart", Referer: "https://www.example.com/a/b"}); err != nil { t.Fatal(err) }
|
||||
|
||||
get := seen[len(seen)-1]
|
||||
if got := get.Header.Get("Referer"); got != "https://www.example.com/" { t.Errorf("cross-site referer %q", got) }
|
||||
if got := get.Header.Get("Sec-Fetch-Site"); got != "cross-site" { t.Errorf("cross-site Sec-Fetch-Site %q", got) }
|
||||
if got := get.Header.Get("Origin"); got != "" { t.Errorf("GET sent Origin %q", got) }
|
||||
|
||||
for _, bad := range []Request{
|
||||
{Method: "GET", URL: srv.URL, Form: url.Values{"a": {"b"}}},
|
||||
{Method: "POST", URL: srv.URL, Body: []byte("{}")},
|
||||
{Method: "DELETE", URL: srv.URL},
|
||||
} {
|
||||
if _, err := c.Do(context.Background(), bad); err == nil { t.Errorf("%s with form %v and body %q: want an error", bad.Method, bad.Form, bad.Body) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientBodyLimit(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(strings.Repeat("x", 2048)))
|
||||
|
||||
Reference in new issue
Block a user