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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
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 = `<clientConfig version="1.1">
<emailProvider id="googlemail.com">
<domain>gmail.com</domain>
<domain>googlemail.com</domain>
<!-- MX, for Google Apps -->
<domain>google.com</domain>
<!-- HACK. Only add ISPs with 100000+ users here -->
<domain>jazztel.es</domain>
<displayName>Google Mail</displayName>
<displayShortName>GMail</displayShortName>
<incomingServer type="imap">
<hostname>imap.gmail.com</hostname>
<port>993</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>OAuth2</authentication>
<authentication>password-cleartext</authentication>
</incomingServer>
<incomingServer type="pop3">
<hostname>pop.gmail.com</hostname>
<port>995</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>OAuth2</authentication>
<authentication>password-cleartext</authentication>
<pop3>
<leaveMessagesOnServer>true</leaveMessagesOnServer>
</pop3>
</incomingServer>
<outgoingServer type="smtp">
<hostname>smtp.gmail.com</hostname>
<port>465</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>OAuth2</authentication>
<authentication>password-cleartext</authentication>
</outgoingServer>
<documentation url="http://mail.google.com/support/bin/answer.py?answer=13273">
<descr>How to enable IMAP/POP3 in GMail</descr>
</documentation>
<documentation url="http://mail.google.com/support/bin/topic.py?topic=12806">
<descr>How to configure email clients for IMAP</descr>
</documentation>
<documentation url="http://mail.google.com/support/bin/topic.py?topic=12805">
<descr>How to configure email clients for POP3</descr>
</documentation>
<documentation url="http://mail.google.com/support/bin/answer.py?answer=86399">
<descr>How to configure TB 2.0 for POP3</descr>
</documentation>
</emailProvider>
<oAuth2>
<issuer>accounts.google.com</issuer>
<!-- https://developers.google.com/identity/protocols/oauth2/scopes -->
<scope>https://mail.google.com/ https://www.googleapis.com/auth/contacts https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/carddav</scope>
<authURL>https://accounts.google.com/o/oauth2/auth</authURL>
<tokenURL>https://www.googleapis.com/oauth2/v3/token</tokenURL>
</oAuth2>
<enable visiturl="https://mail.google.com/mail/?ui=2&shva=1#settings/fwdandpop">
<instruction>You need to enable IMAP access</instruction>
</enable>
<webMail>
<loginPage url="https://accounts.google.com/ServiceLogin?service=mail&continue=http://mail.google.com/mail/"/>
<loginPageInfo url="https://accounts.google.com/ServiceLogin?service=mail&continue=http://mail.google.com/mail/">
<username>%EMAILADDRESS%</username>
<usernameField id="Email"/>
<passwordField id="Passwd"/>
<loginButton id="signIn"/>
</loginPageInfo>
</webMail>
</clientConfig>
`
gmxCfg = `<clientConfig version="1.1">
<emailProvider id="gmx.net">
<domain>gmx.net</domain>
<domain>gmx.de</domain>
<domain>gmx.at</domain>
<domain>gmx.ch</domain>
<domain>gmx.eu</domain>
<domain>gmx.biz</domain>
<domain>gmx.org</domain>
<domain>gmx.info</domain>
<!-- see also other domains below -->
<!-- gmx.com is same company, but different access servers -->
<displayName>GMX Freemail</displayName>
<displayShortName>GMX</displayShortName>
<!-- imap officially costs money, but actually works with freemail accounts, too -->
<incomingServer type="imap">
<hostname>imap.gmx.net</hostname>
<port>993</port>
<socketType>SSL</socketType>
<!-- Kundennummer (customer no) and email address should both work -->
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<incomingServer type="imap">
<hostname>imap.gmx.net</hostname>
<port>143</port>
<socketType>STARTTLS</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<incomingServer type="pop3">
<hostname>pop.gmx.net</hostname>
<port>995</port>
<socketType>SSL</socketType>
<!-- see above -->
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<incomingServer type="pop3">
<hostname>pop.gmx.net</hostname>
<port>110</port>
<socketType>STARTTLS</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<outgoingServer type="smtp">
<hostname>mail.gmx.net</hostname>
<port>465</port>
<socketType>SSL</socketType>
<!-- see above -->
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</outgoingServer>
<outgoingServer type="smtp">
<hostname>mail.gmx.net</hostname>
<port>587</port>
<socketType>STARTTLS</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</outgoingServer>
</emailProvider>
</clientConfig>
`
)
|