feat(imapx): connect, endpoint test, login test with folder listing
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package imapx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func TestLogin(ctx context.Context, ep Endpoint, login, pass string) ([]string, error) {
|
||||
c, err := Connect(ctx, ep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = c.Logout().Wait() }()
|
||||
if err := c.Login(login, pass).Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mboxes, err := c.List("", "*", nil).Collect()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, 0, len(mboxes))
|
||||
for _, m := range mboxes {
|
||||
names = append(names, m.Mailbox)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package imapx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
|
||||
"github.com/emersion/go-imap/v2/imapclient"
|
||||
)
|
||||
|
||||
type Endpoint struct {
|
||||
Host string
|
||||
Port int
|
||||
TLSMode string // ssl | starttls | plain
|
||||
}
|
||||
|
||||
func (e Endpoint) addr() string { return fmt.Sprintf("%s:%d", e.Host, e.Port) }
|
||||
|
||||
func Connect(ctx context.Context, ep Endpoint) (*imapclient.Client, error) {
|
||||
var (
|
||||
c *imapclient.Client
|
||||
err error
|
||||
)
|
||||
switch ep.TLSMode {
|
||||
case "ssl":
|
||||
c, err = imapclient.DialTLS(ep.addr(), &imapclient.Options{
|
||||
TLSConfig: &tls.Config{ServerName: ep.Host},
|
||||
})
|
||||
case "starttls":
|
||||
c, err = imapclient.DialStartTLS(ep.addr(), &imapclient.Options{
|
||||
TLSConfig: &tls.Config{ServerName: ep.Host},
|
||||
})
|
||||
case "plain":
|
||||
c, err = imapclient.DialInsecure(ep.addr(), nil)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown tls_mode %q", ep.TLSMode)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func TestEndpoint(ctx context.Context, ep Endpoint) error {
|
||||
c, err := Connect(ctx, ep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Logout().Wait()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package imapx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testEP(t *testing.T) Endpoint {
|
||||
host := os.Getenv("TEST_IMAP_HOST")
|
||||
if host == "" {
|
||||
t.Skip("TEST_IMAP_HOST not set")
|
||||
}
|
||||
port, _ := strconv.Atoi(os.Getenv("TEST_IMAP_PORT"))
|
||||
return Endpoint{Host: host, Port: port, TLSMode: "plain"}
|
||||
}
|
||||
|
||||
func TestTestEndpointOK(t *testing.T) {
|
||||
ep := testEP(t)
|
||||
if err := TestEndpoint(context.Background(), ep); err != nil {
|
||||
t.Fatalf("TestEndpoint: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestLoginListsFolders(t *testing.T) {
|
||||
ep := testEP(t)
|
||||
// greenmail auto-creates users on first login
|
||||
folders, err := TestLogin(context.Background(), ep, "user1@localhost", "pass1")
|
||||
if err != nil {
|
||||
t.Fatalf("TestLogin: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, f := range folders {
|
||||
if f == "INBOX" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("INBOX not in folders: %v", folders)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user