Skip to content

Commit

Permalink
MLPAB-1757: Extract field name and options to YesNoForm (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Jan 22, 2024
1 parent 72ec064 commit b48d45c
Show file tree
Hide file tree
Showing 42 changed files with 316 additions and 336 deletions.
30 changes: 17 additions & 13 deletions internal/form/enum_yesno.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/form/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ var FieldNames = SharedFieldNames{
Postcode: "address-postcode",
Action: "action",
},
YesNo: "yes-no",
}

type SharedFieldNames struct {
Address AddressFieldNames
LanguagePreference string
YesNo string
}

type AddressFieldNames struct {
Expand Down
7 changes: 4 additions & 3 deletions internal/form/yes_no.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package form

//go:generate enumerator -type YesNo -linecomment -empty
//go:generate enumerator -type YesNo -linecomment -trimprefix
type YesNo uint8

const (
Yes YesNo = iota + 1 // yes
No // no
YesNoUnknown YesNo = iota
Yes // yes
No // no
)
23 changes: 16 additions & 7 deletions internal/form/yes_no_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ type YesNoForm struct {
YesNo YesNo
Error error
ErrorLabel string
Options YesNoOptions
FieldName string
}

func ReadYesNoForm(r *http.Request, errorLabel string) *YesNoForm {
yesNo, err := ParseYesNo(PostFormString(r, "yes-no"))
form := NewYesNoForm(YesNoUnknown)

return &YesNoForm{
YesNo: yesNo,
Error: err,
ErrorLabel: errorLabel,
}
form.YesNo, form.Error = ParseYesNo(PostFormString(r, form.FieldName))
form.ErrorLabel = errorLabel

return form
}

func (f *YesNoForm) Validate() validation.List {
var errors validation.List

errors.Error("yes-no", f.ErrorLabel, f.Error,
errors.Error(f.FieldName, f.ErrorLabel, f.Error,
validation.Selected())

return errors
Expand All @@ -35,3 +36,11 @@ func (f *YesNoForm) Validate() validation.List {
func PostFormString(r *http.Request, name string) string {
return strings.TrimSpace(r.PostFormValue(name))
}

func NewYesNoForm(yesNo YesNo) *YesNoForm {
return &YesNoForm{
YesNo: yesNo,
Options: YesNoValues,
FieldName: FieldNames.YesNo,
}
}
20 changes: 15 additions & 5 deletions internal/form/yes_no_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func TestReadYesNoForm(t *testing.T) {
form := url.Values{"yes-no": {Yes.String()}}
form := url.Values{FieldNames.YesNo: {Yes.String()}}
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

assert.Equal(t, &YesNoForm{YesNo: Yes, ErrorLabel: "a-label"}, ReadYesNoForm(r, "a-label"))
assert.Equal(t, &YesNoForm{YesNo: Yes, ErrorLabel: "a-label", Options: YesNoValues, FieldName: FieldNames.YesNo}, ReadYesNoForm(r, "a-label"))
}

func TestYesNoFormValidate(t *testing.T) {
Expand All @@ -25,11 +25,11 @@ func TestYesNoFormValidate(t *testing.T) {
errors validation.List
}{
"valid": {
form: &YesNoForm{},
form: NewYesNoForm(YesNoUnknown),
},
"invalid": {
form: &YesNoForm{Error: errors.New("err"), ErrorLabel: "a-label"},
errors: validation.With("yes-no", validation.SelectError{Label: "a-label"}),
form: &YesNoForm{Error: errors.New("err"), ErrorLabel: "a-label", FieldName: FieldNames.YesNo},
errors: validation.With(FieldNames.YesNo, validation.SelectError{Label: "a-label"}),
},
}

Expand All @@ -39,3 +39,13 @@ func TestYesNoFormValidate(t *testing.T) {
})
}
}

func TestNewYesNoForm(t *testing.T) {
for _, yesNo := range []YesNo{Yes, No, YesNoUnknown} {
assert.Equal(t, &YesNoForm{
YesNo: yesNo,
Options: YesNoValues,
FieldName: "yes-no",
}, NewYesNoForm(yesNo))
}
}
12 changes: 5 additions & 7 deletions internal/page/attorney/would_like_second_signatory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ import (
)

type wouldLikeSecondSignatoryData struct {
App page.AppData
Errors validation.List
YesNo form.YesNo
Options form.YesNoOptions
App page.AppData
Errors validation.List
Form *form.YesNoForm
}

func WouldLikeSecondSignatory(tmpl template.Template, attorneyStore AttorneyStore, donorStore DonorStore, lpaStoreClient LpaStoreClient) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, attorneyProvidedDetails *actor.AttorneyProvidedDetails) error {
data := &wouldLikeSecondSignatoryData{
App: appData,
YesNo: attorneyProvidedDetails.WouldLikeSecondSignatory,
Options: form.YesNoValues,
App: appData,
Form: form.NewYesNoForm(attorneyProvidedDetails.WouldLikeSecondSignatory),
}

if r.Method == http.MethodPost {
Expand Down
26 changes: 13 additions & 13 deletions internal/page/attorney/would_like_second_signatory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestGetWouldLikeSecondSignatory(t *testing.T) {
template := newMockTemplate(t)
template.EXPECT().
Execute(w, &wouldLikeSecondSignatoryData{
App: testAppData,
Options: form.YesNoValues,
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
}).
Return(nil)

Expand All @@ -42,8 +42,8 @@ func TestGetWouldLikeSecondSignatoryWhenTemplateErrors(t *testing.T) {
template := newMockTemplate(t)
template.EXPECT().
Execute(w, &wouldLikeSecondSignatoryData{
App: testAppData,
Options: form.YesNoValues,
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
}).
Return(expectedError)

Expand All @@ -56,7 +56,7 @@ func TestGetWouldLikeSecondSignatoryWhenTemplateErrors(t *testing.T) {

func TestPostWouldLikeSecondSignatoryWhenYes(t *testing.T) {
f := url.Values{
"yes-no": {form.Yes.String()},
form.FieldNames.YesNo: {form.Yes.String()},
}

w := httptest.NewRecorder()
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPostWouldLikeSecondSignatoryWhenNo(t *testing.T) {
}

f := url.Values{
"yes-no": {form.No.String()},
form.FieldNames.YesNo: {form.No.String()},
}

w := httptest.NewRecorder()
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestPostWouldLikeSecondSignatoryWhenLpaStoreClientErrors(t *testing.T) {
donor := &actor.DonorProvidedDetails{SignedAt: time.Now()}

f := url.Values{
"yes-no": {form.No.String()},
form.FieldNames.YesNo: {form.No.String()},
}

w := httptest.NewRecorder()
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestPostWouldLikeSecondSignatoryWhenLpaStoreClientErrors(t *testing.T) {

func TestPostWouldLikeSecondSignatoryWhenDonorStoreErrors(t *testing.T) {
f := url.Values{
"yes-no": {form.No.String()},
form.FieldNames.YesNo: {form.No.String()},
}

w := httptest.NewRecorder()
Expand All @@ -176,7 +176,7 @@ func TestPostWouldLikeSecondSignatoryWhenDonorStoreErrors(t *testing.T) {

func TestPostWouldLikeSecondSignatoryWhenAttorneyStoreErrors(t *testing.T) {
form := url.Values{
"yes-no": {form.No.String()},
form.FieldNames.YesNo: {form.No.String()},
}

w := httptest.NewRecorder()
Expand All @@ -193,15 +193,15 @@ func TestPostWouldLikeSecondSignatoryWhenAttorneyStoreErrors(t *testing.T) {
}

func TestPostWouldLikeSecondSignatoryWhenValidationError(t *testing.T) {
form := url.Values{
"yes-no": {""},
f := url.Values{
form.FieldNames.YesNo: {""},
}

w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

validationError := validation.With("yes-no", validation.SelectError{Label: "yesIfWouldLikeSecondSignatory"})
validationError := validation.With(form.FieldNames.YesNo, validation.SelectError{Label: "yesIfWouldLikeSecondSignatory"})

template := newMockTemplate(t)
template.EXPECT().
Expand Down
2 changes: 1 addition & 1 deletion internal/page/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ChooseReplacementAttorneysState(donor *actor.DonorProvidedDetails) actor.Ta
}

if donor.ReplacementAttorneys.Len() == 0 {
if donor.WantReplacementAttorneys.Empty() {
if donor.WantReplacementAttorneys.IsUnknown() {
return actor.TaskNotStarted
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)

type areYouApplyingForFeeDiscountOrExemption struct {
type areYouApplyingForFeeDiscountOrExemptionData struct {
App page.AppData
Errors validation.List
CertificateProvider actor.CertificateProvider
Options form.YesNoOptions
Form *form.YesNoForm
}

func AreYouApplyingForFeeDiscountOrExemption(tmpl template.Template, payer Payer, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &areYouApplyingForFeeDiscountOrExemption{
data := &areYouApplyingForFeeDiscountOrExemptionData{
App: appData,
CertificateProvider: donor.CertificateProvider,
Options: form.YesNoValues,
Form: form.NewYesNoForm(form.YesNoUnknown),
}

if r.Method == http.MethodPost {
Expand Down
Loading

0 comments on commit b48d45c

Please sign in to comment.