Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

feature: Add Send Notification Email Endpoint #10

Merged
merged 1 commit into from
Feb 15, 2023
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
29 changes: 19 additions & 10 deletions aws/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@ var (

// SESEmail represents an email context
type SESEmail struct {
To string
Cc string `default:""`
Bcc string `default:""`
Subject string
Body string
To string `json:"to"`
Cc string `json:"cc" default:""`
Bcc string `json:"bcc" default:""`
Subject string `json:"subject"`
Body string `json:"body"`
}

// NewSESEmail returns an initialized SES email context
func NewSESEmail(to, cc, bcc, subject, body string) *SESEmail {
// NewSESEmail returns an initialized SES email context.
func NewSESEmail() *SESEmail {
// Load assumed user role AWS configuration
cfg := AssumedRoleConfig()
// Initialize AWS Simple Email Service Client
client = sesv2.NewFromConfig(cfg)
return &SESEmail{To: to, Cc: cc, Bcc: bcc, Subject: subject, Body: body}
return &SESEmail{}
}

// Send sends an email using AWS SES
// BuildMessage builds email context.
func (e *SESEmail) BuildMessage(to, cc, bcc, subject, body string) {
e.To = to
e.Cc = cc
e.Bcc = bcc
e.Subject = subject
e.Body = body
}

// Send sends an email using AWS SES.
func (e *SESEmail) Send() error {
// Build email context
// Input email content
input := &sesv2.SendEmailInput{
FromEmailAddress: &from,
Destination: &types.Destination{
Expand Down
32 changes: 30 additions & 2 deletions controllers/controllers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package controllers

import (
"encoding/json"
"fmt"
"net/http"

"github.com/cisagov/con-pca-tasks/aws"
"github.com/go-chi/chi/v5"
)

// HealthCheckHandler indicates that the server is up and running.
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// healthCheckHandler indicates that the server is up and running.
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Up and running!")
}

// notificationEmailsHandler manages notificaiton emails.
func notificationEmailsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

decoder := json.NewDecoder(r.Body)

var e aws.SESEmail
decoder.Decode(&e)

email := aws.NewSESEmail()
email.BuildMessage(e.To, e.Cc, e.Bcc, e.Subject, e.Body)
email.Send()

fmt.Fprintf(w, "Notification email sent!")
}

func TasksRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", healthCheckHandler)
r.Post("/notifications/send", notificationEmailsHandler)

return r
}
19 changes: 17 additions & 2 deletions controllers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func TestHealthCheckHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req := httptest.NewRequest(http.MethodGet, "/tasks", nil)
w := httptest.NewRecorder()
HealthCheckHandler(w, req)
healthCheckHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
Expand All @@ -21,3 +21,18 @@ func TestHealthCheckHandler(t *testing.T) {
t.Errorf("expected 'Up and running!', but got %v", string(data))
}
}

func TestNotificationEmailsHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/tasks", nil)
w := httptest.NewRecorder()
notificationEmailsHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if string(data) != "Notification email sent!" {
t.Errorf("expected 'Notification email sent!', but got %v", string(data))
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
version()

mux := chi.NewRouter()
mux.Get("/", controllers.HealthCheckHandler)
mux.Mount("/tasks", controllers.TasksRouter())

port := ":8080"
log.Printf("listening on port %s, version %s", port, Version)
Expand Down