Skip to content

Go

Terminal window
go get github.com/mailcatchr/mailcatchr-sdk/go
package app_test
import (
"context"
"fmt"
"os"
"testing"
"time"
mailcatchr "github.com/mailcatchr/mailcatchr-sdk/go"
)
var (
mc = mailcatchr.New(os.Getenv("MAILCATCHR_API_KEY"))
mailboxID = os.Getenv("MAILCATCHR_MAILBOX_ID")
)
// uniqueAddress gives each test its own recipient so parallel tests never see each other's mail.
func uniqueAddress(label string) string {
return fmt.Sprintf("%s-%d@<mailbox-id>.mailcatchr.com", label, time.Now().UnixMilli())
}
func TestPasswordResetEmailContainsAWorkingLink(t *testing.T) {
t.Parallel()
ctx := context.Background()
address := uniqueAddress("reset")
app.RequestPasswordReset(address)
summary, err := mc.AwaitMessage(ctx, mailboxID, mailcatchr.AwaitOptions{
To: address, Subject: "Reset your password", TimeoutSeconds: 30,
})
if err != nil {
t.Fatal(err)
}
if summary == nil {
t.Fatal("the reset email should arrive within 30 s")
}
message, err := mc.GetMessage(ctx, mailboxID, summary.ID)
if err != nil {
t.Fatal(err)
}
link := resetLinkPattern.FindString(*message.TextBody)
if link == "" {
t.Fatal("reset link in the email body")
}
if got := app.Get(link).StatusCode; got != 200 {
t.Fatalf("reset link answered %d", got)
}
}

AwaitMessage returns nil, nil on timeout and an error only for transport or API failures, so the two cases stay distinct.

code, err := mc.TOTPCode(ctx, os.Getenv("MAILCATCHR_TOTP_ID"))
if mailcatchr.IsStatus(err, 404) {
// out of the key's scope, or deleted
}