From e2bcbdcb6c12e6ab135b33fea3e02782c36c4987 Mon Sep 17 00:00:00 2001
From: Nick Lupton <19624419+mr-nick17@users.noreply.github.com>
Date: Tue, 11 Apr 2023 09:37:52 +0100
Subject: [PATCH 01/19] 1599 refactor handlers and remove redundant code
---
handlers/feedback.go | 39 ++++++++++++++++-----------------------
handlers/feedback_test.go | 15 +++++----------
handlers/handlers.go | 22 ++++++++++++++++++++++
routes/routes.go | 10 ++++++----
4 files changed, 49 insertions(+), 37 deletions(-)
diff --git a/handlers/feedback.go b/handlers/feedback.go
index 4243da9..4b91b63 100644
--- a/handlers/feedback.go
+++ b/handlers/feedback.go
@@ -18,8 +18,8 @@ import (
"github.com/gorilla/schema"
)
-// Feedback represents a user's feedback
-type Feedback struct {
+// FeedbackForm represents a user's feedback
+type FeedbackForm struct {
Type string `schema:"type"`
URI string `schema:":uri"`
URL string `schema:"url"`
@@ -30,9 +30,9 @@ type Feedback struct {
}
// FeedbackThanks loads the Feedback Thank you page
-func FeedbackThanks(rend interfaces.Renderer, cacheService *cacheHelper.Helper) http.HandlerFunc {
+func (f *Feedback) FeedbackThanks() http.HandlerFunc {
return dphandlers.ControllerHandler(func(w http.ResponseWriter, req *http.Request, lang, collectionID, accessToken string) {
- feedbackThanks(w, req, req.Referer(), "", rend, cacheService, lang)
+ feedbackThanks(w, req, req.Referer(), "", f.Render, f.CacheService, lang)
})
}
@@ -75,9 +75,9 @@ func feedbackThanks(w http.ResponseWriter, req *http.Request, url, errorType str
}
// GetFeedback handles the loading of a feedback page
-func GetFeedback(rend interfaces.Renderer, cacheService *cacheHelper.Helper) http.HandlerFunc {
+func (f *Feedback) GetFeedback() http.HandlerFunc {
return dphandlers.ControllerHandler(func(w http.ResponseWriter, req *http.Request, lang, collectionID, accessToken string) {
- getFeedback(w, req, req.Referer(), "", "", "", "", lang, rend, cacheService)
+ getFeedback(w, req, req.Referer(), "", "", "", "", lang, f.Render, f.CacheService)
})
}
@@ -123,13 +123,13 @@ func getFeedback(w http.ResponseWriter, req *http.Request, url, errorType, descr
}
// AddFeedback handles a users feedback request and sends a message to slack
-func AddFeedback(to, from string, isPositive bool, rend interfaces.Renderer, emailSender email.Sender, cacheService *cacheHelper.Helper) http.HandlerFunc {
+func (f *Feedback) AddFeedback() http.HandlerFunc {
return dphandlers.ControllerHandler(func(w http.ResponseWriter, req *http.Request, lang, collectionID, accessToken string) {
- addFeedback(w, req, isPositive, rend, emailSender, from, to, lang, cacheService)
+ addFeedback(w, req, f.Render, f.EmailSender, f.Config.FeedbackFrom, f.Config.FeedbackTo, lang, f.CacheService)
})
}
-func addFeedback(w http.ResponseWriter, req *http.Request, isPositive bool, rend interfaces.Renderer, emailSender email.Sender, from, to, lang string, cacheService *cacheHelper.Helper) {
+func addFeedback(w http.ResponseWriter, req *http.Request, rend interfaces.Renderer, emailSender email.Sender, from, to, lang string, cacheService *cacheHelper.Helper) {
ctx := req.Context()
if err := req.ParseForm(); err != nil {
log.Error(ctx, "unable to parse request form", err)
@@ -140,19 +140,19 @@ func addFeedback(w http.ResponseWriter, req *http.Request, isPositive bool, rend
decoder := schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
- var f Feedback
+ var f FeedbackForm
if err := decoder.Decode(&f, req.Form); err != nil {
log.Error(ctx, "unable to decode request form", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
- if f.Description == "" && !isPositive {
+ if f.Description == "" {
getFeedback(w, req, f.URL, "description", f.Description, f.Name, f.Email, lang, rend, cacheService)
return
}
- if len(f.Email) > 0 && !isPositive {
+ if len(f.Email) > 0 {
if ok, err := regexp.MatchString(`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$`, f.Email); !ok || err != nil {
getFeedback(w, req, f.URL, "email", f.Description, f.Name, f.Email, lang, rend, cacheService)
return
@@ -166,7 +166,7 @@ func addFeedback(w http.ResponseWriter, req *http.Request, isPositive bool, rend
if err := emailSender.Send(
from,
[]string{to},
- generateFeedbackMessage(f, from, to, isPositive),
+ generateFeedbackMessage(f, from, to),
); err != nil {
log.Error(ctx, "failed to send message", err)
w.WriteHeader(http.StatusInternalServerError)
@@ -183,26 +183,19 @@ func addFeedback(w http.ResponseWriter, req *http.Request, isPositive bool, rend
http.Redirect(w, req, redirectURL, http.StatusMovedPermanently)
}
-func generateFeedbackMessage(f Feedback, from, to string, isPositive bool) []byte {
- var description string
- if isPositive {
- description = "Positive feedback received"
- } else {
- description = f.Description
- }
-
+func generateFeedbackMessage(f FeedbackForm, from, to string) []byte {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("From: %s\n", from))
b.WriteString(fmt.Sprintf("To: %s\n", to))
- b.WriteString(fmt.Sprintf("Subject: Feedback received\n\n"))
+ b.WriteString("Subject: Feedback received\n\n")
if len(f.Type) > 0 {
b.WriteString(fmt.Sprintf("Feedback Type: %s\n", f.Type))
}
b.WriteString(fmt.Sprintf("Page URL: %s\n", f.URL))
- b.WriteString(fmt.Sprintf("Description: %s\n", description))
+ b.WriteString(fmt.Sprintf("Description: %s\n", f.Description))
if len(f.Name) > 0 {
b.WriteString(fmt.Sprintf("Name: %s\n", f.Name))
diff --git a/handlers/feedback_test.go b/handlers/feedback_test.go
index 5e0f3b8..42a5e74 100644
--- a/handlers/feedback_test.go
+++ b/handlers/feedback_test.go
@@ -113,7 +113,6 @@ func Test_addFeedback(t *testing.T) {
Convey("Given a valid request", t, func() {
req := httptest.NewRequest("GET", "http://localhost?description=whatever", nil)
w := httptest.NewRecorder()
- isPositive := false
from := ""
to := ""
lang := "en"
@@ -144,7 +143,7 @@ func Test_addFeedback(t *testing.T) {
},
}}
Convey("When addFeedback is called", func() {
- addFeedback(w, req, isPositive, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
+ addFeedback(w, req, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
Convey("Then the renderer is not called", func() {
So(len(mockRenderer.BuildPageCalls()), ShouldEqual, 0)
})
@@ -160,7 +159,6 @@ func Test_addFeedback(t *testing.T) {
Convey("Given an error returned from the sender", t, func() {
req := httptest.NewRequest("GET", "http://localhost?description=whatever", nil)
w := httptest.NewRecorder()
- isPositive := false
from := ""
to := ""
lang := "en"
@@ -191,7 +189,7 @@ func Test_addFeedback(t *testing.T) {
},
}}
Convey("When addFeedback is called", func() {
- addFeedback(w, req, isPositive, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
+ addFeedback(w, req, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
Convey("Then the renderer is not called", func() {
So(len(mockRenderer.BuildPageCalls()), ShouldEqual, 0)
})
@@ -209,7 +207,6 @@ func Test_addFeedback(t *testing.T) {
Convey("Given a request with invalid form data", t, func() {
req := httptest.NewRequest("POST", "http://localhost?!@£$@$£%£$%^^&^&*", nil)
w := httptest.NewRecorder()
- isPositive := false
from := ""
to := ""
lang := "en"
@@ -240,7 +237,7 @@ func Test_addFeedback(t *testing.T) {
},
}}
Convey("When addFeedback is called", func() {
- addFeedback(w, req, isPositive, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
+ addFeedback(w, req, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
Convey("Then the renderer is not called", func() {
So(len(mockRenderer.BuildPageCalls()), ShouldEqual, 0)
})
@@ -259,7 +256,6 @@ func Test_addFeedback(t *testing.T) {
req := httptest.NewRequest("POST", "http://localhost?service=dev", nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
- isPositive := false
from := ""
to := ""
lang := "en"
@@ -290,7 +286,7 @@ func Test_addFeedback(t *testing.T) {
},
}}
Convey("When addFeedback is called", func() {
- addFeedback(w, req, isPositive, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
+ addFeedback(w, req, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
Convey("Then the renderer is called to render the feedback page", func() {
So(len(mockRenderer.BuildPageCalls()), ShouldEqual, 1)
})
@@ -308,7 +304,6 @@ func Test_addFeedback(t *testing.T) {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
- isPositive := false
from := ""
to := ""
lang := "en"
@@ -339,7 +334,7 @@ func Test_addFeedback(t *testing.T) {
},
}}
Convey("When addFeedback is called", func() {
- addFeedback(w, req, isPositive, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
+ addFeedback(w, req, mockRenderer, mockSender, from, to, lang, mockNagivationCache)
Convey("Then the renderer is called to render the feedback page", func() {
So(len(mockRenderer.BuildPageCalls()), ShouldEqual, 1)
})
diff --git a/handlers/handlers.go b/handlers/handlers.go
index f8c144c..6b891f9 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -3,9 +3,31 @@ package handlers
import (
"net/http"
+ cacheHelper "github.com/ONSdigital/dp-frontend-cache-helper/pkg/navigation/helper"
+ "github.com/ONSdigital/dp-frontend-feedback-controller/config"
+ "github.com/ONSdigital/dp-frontend-feedback-controller/email"
+ "github.com/ONSdigital/dp-frontend-feedback-controller/interfaces"
"github.com/ONSdigital/log.go/v2/log"
)
+// Feedback represents the handlers required to provide feedback
+type Feedback struct {
+ Render interfaces.Renderer
+ CacheService *cacheHelper.Helper
+ Config *config.Config
+ EmailSender email.Sender
+}
+
+// NewFeedback creates a new instance of Feedback
+func NewFeedback(rc interfaces.Renderer, c *cacheHelper.Helper, cfg *config.Config, es email.Sender) *Feedback {
+ return &Feedback{
+ Render: rc,
+ CacheService: c,
+ Config: cfg,
+ EmailSender: es,
+ }
+}
+
// ClientError is an interface that can be used to retrieve the status code if a client has errored
type ClientError interface {
Error() string
diff --git a/routes/routes.go b/routes/routes.go
index d4f78ea..2033455 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -36,12 +36,14 @@ func Setup(ctx context.Context, r *mux.Router, cfg *config.Config, rend *render.
Auth: auth,
}
+ f := handlers.NewFeedback(rend, cacheService, cfg, emailSender)
+
log.Info(ctx, "adding routes")
r.StrictSlash(true).Path("/health").HandlerFunc(hc.Handler)
- r.StrictSlash(true).Path("/feedback").Methods("GET").HandlerFunc(handlers.GetFeedback(rend, cacheService))
- r.StrictSlash(true).Path("/feedback").Methods("POST").HandlerFunc(handlers.AddFeedback(cfg.FeedbackTo, cfg.FeedbackFrom, false, rend, emailSender, cacheService))
- r.StrictSlash(true).Path("/feedback/thanks").Methods("GET").HandlerFunc(handlers.FeedbackThanks(rend, cacheService))
- r.StrictSlash(true).Path("/feedback/thanks").Methods("POST").HandlerFunc(handlers.AddFeedback(cfg.FeedbackTo, cfg.FeedbackFrom, false, rend, emailSender, cacheService))
+ r.StrictSlash(true).Path("/feedback").Methods("GET").HandlerFunc(f.GetFeedback())
+ r.StrictSlash(true).Path("/feedback").Methods("POST").HandlerFunc(f.AddFeedback())
+ r.StrictSlash(true).Path("/feedback/thanks").Methods("GET").HandlerFunc(f.FeedbackThanks())
+ r.StrictSlash(true).Path("/feedback/thanks").Methods("POST").HandlerFunc(f.AddFeedback())
}
type unencryptedAuth struct {
From 654bd1c5b7363a36a54a4fe16f4f94f56c108e77 Mon Sep 17 00:00:00 2001
From: Nick Lupton <19624419+mr-nick17@users.noreply.github.com>
Date: Wed, 12 Apr 2023 15:43:19 +0100
Subject: [PATCH 02/19] 1599 update html to design system and refactoring
---
assets/locales/service.cy.toml | 12 +-
assets/locales/service.en.toml | 12 +-
assets/templates/feedback-thanks.tmpl | 51 ++++---
assets/templates/feedback.tmpl | 192 ++++++++++++--------------
go.mod | 3 +-
go.sum | 4 -
handlers/feedback.go | 22 ++-
handlers/feedback_test.go | 10 +-
mocks/mocks.go | 16 +++
model/model.go | 1 +
10 files changed, 173 insertions(+), 150 deletions(-)
create mode 100644 mocks/mocks.go
diff --git a/assets/locales/service.cy.toml b/assets/locales/service.cy.toml
index e8975d8..77bf858 100644
--- a/assets/locales/service.cy.toml
+++ b/assets/locales/service.cy.toml
@@ -14,8 +14,8 @@
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
[FeedbackTitle]
-description = "{{.Metadata.Title}}"
-one = "{{.arg0}}"
+description = "Feedback"
+one = "Feedback"
[FeedbackDesc]
descriptions = "You can use the form below to ask a question, report a problem or suggest an improvement we can make to ONS.GOV.UK"
@@ -38,8 +38,8 @@ description = "{{.ServiceDescription}}"
one = "({{.arg0}})"
[FeedbackWhatOptNewService]
-description = "This new service"
-one = "This new service"
+description = "This {{.ServiceName}} service"
+one = "The {{.arg0}} service"
[FeedbackWhatOptGeneral]
description = "The whole ONS website or general feedback"
@@ -86,8 +86,8 @@ description = "Send feedback"
one = "Send feedback"
[FeedbackFinished]
-description = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us.
Return to {{.Metadata.Description}}"
-one = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us.
Return to {{.arg0}}"
+description = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us."
+one = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us."
[FeedbackWholeWebsite]
description = "The whole ONS website or general feedback"
diff --git a/assets/locales/service.en.toml b/assets/locales/service.en.toml
index f9f5d5f..75ba56c 100644
--- a/assets/locales/service.en.toml
+++ b/assets/locales/service.en.toml
@@ -14,8 +14,8 @@
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
[FeedbackTitle]
-description = "{{.Metadata.Title}}"
-one = "{{.arg0}}"
+description = "Feedback"
+one = "Feedback"
[FeedbackDesc]
descriptions = "You can use the form below to ask a question, report a problem or suggest an improvement we can make to ONS.GOV.UK"
@@ -38,8 +38,8 @@ description = "{{.ServiceDescription}}"
one = "({{.arg0}})"
[FeedbackWhatOptNewService]
-description = "This new service"
-one = "This new service"
+description = "This {{.ServiceName}} service"
+one = "The {{.arg0}} service"
[FeedbackWhatOptGeneral]
description = "The whole ONS website or general feedback"
@@ -86,8 +86,8 @@ description = "Send feedback"
one = "Send feedback"
[FeedbackFinished]
-description = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us.
Return to {{.Metadata.Description}}"
-one = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us.
Return to {{.arg0}}"
+description = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us."
+one = "Your feedback will help us to improve the website. We are unable to respond to all enquiries. If your matter is urgent, please contact us."
[FeedbackWholeWebsite]
description = "The whole ONS website or general feedback"
diff --git a/assets/templates/feedback-thanks.tmpl b/assets/templates/feedback-thanks.tmpl
index 99cc706..1e576cb 100644
--- a/assets/templates/feedback-thanks.tmpl
+++ b/assets/templates/feedback-thanks.tmpl
@@ -1,21 +1,36 @@
-{{$Language := .Language}}
-
{{ localise "FeedbackDesc" $Language 1}}
-