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
@@ -92,6 +92,7 @@ func TestFetchGivesUp(t *testing.T) {
|
||||
type noLimit struct{}
|
||||
|
||||
func (noLimit) WaitCheck(context.Context, string) error { return nil }
|
||||
func (noLimit) Wait(context.Context, string) error { return nil }
|
||||
|
||||
func TestPipelineEscalates(t *testing.T) {
|
||||
c, jar := newChrome(t)
|
||||
@@ -114,3 +115,59 @@ func TestPipelineEscalates(t *testing.T) {
|
||||
if err != nil { t.Fatal(err) }
|
||||
if resp.Verdict != fetch.OK { t.Errorf("tls-client after Chrome: verdict %s", resp.Verdict) }
|
||||
}
|
||||
|
||||
// TestSignIn has the page sign in the way a user would, by script here, and
|
||||
// checks that the cookies reach the jar when the window's time is up.
|
||||
func TestSignIn(t *testing.T) {
|
||||
c, jar := newChrome(t)
|
||||
c.humanTimeout = 3 * time.Second
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := r.Cookie("guest"); err != nil { t.Error("the jar's cookie did not reach Chrome") }
|
||||
|
||||
http.SetCookie(w, &http.Cookie{Name: "account", Value: "signed-in", Path: "/", HttpOnly: true, Expires: time.Now().Add(time.Hour)})
|
||||
_, _ = io.WriteString(w, `<html><body><script>document.cookie = "later=1; path=/"</script>Signed in</body></html>`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
u, _ := url.Parse(srv.URL + "/")
|
||||
jar.SetCookies(u, []*fhttp.Cookie{{Name: "guest", Value: "1", Path: "/"}})
|
||||
|
||||
n, err := c.signIn(context.Background(), u.String(), true)
|
||||
if err != nil { t.Fatal(err) }
|
||||
if n != 3 { t.Errorf("kept %d cookies, want 3", n) }
|
||||
|
||||
got := map[string]string{}
|
||||
for _, ck := range jar.Cookies(u) { got[ck.Name] = ck.Value }
|
||||
if got["account"] != "signed-in" || got["later"] != "1" { t.Errorf("jar after signing in: %v", got) }
|
||||
}
|
||||
|
||||
func TestClick(t *testing.T) {
|
||||
c, _ := newChrome(t)
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/item":
|
||||
_, _ = io.WriteString(w, `<html><body><form method="post" action="/wrong"><input type="hidden" name="id" value="X">
|
||||
<input type="submit" id="add" name="submit.add" value="Add" formaction="/cart"></form></body></html>`)
|
||||
case "/cart":
|
||||
_ = r.ParseForm()
|
||||
_, _ = fmt.Fprintf(w, "<html><body><p id=added>%s %s</p></body></html>", r.PostForm.Get("id"), r.PostForm.Get("submit.add"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
waited := 0
|
||||
wait := func() error {
|
||||
waited++
|
||||
return nil
|
||||
}
|
||||
|
||||
resp, err := c.Click(context.Background(), srv.URL+"/item", "#add", wait)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
if !strings.Contains(string(resp.Body), "X Add") || resp.FinalURL != srv.URL+"/cart" { t.Errorf("after the click: %s %s", resp.FinalURL, resp.Body) }
|
||||
if waited != 1 { t.Errorf("waited %d times before clicking, want 1", waited) }
|
||||
}
|
||||
Reference in new issue
Block a user