-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
156 lines (142 loc) · 3.31 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"net/textproto"
"os"
"path"
"runtime"
"testing"
"time"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-imap/server"
"golang.org/x/crypto/bcrypt"
"github.com/stbenjam/go-imap-notmuch/pkg/config"
"github.com/stbenjam/go-imap-notmuch/pkg/notmuch"
)
func TestNotmuchIMAP(t *testing.T) {
var c *client.Client
var err error
t.Run("can connect to imap server", func(t *testing.T) {
setupIMAPServer(t)
c, err = client.Dial("localhost:6143")
if err != nil {
t.Fatal(err)
}
})
t.Run("notmuch user can login", func(t *testing.T) {
// Check login
err = c.Login("notmuch", "notmuch")
if err != nil {
t.Fatal(err)
}
})
t.Run("can find inbox", func(*testing.T) {
// Check mailboxes
mailboxes := make(chan *imap.MailboxInfo, 1)
done := make(chan error, 1)
done <- c.List("", "*", mailboxes)
found := false
for m := range mailboxes {
if m.Name == "INBOX" {
found = true
break
}
}
if !found {
t.Fatal("Did not find INBOX")
}
})
t.Run("can get last message from mailbox", func(t *testing.T) {
mbox, err := c.Select("INBOX", false)
if err != nil {
t.Fatal(err)
}
i := mbox.UidNext - 1
seqset := new(imap.SeqSet)
seqset.AddRange(i, i)
messages := make(chan *imap.Message, 10)
done := make(chan error, 1)
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
for msg := range messages {
if msg.Envelope.Subject == "[notmuch] [PATCH 1/2] Close message file after parsing message headers" {
return
}
}
t.Error("couldn't load last message from mailbox")
})
t.Run("can search mailbox", func(t *testing.T) {
_, err := c.Select("INBOX", false)
if err != nil {
t.Fatal(err)
}
after, _ := time.Parse("2006-01-02T15:04:05.000Z", "2009-11-18T00:00:00.000Z")
before, _ := time.Parse("2006-01-02T15:04:05.000Z", "2010-12-30T00:00:00.000Z")
seqNums, err := c.Search(&imap.SearchCriteria{
Since: after,
Before: before,
WithoutFlags: []string{imap.SeenFlag},
Or: [][2]*imap.SearchCriteria{
{
{
Body: []string{"Makefile"},
},
{
Header: textproto.MIMEHeader{
"subject": []string{"[PATCH]"},
},
},
},
},
})
if err != nil {
t.Fatal(err)
}
if len(seqNums) != 15 {
t.Fatalf("expected 15 messages to match search, found %d", len(seqNums))
}
})
t.Run("can logout", func(*testing.T) {
if err := c.Logout(); err != nil {
t.Error(err.Error())
}
})
}
func setupIMAPServer(t *testing.T) *server.Server {
_, currentPath, _, ok := runtime.Caller(1)
if !ok {
t.Fatalf("could not get current path")
}
dir := path.Join(path.Dir(currentPath), "fixtures", "database-v1")
hash, err := bcrypt.GenerateFromPassword([]byte("notmuch"), bcrypt.DefaultCost)
if err != nil {
t.Fatal(err)
}
cfg := &config.Config{
Maildir: dir,
Username: "notmuch",
Password: string(hash),
Mailboxes: []config.Mailbox{
{
Name: "INBOX",
Query: "is:unread",
},
},
Debug: true,
UidDatabase: path.Join(dir, "uid.dat"),
}
cfg.SetDefaults()
backend, err := notmuch.New(cfg)
if err != nil {
t.Fatal(err)
}
s := server.New(backend)
s.AllowInsecureAuth = true
s.Addr = ":6143"
s.Debug = os.Stderr
go func() {
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}()
return s
}