package autoconfig
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMozilla(t *testing.T) {
tests := []struct {
address string
correctConfig *Config
}{
{
address: "gmail.com",
correctConfig: &Config{
Found: ProtocolIMAP,
IMAP: Credentials{
Encryption: EncryptionTLS,
Address: "imap.gmail.com",
Port: 993,
Username: "john@gmail.com",
},
SMTP: Credentials{
Encryption: EncryptionTLS,
Address: "smtp.gmail.com",
Port: 465,
Username: "john@gmail.com",
},
},
},
{
address: "gmx.de",
correctConfig: &Config{
Found: ProtocolIMAP,
IMAP: Credentials{
Encryption: EncryptionTLS,
Address: "imap.gmx.net",
Port: 993,
Username: "john@gmx.de",
},
SMTP: Credentials{
Encryption: EncryptionSTARTTLS,
Address: "mail.gmx.net",
Port: 587,
Username: "john@gmx.de",
},
},
},
}
mozillaGet = mozillaTestHTTP
defer func() {
mozillaGet = http.Get
}()
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 getFromMozilla(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 mozillaTestHTTP(url string) (*http.Response, error) {
switch url {
case "https://autoconfig.thunderbird.net/v1.1/gmail.com":
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(gmailCfg)),
}, nil
case "https://autoconfig.thunderbird.net/v1.1/gmx.de":
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(gmxCfg)),
}, nil
case "https://autoconfig.thunderbird.net/v1.1/timeout":
<-time.After(10 * time.Second)
return nil, nil
default:
return nil, errors.New("not prepared")
}
}
const (
gmailCfg = `
gmail.com
googlemail.com
google.com
jazztel.es
Google Mail
GMail
imap.gmail.com
993
SSL
%EMAILADDRESS%
OAuth2
password-cleartext
pop.gmail.com
995
SSL
%EMAILADDRESS%
OAuth2
password-cleartext
true
smtp.gmail.com
465
SSL
%EMAILADDRESS%
OAuth2
password-cleartext
How to enable IMAP/POP3 in GMail
How to configure email clients for IMAP
How to configure email clients for POP3
How to configure TB 2.0 for POP3
accounts.google.com
https://mail.google.com/ https://www.googleapis.com/auth/contacts https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/carddav
https://accounts.google.com/o/oauth2/auth
https://www.googleapis.com/oauth2/v3/token
You need to enable IMAP access
%EMAILADDRESS%
`
gmxCfg = `
gmx.net
gmx.de
gmx.at
gmx.ch
gmx.eu
gmx.biz
gmx.org
gmx.info
GMX Freemail
GMX
imap.gmx.net
993
SSL
%EMAILADDRESS%
password-cleartext
imap.gmx.net
143
STARTTLS
%EMAILADDRESS%
password-cleartext
pop.gmx.net
995
SSL
%EMAILADDRESS%
password-cleartext
pop.gmx.net
110
STARTTLS
%EMAILADDRESS%
password-cleartext
mail.gmx.net
465
SSL
%EMAILADDRESS%
password-cleartext
mail.gmx.net
587
STARTTLS
%EMAILADDRESS%
password-cleartext
`
)