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
@@ -0,0 +1,163 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.security-command.org/Overlord/dibs/internal/fetch"
|
||||
)
|
||||
|
||||
// fakeSession answers the requests of testdata/session/shop.lua.
|
||||
type fakeSession struct {
|
||||
mu sync.Mutex
|
||||
requests []fetch.Request
|
||||
signedIn bool
|
||||
prompts []string
|
||||
// prompt, if set, answers prompts instead of "123456".
|
||||
prompt func(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
func (f *fakeSession) Do(_ context.Context, r fetch.Request) (*fetch.Response, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
f.requests = append(f.requests, r)
|
||||
|
||||
body, header := "<p>sign in</p>", http.Header{}
|
||||
|
||||
switch {
|
||||
case r.Method == "POST" && strings.HasSuffix(r.URL, "/signin"):
|
||||
if r.Form.Get("password") == "hunter2" && r.Form.Get("code") == "123456" {
|
||||
f.signedIn = true
|
||||
body = `<p id="result">welcome</p>`
|
||||
}
|
||||
case r.Method == "POST" && r.URL == "https://shop.test/cart" && r.Header["X-Test"] == "yes":
|
||||
header.Set("X-Cart", "1")
|
||||
}
|
||||
|
||||
return fetch.NewResponse(r.URL, r.URL, 200, header, []byte(body)), nil
|
||||
}
|
||||
|
||||
// Click submits the add button's form, as the shop's page would.
|
||||
func (f *fakeSession) Click(ctx context.Context, rawURL, sel string) (*fetch.Response, error) {
|
||||
form := url.Values{"items[0][quantity]": {"1"}, "submit.add-to-cart": {"Add"}}
|
||||
|
||||
return f.Do(ctx, fetch.Request{Method: "POST", URL: strings.TrimSuffix(rawURL, "item") + "cart", Form: form, Header: map[string]string{"X-Test": "yes"}})
|
||||
}
|
||||
|
||||
func (f *fakeSession) Prompt(ctx context.Context, label string, secret bool) (string, error) {
|
||||
f.mu.Lock()
|
||||
f.prompts = append(f.prompts, label)
|
||||
f.mu.Unlock()
|
||||
|
||||
if !secret { return "", errors.New("want a secret prompt") }
|
||||
if f.prompt != nil { return f.prompt(ctx) }
|
||||
|
||||
return "123456", nil
|
||||
}
|
||||
|
||||
func (f *fakeSession) Cookies(string) map[string]string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if f.signedIn { return map[string]string{"session": "yes"} }
|
||||
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
func shopPlugin(t *testing.T, limits Limits) *Plugin {
|
||||
t.Helper()
|
||||
|
||||
path := filepath.Join(testdata, "session", "shop.lua")
|
||||
|
||||
code, err := os.ReadFile(path)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
p, err := Load("shop", path, code, limits, discardLogger())
|
||||
if err != nil { t.Fatal(err) }
|
||||
t.Cleanup(p.Close)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
var shopAccount = Account{Username: "me@example.com", Password: "hunter2", Site: "https://shop.test/", CartURL: "https://shop.test/cart"}
|
||||
|
||||
func TestSessionLogin(t *testing.T) {
|
||||
p := shopPlugin(t, DefaultLimits())
|
||||
if p.SessionWarning != "Automated logins can lock the account." { t.Errorf("session warning %q", p.SessionWarning) }
|
||||
|
||||
s := &fakeSession{}
|
||||
if err := p.Login(context.Background(), s, shopAccount); err != nil { t.Fatal(err) }
|
||||
|
||||
if len(s.prompts) != 1 || s.prompts[0] != "code for me@example.com" { t.Errorf("prompts %q", s.prompts) }
|
||||
if len(s.requests) != 2 { t.Fatalf("%d requests, want 2", len(s.requests)) }
|
||||
if r := s.requests[1]; r.Referer != "https://shop.test/signin" || r.Form.Get("user") != "me@example.com" { t.Errorf("sign-in form %+v", r) }
|
||||
|
||||
wrong := shopAccount
|
||||
wrong.Password = "wrong"
|
||||
if err := p.Login(context.Background(), &fakeSession{}, wrong); err == nil || !strings.Contains(err.Error(), "refused") { t.Errorf("wrong password: %v", err) }
|
||||
}
|
||||
|
||||
func TestSessionAddToCart(t *testing.T) {
|
||||
p := shopPlugin(t, DefaultLimits())
|
||||
|
||||
cart, err := p.AddToCart(context.Background(), &fakeSession{}, shopAccount, "https://shop.test/item/ok")
|
||||
if err != nil || cart != "https://shop.test/cart" { t.Errorf("added: %q, %v", cart, err) }
|
||||
|
||||
if _, err := p.AddToCart(context.Background(), &fakeSession{}, shopAccount, "https://shop.test/item/signedout"); !errors.Is(err, ErrSignedOut) { t.Errorf("signed out: got %v", err) }
|
||||
if _, err := p.AddToCart(context.Background(), &fakeSession{}, shopAccount, "https://shop.test/item/nonsense"); err == nil { t.Error("a string result: want an error") }
|
||||
|
||||
if _, err := p.AddToCart(context.Background(), &fakeSession{}, shopAccount, "https://shop.test/item/click"); err != nil { t.Errorf("click: %v", err) }
|
||||
}
|
||||
|
||||
// A login waiting for the user must not hold up the plugin's checks.
|
||||
func TestSessionCallDoesNotBlockParse(t *testing.T) {
|
||||
p := shopPlugin(t, DefaultLimits())
|
||||
|
||||
asked := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
s := &fakeSession{prompt: func(context.Context) (string, error) {
|
||||
close(asked)
|
||||
<-release
|
||||
return "123456", nil
|
||||
}}
|
||||
|
||||
go func() { _ = p.Login(context.Background(), s, shopAccount) }()
|
||||
|
||||
<-asked
|
||||
defer close(release)
|
||||
|
||||
parsed := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := parse(p, "https://shop.test/item/ok")
|
||||
parsed <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-parsed:
|
||||
if err != nil { t.Fatal(err) }
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("parse waits for the login")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionPromptTimesOut(t *testing.T) {
|
||||
limits := DefaultLimits()
|
||||
limits.SessionTimeout = 100 * time.Millisecond
|
||||
|
||||
p := shopPlugin(t, limits)
|
||||
s := &fakeSession{prompt: func(ctx context.Context) (string, error) {
|
||||
<-ctx.Done()
|
||||
return "", ctx.Err()
|
||||
}}
|
||||
|
||||
if err := p.Login(context.Background(), s, shopAccount); !errors.Is(err, ErrTimeout) { t.Errorf("got %v, want ErrTimeout", err) }
|
||||
}
|
||||
Reference in new issue
Block a user