MCPcopy Create free account
hub / github.com/coder/coder / CreateMockSMTPServer

Function CreateMockSMTPServer

coderd/notifications/dispatch/smtptest/server.go:183–214  ·  view source on GitHub ↗

nolint:revive // Yes, useTLS is a control flag.

(be *Backend, useTLS bool)

Source from the content-addressed store, hash-verified

181
182// nolint:revive // Yes, useTLS is a control flag.
183func CreateMockSMTPServer(be *Backend, useTLS bool) (*smtp.Server, net.Listener, error) {
184 // nolint:gosec
185 tlsCfg := &tls.Config{
186 GetCertificate: readCert,
187 }
188
189 l, err := net.Listen("tcp", "localhost:0")
190 if err != nil {
191 return nil, nil, xerrors.Errorf("connect: tls? %v: %w", useTLS, err)
192 }
193
194 if useTLS {
195 l = tls.NewListener(l, tlsCfg)
196 }
197
198 addr, ok := l.Addr().(*net.TCPAddr)
199 if !ok {
200 return nil, nil, xerrors.Errorf("unexpected address type: %T", l.Addr())
201 }
202
203 s := smtp.NewServer(be)
204
205 s.Addr = addr.String()
206 s.WriteTimeout = 10 * time.Second
207 s.ReadTimeout = 10 * time.Second
208 s.MaxMessageBytes = 1024 * 1024
209 s.MaxRecipients = 50
210 s.AllowInsecureAuth = !useTLS
211 s.TLSConfig = tlsCfg
212
213 return s, l, nil
214}
215
216func readCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
217 crt, err := tls.X509KeyPair(certFile, keyFile)

Callers 3

TestSMTPFunction · 0.92

Calls 4

ListenMethod · 0.65
ErrorfMethod · 0.45
AddrMethod · 0.45
StringMethod · 0.45

Tested by 3

TestSMTPFunction · 0.74