Debian Codegraph @

lib/autoconfig/autoconfig_test.go

raw
1 package autoconfig
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "net"
8 "net/http"
9 "strings"
10 "testing"
11 "time"
12
13 "github.com/stretchr/testify/assert"
14 )
15
16 func TestAutoconfig(t *testing.T) {
17 tests := []struct {
18 address string
19 correctConfig *Config
20 }{
21 {
22 address: "mailbox.org",
23 correctConfig: &Config{
24 Found: ProtocolIMAP,
25 IMAP: Credentials{
26 Encryption: EncryptionTLS,
27 Address: "imap.mailbox.org",
28 Port: 993,
29 Username: "[email protected]",
30 },
31 SMTP: Credentials{
32 Encryption: EncryptionSTARTTLS,
33 Address: "smtp.mailbox.org",
34 Port: 587,
35 Username: "[email protected]",
36 },
37 },
38 },
39 {
40 address: "poldi1405.srht.site",
41 correctConfig: &Config{
42 Found: ProtocolIMAP,
43 IMAP: Credentials{
44 Encryption: EncryptionSTARTTLS,
45 Address: "mail.example.com",
46 Port: 143,
47 Username: "your-username",
48 },
49 SMTP: Credentials{
50 Encryption: EncryptionSTARTTLS,
51 Address: "mail.example.com",
52 Port: 587,
53 Username: "your-username",
54 },
55 },
56 },
57 {
58 address: "timeout",
59 correctConfig: nil,
60 },
61 }
62
63 httpGet = autoconfigTestGet
64 lookupSRV = customLookupSRV
65 defer func() {
66 httpGet = http.Get
67 lookupSRV = net.LookupSRV
68 }()
69 for _, test := range tests {
70 t.Run(test.address, func(t *testing.T) {
71 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
72 defer cancel()
73
74 result := make(chan *Config)
75 go getFromAutoconfig(ctx, "john", test.address, result)
76 res := <-result
77 assert.Equal(t, test.correctConfig, res)
78 })
79 }
80 }
81
82 func autoconfigTestGet(url string) (*http.Response, error) {
83 switch url {
84 case "https://autoconfig.mailbox.org/mail/config-v1.1.xml?emailaddress=john%40mailbox.org":
85 return &http.Response{
86 StatusCode: 200,
87 Body: io.NopCloser(strings.NewReader(mailboxAutoconf)),
88 }, nil
89 case "https://poldi1405.srht.site/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=john%40poldi1405.srht.site":
90 return &http.Response{
91 StatusCode: 200,
92 Body: io.NopCloser(strings.NewReader(srhtAutoconf)),
93 }, nil
94 case "https://timeout/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=john%40timeout":
95 <-time.After(5 * time.Minute)
96 return nil, nil
97 default:
98 return nil, fmt.Errorf("%q not prepared", url)
99 }
100 }
101
102 const (
103 mailboxAutoconf = `<?xml version="1.0" encoding="UTF-8"?>
104
105 <clientConfig version="1.1">
106 <emailProvider id="mailbox.org">
107 <domain>mailbox.org</domain>
108 <displayName>mailbox.org -- damit Privates privat bleibt</displayName>
109 <displayShortName>mailbox.org</displayShortName>
110
111 <incomingServer type="imap">
112 <hostname>imap.mailbox.org</hostname>
113 <port>993</port>
114 <socketType>SSL</socketType>
115 <authentication>password-cleartext</authentication>
116 <username>%EMAILADDRESS%</username>
117 </incomingServer>
118 <incomingServer type="imap">
119 <hostname>imap.mailbox.org</hostname>
120 <port>143</port>
121 <socketType>STARTTLS</socketType>
122 <authentication>password-cleartext</authentication>
123 <username>%EMAILADDRESS%</username>
124 </incomingServer>
125
126 <incomingServer type="pop3">
127 <hostname>pop3.mailbox.org</hostname>
128 <port>995</port>
129 <socketType>SSL</socketType>
130 <authentication>password-cleartext</authentication>
131 <username>%EMAILADDRESS%</username>
132 </incomingServer>
133 <incomingServer type="pop3">
134 <hostname>pop3.mailbox.org</hostname>
135 <port>110</port>
136 <socketType>STARTTLS</socketType>
137 <authentication>password-cleartext</authentication>
138 <username>%EMAILADDRESS%</username>
139 </incomingServer>
140
141 <outgoingServer type="smtp">
142 <hostname>smtp.mailbox.org</hostname>
143 <port>465</port>
144 <socketType>SSL</socketType>
145 <authentication>password-cleartext</authentication>
146 <username>%EMAILADDRESS%</username>
147 </outgoingServer>
148 <outgoingServer type="smtp">
149 <hostname>smtp.mailbox.org</hostname>
150 <port>587</port>
151 <socketType>STARTTLS</socketType>
152 <authentication>password-cleartext</authentication>
153 <username>%EMAILADDRESS%</username>
154 </outgoingServer>
155
156
157 <documentation url="http://www.mailbox.org/">
158 <descr lang="de">FAQ und Support-Datenbank</descr>
159 <descr lang="en">Frequently Asked Questions (FAQ)</descr>
160 </documentation>
161 </emailProvider>
162 </clientConfig>
163
164 `
165 srhtAutoconf = `<clientConfig version="1.1">
166 <emailProvider id="example.com">
167 <domain>example.com</domain>
168 <displayName>Not valid</displayName>
169 <displayShortName>Not valid</displayShortName>
170 <incomingServer type="imap">
171 <hostname>mail.example.com</hostname>
172 <port>143</port>
173 <socketType>STARTTLS</socketType>
174 <username>your-username</username>
175 <authentication>password-cleartext</authentication>
176 </incomingServer>
177 <outgoingServer type="smtp">
178 <hostname>mail.example.com</hostname>
179 <port>587</port>
180 <socketType>STARTTLS</socketType>
181 <username>your-username</username>
182 <authentication>password-cleartext</authentication>
183 </outgoingServer>
184 </emailProvider>
185 </clientConfig>
186 `
187 )