Skip to content

Commit

Permalink
MLPAB-1873 Add email field to supporter your details page (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Mar 8, 2024
1 parent 6b86bcb commit f2e38b1
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func App(
shareCodeStore := &shareCodeStore{dynamoClient: lpaDynamoClient}
dashboardStore := &dashboardStore{dynamoClient: lpaDynamoClient}
evidenceReceivedStore := &evidenceReceivedStore{dynamoClient: lpaDynamoClient}
organisationStore := &organisationStore{dynamoClient: lpaDynamoClient, now: time.Now, uuidString: uuid.NewString}
organisationStore := &organisationStore{dynamoClient: lpaDynamoClient, now: time.Now, uuidString: uuid.NewString, newUID: actoruid.New}
memberStore := &memberStore{dynamoClient: lpaDynamoClient, now: time.Now, uuidString: uuid.NewString}

shareCodeSender := page.NewShareCodeSender(shareCodeStore, notifyClient, appPublicURL, random.String, eventClient)
Expand Down
6 changes: 6 additions & 0 deletions internal/app/organisation_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"time"

"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
)

type organisationStore struct {
dynamoClient DynamoClient
uuidString func() string
newUID func() actoruid.UID
randomString func(int) string
now func() time.Time
}
Expand Down Expand Up @@ -124,13 +126,17 @@ func (s *organisationStore) CreateLPA(ctx context.Context) (*actor.DonorProvided
}

lpaID := s.uuidString()
donorUID := s.newUID()

donor := &actor.DonorProvidedDetails{
PK: lpaKey(lpaID),
SK: organisationKey(data.OrganisationID),
LpaID: lpaID,
CreatedAt: s.now(),
Version: 1,
Donor: actor.Donor{
UID: donorUID,
},
}

if donor.Hash, err = donor.GenerateHash(); err != nil {
Expand Down
17 changes: 15 additions & 2 deletions internal/app/organisation_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ func TestOrganisationStoreCreateLPA(t *testing.T) {
LpaID: "a-uuid",
CreatedAt: testNow,
Version: 1,
Donor: actor.Donor{
UID: testUID,
},
}
expectedDonor.Hash, _ = expectedDonor.GenerateHash()

Expand All @@ -238,7 +241,12 @@ func TestOrganisationStoreCreateLPA(t *testing.T) {
Create(ctx, expectedDonor).
Return(nil)

organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn, uuidString: func() string { return "a-uuid" }}
organisationStore := &organisationStore{
dynamoClient: dynamoClient,
now: testNowFn,
uuidString: func() string { return "a-uuid" },
newUID: testUIDFn,
}

donor, err := organisationStore.CreateLPA(ctx)

Expand Down Expand Up @@ -270,7 +278,12 @@ func TestOrganisationStoreCreateLPAWhenDynamoError(t *testing.T) {
Create(ctx, mock.Anything).
Return(expectedError)

organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn, uuidString: func() string { return "a-uuid" }}
organisationStore := &organisationStore{
dynamoClient: dynamoClient,
now: testNowFn,
uuidString: func() string { return "a-uuid" },
newUID: testUIDFn,
}

_, err := organisationStore.CreateLPA(ctx)

Expand Down
6 changes: 6 additions & 0 deletions internal/page/donor/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ var (
LpaID: "lpa-id",
Lang: localize.En,
}
testSupporterAppData = page.AppData{
SessionID: "session-id",
LpaID: "lpa-id",
IsSupporter: true,
Lang: localize.En,
}
testNow = time.Date(2023, time.July, 3, 4, 5, 6, 1, time.UTC)
testNowFn = func() time.Time { return testNow }
testUID = actoruid.New()
Expand Down
28 changes: 19 additions & 9 deletions internal/page/donor/your_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ func YourDetails(tmpl template.Template, donorStore DonorStore, sessionStore Ses
}

if r.Method == http.MethodPost {
loginSession, err := sessionStore.Login(r)
if err != nil {
return err
}
if loginSession.Email == "" {
return fmt.Errorf("no email in login session")
}

data.Form = readYourDetailsForm(r)
data.Errors = data.Form.Validate()
dobWarning := data.Form.DobWarning()
Expand Down Expand Up @@ -79,7 +71,20 @@ func YourDetails(tmpl template.Template, donorStore DonorStore, sessionStore Ses

donor.Donor.OtherNames = data.Form.OtherNames
donor.Donor.ThinksCanSign = data.Form.CanSign
donor.Donor.Email = loginSession.Email

if appData.IsSupporter {
donor.Donor.Email = data.Form.Email
} else {
loginSession, err := sessionStore.Login(r)
if err != nil {
return err
}
if loginSession.Email == "" {
return fmt.Errorf("no email in login session")
}

donor.Donor.Email = loginSession.Email
}

if donor.Donor.ThinksCanSign.IsYes() {
donor.Donor.CanSign = form.Yes
Expand Down Expand Up @@ -111,6 +116,7 @@ type yourDetailsForm struct {
FirstNames string
LastName string
OtherNames string
Email string
Dob date.Date
CanSign actor.YesNoMaybe
CanSignError error
Expand All @@ -124,6 +130,7 @@ func readYourDetailsForm(r *http.Request) *yourDetailsForm {
d.FirstNames = page.PostFormString(r, "first-names")
d.LastName = page.PostFormString(r, "last-name")
d.OtherNames = page.PostFormString(r, "other-names")
d.Email = page.PostFormString(r, "email")

d.Dob = date.New(
page.PostFormString(r, "date-of-birth-year"),
Expand All @@ -149,6 +156,9 @@ func (f *yourDetailsForm) Validate() validation.List {
validation.Empty(),
validation.StringTooLong(61))

errors.String("email", "email", f.Email,
validation.Email())

errors.String("other-names", "otherNamesYouAreKnownBy", f.OtherNames,
validation.StringTooLong(50))

Expand Down
91 changes: 85 additions & 6 deletions internal/page/donor/your_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,90 @@ func TestPostYourDetails(t *testing.T) {
}
}

func TestPostYourDetailsWhenSupporter(t *testing.T) {
validBirthYear := strconv.Itoa(time.Now().Year() - 40)

testCases := map[string]struct {
form url.Values
person actor.Donor
redirect page.LpaPath
}{
"with email": {
form: url.Values{
"first-names": {"John"},
"last-name": {"Doe"},
"email": {"[email protected]"},
"date-of-birth-day": {"2"},
"date-of-birth-month": {"1"},
"date-of-birth-year": {validBirthYear},
"can-sign": {actor.Yes.String()},
},
person: actor.Donor{
FirstNames: "John",
LastName: "Doe",
DateOfBirth: date.New(validBirthYear, "1", "2"),
Address: place.Address{Line1: "abc"},
Email: "[email protected]",
ThinksCanSign: actor.Yes,
CanSign: form.Yes,
},
redirect: page.Paths.YourAddress,
},
"without email": {
form: url.Values{
"first-names": {"John"},
"last-name": {"Doe"},
"date-of-birth-day": {"2"},
"date-of-birth-month": {"1"},
"date-of-birth-year": {validBirthYear},
"can-sign": {actor.Yes.String()},
},
person: actor.Donor{
FirstNames: "John",
LastName: "Doe",
DateOfBirth: date.New(validBirthYear, "1", "2"),
Address: place.Address{Line1: "abc"},
Email: "",
ThinksCanSign: actor.Yes,
CanSign: form.Yes,
},
redirect: page.Paths.YourAddress,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()

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

donorStore := newMockDonorStore(t)
donorStore.EXPECT().
Put(r.Context(), &actor.DonorProvidedDetails{
LpaID: "lpa-id",
Donor: tc.person,
Tasks: actor.DonorTasks{YourDetails: actor.TaskInProgress},
}).
Return(nil)

err := YourDetails(nil, donorStore, nil)(testSupporterAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
Donor: actor.Donor{
FirstNames: "John",
Address: place.Address{Line1: "abc"},
},
HasSentApplicationUpdatedEvent: true,
})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, tc.redirect.Format("lpa-id"), resp.Header.Get("Location"))
})
}
}

func TestPostYourDetailsWhenDetailsNotChanged(t *testing.T) {
validBirthYear := strconv.Itoa(time.Now().Year() - 40)
f := url.Values{
Expand Down Expand Up @@ -425,12 +509,7 @@ func TestPostYourDetailsWhenInputRequired(t *testing.T) {
})).
Return(nil)

sessionStore := newMockSessionStore(t)
sessionStore.EXPECT().
Login(mock.Anything).
Return(&sesh.LoginSession{Sub: "xyz", Email: "[email protected]"}, nil)

err := YourDetails(template.Execute, nil, sessionStore)(testAppData, w, r, &actor.DonorProvidedDetails{})
err := YourDetails(template.Execute, nil, nil)(testAppData, w, r, &actor.DonorProvidedDetails{})
resp := w.Result()

assert.Nil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions web/template/donor/your_details.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

{{ template "input" (input . "other-names" "otherNamesYouAreKnownByOptional" .Form.OtherNames "hint" "otherNamesHint" "classes" "govuk-input--width-20") }}

{{ if .App.IsSupporter }}
{{ template "input" (input . "email" "emailOptional" .Form.Email "classes" "govuk-input--width-20" "type" "email" "spellcheck" "false" "autocomplete" "email") }}
{{ end }}

{{ template "date" (input . "date-of-birth" "dateOfBirth" .Form.Dob "hint" "dateOfBirthHint") }}

{{ template "dob-warning" . }}
Expand Down

0 comments on commit f2e38b1

Please sign in to comment.