package api import ( "encoding/json" "strings" "testing" "time" "git.stinnesbeck.com/nils/allmende/internal/jcs" "git.stinnesbeck.com/nils/allmende/internal/listing" "git.stinnesbeck.com/nils/allmende/internal/pow" "git.stinnesbeck.com/nils/allmende/internal/report" "git.stinnesbeck.com/nils/allmende/internal/store" ) func (f *fixture) found(t *testing.T, query string) int { t.Helper() code, body := f.do(t, "GET", "/api/v1/listings"+query, "", false) var ls []json.RawMessage if code != 200 || json.Unmarshal(body, &ls) != nil { t.Fatalf("search: %d %s", code, body) } return len(ls) } func (f *fixture) summary(t *testing.T, l *listing.Listing) store.ReportSummary { t.Helper() code, body := f.do(t, "GET", "/api/v1/reports?author="+l.Author+"&id="+l.ID, "", false) var s store.ReportSummary if code != 200 || json.Unmarshal(body, &s) != nil { t.Fatalf("summary: %d %s", code, body) } return s } func reportJSON(l *listing.Listing, category, note string) string { b, _ := json.Marshal(map[string]any{ "listing": map[string]string{"author": l.Author, "id": l.ID}, "category": category, "note": note, }) return string(b) } func TestTheOperatorReportsAListingAndItDisappearsFromSearchButNotFromItsLink(t *testing.T) { f := newFixture(t, token) l := f.publish(t) if f.found(t, "") != 1 { t.Fatal("the listing is not found before the report") } if code, _ := f.do(t, "POST", "/api/v1/reports", reportJSON(l, report.Scam, "Vorkasse"), false); code != 401 { t.Fatalf("reporting without the token: %d", code) } if code, body := f.do(t, "POST", "/api/v1/reports", reportJSON(l, "spam", ""), true); code != 400 { t.Fatalf("an unknown category: %d %s", code, body) } code, body := f.do(t, "POST", "/api/v1/reports", reportJSON(l, report.Scam, "Vorkasse"), true) var sum store.ReportSummary if code != 200 || json.Unmarshal(body, &sum) != nil || !sum.Hidden || sum.Own != report.Scam { t.Fatalf("report: %d %s", code, body) } if f.found(t, "") != 0 { t.Fatal("a hidden listing still shows up in search") } if f.found(t, "?hidden=1") != 1 { t.Fatal("hidden=1 must still list it") } if code, _ := f.do(t, "GET", "/api/v1/listings/"+l.Author+"/"+l.ID, "", false); code != 200 { t.Fatalf("a hidden listing must stay reachable by its link: %d", code) } if s := f.summary(t, l); !s.Hidden || s.Trusted[report.Scam] != 1 { t.Fatalf("summary: %+v", s) } // Taking the report back makes it visible again. time.Sleep(1100 * time.Millisecond) // a newer report needs a later second if code, body := f.do(t, "POST", "/api/v1/reports", reportJSON(l, report.Withdrawn, ""), true); code != 200 { t.Fatalf("withdraw: %d %s", code, body) } if f.found(t, "") != 1 || f.summary(t, l).Hidden { t.Fatal("a withdrawn report still hides the listing") } } func TestReportSummaryNeedsAWellFormedListing(t *testing.T) { f := newFixture(t, token) if code, _ := f.do(t, "GET", "/api/v1/reports?author=zz&id=1", "", false); code != 400 { t.Fatalf("bad parameters: %d", code) } } // tip is what a visitor's browser sends. With enough = false it has too little // proof-of-work to count. func tip(t *testing.T, l *listing.Listing, category, note string, enough bool) string { t.Helper() q := signalRequest{Category: category, CreatedAt: time.Now().Unix(), Note: note, Version: 1} q.Listing.Author, q.Listing.ID = l.Author, l.ID for ; ; q.PoW++ { b, err := jcs.Marshal(&q) if err != nil { t.Fatal(err) } if (pow.LeadingZeroBits(b) >= msgDifficulty) == enough { return string(b) } } } // postTo sends a request body without a token and returns the status. func (f *fixture) postTo(t *testing.T, path, body string) int { t.Helper() code, _ := f.do(t, "POST", path, body, false) return code } func TestVisitorsCanTipOffTheOperatorWithoutHidingAnything(t *testing.T) { f := newFixture(t, token) l := f.publish(t) post := func(body string) int { return f.postTo(t, "/api/v1/signals", body) } good := tip(t, l, report.Scam, "Will Vorkasse nach Spanien", true) if code := post(good); code != 202 { t.Fatalf("a tip: %d", code) } if code := post(good); code != 202 { t.Fatalf("the same tip again: %d", code) } if f.found(t, "") != 1 || f.summary(t, l).Hidden { t.Fatal("a tip must never hide a listing") } if s := f.summary(t, l); s.Signals != 1 { t.Fatalf("a tip sent twice counts %d times, want once", s.Signals) } // Only the operator reads them. if code, _ := f.do(t, "GET", "/api/v1/signals", "", false); code != 401 { t.Fatalf("reading tips without the token: %d", code) } var tips []struct { store.Signal Title string `json:"title"` } code, body := f.do(t, "GET", "/api/v1/signals", "", true) if code != 200 || json.Unmarshal(body, &tips) != nil || len(tips) != 1 || tips[0].Category != report.Scam || tips[0].Title != l.Title || tips[0].Note != "Will Vorkasse nach Spanien" { t.Fatalf("tips: %d %s", code, body) } for name, c := range map[string]struct { body string want int }{ "too little work": {tip(t, l, report.Scam, "x", false), 400}, "unknown category": {tip(t, l, "spam", "x", true), 400}, "withdrawn": {tip(t, l, report.Withdrawn, "x", true), 400}, "a note with controls": {tip(t, l, report.Scam, "a\x01b", true), 400}, "a note too long": {tip(t, l, report.Scam, strings.Repeat("a", report.MaxNote+1), true), 400}, "not canonical": {" " + good, 400}, "not JSON": {"hallo", 400}, } { if got := post(c.body); got != c.want { t.Errorf("%s: %d, want %d", name, got, c.want) } } stranger := *l stranger.ID = strings.Repeat("0", 32) if code := post(tip(t, &stranger, report.Scam, "x", true)); code != 404 { t.Fatalf("a listing this node does not know: %d", code) } // The operator dismisses them, or turns them into a report (which clears them). if code, _ := f.do(t, "DELETE", "/api/v1/signals/"+l.Author+"/"+l.ID, "", true); code != 204 { t.Fatalf("dismiss: %d", code) } if f.summary(t, l).Signals != 0 { t.Fatal("a dismissed tip is still counted") } post(tip(t, l, report.Illegal, "anders", true)) f.do(t, "POST", "/api/v1/reports", reportJSON(l, report.Illegal, "bestÃĪtigt"), true) if s := f.summary(t, l); s.Signals != 0 || !s.Hidden { t.Fatalf("after reporting: %+v", s) } }