Skip to content

Commit

Permalink
Move WitnessCodeSender from page to donor package
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Aug 8, 2024
1 parent afb4c02 commit dd72f78
Show file tree
Hide file tree
Showing 15 changed files with 917 additions and 79 deletions.
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func App(
progressTracker := task.ProgressTracker{Localizer: localizer}

shareCodeSender := sharecode.NewSender(shareCodeStore, notifyClient, appPublicURL, random.String, eventClient)
witnessCodeSender := page.NewWitnessCodeSender(donorStore, notifyClient)
witnessCodeSender := donor.NewWitnessCodeSender(donorStore, notifyClient, localizer)

lpaStoreResolvingService := lpastore.NewResolvingService(donorStore, lpaStoreClient)

Expand Down
26 changes: 13 additions & 13 deletions internal/donor/donorpage/change_mobile_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type changeMobileNumberData struct {
}

func ChangeMobileNumber(tmpl template.Template, witnessCodeSender WitnessCodeSender, actorType actor.Type) Handler {
var send func(context.Context, *donordata.Provided, page.Localizer) error
var send func(context.Context, *donordata.Provided) error
var redirect donor.Path
switch actorType {
case actor.TypeIndependentWitness:
Expand All @@ -36,16 +36,16 @@ func ChangeMobileNumber(tmpl template.Template, witnessCodeSender WitnessCodeSen
panic("ChangeMobileNumber only supports IndependentWitness or CertificateProvider actors")
}

return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, donor *donordata.Provided) error {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &changeMobileNumberData{
App: appData,
Form: &changeMobileNumberForm{},
ActorType: actorType,
FirstNames: donor.CertificateProvider.FirstNames,
FirstNames: provided.CertificateProvider.FirstNames,
}

if actorType == actor.TypeIndependentWitness {
data.FirstNames = donor.IndependentWitness.FirstNames
data.FirstNames = provided.IndependentWitness.FirstNames
}

if r.Method == http.MethodPost {
Expand All @@ -54,31 +54,31 @@ func ChangeMobileNumber(tmpl template.Template, witnessCodeSender WitnessCodeSen

if data.Errors.None() {
if actorType == actor.TypeIndependentWitness {
donor.IndependentWitness.HasNonUKMobile = data.Form.HasNonUKMobile
provided.IndependentWitness.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.IndependentWitness.Mobile = data.Form.NonUKMobile
provided.IndependentWitness.Mobile = data.Form.NonUKMobile
} else {
donor.IndependentWitness.Mobile = data.Form.Mobile
provided.IndependentWitness.Mobile = data.Form.Mobile
}
} else {
donor.CertificateProvider.HasNonUKMobile = data.Form.HasNonUKMobile
provided.CertificateProvider.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.CertificateProvider.Mobile = data.Form.NonUKMobile
provided.CertificateProvider.Mobile = data.Form.NonUKMobile
} else {
donor.CertificateProvider.Mobile = data.Form.Mobile
provided.CertificateProvider.Mobile = data.Form.Mobile
}
}

if err := send(r.Context(), donor, appData.Localizer); err != nil {
if errors.Is(err, page.ErrTooManyWitnessCodeRequests) {
if err := send(r.Context(), provided); err != nil {
if errors.Is(err, donor.ErrTooManyWitnessCodeRequests) {
data.Errors.Add("request", validation.CustomError{Label: "pleaseWaitOneMinute"})
return tmpl(w, data)
}

return err
}

return redirect.Redirect(w, r, appData, donor)
return redirect.Redirect(w, r, appData, provided)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donorpage/change_mobile_number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestPostChangeMobileNumber(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.
On(tc.send, r.Context(), tc.donor, testAppData.Localizer).
On(tc.send, r.Context(), tc.donor).
Return(nil)

err := ChangeMobileNumber(nil, witnessCodeSender, tc.actorType)(testAppData, w, r, &donordata.Provided{
Expand All @@ -237,7 +237,7 @@ func TestPostChangeMobileNumberWhenSendErrors(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.EXPECT().
SendToCertificateProvider(mock.Anything, mock.Anything, mock.Anything).
SendToCertificateProvider(mock.Anything, mock.Anything).
Return(expectedError)

err := ChangeMobileNumber(nil, witnessCodeSender, actor.TypeCertificateProvider)(testAppData, w, r, &donordata.Provided{LpaID: "lpa-id"})
Expand All @@ -255,8 +255,8 @@ func TestPostChangeMobileNumberWhenSendErrorsWithTooManyRequests(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.EXPECT().
SendToCertificateProvider(mock.Anything, mock.Anything, mock.Anything).
Return(page.ErrTooManyWitnessCodeRequests)
SendToCertificateProvider(mock.Anything, mock.Anything).
Return(donor.ErrTooManyWitnessCodeRequests)

template := newMockTemplate(t)
template.EXPECT().
Expand Down
44 changes: 20 additions & 24 deletions internal/donor/donorpage/mock_WitnessCodeSender_test.go

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

4 changes: 2 additions & 2 deletions internal/donor/donorpage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ type SessionStore interface {
}

type WitnessCodeSender interface {
SendToCertificateProvider(context.Context, *donordata.Provided, page.Localizer) error
SendToIndependentWitness(context.Context, *donordata.Provided, page.Localizer) error
SendToCertificateProvider(context.Context, *donordata.Provided) error
SendToIndependentWitness(context.Context, *donordata.Provided) error
}

type UidClient interface {
Expand Down
9 changes: 4 additions & 5 deletions internal/donor/donorpage/resend_witness_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)

Expand All @@ -27,22 +26,22 @@ func ResendWitnessCode(tmpl template.Template, witnessCodeSender WitnessCodeSend
redirect = donor.PathWitnessingAsIndependentWitness
}

return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, donor *donordata.Provided) error {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &resendWitnessCodeData{
App: appData,
}

if r.Method == http.MethodPost {
if err := send(r.Context(), donor, appData.Localizer); err != nil {
if errors.Is(err, page.ErrTooManyWitnessCodeRequests) {
if err := send(r.Context(), provided); err != nil {
if errors.Is(err, donor.ErrTooManyWitnessCodeRequests) {
data.Errors.Add("request", validation.CustomError{Label: "pleaseWaitOneMinute"})
return tmpl(w, data)
}

return err
}

return redirect.Redirect(w, r, appData, donor)
return redirect.Redirect(w, r, appData, provided)
}

return tmpl(w, data)
Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donorpage/resend_witness_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestPostResendWitnessCode(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.
On(tc.method, r.Context(), donor, testAppData.Localizer).
On(tc.method, r.Context(), donor).
Return(nil)

err := ResendWitnessCode(nil, witnessCodeSender, actorType)(testAppData, w, r, donor)
Expand All @@ -105,7 +105,7 @@ func TestPostResendWitnessCodeWhenSendErrors(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.EXPECT().
SendToCertificateProvider(r.Context(), donor, mock.Anything).
SendToCertificateProvider(r.Context(), donor).
Return(expectedError)

err := ResendWitnessCode(nil, witnessCodeSender, actor.TypeCertificateProvider)(testAppData, w, r, donor)
Expand Down Expand Up @@ -142,8 +142,8 @@ func TestPostResendWitnessCodeWhenTooRecentlySent(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.
On(tc.send, r.Context(), tc.donor, testAppData.Localizer).
Return(page.ErrTooManyWitnessCodeRequests)
On(tc.send, r.Context(), tc.donor).
Return(donor.ErrTooManyWitnessCodeRequests)

template := newMockTemplate(t)
template.EXPECT().
Expand Down
4 changes: 2 additions & 2 deletions internal/donor/donorpage/witnessing_your_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type witnessingYourSignatureData struct {
func WitnessingYourSignature(tmpl template.Template, witnessCodeSender WitnessCodeSender, donorStore DonorStore) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
if r.Method == http.MethodPost {
if err := witnessCodeSender.SendToCertificateProvider(r.Context(), provided, appData.Localizer); err != nil {
if err := witnessCodeSender.SendToCertificateProvider(r.Context(), provided); err != nil {
return err
}

Expand All @@ -31,7 +31,7 @@ func WitnessingYourSignature(tmpl template.Template, witnessCodeSender WitnessCo
return err
}

if err := witnessCodeSender.SendToIndependentWitness(r.Context(), lpa, appData.Localizer); err != nil {
if err := witnessCodeSender.SendToIndependentWitness(r.Context(), lpa); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions internal/donor/donorpage/witnessing_your_signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestPostWitnessingYourSignature(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.EXPECT().
SendToCertificateProvider(r.Context(), provided, mock.Anything).
SendToCertificateProvider(r.Context(), provided).
Return(nil)

err := WitnessingYourSignature(nil, witnessCodeSender, nil)(testAppData, w, r, provided)
Expand All @@ -84,15 +84,15 @@ func TestPostWitnessingYourSignatureCannotSign(t *testing.T) {

witnessCodeSender := newMockWitnessCodeSender(t)
witnessCodeSender.EXPECT().
SendToCertificateProvider(r.Context(), provided, mock.Anything).
SendToCertificateProvider(r.Context(), provided).
Return(nil)
witnessCodeSender.EXPECT().
SendToIndependentWitness(r.Context(), &donordata.Provided{
LpaID: "lpa-id",
Donor: donordata.Donor{CanSign: form.No},
DonorIdentityUserData: identity.UserData{Status: identity.StatusConfirmed},
CertificateProvider: donordata.CertificateProvider{Mobile: "07535111111"},
}, mock.Anything).
}).
Return(nil)

donorStore := newMockDonorStore(t)
Expand Down Expand Up @@ -123,18 +123,18 @@ func TestPostWitnessingYourSignatureWhenWitnessCodeSenderErrors(t *testing.T) {
"SendToCertificateProvider": {
setupWitnessCodeSender: func(witnessCodeSender *mockWitnessCodeSender) {
witnessCodeSender.EXPECT().
SendToCertificateProvider(mock.Anything, mock.Anything, mock.Anything).
SendToCertificateProvider(mock.Anything, mock.Anything).
Return(expectedError)
},
setupDonorStore: func(donorStore *mockDonorStore) {},
},
"SendToIndependentWitness": {
setupWitnessCodeSender: func(witnessCodeSender *mockWitnessCodeSender) {
witnessCodeSender.EXPECT().
SendToCertificateProvider(mock.Anything, mock.Anything, mock.Anything).
SendToCertificateProvider(mock.Anything, mock.Anything).
Return(nil)
witnessCodeSender.EXPECT().
SendToIndependentWitness(mock.Anything, mock.Anything, mock.Anything).
SendToIndependentWitness(mock.Anything, mock.Anything).
Return(expectedError)
},
setupDonorStore: func(donorStore *mockDonorStore) {
Expand Down
Loading

0 comments on commit dd72f78

Please sign in to comment.