Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Fixed testing that was broken from the SMTP changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcourant committed Sep 11, 2021
1 parent 8bd5d5d commit bd00852
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/environments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
ref: 'main'
- name: fetch
run: git fetch --prune --unshallow
- name: Push To Dog Food
- name: Push To Acceptance
run: git push origin $(go run github.com/monetr/rest-api/tools/releaser --since=-24h):acceptance
release-dog:
name: Dog Food
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Controller) configEndpoint(ctx *context.Context) {

// We can only allow forgot password if SMTP is enabled. Otherwise we have
// no way of sending an email to the user.
if c.configuration.SMTP.Enabled {
if c.configuration.EMail.Enabled {
configuration.AllowForgotPassword = true
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package controller
import (
"context"
"fmt"
"github.com/monetr/rest-api/pkg/internal/platypus"
"github.com/monetr/rest-api/pkg/mail"
"net/http"
"net/smtp"
"strings"
Expand All @@ -20,6 +18,7 @@ import (
"github.com/monetr/rest-api/pkg/build"
"github.com/monetr/rest-api/pkg/cache"
"github.com/monetr/rest-api/pkg/config"
"github.com/monetr/rest-api/pkg/internal/platypus"
"github.com/monetr/rest-api/pkg/internal/stripe_helper"
"github.com/monetr/rest-api/pkg/jobs"
"github.com/monetr/rest-api/pkg/metrics"
Expand Down Expand Up @@ -49,7 +48,6 @@ type Controller struct {
stripe stripe_helper.Stripe
ps pubsub.PublishSubscribe
cache *redis.Pool
email mail.Communication
accounts billing.AccountRepository
paywall billing.BasicPayWall
billing billing.BasicBilling
Expand Down Expand Up @@ -100,7 +98,6 @@ func NewController(
stripe: stripe,
ps: pubSub,
cache: cachePool,
email: mail.NewSMTPCommunication(log, configuration.SMTP),
accounts: accountsRepo,
paywall: basicPaywall,
billing: basicBilling,
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package controller_test

import (
"net/http"
"testing"

"github.com/alicebob/miniredis/v2"
"github.com/brianvoe/gofakeit/v6"
"github.com/gomodule/redigo/redis"
Expand All @@ -19,8 +22,6 @@ import (
"github.com/monetr/rest-api/pkg/secrets"
"github.com/plaid/plaid-go/plaid"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

func NewTestApplicationConfig(t *testing.T) config.Configuration {
Expand All @@ -34,7 +35,7 @@ func NewTestApplicationConfig(t *testing.T) config.Configuration {
RegistrationJwtSecret: gofakeit.UUID(),
},
PostgreSQL: config.PostgreSQL{},
SMTP: config.SMTPClient{},
EMail: config.Email{},
ReCAPTCHA: config.ReCAPTCHA{},
Plaid: config.Plaid{
ClientID: gofakeit.UUID(),
Expand Down
15 changes: 3 additions & 12 deletions pkg/controller/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"context"
"fmt"
"github.com/monetr/rest-api/pkg/mail"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -129,7 +128,7 @@ func (c *Controller) registerEndpoint(ctx iris.Context) {
hashedPassword,
registerRequest.FirstName,
registerRequest.LastName,
!(c.configuration.SMTP.Enabled && c.configuration.SMTP.VerifyEmails),
!(c.configuration.EMail.ShouldVerifyEmails()), // Set isEnabled to true if we are not verifying emails.
)
if err != nil {
c.wrapAndReturnError(ctx, err, http.StatusInternalServerError,
Expand Down Expand Up @@ -226,16 +225,8 @@ func (c *Controller) registerEndpoint(ctx iris.Context) {

// If SMTP is enabled and we are verifying emails then we want to create a
// registration record and send the user a verification email.
if c.configuration.SMTP.ShouldVerifyEmails() {
if err = c.email.Send(c.getContext(ctx), mail.SendEmailRequest{
From: fmt.Sprintf("no-reply@%s", "monetr.mini"),
To: registerRequest.Email,
Subject: "Verify your email address",
IsHTML: true,
Content: "<html><body><h1>Hello World!</h1></body></html>",
}); err != nil {
log.WithError(err).Warn("failed to send verification email for new user registration")
}
if c.configuration.EMail.ShouldVerifyEmails() {
log.Debug("TODO send email here")
}

// If we are not requiring email verification to activate an account we can
Expand Down
34 changes: 0 additions & 34 deletions pkg/controller/verify.go
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
package controller

import (
"fmt"
"github.com/pkg/errors"
"net/smtp"
)

func (c *Controller) sendEmailVerification(emailAddress, code string) error {
conf := c.configuration.SMTP
auth := smtp.PlainAuth(
conf.Identity,
conf.Username,
conf.Password,
conf.Host,
)

to := []string{emailAddress}
from := "[email protected]"
msg := []byte(fmt.Sprintf("To: %s\r\n"+
"From: no-reply\r\n"+
"Subject: Please Verify Your Email Address\r\n"+
"\r\n"+
"Click the link to verify your account: %s/registration/verify?token=%s\r\n",
emailAddress,
c.configuration.UIDomainName,
code,
))
address := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
if err := smtp.SendMail(address, auth, from, to, msg); err != nil {
return errors.Wrap(err, "failed to send email")
}

return nil
}

0 comments on commit bd00852

Please sign in to comment.