Files
OverlordandClaude Bot 15a34e1f84 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>
2026-10-14 17:26:26 +02:00

96 lines
3.4 KiB
Go

package notify
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"git.security-command.org/Overlord/dibs/internal/alert"
"git.security-command.org/Overlord/dibs/internal/config"
)
// TestRetries sends one alert to a webhook that fails with each status
// until the last attempt.
func TestRetries(t *testing.T) {
price := 129.99
a := alert.Alert{Item: "metro", Shop: "amazon", URL: "https://www.amazon.de/dp/B0HMX2FSTF", Price: &price, Currency: "EUR", Time: time.Now()}
cases := []struct {
status int
requests int32
ok bool
} {
{http.StatusServiceUnavailable, retries + 1, false},
{http.StatusTooManyRequests, retries + 1, false},
{http.StatusNotFound, 1, false},
{http.StatusNoContent, 1, true},
}
for _, c := range cases {
var requests atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests.Add(1)
w.WriteHeader(c.status)
}))
// The path stands in for a webhook token.
n := New(config.Notify{Webhooks: []config.Webhook{{URL: srv.URL + "/secret-token", Format: config.FormatJSON}}}, "dibs/test")
var waited time.Duration
n.wait = func(_ context.Context, d time.Duration) error {
waited += d
return nil
}
err := n.Send(context.Background(), a)
srv.Close()
if got := requests.Load(); got != c.requests { t.Errorf("%d: %d requests, want %d", c.status, got, c.requests) }
if (err == nil) != c.ok { t.Errorf("%d: err = %v", c.status, err) }
if err != nil && strings.Contains(err.Error(), "secret-token") { t.Errorf("%d: error shows the webhook URL: %v", c.status, err) }
if c.requests > 1 && waited != 7*time.Second { t.Errorf("%d: waited %s between attempts, want 7s", c.status, waited) }
}
}
// TestCartSteps checks where each session-mode notification leads. While
// the item is being added, nothing may link to the cart link, which would
// add a second copy.
func TestCartSteps(t *testing.T) {
const (
page = "https://www.amazon.de/dp/B0HMX2FSTF"
addLink = "https://www.amazon.de/gp/aws/cart/add.html?ASIN.1=B0HMX2FSTF&Quantity.1=1"
cart = "https://www.amazon.de/gp/cart/view.html"
)
base := alert.Alert{Item: "metro", Shop: "amazon", URL: page, CartLink: addLink, Time: time.Now()}
cases := []struct {
cart, cartURL, cartErr string
headline, link string
} {
{"", "", "", "In stock: metro", addLink},
{alert.CartAdding, "", "", "In stock: metro, adding to cart", page},
{alert.CartAdded, cart, "", "In cart: metro, finish checkout", cart},
{alert.CartFailed, "", "timeout", "In stock: metro, adding to cart failed", addLink},
}
for _, tc := range cases {
a := base
a.Cart, a.CartURL, a.CartError = tc.cart, tc.cartURL, tc.cartErr
if got := headline(a); got != tc.headline { t.Errorf("%q: headline %q, want %q", tc.cart, got, tc.headline) }
if got := link(a); got != tc.link { t.Errorf("%q: link %q, want %q", tc.cart, got, tc.link) }
msg := ntfyMessage(a)
desc := discordPayload(a).Embeds[0].Description
if hasAdd := strings.Contains(msg, addLink) || strings.Contains(desc, addLink); hasAdd != (tc.link == addLink) { t.Errorf("%q: cart link in ntfy %v or discord %q", tc.cart, strings.Contains(msg, addLink), desc) }
if tc.cartErr != "" && !strings.Contains(msg, tc.cartErr) { t.Errorf("%q: ntfy message without the error: %q", tc.cart, msg) }
}
}