mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Every function and method, tests included, has a doc comment: what it is or does, what it returns and anything a caller needs to know. Only comments are added; gofmt split a few aligned blocks of one-line functions once each had its own comment. The generated protobuf code is left as protoc writes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
559 lines
18 KiB
Go
559 lines
18 KiB
Go
package store
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// open is a fresh store: SQLite here, or the engine of BBS_TEST_DATABASE.
|
|
func open(t *testing.T) *Store {
|
|
t.Helper()
|
|
BcryptCost = 4
|
|
s, err := Open(filepath.Join(t.TempDir(), "bbs.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
return s
|
|
}
|
|
|
|
// TestEngine: what the engines do differently, done alike.
|
|
func TestEngine(t *testing.T) {
|
|
s := open(t)
|
|
t.Logf("engine %s", s.Engine())
|
|
|
|
// names: any case finds one; a name in another case is taken; accents
|
|
// are not case
|
|
u, err := s.CreateUser("Alice", "alicepass1", "Here", false)
|
|
if err != nil || u.ID == 0 || u.Name != "Alice" || u.Level != 20 || u.Sysop {
|
|
t.Fatalf("create: %+v %v", u, err)
|
|
}
|
|
if got, err := s.UserByName("aLiCe"); err != nil || got.ID != u.ID {
|
|
t.Fatalf("by name in another case: %v %v", got, err)
|
|
}
|
|
if _, err := s.CreateUser("ALICE", "x", "", false); !errors.Is(err, ErrExists) {
|
|
t.Fatalf("ALICE again: %v", err)
|
|
}
|
|
r1, err1 := s.CreateUser("Rene", "renepass1", "", false)
|
|
r2, err2 := s.CreateUser("René", "renepass1", "", false)
|
|
if strings.Contains(s.Engine(), "accents ignored") {
|
|
// MariaDB before 10.10 has no collation that keeps accents but not case
|
|
if err1 != nil || !errors.Is(err2, ErrExists) {
|
|
t.Fatalf("Rene and René: %v %v", err1, err2)
|
|
}
|
|
r2, _ = s.CreateUser("Renée", "renepass1", "", false)
|
|
} else if err1 != nil || err2 != nil || r1.ID == r2.ID {
|
|
t.Fatalf("Rene and René: %v %v", err1, err2)
|
|
}
|
|
if got, _ := s.UserByName(strings.ToLower(r2.Name)); got == nil || got.ID != r2.ID {
|
|
// SQLite's NOCASE folds ASCII only: René is found as written
|
|
if s.Engine() != SQLite {
|
|
t.Errorf("rené: %v", got)
|
|
}
|
|
}
|
|
sysop, _ := s.CreateUser("sysop", "sysoppass1", "", true)
|
|
if !sysop.Sysop || sysop.ID <= u.ID {
|
|
t.Errorf("sysop: %+v", sysop)
|
|
}
|
|
// booleans both ways
|
|
if err := s.SetLocked(u.ID, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := s.UserByID(u.ID); !got.Locked {
|
|
t.Error("not locked")
|
|
}
|
|
s.SetLocked(u.ID, false)
|
|
if w, err := s.UsersBelow(30); err != nil || len(w) != 3 {
|
|
t.Errorf("below 30: %d %v", len(w), err)
|
|
}
|
|
// an update to the same value is no "not found"
|
|
if err := s.SetLevel(u.ID, 20); err != nil {
|
|
t.Errorf("same level: %v", err)
|
|
}
|
|
if err := s.SetLevel(99999, 20); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("no user: %v", err)
|
|
}
|
|
|
|
// areas, messages, ids, blobs, long text
|
|
a, err := s.CreateArea("General", "Talk", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.CreateArea("general", "", false); !errors.Is(err, ErrExists) {
|
|
t.Errorf("area again: %v", err)
|
|
}
|
|
raw := []byte{0, 1, 2, 0xff, 0xfe, '\n', 0x80}
|
|
body := strings.Repeat("A line of a long message, with ünïcødé and 'quotes' and ? marks.\n", 2000)
|
|
id1, err := s.Post(&Message{AreaID: a.ID, FromID: u.ID, From: u.Name, To: "All", Subject: "First?",
|
|
Body: body, MessageID: "<1@test>", Raw: raw})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id2, err := s.Post(&Message{AreaID: a.ID, FromID: u.ID, From: u.Name, To: "All", Subject: "Second", Body: "b"})
|
|
if err != nil || id2 <= id1 {
|
|
t.Fatalf("second: %d %v", id2, err)
|
|
}
|
|
if _, err := s.Post(&Message{AreaID: a.ID, From: "x", Subject: "dup", Body: "b", MessageID: "<1@test>"}); !errors.Is(err, ErrExists) {
|
|
t.Errorf("a Message-ID twice: %v", err)
|
|
}
|
|
m, err := s.Message(id1)
|
|
if err != nil || m.Body != body || m.Subject != "First?" || m.MessageID != "<1@test>" {
|
|
t.Fatalf("message: %v", err)
|
|
}
|
|
if got, err := s.MessageByMessageID("<1@test>"); err != nil || got.ID != id1 {
|
|
t.Errorf("by Message-ID: %v", err)
|
|
}
|
|
if r, err := s.Raw(id1); err != nil || !bytes.Equal(r, raw) {
|
|
t.Errorf("raw: %x %v", r, err)
|
|
}
|
|
if seen, _ := s.Seen("<1@test>"); !seen {
|
|
t.Error("not in the history")
|
|
}
|
|
if err := s.Remember("<1@test>"); err != nil {
|
|
t.Errorf("remember again: %v", err)
|
|
}
|
|
if got, _ := s.UserByID(u.ID); got.Posts != 2 {
|
|
t.Errorf("posts %d", got.Posts)
|
|
}
|
|
// the last read only goes forward (upsert, greatest)
|
|
for _, id := range []int64{id2, id1} {
|
|
if err := s.SetLastRead(u.ID, a.ID, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if lr, _ := s.LastRead(u.ID, a.ID); lr != id2 {
|
|
t.Errorf("last read %d, want %d", lr, id2)
|
|
}
|
|
if n, _ := s.NewCount(u.ID, a.ID); n != 0 {
|
|
t.Errorf("new %d", n)
|
|
}
|
|
// newsgroups, peers (upserts)
|
|
if err := s.SetNewsgroup(a.ID, "bbs.general"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := s.CreateArea("Other", "", false)
|
|
if err := s.SetNewsgroup(b.ID, "bbs.general"); !errors.Is(err, ErrExists) {
|
|
t.Errorf("a newsgroup twice: %v", err)
|
|
}
|
|
s.SetNewsgroup(b.ID, "") // NULLs do not clash
|
|
c, _ := s.CreateArea("Third", "", false)
|
|
if err := s.SetNewsgroup(c.ID, ""); err != nil {
|
|
t.Errorf("two areas with no newsgroup: %v", err)
|
|
}
|
|
for _, last := range []int64{5, 9} {
|
|
if err := s.SetPeerLast("peer", "bbs.general", last); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SetPeerPushed("peer", last); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if n, ok, _ := s.PeerLast("peer", "bbs.general"); !ok || n != 9 {
|
|
t.Errorf("peer last %d %v", n, ok)
|
|
}
|
|
if n, ok, _ := s.PeerPushed("peer"); !ok || n != 9 {
|
|
t.Errorf("peer pushed %d %v", n, ok)
|
|
}
|
|
|
|
// mail, "read"
|
|
if _, err := s.SendMail(&Message{FromID: sysop.ID, From: "sysop", ToID: u.ID, To: u.Name, Subject: "hi", Body: "b"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := s.UnreadMail(u.ID); n != 1 {
|
|
t.Errorf("unread %d", n)
|
|
}
|
|
inbox, _ := s.Inbox(u.ID)
|
|
if err := s.MarkMailRead(u.ID, inbox[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := s.UnreadMail(u.ID); n != 0 {
|
|
t.Errorf("unread after reading %d", n)
|
|
}
|
|
|
|
// tokens and keys ("key")
|
|
if _, err := s.AddToken(&Token{UserID: u.ID, Name: "t", Hash: "h1", Scopes: "read", Session: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tk, err := s.TokenByHash("h1"); err != nil || !tk.Session || tk.User != "Alice" {
|
|
t.Errorf("token: %+v %v", tk, err)
|
|
}
|
|
k := &SSHKey{UserID: u.ID, Type: "ssh-ed25519", Fingerprint: "SHA256:x", Key: "ssh-ed25519 AAAA c"}
|
|
if _, err := s.AddSSHKey(k); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := s.SSHKeyByFingerprint("SHA256:x"); err != nil || got.Key != k.Key {
|
|
t.Errorf("key: %+v %v", got, err)
|
|
}
|
|
|
|
// file areas and files, the day's counts
|
|
fa, err := s.CreateFileArea("Uploads", "", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fid, err := s.AddFile(&File{AreaID: fa.ID, Name: "a.zip", Size: 10, SHA256: "ab", UploaderID: u.ID, Uploader: u.Name})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.AddFile(&File{AreaID: fa.ID, Name: "A.ZIP", Size: 1, SHA256: "cd", UploaderID: u.ID, Uploader: u.Name}); !errors.Is(err, ErrExists) {
|
|
t.Errorf("a file name twice: %v", err)
|
|
}
|
|
s.CountDownload(fid)
|
|
s.CountUserDownload(u.ID)
|
|
s.AddTime(u.ID, 90)
|
|
s.AddTime(u.ID, 30)
|
|
if got, _ := s.UserByID(u.ID); got.DownloadsToday() != 1 || got.TimeToday() != 120 {
|
|
t.Errorf("today: %d downloads, %d s", got.DownloadsToday(), got.TimeToday())
|
|
}
|
|
if fas, _ := s.FileAreas(); len(fas) != 1 || fas[0].Files != 1 || fas[0].Bytes != 10 {
|
|
t.Errorf("file areas: %+v", fas[0])
|
|
}
|
|
|
|
// netmail
|
|
if _, err := s.QueueNetmail(&Netmail{FromID: u.ID, From: u.Name, To: "Bob", ToAddr: "1:2/3",
|
|
Subject: "s", Body: "b", MsgID: "1:2/4 1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// deleting a user takes their mail, tokens and keys with them
|
|
if err := s.DeleteUser(u.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ts, _ := s.Tokens(u.ID); len(ts) != 0 {
|
|
t.Errorf("tokens left: %d", len(ts))
|
|
}
|
|
if _, err := s.SSHKeyByFingerprint("SHA256:x"); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("key left: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestCopy: a database copied into another (SQLite into the test engine,
|
|
// or into SQLite again): everything there, and ids going on after.
|
|
func TestCopy(t *testing.T) {
|
|
src, err := Open("sqlite:" + filepath.Join(t.TempDir(), "src.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer src.Close()
|
|
u, _ := src.CreateUser("Alice", "alicepass1", "Here", true)
|
|
a, _ := src.CreateArea("General", "", false)
|
|
src.Post(&Message{AreaID: a.ID, FromID: u.ID, From: "Alice", Subject: "s", Body: "b", MessageID: "<m@x>", Raw: []byte{0, 0xff}})
|
|
src.SendMail(&Message{FromID: u.ID, From: "Alice", ToID: u.ID, To: "Alice", Subject: "s", Body: "b"})
|
|
src.AddToken(&Token{UserID: u.ID, Name: "t", Hash: "h", Scopes: "read", Session: true})
|
|
src.SetPeerLast("p", "g", 3)
|
|
|
|
dst := open(t)
|
|
var tables []string
|
|
if err := Copy(src, dst, func(table string, n int) { tables = append(tables, fmt.Sprintf("%s %d", table, n)) }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(strings.Join(tables, ","), "users 1,areas 1,messages 1") {
|
|
t.Errorf("copied: %v", tables)
|
|
}
|
|
got, err := dst.UserByName("alice")
|
|
if err != nil || !got.Sysop || got.Location != "Here" {
|
|
t.Fatalf("user: %+v %v", got, err)
|
|
}
|
|
if _, err := dst.CheckPassword("Alice", "alicepass1"); err != nil {
|
|
t.Errorf("password: %v", err)
|
|
}
|
|
m, _ := dst.MessageByMessageID("<m@x>")
|
|
if raw, _ := dst.Raw(m.ID); !bytes.Equal(raw, []byte{0, 0xff}) {
|
|
t.Errorf("raw %x", raw)
|
|
}
|
|
if tk, _ := dst.TokenByHash("h"); tk == nil || !tk.Session {
|
|
t.Errorf("token %+v", tk)
|
|
}
|
|
// new rows get new ids
|
|
b, err := dst.CreateUser("Bob", "bobpass123", "", false)
|
|
if err != nil || b.ID <= got.ID {
|
|
t.Errorf("a new user after the copy: %+v %v", b, err)
|
|
}
|
|
if err := Copy(src, dst, nil); err == nil {
|
|
t.Error("copied into a database with users")
|
|
}
|
|
}
|
|
|
|
// TestScanAndSearch: an area left out of a user's new scan, once however
|
|
// often, and put back; messages to a name in any case; searches in any case,
|
|
// in the areas given only, with % and _ taken as themselves.
|
|
func TestScanAndSearch(t *testing.T) {
|
|
s := open(t)
|
|
u, _ := s.CreateUser("alice", "password1", "", false)
|
|
a, _ := s.CreateArea("General", "", false)
|
|
b, _ := s.CreateArea("Other", "", false)
|
|
post := func(area int64, to, subject, body string) int64 {
|
|
id, err := s.Post(&Message{AreaID: area, FromID: u.ID, From: "bob", To: to, Subject: subject, Body: body})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|
|
post(a.ID, "Alice", "Hello", "about 100% of it")
|
|
post(b.ID, "All", "Weather", "rain_today and snow")
|
|
post(a.ID, "alice", "Again", "x")
|
|
|
|
if err := s.SetScan(u.ID, b.ID, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.SetScan(u.ID, b.ID, false) // twice is once
|
|
off, err := s.ScanOff(u.ID)
|
|
if err != nil || len(off) != 1 || !off[b.ID] {
|
|
t.Fatalf("off %v %v", off, err)
|
|
}
|
|
s.SetScan(u.ID, b.ID, true)
|
|
if off, _ := s.ScanOff(u.ID); len(off) != 0 {
|
|
t.Errorf("still off %v", off)
|
|
}
|
|
|
|
to, err := s.MessagesTo("ALICE", 10)
|
|
if err != nil || len(to) != 2 || to[0].Subject != "Again" {
|
|
t.Fatalf("to alice: %v %v", to, err)
|
|
}
|
|
find := func(areas []int64, text string) []string {
|
|
ms, err := s.SearchMessages(areas, text, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, m := range ms {
|
|
out = append(out, m.Subject)
|
|
}
|
|
return out
|
|
}
|
|
for _, c := range []struct {
|
|
areas []int64
|
|
text string
|
|
want string
|
|
}{{[]int64{a.ID, b.ID}, "RAIN", "Weather"}, {[]int64{a.ID}, "rain", ""},
|
|
{[]int64{a.ID, b.ID}, "100%", "Hello"}, {[]int64{a.ID, b.ID}, "0%", "Hello"},
|
|
{[]int64{a.ID, b.ID}, "n_t", "Weather"}, {[]int64{a.ID, b.ID}, "n t", ""},
|
|
{[]int64{a.ID, b.ID}, "BOB", "Again Weather Hello"}, {nil, "x", ""}} {
|
|
if got := strings.Join(find(c.areas, c.text), " "); got != c.want {
|
|
t.Errorf("%q in %v: %q", c.text, c.areas, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestFilesFound: files found by name, description or long description in
|
|
// the areas given, and by when they came; a user's uploads and downloads
|
|
// counted.
|
|
func TestFilesFound(t *testing.T) {
|
|
s := open(t)
|
|
u, _ := s.CreateUser("alice", "password1", "", false)
|
|
a, _ := s.CreateFileArea("Games", "", false)
|
|
b, _ := s.CreateFileArea("Hidden", "", false)
|
|
for _, f := range []*File{{AreaID: a.ID, Name: "doom.zip", Description: "Shooter", LongDesc: "DOOM\nby id 100%"},
|
|
{AreaID: a.ID, Name: "zork.zip", Description: "Adventure"},
|
|
{AreaID: b.ID, Name: "secret.zip", Description: "Shooter too"}} {
|
|
f.UploaderID, f.Uploader, f.SHA256, f.Size = u.ID, u.Name, "x", 1
|
|
if _, err := s.AddFile(f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
names := func(fs []*File, err error) string {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, f := range fs {
|
|
out = append(out, f.Name)
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
if got := names(s.SearchFiles([]int64{a.ID}, "SHOOT", 10)); got != "doom.zip" {
|
|
t.Errorf("by description %q", got)
|
|
}
|
|
if got := names(s.SearchFiles([]int64{a.ID, b.ID}, "0%", 10)); got != "doom.zip" {
|
|
t.Errorf("by the long one %q", got)
|
|
}
|
|
if got := names(s.SearchFiles([]int64{a.ID}, "zork", 10)); got != "zork.zip" {
|
|
t.Errorf("by name %q", got)
|
|
}
|
|
if got := names(s.FilesSince([]int64{a.ID}, time.Now().Add(-time.Hour), 10)); got != "zork.zip doom.zip" {
|
|
t.Errorf("since %q", got)
|
|
}
|
|
if got := names(s.FilesSince([]int64{a.ID}, time.Now().Add(time.Hour), 10)); got != "" {
|
|
t.Errorf("since later %q", got)
|
|
}
|
|
s.CountUserDownload(u.ID)
|
|
s.CountUserDownload(u.ID)
|
|
if u, _ = s.UserByID(u.ID); u.Uploads != 3 || u.Downloads != 2 || u.DownloadsToday() != 2 {
|
|
t.Errorf("counts %d %d %d", u.Uploads, u.Downloads, u.DownloadsToday())
|
|
}
|
|
f, _ := s.FileByName(a.ID, "doom.zip")
|
|
if f.LongDesc != "DOOM\nby id 100%" {
|
|
t.Errorf("long %q", f.LongDesc)
|
|
}
|
|
}
|
|
|
|
// TestScriptData: a script's values, its own and each user's apart from
|
|
// another script's; names listed by prefix (_ taken as itself) and limit;
|
|
// counted, deleted, and a deleted user's gone with them.
|
|
func TestScriptData(t *testing.T) {
|
|
s := open(t)
|
|
u, _ := s.CreateUser("alice", "password1", "", false)
|
|
if _, ok, err := s.ScriptGet("one", 0, "x"); ok || err != nil {
|
|
t.Fatalf("none: %v %v", ok, err)
|
|
}
|
|
for _, kv := range [][2]string{{"line_1", "a"}, {"line_2", "b"}, {"line%3", "c"}, {"other", "d"}, {"line_1", "A"}} {
|
|
if err := s.ScriptSet("one", 0, kv[0], kv[1]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
s.ScriptSet("one", u.ID, "line_1", "mine")
|
|
s.ScriptSet("two", 0, "line_1", "theirs")
|
|
if v, ok, _ := s.ScriptGet("one", 0, "line_1"); !ok || v != "A" {
|
|
t.Errorf("get %q", v)
|
|
}
|
|
if v, _, _ := s.ScriptGet("one", u.ID, "line_1"); v != "mine" {
|
|
t.Errorf("the user's %q", v)
|
|
}
|
|
names, err := s.ScriptNames("one", 0, "line_", 10)
|
|
if err != nil || strings.Join(names, " ") != "line_1 line_2" {
|
|
t.Errorf("names %v %v", names, err)
|
|
}
|
|
if names, _ := s.ScriptNames("one", 0, "", 2); len(names) != 2 {
|
|
t.Errorf("limit %v", names)
|
|
}
|
|
s.ScriptDelete("one", 0, "other")
|
|
if n, _ := s.ScriptCount("one", 0); n != 3 {
|
|
t.Errorf("count %d", n)
|
|
}
|
|
s.DeleteUser(u.ID)
|
|
if n, _ := s.ScriptCount("one", u.ID); n != 0 {
|
|
t.Errorf("a deleted user's: %d", n)
|
|
}
|
|
}
|
|
|
|
// TestMessageBaseDepth: a thread in order with its depths; votes, a vote
|
|
// changed and taken back; a poll; aliases and signatures, an alias not
|
|
// another's name or alias; area groups, and keeping by count and by age.
|
|
func TestMessageBaseDepth(t *testing.T) {
|
|
s := open(t)
|
|
u, _ := s.CreateUser("alice", "password1", "", false)
|
|
b, _ := s.CreateUser("bob", "password1", "", false)
|
|
a, _ := s.CreateArea("General", "", false)
|
|
post := func(subject string, reply int64, ago time.Duration) int64 {
|
|
id, err := s.Post(&Message{AreaID: a.ID, FromID: u.ID, From: "alice", To: "All", Subject: subject, Body: "x", ReplyTo: reply})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ago > 0 {
|
|
s.db.Exec(`UPDATE messages SET created = ? WHERE id = ?`, time.Now().Add(-ago).Unix(), id)
|
|
}
|
|
return id
|
|
}
|
|
root := post("Start", 0, 0)
|
|
r1 := post("Re: Start", root, 0)
|
|
post("Other", 0, 0)
|
|
r2 := post("Re: Re: Start", r1, 0)
|
|
r3 := post("Re: Start again", root, 0)
|
|
m, _ := s.Message(r2)
|
|
thread, depth, err := s.Thread(m)
|
|
var got []string
|
|
for i, m := range thread {
|
|
got = append(got, fmt.Sprintf("%d:%s", depth[i], m.Subject))
|
|
}
|
|
if err != nil || strings.Join(got, ",") != "0:Start,1:Re: Start,2:Re: Re: Start,1:Re: Start again" {
|
|
t.Errorf("thread %v %v", got, err)
|
|
}
|
|
_ = r3
|
|
|
|
// votes
|
|
s.Vote(root, u.ID, 1)
|
|
s.Vote(root, b.ID, -1)
|
|
s.Vote(root, b.ID, 1)
|
|
if up, down, mine, err := s.Votes(root, b.ID); up != 2 || down != 0 || mine != 1 || err != nil {
|
|
t.Errorf("votes %d %d %d %v", up, down, mine, err)
|
|
}
|
|
s.Vote(root, b.ID, 0)
|
|
if up, _, mine, _ := s.Votes(root, b.ID); up != 1 || mine != 0 {
|
|
t.Errorf("unvoted %d %d", up, mine)
|
|
}
|
|
|
|
// a poll
|
|
p := post("Tea or coffee?", 0, 0)
|
|
if ans, _, _, _ := s.Poll(root, u.ID); ans != nil {
|
|
t.Error("not a poll")
|
|
}
|
|
s.SetPoll(p, []string{"Tea", "Coffee"})
|
|
s.PollVote(p, u.ID, 1)
|
|
s.PollVote(p, b.ID, 0)
|
|
s.PollVote(p, b.ID, 1)
|
|
if ans, counts, mine, err := s.Poll(p, b.ID); len(ans) != 2 || counts[0] != 0 || counts[1] != 2 || mine != 1 || err != nil {
|
|
t.Errorf("poll %v %v %d %v", ans, counts, mine, err)
|
|
}
|
|
|
|
// aliases and signatures
|
|
if err := s.SetAlias(u.ID, "Bob"); err != ErrExists {
|
|
t.Errorf("another's name: %v", err)
|
|
}
|
|
if err := s.SetAlias(u.ID, "Night Owl"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SetAlias(b.ID, "night owl"); err != ErrExists {
|
|
t.Errorf("another's alias: %v", err)
|
|
}
|
|
s.SetSignature(u.ID, "-- alice\nat the keys")
|
|
if x, _ := s.UserByID(u.ID); x.Alias != "Night Owl" || x.Signature != "-- alice\nat the keys" {
|
|
t.Errorf("user %+v", x)
|
|
}
|
|
|
|
// groups and keeping
|
|
s.SetAreaGroup(a.ID, "Local")
|
|
s.SetAreaAliases(a.ID, true)
|
|
s.SetAreaKeep(a.ID, 3, 0)
|
|
a, _ = s.Area(a.ID)
|
|
if a.Group != "Local" || !a.Aliases || a.MaxMsgs != 3 {
|
|
t.Errorf("area %+v", a)
|
|
}
|
|
n, err := s.Purge(a)
|
|
if err != nil || n != 3 || a.Messages != 6 {
|
|
t.Errorf("purged %d %v (of %d)", n, err, a.Messages)
|
|
}
|
|
if _, err := s.Message(root); err != ErrNotFound {
|
|
t.Error("the oldest kept")
|
|
}
|
|
old := post("Old", 0, 40*24*time.Hour)
|
|
s.SetAreaKeep(a.ID, 0, 30)
|
|
a, _ = s.Area(a.ID)
|
|
if n, _ := s.Purge(a); n != 1 {
|
|
t.Errorf("by age: %d", n)
|
|
}
|
|
if _, err := s.Message(old); err != ErrNotFound {
|
|
t.Error("an old one kept")
|
|
}
|
|
}
|
|
|
|
// TestSnapshot: a database (whatever its engine: open's) written whole
|
|
// into a SQLite file, which opens as the same; not over a file there.
|
|
func TestSnapshot(t *testing.T) {
|
|
src := open(t)
|
|
u, _ := src.CreateUser("Alice", "alicepass1", "Here", true)
|
|
a, _ := src.CreateArea("General", "", false)
|
|
src.Post(&Message{AreaID: a.ID, FromID: u.ID, From: "Alice", Subject: "s", Body: "b", MessageID: "<m@x>"})
|
|
p := filepath.Join(t.TempDir(), "snap.db")
|
|
if err := src.Snapshot(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := src.Snapshot(p); err == nil {
|
|
t.Error("written over a file there")
|
|
}
|
|
snap, err := Open("sqlite:" + p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer snap.Close()
|
|
if _, err := snap.CheckPassword("Alice", "alicepass1"); err != nil {
|
|
t.Errorf("password: %v", err)
|
|
}
|
|
if m, err := snap.MessageByMessageID("<m@x>"); err != nil || m.Body != "b" {
|
|
t.Errorf("message: %+v %v", m, err)
|
|
}
|
|
}
|