package main import ( "encoding/json" "net/http" "strings" "testing" ) // Block = no new contact, server-enforced: no fresh DM either way, no group // adds either way, any existing DM closes at once, pushes from the blocked // stop, and unblocking restores possibility (not the conversation). func TestBlock(t *testing.T) { ts, srv := newTestServer(t) tokA, uidA, _ := joinedMember(t, srv, "Alice") tokB, uidB, _ := joinedMember(t, srv, "Bob") // An existing DM, open and working. code, out := modPost(t, ts.URL+"/api/threads", dmBody(tokA, uidA, uidB)) if code != 200 { t.Fatalf("create dm: %d", code) } tid := out["thread_id"].(string) // Alice blocks Bob. if code, _ := modPost(t, ts.URL+"/api/blocks", `{"token":"`+tokA+`","user_id":"`+uidB+`"}`); code != 200 { t.Fatal("block should succeed") } // Her list shows him. resp, err := http.Get(ts.URL + "/api/blocks?token=" + tokA) if err != nil { t.Fatal(err) } var got struct { Blocked []string `json:"blocked"` } _ = json.NewDecoder(resp.Body).Decode(&got) resp.Body.Close() if len(got.Blocked) != 1 || got.Blocked[0] != uidB { t.Fatalf("blocks list: %v", got.Blocked) } // The DM closed with the block. if closed, by := srv.db.dmClosed(tid); !closed || by != uidA { t.Fatalf("block should close the DM, got closed=%v by=%q", closed, by) } if code, _ := modPost(t, ts.URL+"/api/messages?token="+tokB, `{"thread_id":"`+tid+`","blob":"hi"}`); code != 403 { t.Fatal("messages into the blocked DM should bounce") } // Bob can't reopen it, and can't open a "new" DM either (dedupe returns // the existing one, which stays closed — so contact stays cut). if code, _ := modPost(t, ts.URL+"/api/threads/"+tid+"/reopen-dm", `{"token":"`+tokB+`"}`); code != 403 { t.Fatal("blocked party reopening should be 403") } // Alice can't reopen through her own block either — unblock first. if code, _ := modPost(t, ts.URL+"/api/threads/"+tid+"/reopen-dm", `{"token":"`+tokA+`"}`); code != 403 { t.Fatal("blocker reopening without unblocking should be 403") } // Neither can add the other to a group. gidA, err := srv.db.createThread("group", "", []ThreadMemberKey{{UserID: uidA, Wrapped: "w"}}) if err != nil { t.Fatal(err) } if code, _ := modPost(t, ts.URL+"/api/threads/"+gidA+"/members", `{"token":"`+tokA+`","user_id":"`+uidB+`","wrapped":"w"}`); code != 403 { t.Fatal("blocker adding blocked to a group should be 403") } gidB, err := srv.db.createThread("group", "", []ThreadMemberKey{{UserID: uidB, Wrapped: "w"}}) if err != nil { t.Fatal(err) } if code, _ := modPost(t, ts.URL+"/api/threads/"+gidB+"/members", `{"token":"`+tokB+`","user_id":"`+uidA+`","wrapped":"w"}`); code != 403 { t.Fatal("blocked adding blocker to a group should be 403") } // Push fan-out: Alice (who blocks Bob) drops out of Bob's recipient sets. set := map[string]bool{uidA: true} srv.dropBlockingRecipients(uidB, set) if set[uidA] { t.Fatal("a blocker should never be pushed the blocked member's messages") } // Admin still passes (a reported member can't dodge a warning by blocking). tokAdm, uidAdm, _ := joinedMember(t, srv, "Root") if err := srv.db.setUserRole(uidAdm, "admin"); err != nil { t.Fatal(err) } if code, _ := modPost(t, ts.URL+"/api/blocks", `{"token":"`+tokB+`","user_id":"`+uidAdm+`"}`); code != 200 { t.Fatal("blocking the admin records fine") } if code, _ := modPost(t, ts.URL+"/api/threads", dmBody(tokAdm, uidAdm, uidB)); code != 200 { t.Fatal("the admin's DM passes a block (formal notices must land)") } // Unblock: contact becomes possible again, but nothing reopens by itself. if code2, _ := modDelete(t, ts.URL+"/api/blocks/"+uidB, `{"token":"`+tokA+`"}`); code2 != 200 { t.Fatal("unblock should succeed") } if closed, _ := srv.db.dmClosed(tid); !closed { t.Fatal("unblocking must not silently reopen the conversation") } if code, _ := modPost(t, ts.URL+"/api/threads/"+tid+"/reopen-dm", `{"token":"`+tokA+`"}`); code != 200 { t.Fatal("after unblocking, reopening is a normal ask again") } } func modDelete(t *testing.T, url, body string) (int, map[string]any) { t.Helper() req, err := http.NewRequest("DELETE", url, strings.NewReader(body)) if err != nil { t.Fatal(err) } req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatalf("delete %s: %v", url, err) } defer resp.Body.Close() out := map[string]any{} _ = json.NewDecoder(resp.Body).Decode(&out) return resp.StatusCode, out }