smtptest

package
v0.0.0-...-c05b5d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractItems

func ExtractItems(body string) []string

extractLinks takes a single email body and returns a slice of raw HTML link items. If an e2e test is failing and calls this function, make sure that the pattern it uses to match links is up to date.

func GenerateTLSFiles

func GenerateTLSFiles(t *testing.T) (keyPath string, certPath string, err error)

GenerateTLSFiles writes a TLS key and certificate to a temporary test directory that is removed after the test suite runs. It returns the file paths of the key and certificate. The certificate is a root cert.

Types

type Backend

type Backend struct {
	*InMemoryEmailStore
}

Backend implements smtp.Backend. It's a thin authentication wrapper for an InMemoryEmailStore.

func (*Backend) AnonymousLogin

func (be *Backend) AnonymousLogin(_ *smtp.ConnectionState) (smtp.Session, error)

AnonymouseLogin implements smtp.Backend. Not supported since we want to enforce AUTH.

func (*Backend) Login

func (be *Backend) Login(_ *smtp.ConnectionState, username string, password string) (smtp.Session, error)

Login implements smtp.Backend. Any username/password is fine, since we don't want to couple this with specific test configurations.

type InMemoryEmailStore

type InMemoryEmailStore struct {
	// contains filtered or unexported fields
}

InMemoryEmailStore retains email bodies in memory for comparison against a test's expected output. Implements smtp.Session. Designed to be goroutine safe since we don't know how many goroutines will be hitting the server at once.

func (*InMemoryEmailStore) Data

func (es *InMemoryEmailStore) Data(r io.Reader) error

Rcpt implements smtp.Session. Stores the email data in memory for retrieval at the end of the test.

func (*InMemoryEmailStore) Logout

func (es *InMemoryEmailStore) Logout() error

Logout implements smtp.Session. No-op here.

func (*InMemoryEmailStore) Mail

func (es *InMemoryEmailStore) Mail(_ string, _ smtp.MailOptions) error

Mail implements smtp.Session. No-op here.

func (*InMemoryEmailStore) Rcpt

func (es *InMemoryEmailStore) Rcpt(to string) error

Rcpt implements smtp.Session. No-op here.

func (*InMemoryEmailStore) Reset

func (es *InMemoryEmailStore) Reset()

Reset implements smtp.Session. No-op here.

func (*InMemoryEmailStore) RetrieveEmails

func (es *InMemoryEmailStore) RetrieveEmails(t int64) ([]string, error)

RetrieveEmails returns a slice of all message bodies (as strings) sent after epoch nanoseconds t Satisfies smtptest.Server but isn't expected to return an error.

type InProcessServer

type InProcessServer struct {
	*smtp.Server
	// We're also using this as an smtp.Session, i.e., the BAckend of the
	// *smtp.Server. This is kind of gross, but allows us to access the
	// *InmemoryEmailStore. Otherwise, we're stuck with *smtp.Server.Backend,
	// which just leaves us with the Backend interface methods.
	*InMemoryEmailStore
}

InProcessServer is an SMTPServer that runs in the same process as the test suite, letting us inspect sent emails. You must initialize this via NewInProcessServer

func NewInProcessServer

func NewInProcessServer(keypath string, certpath string) *InProcessServer

NewInProcessServer creates an InProcessServer, including configuring its SMTP server to store incoming messages in memory. Must provide the paths to the key and cert used for TLS. The cert must be a root cert.

func (*InProcessServer) Address

func (is *InProcessServer) Address() string

Address returns the host:port of the test SMTP server.

func (*InProcessServer) Close

func (is *InProcessServer) Close()

Close shuts down the test server daemon. You must initialize a new InProcessServer instead of restarting this one.

func (*InProcessServer) Start

func (is *InProcessServer) Start() error

Start starts the test server. Blocking.

type Server

type Server interface {
	// Start launches the server as a separate process and returns an error
	// if this fails. Retry behavior is left to the caller. start should also
	// set up any resources, such as local files, required to run the server.
	Start() error

	// Close terminates the serve process and any required resources. While
	// this is designed not to return an error so it's easier to use with defer,
	// implementations should log failures to close so the test operator can
	// chase down rogue server processes.
	Close()

	// RetrieveEmails returns the payloads of all email messages sent to the
	// server during the test/suite after time t in Unix epoch seconds.
	RetrieveEmails(t int64) ([]string, error)

	// Address returns the address of the server. Getting this could vary
	// between implementations, so we make it a method.
	Address() string
}

Server contains state information for an SMTP server running as a separate process. The SMTP server should be able to return the payloads of messages sent to it during the test suite. The server is meant to start during a test (or test suite) and stop right after.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL