Add alerts, currency conversion and notifications.
Items fire when a page is available from an accepted seller at or below max_price, converted with the ECB's daily rates when the page uses another currency. A per-item cooldown, kept in the state file, holds back repeat alerts unless the price drops. Alerts go to Discord, ntfy and JSON webhooks with retries, and to the desktop; 'dibs notify test' sends a sample.
This commit is contained in:
1 parent
2175c0e5aa
commit
8f0eda5d4d
16 files changed
+1409
-15
No files matched your search
@@ -0,0 +1,58 @@
|
||||
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) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user