8058
Ir
irvc / files
irvc / refresh_test.go
3.3 KB
1package main 2 3import ( 4 "net/http" 5 "net/http/httptest" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 "time" 11) 12 13// A card that appears after a message was sent must reach connected clients 14// as an edit event carrying the preview. 15func TestRefreshPreviewBroadcasts(t *testing.T) { 16 page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 17 w.Header().Set("Content-Type", "text/html") 18 w.Write([]byte(`<head><meta property="og:title" content="Hi"></head>`)) 19 })) 20 defer page.Close() 21 22 dir := t.TempDir() 23 db := OpenDB(filepath.Join(dir, "t.db")) 24 s := &Server{db: db, hub: newHub(db), previews: newPreviewer(dir, true)} 25 db.Exec(`INSERT INTO users (id, username, pass_hash, created_at) VALUES (1, 'a', 'x', 0)`) 26 var ch int64 27 db.QueryRow(`SELECT id FROM channels WHERE type = 'text' LIMIT 1`).Scan(&ch) 28 res, _ := db.Exec(`INSERT INTO messages (channel_id, user_id, body, created_at) VALUES (?, 1, ?, 0)`, ch, "see "+page.URL) 29 id, _ := res.LastInsertId() 30 viewer := testClient(s.hub, 9, "viewer") 31 32 s.refreshPreview(id) 33 34 select { 35 case data := <-viewer.send: 36 if ev := string(data); !strings.Contains(ev, `"message_edit"`) || !strings.Contains(ev, `"kind":"card"`) { 37 t.Fatalf("unexpected event %s", ev) 38 } 39 case <-time.After(time.Second): 40 t.Fatal("no event broadcast for the new card") 41 } 42} 43 44// Only the author can remove a card; removal sticks (the same link is not 45// fetched again), deletes the cached thumbnail, and a slow fetch that finishes 46// afterwards cannot bring the card back. 47func TestRemovePreview(t *testing.T) { 48 hits := 0 49 page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 50 hits++ 51 w.Header().Set("Content-Type", "text/html") 52 w.Write([]byte(`<head><meta property="og:title" content="Hi"></head>`)) 53 })) 54 defer page.Close() 55 56 dir := t.TempDir() 57 db := OpenDB(filepath.Join(dir, "t.db")) 58 s := &Server{cfg: &Config{MediaDir: dir}, db: db, hub: newHub(db), previews: newPreviewer(dir, true)} 59 db.Exec(`INSERT INTO users (id, username, pass_hash, created_at) VALUES (1, 'a', 'x', 0), (2, 'b', 'x', 0)`) 60 var ch int64 61 db.QueryRow(`SELECT id FROM channels WHERE type = 'text' LIMIT 1`).Scan(&ch) 62 res, _ := db.Exec(`INSERT INTO messages (channel_id, user_id, body, created_at) VALUES (?, 1, ?, 0)`, ch, "see "+page.URL) 63 id, _ := res.LastInsertId() 64 65 s.refreshPreview(id) 66 if p := s.messagePreview(id); p == nil || p.Kind != kindCard { 67 t.Fatalf("expected a card, got %+v", p) 68 } 69 70 thumb := "thumb.png" 71 os.WriteFile(filepath.Join(dir, thumb), []byte("x"), 0o644) 72 db.Exec(`UPDATE messages SET preview = ? WHERE id = ?`, `{"url":"`+page.URL+`","kind":"card","title":"Hi","image":"/api/media/`+thumb+`"}`, id) 73 74 if _, err := s.removePreview(id, 2); err == nil { 75 t.Fatal("another user must not be able to remove the card") 76 } 77 if _, err := s.removePreview(id, 1); err != nil { 78 t.Fatal(err) 79 } 80 if p := s.messagePreview(id); p == nil || p.Kind != kindNone || p.URL != page.URL { 81 t.Fatalf("removed card should be remembered as none, got %+v", p) 82 } 83 if _, err := os.Stat(filepath.Join(dir, thumb)); err == nil { 84 t.Fatal("cached thumbnail should be deleted") 85 } 86 87 before := hits 88 s.refreshPreview(id) 89 if hits != before || s.messagePreview(id).Kind != kindNone { 90 t.Fatal("the removed link must not be fetched or restored") 91 } 92}