mirror of
https://git.sr.ht/~rjarry/aerc
synced 2026-03-02 18:23:33 +01:00
Go has evolved significantly over the years and has introduced some handy helper functions that make the code easier to read. Use helper functions like slices.Contains, map.Copy, and strings.CutPrefix, when appropriate. Signed-off-by: Moritz Poldrack <git@moritz.sh> Acked-by: Robin Jarry <robin@jarry.cc>
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package autoconfig
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMX(t *testing.T) {
|
|
tests := []struct {
|
|
address string
|
|
correctConfig *Config
|
|
}{
|
|
{
|
|
address: "poldrack.dev",
|
|
correctConfig: &Config{
|
|
Found: ProtocolIMAP,
|
|
IMAP: Credentials{
|
|
Encryption: EncryptionSTARTTLS,
|
|
Address: "mail.moritz.sh",
|
|
Port: 143,
|
|
Username: "john@poldrack.dev",
|
|
},
|
|
SMTP: Credentials{
|
|
Encryption: EncryptionSTARTTLS,
|
|
Address: "mail.moritz.sh",
|
|
Port: 587,
|
|
Username: "john@poldrack.dev",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
netDial = mxTestDialer
|
|
lookupMX = mxTestLookup
|
|
defer func() {
|
|
netDial = net.Dial
|
|
lookupMX = net.LookupMX
|
|
}()
|
|
for _, test := range tests {
|
|
t.Run(test.address, func(t *testing.T) {
|
|
result := make(chan *Config)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
go guessMX(ctx, "john", test.address, result)
|
|
|
|
select {
|
|
case res := <-result:
|
|
if res == nil {
|
|
t.Log("no result")
|
|
t.FailNow()
|
|
}
|
|
assert.Equal(t, test.correctConfig, res)
|
|
case <-ctx.Done():
|
|
t.Error("retrieval timed out!")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func mxTestLookup(address string) ([]*net.MX, error) {
|
|
switch address {
|
|
case "poldrack.dev":
|
|
return []*net.MX{
|
|
{Host: "mail.moritz.sh", Pref: 1},
|
|
}, nil
|
|
default:
|
|
return nil, errors.New("unknown address")
|
|
}
|
|
}
|