Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Email Tests #250

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/emails/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Add the following to a .env file in this directory to
# enable live email testing when you run:
#
# go test -run ^TestLiveEmails$ ./pkg/emails

# Set to 0 in a .env file to skip the live email tests
TEST_LIVE_EMAILS=1

# Set to the email address you'd like messages sent to
TEST_LIVE_EMAIL_RECIPIENT="Tester <test@rotational.io>"

# The sender all emails will come from
TRISA_EMAIL_SENDER="Localhost Envoy <noreply@rotational.io>"

# Configure for SendGrid
TRISA_EMAIL_SENDGRID_API_KEY=

# Configure for SMTP
TRISA_EMAIL_SMTP_HOST=
TRISA_EMAIL_SMTP_PORT=
TRISA_EMAIL_SMTP_USERNAME=
TRISA_EMAIL_SMTP_PASSWORD=
TRISA_EMAIL_SMTP_USE_CRAMMD5=false
76 changes: 76 additions & 0 deletions pkg/emails/emails_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package emails_test

import (
"net/url"
"os"
"strings"
"testing"

"github.com/joho/godotenv"
"github.com/rotationalio/confire"
"github.com/stretchr/testify/require"
. "github.com/trisacrypto/envoy/pkg/emails"
"github.com/trisacrypto/envoy/pkg/sunrise"
)

func TestLiveEmails(t *testing.T) {
// Load local .env if it exists to make setting envvars easier.
godotenv.Load()

// This test will send actual emails to an account as configured by the environment.
// The $TEST_LIVE_EMAILS environment variable must be set to true to not skip.
SkipByEnvVar(t, "TEST_LIVE_EMAILS")
CheckEnvVars(t, "TEST_LIVE_EMAIL_RECIPIENT")

// Configure email sending from the environment. See .env.template for requirements.
conf := Config{}
err := confire.Process("trisa_email", &conf)
require.NoError(t, err, "environment not setup to send live emails; see .env.template")
require.True(t, conf.Available(), "no backend setup to send live emails; see .env.template")
require.NoError(t, Configure(conf), "could not configure email sending")

recipient := os.Getenv("TEST_LIVE_EMAIL_RECIPIENT")

t.Run("Invite", func(t *testing.T) {
data := SunriseInviteData{
OriginatorName: "Alice Duncan",
BeneficiaryName: "Benedict Smith",
BaseURL: &url.URL{Scheme: "http", Host: "envoy.local:8000", Path: "/sunrise/verify"},
Token: sunrise.VerificationToken("abc123"),
}

email, err := NewSunriseInvite(recipient, data)
require.NoError(t, err, "could not create sunrise invite email")

err = email.Send()
require.NoError(t, err, "could not send sunrise invite email")
})

t.Run("Verify", func(t *testing.T) {
data := VerifyEmailData{
Code: "ABC123",
}

email, err := NewVerifyEmail(recipient, data)
require.NoError(t, err, "could not create sunrise verify email")

err = email.Send()
require.NoError(t, err, "could not send sunrise verify email")
})
}

func CheckEnvVars(t *testing.T, envs ...string) {
for _, env := range envs {
require.NotEmpty(t, os.Getenv(env), "required environment variable $%s not set", env)
}
}

func SkipByEnvVar(t *testing.T, env string) {
val := strings.ToLower(strings.TrimSpace(os.Getenv(env)))
switch val {
case "1", "t", "true":
return
default:
t.Skipf("this test depends on the $%s envvar to run", env)
}
}
4 changes: 4 additions & 0 deletions pkg/web/sunrise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
"testing"
"time"

"github.com/joho/godotenv"
"github.com/stretchr/testify/require"
"github.com/trisacrypto/envoy/pkg/web/api/v1"
)

func TestSunriseIntegration(t *testing.T) {
// Load local .env if it exists to make setting envvars easier.
godotenv.Load()

// This test sends a sunrise message to a locally running server; it is skipped if
// the $SUNRISE_TEST_INTEGRATION environment variable is not set to a boolean true.
SkipByEnvVar(t, "SUNRISE_TEST_INTEGRATION")
Expand Down
Loading