| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net" |
| 6 |
"net/http" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestConfigRetrieval(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
address string |
| 16 |
correctConfig *Config |
| 17 |
}{ |
| 18 |
{ |
| 19 |
address: "mailbox.org", |
| 20 |
correctConfig: &Config{ |
| 21 |
Found: ProtocolIMAP, |
| 22 |
IMAP: Credentials{ |
| 23 |
Encryption: EncryptionTLS, |
| 24 |
Address: "imap.mailbox.org", |
| 25 |
Port: 993, |
| 26 |
Username: "[email protected]", |
| 27 |
}, |
| 28 |
SMTP: Credentials{ |
| 29 |
Encryption: EncryptionSTARTTLS, |
| 30 |
Address: "smtp.mailbox.org", |
| 31 |
Port: 587, |
| 32 |
Username: "[email protected]", |
| 33 |
}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
address: "poldi1405.srht.site", |
| 38 |
correctConfig: &Config{ |
| 39 |
Found: ProtocolIMAP, |
| 40 |
IMAP: Credentials{ |
| 41 |
Encryption: EncryptionSTARTTLS, |
| 42 |
Address: "mail.example.com", |
| 43 |
Port: 143, |
| 44 |
Username: "your-username", |
| 45 |
}, |
| 46 |
SMTP: Credentials{ |
| 47 |
Encryption: EncryptionSTARTTLS, |
| 48 |
Address: "mail.example.com", |
| 49 |
Port: 587, |
| 50 |
Username: "your-username", |
| 51 |
}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
address: "gmail.com", |
| 56 |
correctConfig: &Config{ |
| 57 |
Found: ProtocolIMAP, |
| 58 |
IMAP: Credentials{ |
| 59 |
Encryption: EncryptionTLS, |
| 60 |
Address: "imap.gmail.com", |
| 61 |
Port: 993, |
| 62 |
Username: "[email protected]", |
| 63 |
}, |
| 64 |
SMTP: Credentials{ |
| 65 |
Encryption: EncryptionSTARTTLS, |
| 66 |
Address: "smtp.gmail.com", |
| 67 |
Port: 587, |
| 68 |
Username: "[email protected]", |
| 69 |
}, |
| 70 |
}, |
| 71 |
}, |
| 72 |
{ |
| 73 |
address: "fastmail.com", |
| 74 |
correctConfig: &Config{ |
| 75 |
Found: ProtocolJMAP, |
| 76 |
JMAP: Credentials{ |
| 77 |
Encryption: EncryptionTLS, |
| 78 |
Address: "api.fastmail.com", |
| 79 |
Port: 443, |
| 80 |
Username: "[email protected]", |
| 81 |
}, |
| 82 |
IMAP: Credentials{ |
| 83 |
Encryption: EncryptionTLS, |
| 84 |
Address: "imap.fastmail.com", |
| 85 |
Port: 993, |
| 86 |
Username: "[email protected]", |
| 87 |
}, |
| 88 |
SMTP: Credentials{ |
| 89 |
Encryption: EncryptionSTARTTLS, |
| 90 |
Address: "smtp.fastmail.com", |
| 91 |
Port: 587, |
| 92 |
Username: "[email protected]", |
| 93 |
}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
address: "gmx.de", |
| 98 |
correctConfig: &Config{ |
| 99 |
Found: ProtocolIMAP, |
| 100 |
IMAP: Credentials{ |
| 101 |
Encryption: EncryptionTLS, |
| 102 |
Address: "imap.gmx.net", |
| 103 |
Port: 993, |
| 104 |
Username: "[email protected]", |
| 105 |
}, |
| 106 |
SMTP: Credentials{ |
| 107 |
Encryption: EncryptionSTARTTLS, |
| 108 |
Address: "mail.gmx.net", |
| 109 |
Port: 587, |
| 110 |
Username: "[email protected]", |
| 111 |
}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
address: "moritz.sh", |
| 116 |
correctConfig: &Config{ |
| 117 |
Found: ProtocolIMAP, |
| 118 |
IMAP: Credentials{ |
| 119 |
Encryption: EncryptionSTARTTLS, |
| 120 |
Address: "mail.moritz.sh", |
| 121 |
Port: 143, |
| 122 |
Username: "[email protected]", |
| 123 |
}, |
| 124 |
SMTP: Credentials{ |
| 125 |
Encryption: EncryptionSTARTTLS, |
| 126 |
Address: "mail.moritz.sh", |
| 127 |
Port: 587, |
| 128 |
Username: "[email protected]", |
| 129 |
}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
{ |
| 133 |
address: "poldrack.dev", |
| 134 |
correctConfig: &Config{ |
| 135 |
Found: ProtocolIMAP, |
| 136 |
IMAP: Credentials{ |
| 137 |
Encryption: EncryptionSTARTTLS, |
| 138 |
Address: "mail.moritz.sh", |
| 139 |
Port: 143, |
| 140 |
Username: "[email protected]", |
| 141 |
}, |
| 142 |
SMTP: Credentials{ |
| 143 |
Encryption: EncryptionSTARTTLS, |
| 144 |
Address: "mail.moritz.sh", |
| 145 |
Port: 587, |
| 146 |
Username: "[email protected]", |
| 147 |
}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
address: "timeout", |
| 152 |
correctConfig: nil, |
| 153 |
}, |
| 154 |
} |
| 155 |
|
| 156 |
lookupSRV = customLookupSRV |
| 157 |
httpGet = autoconfigTestGet |
| 158 |
mozillaGet = mozillaTestHTTP |
| 159 |
netDial = mxTestDialer |
| 160 |
lookupMX = mxTestLookup |
| 161 |
defer func() { |
| 162 |
lookupSRV = net.LookupSRV |
| 163 |
httpGet = http.Get |
| 164 |
mozillaGet = http.Get |
| 165 |
netDial = net.Dial |
| 166 |
lookupMX = net.LookupMX |
| 167 |
}() |
| 168 |
|
| 169 |
for _, test := range tests { |
| 170 |
t.Run(test.address, func(t *testing.T) { |
| 171 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 172 |
defer cancel() |
| 173 |
|
| 174 |
res := GetConfig(ctx, "john@"+test.address) |
| 175 |
if test.correctConfig == nil { |
| 176 |
if res != nil { |
| 177 |
t.Fatalf("expected no result, but got %v", res) |
| 178 |
} |
| 179 |
return |
| 180 |
} |
| 181 |
assert.Equal(t, test.correctConfig, res) |
| 182 |
}) |
| 183 |
} |
| 184 |
} |