Fix sendmail test and rewrite fetch ws call

This commit is contained in:
Melon 2023-11-25 12:22:58 +00:00
parent 6e4c9baa0f
commit 43d1e56c88
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
6 changed files with 48 additions and 82 deletions

View File

@ -108,8 +108,8 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt
// read incoming message
var m struct {
Action string `json:"action"`
Args []string `json:"args"`
Action string `json:"action"`
Args json.RawMessage `json:"args"`
}
err := c.ReadJSON(&m)
if err != nil {

1
go.mod
View File

@ -28,4 +28,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
zappem.net/pub/debug/xxd v1.0.0 // indirect
)

2
go.sum
View File

@ -85,3 +85,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
zappem.net/pub/debug/xxd v1.0.0 h1:weitvkgR0yOBP4QfGTPyQDD25rxAJxYw6+Dy2wrA3Zs=
zappem.net/pub/debug/xxd v1.0.0/go.mod h1:7m1I+mBsdwBWcaVp8P0w0YQP9UWRsJEkrXB4OwF6b/o=

View File

@ -1,11 +1,11 @@
package imap
import (
"encoding/json"
"errors"
"github.com/1f349/lotus/imap/marshal"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"strconv"
)
var imapStatusFlags = []imap.StatusItem{
@ -22,7 +22,7 @@ type Client struct {
var ErrInvalidArguments = errors.New("invalid arguments")
func (c *Client) HandleWS(action string, args []string) (map[string]any, error) {
func (c *Client) HandleWS(action string, args json.RawMessage) (map[string]any, error) {
switch action {
case "copy":
// TODO: implementation
@ -31,41 +31,45 @@ func (c *Client) HandleWS(action string, args []string) (map[string]any, error)
case "delete":
// TODO: implementation
case "list":
if len(args) != 2 {
var listArgs []string
err := json.Unmarshal(args, &listArgs)
if err != nil {
return nil, err
}
if len(listArgs) != 2 {
return nil, ErrInvalidArguments
}
// do list
list, err := c.list(args[0], args[1])
list, err := c.list(listArgs[0], listArgs[1])
if err != nil {
return nil, err
}
return map[string]any{"type": "list", "value": list}, nil
case "fetch":
if len(args) != 4 {
var fetchArgs struct {
Sync uint64 `json:"sync"`
Path string `json:"path"`
Start uint32 `json:"start"`
End uint32 `json:"end"`
Limit uint32 `json:"limit"`
}
err := json.Unmarshal(args, &fetchArgs)
if err != nil {
return nil, err
}
if fetchArgs.Sync == 0 || len(fetchArgs.Path) == 0 || fetchArgs.Start == 0 || fetchArgs.End == 0 || fetchArgs.Limit == 0 {
return nil, ErrInvalidArguments
}
// parse numeric parameters
arg1i, err := strconv.Atoi(args[1])
if err != nil {
return nil, err
}
arg2i, err := strconv.Atoi(args[2])
if err != nil {
return nil, err
}
arg3i, err := strconv.Atoi(args[3])
if err != nil {
return nil, err
}
// do fetch
fetch, err := c.fetch(args[0], uint32(arg1i), uint32(arg2i), uint32(arg3i))
fetch, err := c.fetch(fetchArgs.Path, fetchArgs.Start, fetchArgs.End, fetchArgs.Limit)
if err != nil {
return nil, err
}
return map[string]any{"type": "fetch", "value": marshal.MessageSliceJson(fetch)}, nil
return map[string]any{"type": "fetch", "sync": 0, "value": marshal.MessageSliceJson(fetch)}, nil
case "move":
// TODO: implementation
case "rename":

View File

@ -1,45 +0,0 @@
package fake
import (
"github.com/emersion/go-smtp"
"io"
"log"
)
type SmtpBackend struct {
Debug chan []byte
}
func (f *SmtpBackend) NewSession(c *smtp.Conn) (smtp.Session, error) {
return &SmtpSession{f.Debug}, nil
}
type SmtpSession struct {
Debug chan []byte
}
func (f *SmtpSession) Reset() {}
func (f *SmtpSession) Logout() error { return nil }
func (f *SmtpSession) AuthPlain(username, password string) error { return nil }
func (f *SmtpSession) Mail(from string, opts *smtp.MailOptions) error {
log.Println("MAIL " + from)
f.Debug <- []byte("MAIL " + from + "\n")
return nil
}
func (f *SmtpSession) Rcpt(to string) error {
f.Debug <- []byte("RCPT " + to + "\n")
return nil
}
func (f *SmtpSession) Data(r io.Reader) error {
all, err := io.ReadAll(r)
if err != nil {
return err
}
f.Debug <- all
return nil
}

View File

@ -5,8 +5,8 @@ import (
"github.com/emersion/go-message"
"github.com/emersion/go-message/mail"
"github.com/stretchr/testify/assert"
"io"
"log"
"net"
"os"
"os/exec"
"strings"
@ -34,34 +34,38 @@ func init() {
sendTestMessage = out.Bytes()
}
func TestSmtp_Send(t *testing.T) {
func TestSendMail_Send(t *testing.T) {
execCommand = func(name string, arg ...string) *exec.Cmd {
cs := append([]string{"-test.run=TestHelperProcess", "--", name}, arg...)
log.Println("Hello")
cs := append([]string{"-test.run=TestSendMailHelperProcess", "--", name}, arg...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
log.Println(cmd.Path)
return cmd
}
defer func() { execCommand = exec.Command }()
m := &Mail{From: &mail.Address{Address: "test@localhost"}, Body: sendTestMessage}
temp, err := os.CreateTemp("", "sendmail")
if err != nil {
return
}
addr, err := net.ResolveUnixAddr("")
assert.NoError(t, err)
listen, err := net.ListenUnix("", addr)
assert.NoError(t, err)
s := &SendMail{SendMailCommand: "/tmp/sendmailXXXXX"}
assert.NoError(t, s.Send(m))
}
func TestSmtpHelperProcess(t *testing.T) {
func TestSendMailHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
log.Println("This is a test")
all, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
assert.Equal(t, strings.ReplaceAll(`Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
To: "A" <a@localhost>
From: "Test" <test@localhost>
Subject: Happy Millennium
Date: Sat, 01 Jan 2000 00:00:00 +0000
Thanks`, "\n", "\r\n"), string(all))
}