Skip to content

Commit

Permalink
feature: tests for the "send_email" handler
Browse files Browse the repository at this point in the history
RHCLOUD-29047
  • Loading branch information
MikelAlejoBR authored and MikelAlejoBR committed Mar 25, 2024
1 parent 46aae06 commit 8168e13
Show file tree
Hide file tree
Showing 4 changed files with 700 additions and 49 deletions.
102 changes: 53 additions & 49 deletions internal/handlers/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@ import (
"github.com/redhatinsights/mbop/internal/service/mailer"
)

// SendEmails sends the incoming payload's emails through the configured mailer module.
func SendEmails(w http.ResponseWriter, r *http.Request) {
switch config.Get().MailerModule {
case awsModule, printModule:
body, err := io.ReadAll(r.Body)
if err != nil {
do500(w, "failed to read request body: "+err.Error())
return
}
defer r.Body.Close()

var emails models.Emails
err = json.Unmarshal(body, &emails)
if err != nil {
do400(w, "failed to parse request body: "+err.Error())
return
}

// create our mailer (using the correct interface)
sender, err := mailer.NewMailer()
if err != nil {
Expand All @@ -36,50 +23,67 @@ func SendEmails(w http.ResponseWriter, r *http.Request) {
return
}

// The user might have wanted to override the sender that goes in the "from" field of the email.
var fromAddress string
if emails.EmailSender != "" {
fromAddress = emails.EmailSender
} else {
fromAddress = config.Get().FromEmail
}
sendEmails(w, r, sender)
default:
CatchAll(w, r)
}
}

for _, email := range emails.Emails {
// creating a copy in order to pass it down into sub-functions
email := email
// sendEmails reads the unmarshalls the incoming body, and it verifies that it is correct. It determines if the email
// sender was overridden and resolves the specified non-email users through the configured user module. Finally, it
// determines if the default recipient must be grabbed from the config or if, on the other hand, we must set it to what
// the user asked it to be set.
func sendEmails(w http.ResponseWriter, r *http.Request, sender mailer.Emailer) {
body, err := io.ReadAll(r.Body)
if err != nil {
do500(w, "failed to read request body: "+err.Error())
return
}
defer r.Body.Close()

// Lookup the emails for the given usernames unless the client specified otherwise.
if !emails.SkipUsersResolution {
err := mailer.LookupEmailsForUsernames(r.Context(), &email)
if err != nil {
l.Log.Error(err, "error translating usernames")
continue
}
var emails models.Emails
err = json.Unmarshal(body, &emails)
if err != nil {
do400(w, "failed to parse request body: "+err.Error())
return
}

if len(email.Recipients) == 0 {
email.Recipients = []string{config.Get().ToEmail}
}
}
// The user might have wanted to override the sender that goes in the "from" field of the email.
var fromAddress string
if emails.EmailSender != "" {
fromAddress = emails.EmailSender
} else {
fromAddress = config.Get().FromEmail
}

// Should the user not specify any recipients, we need to determine if we should use the "default
// recipient" the user might have specified, or the default one that we set up in the configuration.
if len(email.Recipients) == 0 {
if emails.DefaultRecipient != "" {
email.Recipients = []string{emails.DefaultRecipient}
} else {
email.Recipients = []string{config.Get().ToEmail}
}
}
for _, email := range emails.Emails {
// creating a copy in order to pass it down into sub-functions
email := email

err = sender.SendEmail(r.Context(), &email, fromAddress)
// Lookup the emails for the given usernames unless the client specified otherwise.
if !emails.SkipUsersResolution {
err := mailer.LookupEmailsForUsernames(r.Context(), &email)
if err != nil {
l.Log.Error(err, "Error sending email", "email", email)
l.Log.Error(err, "error translating usernames")
continue
}
}

sendJSON(w, newResponse("success"))
// Should the user not specify any recipients, we need to determine if we should use the "default
// recipient" the user might have specified, or the default one that we set up in the configuration.
if len(email.Recipients) == 0 {
if emails.DefaultRecipient == "" {
email.Recipients = []string{config.Get().ToEmail}
} else {
email.Recipients = []string{emails.DefaultRecipient}
}
}

default:
CatchAll(w, r)
err = sender.SendEmail(r.Context(), &email, fromAddress)
if err != nil {
l.Log.Error(err, "Error sending email", "email", email)
}
}

sendJSON(w, newResponse("success"))
}
Loading

0 comments on commit 8168e13

Please sign in to comment.