First commit Go counter service
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
|
||||
"migadu_counters/internal/config"
|
||||
"migadu_counters/internal/domains"
|
||||
"migadu_counters/internal/server"
|
||||
"migadu_counters/internal/store"
|
||||
)
|
||||
|
||||
// Date exactly 31 days back: its keys are still alive (TTL refreshed by the
|
||||
// day's last increment, so they live until ~23:59 today) but counterctl claims
|
||||
// it is "more than 31 days ago" and "will read as 0".
|
||||
func TestReviewB_31DayWarningOffByOne(t *testing.T) {
|
||||
f := &fakeAPI{body: sampleResponse()}
|
||||
s := httptest.NewServer(f)
|
||||
defer s.Close()
|
||||
cfg := writeConfig(t, "auth_token = \"t\"\n")
|
||||
code, _, errOut := runCLI(t, noEnv, "--config", cfg, "--url", s.URL, "--address", "me@example.com", "--date", "2026-09-12")
|
||||
t.Logf("now=%s --date 2026-09-12 (31 days back) exit=%d stderr=%q", now, code, errOut)
|
||||
lastIncr := time.Date(2026, 9, 12, 23, 59, 0, 0, time.UTC)
|
||||
t.Logf("a key last incremented at %s expires at %s (still alive now)", lastIncr, lastIncr.Add(store.TTL))
|
||||
if strings.Contains(errOut, "will read as 0") {
|
||||
t.Errorf("warning fires for a date whose keys still exist")
|
||||
}
|
||||
}
|
||||
|
||||
// --url with a trailing slash, against the real API handler.
|
||||
func TestReviewB_URLTrailingSlash(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
st, err := store.New(config.RedisConfig{WriteAddr: mr.Addr(), PoolSize: 2, TimeoutMS: 1000})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db, err := domains.Open("../../test_files/rspamd_domains_db.cdb")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := server.New(server.Deps{AuthToken: "t", Store: st, Domains: func() *domains.DB { return db }, Now: func() time.Time { return now }})
|
||||
s := httptest.NewServer(srv.Handler())
|
||||
defer s.Close()
|
||||
cfg := writeConfig(t, "auth_token = \"t\"\n")
|
||||
for _, u := range []string{s.URL, s.URL + "/", s.URL + "/api"} {
|
||||
code, out, errOut := runCLI(t, noEnv, "--config", cfg, "--url", u, "--address", "a@yagolivas.com")
|
||||
t.Logf("--url %q -> exit %d stdout=%q stderr=%q", u, code, firstLine(out), strings.TrimSpace(errOut))
|
||||
}
|
||||
}
|
||||
|
||||
func firstLine(s string) string {
|
||||
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
||||
return s[:i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user