Skip to content

Commit

Permalink
Merge 99d8efb into 63e1788
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Aug 22, 2024
2 parents 63e1788 + 99d8efb commit 87b1830
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 102 deletions.
2 changes: 1 addition & 1 deletion internal/voucher/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p Path) Redirect(w http.ResponseWriter, r *http.Request, appData appcontex
func (p Path) CanGoTo(provided *voucherdata.Provided) bool {
switch p {
case PathYourName:
return !provided.Tasks.ConfirmYourIdentity.IsCompleted()
return provided.Tasks.ConfirmYourIdentity.IsNotStarted()

case PathVerifyDonorDetails:
return provided.Tasks.ConfirmYourName.IsCompleted() &&
Expand Down
12 changes: 10 additions & 2 deletions internal/voucher/voucherpage/confirm_allowed_to_vouch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package voucherpage
import (
"errors"
"net/http"
"strings"

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
Expand All @@ -20,6 +21,7 @@ type confirmAllowedToVouchData struct {
Form *form.YesNoForm
Lpa *lpadata.Lpa
SurnameMatchesDonor bool
MatchIdentity bool
}

func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaStoreResolvingService, voucherStore VoucherStore) Handler {
Expand All @@ -33,7 +35,8 @@ func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaS
App: appData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: lpa,
SurnameMatchesDonor: provided.LastName == lpa.Donor.LastName,
SurnameMatchesDonor: strings.EqualFold(provided.LastName, lpa.Donor.LastName),
MatchIdentity: provided.Tasks.ConfirmYourIdentity.IsInProgress(),
}

if r.Method == http.MethodPost {
Expand All @@ -45,7 +48,12 @@ func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaS
return errors.New("// TODO there should be a page here but it hasn't been built yet")
}

provided.Tasks.ConfirmYourName = task.StateCompleted
if provided.Tasks.ConfirmYourIdentity.IsInProgress() {
provided.Tasks.ConfirmYourIdentity = task.StateCompleted
} else {
provided.Tasks.ConfirmYourName = task.StateCompleted
}

if err := voucherStore.Put(r.Context(), provided); err != nil {
return err
}
Expand Down
165 changes: 117 additions & 48 deletions internal/voucher/voucherpage/confirm_allowed_to_vouch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,96 @@ import (
)

func TestGetConfirmAllowedToVouch(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)

lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(&lpadata.Lpa{
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
}, nil)

template := newMockTemplate(t)
template.EXPECT().
Execute(w, &confirmAllowedToVouchData{
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: &lpadata.Lpa{
testcases := map[string]struct {
lpa *lpadata.Lpa
provided *voucherdata.Provided
data *confirmAllowedToVouchData
}{
"actor matches": {
lpa: &lpadata.Lpa{
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
SurnameMatchesDonor: true,
}).
Return(nil)
provided: &voucherdata.Provided{
LpaID: "lpa-id",
FirstNames: "V",
LastName: "W",
},
data: &confirmAllowedToVouchData{
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: &lpadata.Lpa{
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
},
},
"surname matches donor": {
lpa: &lpadata.Lpa{
Donor: lpadata.Donor{FirstNames: "A", LastName: "W"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
provided: &voucherdata.Provided{
LpaID: "lpa-id",
FirstNames: "V",
LastName: "W",
},
data: &confirmAllowedToVouchData{
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: &lpadata.Lpa{
Donor: lpadata.Donor{FirstNames: "A", LastName: "W"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
SurnameMatchesDonor: true,
},
},
"matches actor after identity": {
lpa: &lpadata.Lpa{
Donor: lpadata.Donor{FirstNames: "A", LastName: "W"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
provided: &voucherdata.Provided{
LpaID: "lpa-id",
FirstNames: "V",
LastName: "W",
Tasks: voucherdata.Tasks{
ConfirmYourIdentity: task.StateInProgress,
},
},
data: &confirmAllowedToVouchData{
App: testAppData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: &lpadata.Lpa{
Donor: lpadata.Donor{FirstNames: "A", LastName: "W"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
SurnameMatchesDonor: true,
MatchIdentity: true,
},
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)

err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
resp := w.Result()
lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(tc.lpa, nil)

assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
template := newMockTemplate(t)
template.EXPECT().
Execute(w, tc.data).
Return(nil)

err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil)(testAppData, w, r, tc.provided)
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}
}

func TestGetConfirmAllowedToVouchWhenLpaStoreResolvingServiceErrors(t *testing.T) {
Expand Down Expand Up @@ -83,33 +146,39 @@ func TestGetConfirmAllowedToVouchWhenTemplateErrors(t *testing.T) {
}

func TestPostConfirmAllowedToVouch(t *testing.T) {
f := url.Values{
form.FieldNames.YesNo: {form.Yes.String()},
testcases := map[task.State]voucherdata.Tasks{
task.StateNotStarted: voucherdata.Tasks{ConfirmYourName: task.StateCompleted},
task.StateInProgress: voucherdata.Tasks{ConfirmYourIdentity: task.StateCompleted},
}

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

lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(&lpadata.Lpa{Donor: lpadata.Donor{LastName: "Smith"}}, nil)

voucherStore := newMockVoucherStore(t)
voucherStore.EXPECT().
Put(r.Context(), &voucherdata.Provided{
LpaID: "lpa-id",
Tasks: voucherdata.Tasks{ConfirmYourName: task.StateCompleted},
}).
Return(nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, voucher.PathTaskList.Format("lpa-id"), resp.Header.Get("Location"))
for taskState, tasks := range testcases {
t.Run(taskState.String(), func(t *testing.T) {
f := url.Values{
form.FieldNames.YesNo: {form.Yes.String()},
}

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

lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(&lpadata.Lpa{Donor: lpadata.Donor{LastName: "Smith"}}, nil)

voucherStore := newMockVoucherStore(t)
voucherStore.EXPECT().
Put(r.Context(), &voucherdata.Provided{LpaID: "lpa-id", Tasks: tasks}).
Return(nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id", Tasks: voucherdata.Tasks{ConfirmYourIdentity: taskState}})
resp := w.Result()

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

func TestPostConfirmAllowedToVouchWhenNo(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/voucher/voucherpage/confirm_your_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package voucherpage

import (
"net/http"
"strings"

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
Expand Down Expand Up @@ -45,7 +46,8 @@ func ConfirmYourName(tmpl template.Template, lpaStoreResolvingService LpaStoreRe
provided.FirstNames = firstNames
provided.LastName = lastName

if lastName == lpa.Donor.LastName || !provided.NameMatches(lpa).IsNone() {
if !provided.Tasks.ConfirmYourName.IsCompleted() &&
(strings.EqualFold(lastName, lpa.Donor.LastName) || !provided.NameMatches(lpa).IsNone()) {
redirect = voucher.PathConfirmAllowedToVouch
state = task.StateInProgress
}
Expand Down
13 changes: 11 additions & 2 deletions internal/voucher/voucherpage/identity_with_one_login_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ func IdentityWithOneLoginCallback(oneLoginClient OneLoginClient, sessionStore Se
}

provided.IdentityUserData = userData
provided.Tasks.ConfirmYourIdentity = task.StateCompleted
if provided.NameMatches(lpa).IsNone() {
provided.Tasks.ConfirmYourIdentity = task.StateCompleted
} else {
provided.Tasks.ConfirmYourIdentity = task.StateInProgress
}

if err := voucherStore.Put(r.Context(), provided); err != nil {
return err
}
Expand All @@ -66,6 +71,10 @@ func IdentityWithOneLoginCallback(oneLoginClient OneLoginClient, sessionStore Se
return voucher.PathUnableToConfirmIdentity.Redirect(w, r, appData, appData.LpaID)
}

return voucher.PathOneLoginIdentityDetails.Redirect(w, r, appData, appData.LpaID)
if provided.Tasks.ConfirmYourIdentity.IsCompleted() {
return voucher.PathOneLoginIdentityDetails.Redirect(w, r, appData, appData.LpaID)
}

return voucher.PathConfirmAllowedToVouch.Redirect(w, r, appData, appData.LpaID)
}
}
Loading

0 comments on commit 87b1830

Please sign in to comment.