mox/store/import_test.go
Mechiel Lukkien 5336032088
add funtionality to import zip/tgz with maildirs/mboxes to account page
so users can easily take their email out of somewhere else, and import it into mox.

this goes a little way to give feedback as the import progresses: upload
progress is shown (surprisingly, browsers aren't doing this...), imported
mailboxes/messages are counted (batched) and import issues/warnings are
displayed, all sent over an SSE connection. an import token is stored in
sessionstorage. if you reload the page (e.g. after a connection error), the
browser will reconnect to the running import and show its progress again. and
you can just abort the import before it is finished and committed, and nothing
will have changed.

this also imports flags/keywords from mbox files.
2023-02-16 09:57:27 +01:00

98 lines
2.3 KiB
Go

package store
import (
"io"
"os"
"strings"
"testing"
"github.com/mjl-/mox/mlog"
)
func TestMboxReader(t *testing.T) {
createTemp := func(pattern string) (*os.File, error) {
return os.CreateTemp("", pattern)
}
mboxf, err := os.Open("../testdata/importtest.mbox")
if err != nil {
t.Fatalf("open mbox: %v", err)
}
defer mboxf.Close()
mr := NewMboxReader(createTemp, mboxf.Name(), mboxf, mlog.New("mboxreader"))
_, mf0, _, err := mr.Next()
if err != nil {
t.Fatalf("next mbox message: %v", err)
}
defer mf0.Close()
defer os.Remove(mf0.Name())
_, mf1, _, err := mr.Next()
if err != nil {
t.Fatalf("next mbox message: %v", err)
}
defer mf1.Close()
defer os.Remove(mf1.Name())
_, _, _, err = mr.Next()
if err != io.EOF {
t.Fatalf("got err %v, expected eof for next mbox message", err)
}
}
func TestMaildirReader(t *testing.T) {
createTemp := func(pattern string) (*os.File, error) {
return os.CreateTemp("", pattern)
}
// todo: rename 1642966915.1.mox to "1642966915.1.mox:2,"? cannot have that name in the git repo because go module (or the proxy) doesn't like it. could also add some flags and test they survive the import.
newf, err := os.Open("../testdata/importtest.maildir/new")
if err != nil {
t.Fatalf("open maildir new: %v", err)
}
defer newf.Close()
curf, err := os.Open("../testdata/importtest.maildir/cur")
if err != nil {
t.Fatalf("open maildir cur: %v", err)
}
defer curf.Close()
mr := NewMaildirReader(createTemp, newf, curf, mlog.New("maildirreader"))
_, mf0, _, err := mr.Next()
if err != nil {
t.Fatalf("next maildir message: %v", err)
}
defer mf0.Close()
defer os.Remove(mf0.Name())
_, mf1, _, err := mr.Next()
if err != nil {
t.Fatalf("next maildir message: %v", err)
}
defer mf1.Close()
defer os.Remove(mf1.Name())
_, _, _, err = mr.Next()
if err != io.EOF {
t.Fatalf("got err %v, expected eof for next maildir message", err)
}
}
func TestParseDovecotKeywords(t *testing.T) {
const data = `0 Old
1 Junk
2 NonJunk
3 $Forwarded
4 $Junk
`
keywords, err := ParseDovecotKeywords(strings.NewReader(data), mlog.New("dovecotkeywords"))
if err != nil {
t.Fatalf("parsing dovecot-keywords: %v", err)
}
got := strings.Join(keywords, ",")
want := "Old,Junk,NonJunk,$Forwarded,$Junk"
if got != want {
t.Fatalf("parsing dovecot keywords, got %q, want %q", got, want)
}
}