Skip to content

Commit

Permalink
Merge 8446c10 into 50af747
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Aug 15, 2024
2 parents 50af747 + 8446c10 commit 7bfcd9f
Show file tree
Hide file tree
Showing 14 changed files with 597 additions and 33 deletions.
48 changes: 48 additions & 0 deletions cypress/e2e/voucher/confirm-your-name.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe('Confirm your name', () => {
beforeEach(() => {
cy.visit('/fixtures/voucher?redirect=/confirm-your-name');
});

it('shows my name', () => {
cy.checkA11yApp();

cy.contains('Vivian');
cy.contains('Vaughn');

cy.contains('button', 'Continue').click();
cy.get('ul li:first-child').should('contain', 'Completed');
});

it('can update my name', () => {
cy.contains('div', 'Vivian').contains('a', 'Change').click();

cy.url().should('contain', '/your-name')
cy.checkA11yApp();
cy.get('#f-first-names').clear().type('Barry');
cy.contains('button', 'Save and continue').click();

cy.url().should('contain', '/confirm-your-name')
cy.contains('Barry');

cy.contains('button', 'Continue').click();
cy.get('ul li:first-child').should('contain', 'Completed');
});

it('warns when last name matches donor', () => {
cy.contains('div', 'Vivian').contains('a', 'Change').click();

cy.url().should('contain', '/your-name')
cy.checkA11yApp();
cy.get('#f-last-name').clear().type('Smith');
cy.contains('button', 'Save and continue').click();

cy.contains('button', 'Continue').click();

cy.url().should('contain', '/confirm-allowed-to-vouch');
cy.checkA11yApp();

cy.contains('label', 'Yes').click();
cy.contains('button', 'Continue').click();
cy.get('ul li:first-child').should('contain', 'Completed');
});
});
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func App(
handleRoot(page.PathSupporterFixtures, None,
fixtures.Supporter(tmpls.Get("supporter_fixtures.gohtml"), sessionStore, organisationStore, donorStore, memberStore, lpaDynamoClient, searchClient, shareCodeStore, certificateProviderStore, attorneyStore, documentStore, eventClient, lpaStoreClient))
handleRoot(page.PathVoucherFixtures, None,
fixtures.Voucher(tmpls.Get("voucher_fixtures.gohtml"), shareCodeStore, donorStore))
fixtures.Voucher(tmpls.Get("voucher_fixtures.gohtml"), sessionStore, shareCodeStore, donorStore, voucherStore))
handleRoot(page.PathDashboardFixtures, None,
fixtures.Dashboard(tmpls.Get("dashboard_fixtures.gohtml"), sessionStore, donorStore, certificateProviderStore, attorneyStore, shareCodeStore))
}
Expand Down
51 changes: 46 additions & 5 deletions internal/page/fixtures/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,49 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore/lpadata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode/sharecodedata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/voucher"
"github.com/ministryofjustice/opg-modernising-lpa/internal/voucher/voucherdata"
)

func Voucher(tmpl template.Template, shareCodeStore *sharecode.Store, donorStore *donor.Store) page.Handler {
func Voucher(
tmpl template.Template,
sessionStore *sesh.Store,
shareCodeStore *sharecode.Store,
donorStore *donor.Store,
voucherStore *voucher.Store,
) page.Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request) error {
acceptCookiesConsent(w)

var (
voucherSub = r.FormValue("voucherSub")
shareCode = r.FormValue("withShareCode")
redirect = r.FormValue("redirect")
)

if voucherSub == "" {
voucherSub = random.String(16)
}

if err := sessionStore.SetLogin(r, w, &sesh.LoginSession{Sub: voucherSub, Email: testEmail}); err != nil {
return err
}

if r.Method != http.MethodPost && !r.URL.Query().Has("redirect") {
return tmpl(w, &fixturesData{App: appData, Sub: voucherSub})
}

var (
donorSub = random.String(16)
donorSessionID = base64.StdEncoding.EncodeToString([]byte(donorSub))
donorSub = random.String(16)
donorSessionID = base64.StdEncoding.EncodeToString([]byte(donorSub))
voucherSessionID = base64.StdEncoding.EncodeToString([]byte(voucherSub))
)

createSession := &appcontext.Session{SessionID: donorSessionID}
Expand All @@ -46,6 +62,11 @@ func Voucher(tmpl template.Template, shareCodeStore *sharecode.Store, donorStore
return err
}

var (
donorCtx = appcontext.ContextWithSession(r.Context(), &appcontext.Session{SessionID: donorSessionID, LpaID: donorDetails.LpaID})
voucherCtx = appcontext.ContextWithSession(r.Context(), &appcontext.Session{SessionID: voucherSessionID, LpaID: donorDetails.LpaID})
)

donorDetails.SignedAt = time.Now()
donorDetails.Donor = makeDonor(testEmail)
donorDetails.LpaUID = makeUID()
Expand All @@ -63,6 +84,10 @@ func Voucher(tmpl template.Template, shareCodeStore *sharecode.Store, donorStore
Allowed: true,
}

if err := donorStore.Put(donorCtx, donorDetails); err != nil {
return err
}

if shareCode != "" {
if err := shareCodeStore.Put(r.Context(), actor.TypeVoucher, shareCode, sharecodedata.Link{
LpaKey: donorDetails.PK,
Expand All @@ -71,13 +96,29 @@ func Voucher(tmpl template.Template, shareCodeStore *sharecode.Store, donorStore
}); err != nil {
return err
}

http.Redirect(w, r, page.PathVoucherStart.Format(), http.StatusFound)
return nil
}

if err := donorStore.Put(appcontext.ContextWithSession(r.Context(), &appcontext.Session{SessionID: donorSessionID, LpaID: donorDetails.LpaID}), donorDetails); err != nil {
voucherDetails := &voucherdata.Provided{
PK: donorDetails.PK,
SK: dynamo.VoucherKey(voucherSessionID),
LpaID: donorDetails.LpaID,
Email: testEmail,
}

if err := voucherStore.Put(voucherCtx, voucherDetails); err != nil {
return err
}

http.Redirect(w, r, page.PathVoucherStart.Format(), http.StatusFound)
if redirect == "" {
redirect = page.PathDashboard.Format()
} else {
redirect = "/voucher/" + donorDetails.LpaID + redirect
}

http.Redirect(w, r, redirect, http.StatusFound)
return nil
}
}
13 changes: 7 additions & 6 deletions internal/voucher/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
)

const (
PathTaskList = Path("/task-list")
PathConfirmYourName = Path("/confirm-your-name")
PathYourName = Path("/your-name")
PathVerifyDonorDetails = Path("/verify-donor-details")
PathConfirmYourIdentity = Path("/confirm-your-identity")
PathSignTheDeclaration = Path("/sign-the-declaration")
PathTaskList = Path("/task-list")
PathConfirmAllowedToVouch = Path("/confirm-allowed-to-vouch")
PathConfirmYourName = Path("/confirm-your-name")
PathYourName = Path("/your-name")
PathVerifyDonorDetails = Path("/verify-donor-details")
PathConfirmYourIdentity = Path("/confirm-your-identity")
PathSignTheDeclaration = Path("/sign-the-declaration")
)

type Path string
Expand Down
6 changes: 6 additions & 0 deletions internal/voucher/voucherdata/provided.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Provided struct {
Tasks Tasks
// Email is the email address of the voucher
Email string
// FirstNames is the first names provided by the voucher. If set it overrides
// that provided by the donor.
FirstNames string
// LastName is a last name provided by the voucher. If set it overrides that
// provided by the donor.
LastName string
}

type Tasks struct {
Expand Down
57 changes: 57 additions & 0 deletions internal/voucher/voucherpage/confirm_allowed_to_vouch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package voucherpage

import (
"errors"
"net/http"

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore/lpadata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
"github.com/ministryofjustice/opg-modernising-lpa/internal/voucher"
"github.com/ministryofjustice/opg-modernising-lpa/internal/voucher/voucherdata"
)

type confirmAllowedToVouchData struct {
App appcontext.Data
Errors validation.List
Form *form.YesNoForm
Lpa *lpadata.Lpa
}

func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaStoreResolvingService, voucherStore VoucherStore) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *voucherdata.Provided) error {
lpa, err := lpaStoreResolvingService.Get(r.Context())
if err != nil {
return err
}

data := &confirmAllowedToVouchData{
App: appData,
Form: form.NewYesNoForm(form.YesNoUnknown),
Lpa: lpa,
}

if r.Method == http.MethodPost {
data.Form = form.ReadYesNoForm(r, "yesIfAllowedToVouch")
data.Errors = data.Form.Validate()

if data.Errors.None() {
if data.Form.YesNo.IsNo() {
return errors.New("// TODO there should be a page here but it hasn't been built yet")
}

provided.Tasks.ConfirmYourName = task.StateCompleted
if err := voucherStore.Put(r.Context(), provided); err != nil {
return err
}

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

return tmpl(w, data)
}
}
49 changes: 34 additions & 15 deletions internal/voucher/voucherpage/confirm_your_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,52 @@ import (
)

type confirmYourNameData struct {
App appcontext.Data
Errors validation.List
Lpa *lpadata.Lpa
App appcontext.Data
Errors validation.List
Lpa *lpadata.Lpa
FirstNames string
LastName string
}

func ConfirmYourName(tmpl template.Template, lpaStoreResolvingService LpaStoreResolvingService, voucherStore VoucherStore) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *voucherdata.Provided) error {
lpa, err := lpaStoreResolvingService.Get(r.Context())
if err != nil {
return err
}

firstNames := provided.FirstNames
if firstNames == "" {
firstNames = lpa.Voucher.FirstNames
}

lastName := provided.LastName
if lastName == "" {
lastName = lpa.Voucher.LastName
}

if r.Method == http.MethodPost {
if provided.Tasks.ConfirmYourName.NotStarted() {
provided.Tasks.ConfirmYourName = task.StateInProgress
redirect := voucher.PathTaskList
state := task.StateCompleted

if err := voucherStore.Put(r.Context(), provided); err != nil {
return err
}
if lastName == lpa.Donor.LastName {
redirect = voucher.PathConfirmAllowedToVouch
state = task.StateInProgress
}

return voucher.PathTaskList.Redirect(w, r, appData, appData.LpaID)
}
provided.Tasks.ConfirmYourName = state
if err := voucherStore.Put(r.Context(), provided); err != nil {
return err
}

lpa, err := lpaStoreResolvingService.Get(r.Context())
if err != nil {
return err
return redirect.Redirect(w, r, appData, appData.LpaID)
}

return tmpl(w, &confirmYourNameData{
App: appData,
Lpa: lpa,
App: appData,
Lpa: lpa,
FirstNames: firstNames,
LastName: lastName,
})
}
}
42 changes: 38 additions & 4 deletions internal/voucher/voucherpage/confirm_your_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,63 @@ import (
)

func TestGetConfirmYourName(t *testing.T) {
voucherProvidedDetails := &voucherdata.Provided{LpaID: "lpa-id"}
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, &confirmYourNameData{
App: testAppData,
Lpa: &lpadata.Lpa{
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
FirstNames: "V",
LastName: "W",
}).
Return(nil)

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

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

func TestGetConfirmYourNameWhenChanged(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"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
}, nil)

template := newMockTemplate(t)
template.EXPECT().
Execute(w, &confirmYourNameData{
App: testAppData,
Lpa: &lpadata.Lpa{
Voucher: lpadata.Voucher{FirstNames: "V"},
Voucher: lpadata.Voucher{FirstNames: "V", LastName: "W"},
},
FirstNames: "A",
LastName: "B",
}).
Return(nil)

err := ConfirmYourName(template.Execute, lpaStoreResolvingService, nil)(testAppData, w, r, voucherProvidedDetails)
err := ConfirmYourName(template.Execute, lpaStoreResolvingService, nil)(testAppData, w, r, &voucherdata.Provided{
LpaID: "lpa-id",
FirstNames: "A",
LastName: "B",
})
resp := w.Result()

assert.Nil(t, err)
Expand Down
Loading

0 comments on commit 7bfcd9f

Please sign in to comment.