| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMozilla(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
address string |
| 18 |
correctConfig *Config |
| 19 |
}{ |
| 20 |
{ |
| 21 |
address: "gmail.com", |
| 22 |
correctConfig: &Config{ |
| 23 |
Found: ProtocolIMAP, |
| 24 |
IMAP: Credentials{ |
| 25 |
Encryption: EncryptionTLS, |
| 26 |
Address: "imap.gmail.com", |
| 27 |
Port: 993, |
| 28 |
Username: "[email protected]", |
| 29 |
}, |
| 30 |
SMTP: Credentials{ |
| 31 |
Encryption: EncryptionTLS, |
| 32 |
Address: "smtp.gmail.com", |
| 33 |
Port: 465, |
| 34 |
Username: "[email protected]", |
| 35 |
}, |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
address: "gmx.de", |
| 40 |
correctConfig: &Config{ |
| 41 |
Found: ProtocolIMAP, |
| 42 |
IMAP: Credentials{ |
| 43 |
Encryption: EncryptionTLS, |
| 44 |
Address: "imap.gmx.net", |
| 45 |
Port: 993, |
| 46 |
Username: "[email protected]", |
| 47 |
}, |
| 48 |
SMTP: Credentials{ |
| 49 |
Encryption: EncryptionSTARTTLS, |
| 50 |
Address: "mail.gmx.net", |
| 51 |
Port: 587, |
| 52 |
Username: "[email protected]", |
| 53 |
}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
mozillaGet = mozillaTestHTTP |
| 59 |
defer func() { |
| 60 |
mozillaGet = http.Get |
| 61 |
}() |
| 62 |
for _, test := range tests { |
| 63 |
t.Run(test.address, func(t *testing.T) { |
| 64 |
result := make(chan *Config) |
| 65 |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 66 |
defer cancel() |
| 67 |
|
| 68 |
go getFromMozilla(ctx, "john", test.address, result) |
| 69 |
|
| 70 |
select { |
| 71 |
case res := <-result: |
| 72 |
if res == nil { |
| 73 |
t.Log("no result") |
| 74 |
t.FailNow() |
| 75 |
} |
| 76 |
assert.Equal(t, test.correctConfig, res) |
| 77 |
case <-ctx.Done(): |
| 78 |
t.Error("retrieval timed out!") |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func mozillaTestHTTP(url string) (*http.Response, error) { |
| 85 |
switch url { |
| 86 |
case "https://autoconfig.thunderbird.net/v1.1/gmail.com": |
| 87 |
return &http.Response{ |
| 88 |
StatusCode: 200, |
| 89 |
Body: io.NopCloser(strings.NewReader(gmailCfg)), |
| 90 |
}, nil |
| 91 |
case "https://autoconfig.thunderbird.net/v1.1/gmx.de": |
| 92 |
return &http.Response{ |
| 93 |
StatusCode: 200, |
| 94 |
Body: io.NopCloser(strings.NewReader(gmxCfg)), |
| 95 |
}, nil |
| 96 |
case "https://autoconfig.thunderbird.net/v1.1/timeout": |
| 97 |
<-time.After(10 * time.Second) |
| 98 |
return nil, nil |
| 99 |
default: |
| 100 |
return nil, errors.New("not prepared") |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
const ( |
| 105 |
gmailCfg = `<clientConfig version="1.1"> |
| 106 |
<emailProvider id="googlemail.com"> |
| 107 |
<domain>gmail.com</domain> |
| 108 |
<domain>googlemail.com</domain> |
| 109 |
<!-- MX, for Google Apps --> |
| 110 |
<domain>google.com</domain> |
| 111 |
<!-- HACK. Only add ISPs with 100000+ users here --> |
| 112 |
<domain>jazztel.es</domain> |
| 113 |
|
| 114 |
<displayName>Google Mail</displayName> |
| 115 |
<displayShortName>GMail</displayShortName> |
| 116 |
|
| 117 |
<incomingServer type="imap"> |
| 118 |
<hostname>imap.gmail.com</hostname> |
| 119 |
<port>993</port> |
| 120 |
<socketType>SSL</socketType> |
| 121 |
<username>%EMAILADDRESS%</username> |
| 122 |
<authentication>OAuth2</authentication> |
| 123 |
<authentication>password-cleartext</authentication> |
| 124 |
</incomingServer> |
| 125 |
<incomingServer type="pop3"> |
| 126 |
<hostname>pop.gmail.com</hostname> |
| 127 |
<port>995</port> |
| 128 |
<socketType>SSL</socketType> |
| 129 |
<username>%EMAILADDRESS%</username> |
| 130 |
<authentication>OAuth2</authentication> |
| 131 |
<authentication>password-cleartext</authentication> |
| 132 |
<pop3> |
| 133 |
<leaveMessagesOnServer>true</leaveMessagesOnServer> |
| 134 |
</pop3> |
| 135 |
</incomingServer> |
| 136 |
<outgoingServer type="smtp"> |
| 137 |
<hostname>smtp.gmail.com</hostname> |
| 138 |
<port>465</port> |
| 139 |
<socketType>SSL</socketType> |
| 140 |
<username>%EMAILADDRESS%</username> |
| 141 |
<authentication>OAuth2</authentication> |
| 142 |
<authentication>password-cleartext</authentication> |
| 143 |
</outgoingServer> |
| 144 |
|
| 145 |
<documentation url="http://mail.google.com/support/bin/answer.py?answer=13273"> |
| 146 |
<descr>How to enable IMAP/POP3 in GMail</descr> |
| 147 |
</documentation> |
| 148 |
<documentation url="http://mail.google.com/support/bin/topic.py?topic=12806"> |
| 149 |
<descr>How to configure email clients for IMAP</descr> |
| 150 |
</documentation> |
| 151 |
<documentation url="http://mail.google.com/support/bin/topic.py?topic=12805"> |
| 152 |
<descr>How to configure email clients for POP3</descr> |
| 153 |
</documentation> |
| 154 |
<documentation url="http://mail.google.com/support/bin/answer.py?answer=86399"> |
| 155 |
<descr>How to configure TB 2.0 for POP3</descr> |
| 156 |
</documentation> |
| 157 |
</emailProvider> |
| 158 |
|
| 159 |
<oAuth2> |
| 160 |
<issuer>accounts.google.com</issuer> |
| 161 |
<!-- https://developers.google.com/identity/protocols/oauth2/scopes --> |
| 162 |
<scope>https://mail.google.com/ https://www.googleapis.com/auth/contacts https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/carddav</scope> |
| 163 |
<authURL>https://accounts.google.com/o/oauth2/auth</authURL> |
| 164 |
<tokenURL>https://www.googleapis.com/oauth2/v3/token</tokenURL> |
| 165 |
</oAuth2> |
| 166 |
|
| 167 |
<enable visiturl="https://mail.google.com/mail/?ui=2&shva=1#settings/fwdandpop"> |
| 168 |
<instruction>You need to enable IMAP access</instruction> |
| 169 |
</enable> |
| 170 |
|
| 171 |
<webMail> |
| 172 |
<loginPage url="https://accounts.google.com/ServiceLogin?service=mail&continue=http://mail.google.com/mail/"/> |
| 173 |
<loginPageInfo url="https://accounts.google.com/ServiceLogin?service=mail&continue=http://mail.google.com/mail/"> |
| 174 |
<username>%EMAILADDRESS%</username> |
| 175 |
<usernameField id="Email"/> |
| 176 |
<passwordField id="Passwd"/> |
| 177 |
<loginButton id="signIn"/> |
| 178 |
</loginPageInfo> |
| 179 |
</webMail> |
| 180 |
|
| 181 |
</clientConfig> |
| 182 |
` |
| 183 |
gmxCfg = `<clientConfig version="1.1"> |
| 184 |
<emailProvider id="gmx.net"> |
| 185 |
<domain>gmx.net</domain> |
| 186 |
<domain>gmx.de</domain> |
| 187 |
<domain>gmx.at</domain> |
| 188 |
<domain>gmx.ch</domain> |
| 189 |
<domain>gmx.eu</domain> |
| 190 |
<domain>gmx.biz</domain> |
| 191 |
<domain>gmx.org</domain> |
| 192 |
<domain>gmx.info</domain> |
| 193 |
<!-- see also other domains below --> |
| 194 |
<!-- gmx.com is same company, but different access servers --> |
| 195 |
<displayName>GMX Freemail</displayName> |
| 196 |
<displayShortName>GMX</displayShortName> |
| 197 |
<!-- imap officially costs money, but actually works with freemail accounts, too --> |
| 198 |
<incomingServer type="imap"> |
| 199 |
<hostname>imap.gmx.net</hostname> |
| 200 |
<port>993</port> |
| 201 |
<socketType>SSL</socketType> |
| 202 |
<!-- Kundennummer (customer no) and email address should both work --> |
| 203 |
<username>%EMAILADDRESS%</username> |
| 204 |
<authentication>password-cleartext</authentication> |
| 205 |
</incomingServer> |
| 206 |
<incomingServer type="imap"> |
| 207 |
<hostname>imap.gmx.net</hostname> |
| 208 |
<port>143</port> |
| 209 |
<socketType>STARTTLS</socketType> |
| 210 |
<username>%EMAILADDRESS%</username> |
| 211 |
<authentication>password-cleartext</authentication> |
| 212 |
</incomingServer> |
| 213 |
<incomingServer type="pop3"> |
| 214 |
<hostname>pop.gmx.net</hostname> |
| 215 |
<port>995</port> |
| 216 |
<socketType>SSL</socketType> |
| 217 |
<!-- see above --> |
| 218 |
<username>%EMAILADDRESS%</username> |
| 219 |
<authentication>password-cleartext</authentication> |
| 220 |
</incomingServer> |
| 221 |
<incomingServer type="pop3"> |
| 222 |
<hostname>pop.gmx.net</hostname> |
| 223 |
<port>110</port> |
| 224 |
<socketType>STARTTLS</socketType> |
| 225 |
<username>%EMAILADDRESS%</username> |
| 226 |
<authentication>password-cleartext</authentication> |
| 227 |
</incomingServer> |
| 228 |
<outgoingServer type="smtp"> |
| 229 |
<hostname>mail.gmx.net</hostname> |
| 230 |
<port>465</port> |
| 231 |
<socketType>SSL</socketType> |
| 232 |
<!-- see above --> |
| 233 |
<username>%EMAILADDRESS%</username> |
| 234 |
<authentication>password-cleartext</authentication> |
| 235 |
</outgoingServer> |
| 236 |
<outgoingServer type="smtp"> |
| 237 |
<hostname>mail.gmx.net</hostname> |
| 238 |
<port>587</port> |
| 239 |
<socketType>STARTTLS</socketType> |
| 240 |
<username>%EMAILADDRESS%</username> |
| 241 |
<authentication>password-cleartext</authentication> |
| 242 |
</outgoingServer> |
| 243 |
</emailProvider> |
| 244 |
</clientConfig> |
| 245 |
` |
| 246 |
) |