diff --git a/internal/app/donor_store.go b/internal/app/donor_store.go index d0ae7a7a85..5f2223e731 100644 --- a/internal/app/donor_store.go +++ b/internal/app/donor_store.go @@ -3,6 +3,7 @@ package app import ( "context" "errors" + "log" "time" "github.com/aws/aws-sdk-go-v2/service/s3" @@ -189,40 +190,43 @@ func (s *donorStore) Put(ctx context.Context, lpa *page.Lpa) error { } if lpa.UID != "" && lpa.Tasks.PayForLpa.IsPending() { + log.Println("reduced-fee-requested event start") + documents, err := s.documentStore.GetAll(ctx) if err != nil { s.logger.Print(err) return s.dynamoClient.Put(ctx, lpa) } - var unsentKeys []string + var unsentDocuments page.Documents for _, document := range documents { - if document.Sent.IsZero() && !document.Scanned { - unsentKeys = append(unsentKeys, document.Key) + log.Printf("%#v", document) + if document.Sent.IsZero() && document.Scanned && !document.VirusDetected { + unsentDocuments = append(unsentDocuments, document) } } - if len(unsentKeys) > 0 { + log.Println("reduced-fee-requested unsentDocuments: ", len(unsentDocuments)) + + if len(unsentDocuments) > 0 { + log.Println("reduced-fee-requested sending event") + if err := s.eventClient.SendReducedFeeRequested(ctx, event.ReducedFeeRequested{ UID: lpa.UID, RequestType: lpa.FeeType.String(), - Evidence: unsentKeys, + Evidence: unsentDocuments.Keys(), EvidenceDelivery: lpa.EvidenceDelivery.String(), }); err != nil { return err } + log.Println("reduced-fee-requested sent event") - var updatedDocuments page.Documents - - for _, document := range documents { - if document.Sent.IsZero() && !document.Scanned { - document.Sent = s.now() - updatedDocuments = append(updatedDocuments, document) - } + for i := range unsentDocuments { + unsentDocuments[i].Sent = s.now() } - if err := s.documentStore.BatchPut(ctx, updatedDocuments); err != nil { + if err := s.documentStore.BatchPut(ctx, unsentDocuments); err != nil { s.logger.Print(err) } } diff --git a/internal/app/donor_store_test.go b/internal/app/donor_store_test.go index c53fd88f69..0d92eac619 100644 --- a/internal/app/donor_store_test.go +++ b/internal/app/donor_store_test.go @@ -483,10 +483,10 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocuments(t *testing.T) { documentStore. On("GetAll", ctx). Return(page.Documents{ - {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"}, + {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true}, }, nil) documentStore. - On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Sent: now}}). + On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true, Sent: now}}). Return(nil) donorStore := &donorStore{dynamoClient: dynamoClient, eventClient: eventClient, now: func() time.Time { return now }, documentStore: documentStore} @@ -585,7 +585,7 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocumentsWhenEventClientSe documentStore. On("GetAll", ctx). Return(page.Documents{ - {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"}, + {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true}, }, nil) donorStore := &donorStore{eventClient: eventClient, now: func() time.Time { return now }, documentStore: documentStore} @@ -622,10 +622,10 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocumentsWhenDocumentStore documentStore. On("GetAll", ctx). Return(page.Documents{ - {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"}, + {Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true}, }, nil) documentStore. - On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Sent: now}}). + On("BatchPut", ctx, mock.Anything). Return(expectedError) logger := newMockLogger(t) diff --git a/internal/page/donor/payment_confirmation.go b/internal/page/donor/payment_confirmation.go index fec92ecff9..060b643146 100644 --- a/internal/page/donor/payment_confirmation.go +++ b/internal/page/donor/payment_confirmation.go @@ -3,10 +3,7 @@ package donor import ( "fmt" "net/http" - "time" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/gorilla/sessions" "github.com/ministryofjustice/opg-go-common/template" "github.com/ministryofjustice/opg-modernising-lpa/internal/actor" @@ -25,7 +22,7 @@ type paymentConfirmationData struct { EvidenceDelivery pay.EvidenceDelivery } -func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayClient, donorStore DonorStore, sessionStore sessions.Store, evidenceS3Client S3Client, now func() time.Time, documentStore DocumentStore) Handler { +func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayClient, donorStore DonorStore, sessionStore sessions.Store) Handler { return func(appData page.AppData, w http.ResponseWriter, r *http.Request, lpa *page.Lpa) error { paymentSession, err := sesh.Payment(sessionStore, r) if err != nil { @@ -62,29 +59,6 @@ func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayCli lpa.Tasks.PayForLpa = actor.PaymentTaskCompleted } else { lpa.Tasks.PayForLpa = actor.PaymentTaskPending - - documents, err := documentStore.GetAll(r.Context()) - if err != nil { - return err - } - - for _, document := range documents { - if document.Sent.IsZero() { - err := evidenceS3Client.PutObjectTagging(r.Context(), document.Key, []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }) - - if err != nil { - logger.Print(fmt.Sprintf("error tagging evidence: %s", err.Error())) - return err - } - - document.Sent = now() - if err := documentStore.Put(r.Context(), document); err != nil { - return err - } - } - } } if err := donorStore.Put(r.Context(), lpa); err != nil { diff --git a/internal/page/donor/payment_confirmation_test.go b/internal/page/donor/payment_confirmation_test.go index 0a4d1b06f2..12af2e555d 100644 --- a/internal/page/donor/payment_confirmation_test.go +++ b/internal/page/donor/payment_confirmation_test.go @@ -5,10 +5,7 @@ import ( "net/http" "net/http/httptest" "testing" - "time" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/gorilla/sessions" "github.com/ministryofjustice/opg-modernising-lpa/internal/actor" "github.com/ministryofjustice/opg-modernising-lpa/internal/page" @@ -55,7 +52,7 @@ func TestGetPaymentConfirmationFullFee(t *testing.T) { }). Return(nil) - err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{ + err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{ FeeType: pay.FullFee, CertificateProvider: actor.CertificateProvider{ Email: "certificateprovider@example.com", @@ -87,8 +84,6 @@ func TestGetPaymentConfirmationHalfFee(t *testing.T) { withPaySession(r). withExpiredPaySession(r, w) - now := time.Now() - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), &page.Lpa{ @@ -107,25 +102,7 @@ func TestGetPaymentConfirmationHalfFee(t *testing.T) { }). Return(nil) - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{ - {Key: "evidence-key"}, - {Key: "another-evidence-key", Sent: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC)}, - }, nil) - documentStore. - On("Put", r.Context(), page.Document{Key: "evidence-key", Sent: now}). - Return(nil) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", r.Context(), "evidence-key", []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }). - Return(nil) - - err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{ + err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{ FeeType: pay.HalfFee, CertificateProvider: actor.CertificateProvider{ Email: "certificateprovider@example.com", @@ -148,7 +125,7 @@ func TestGetPaymentConfirmationWhenErrorGettingSession(t *testing.T) { On("Get", r, "pay"). Return(&sessions.Session{}, expectedError) - err := PaymentConfirmation(nil, template.Execute, newMockPayClient(t), nil, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{}) + err := PaymentConfirmation(nil, template.Execute, newMockPayClient(t), nil, sessionStore)(testAppData, w, r, &page.Lpa{}) resp := w.Result() assert.Equal(t, expectedError, err) @@ -174,7 +151,7 @@ func TestGetPaymentConfirmationWhenErrorGettingPayment(t *testing.T) { template := newMockTemplate(t) - err := PaymentConfirmation(logger, template.Execute, payClient, nil, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{}) + err := PaymentConfirmation(logger, template.Execute, payClient, nil, sessionStore)(testAppData, w, r, &page.Lpa{}) resp := w.Result() assert.Equal(t, expectedError, err) @@ -208,7 +185,7 @@ func TestGetPaymentConfirmationWhenErrorExpiringSession(t *testing.T) { On("Execute", w, mock.Anything). Return(nil) - err := PaymentConfirmation(logger, template.Execute, payClient, donorStore, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{CertificateProvider: actor.CertificateProvider{ + err := PaymentConfirmation(logger, template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{CertificateProvider: actor.CertificateProvider{ Email: "certificateprovider@example.com", }}) resp := w.Result() @@ -217,113 +194,6 @@ func TestGetPaymentConfirmationWhenErrorExpiringSession(t *testing.T) { assert.Equal(t, http.StatusOK, resp.StatusCode) } -func TestGetPaymentConfirmationHalfFeeWhenDocumentStoreGetAllError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil) - - payClient := newMockPayClient(t). - withASuccessfulPayment("abc123", "123456789012", 4100) - - sessionStore := newMockSessionStore(t). - withPaySession(r). - withExpiredPaySession(r, w) - - now := time.Now() - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{}, expectedError) - - err := PaymentConfirmation(nil, nil, payClient, nil, sessionStore, nil, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{ - FeeType: pay.HalfFee, - CertificateProvider: actor.CertificateProvider{ - Email: "certificateprovider@example.com", - }, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - -func TestGetPaymentConfirmationHalfFeeWhenS3ClientPutTaggingObjectError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil) - - payClient := newMockPayClient(t). - withASuccessfulPayment("abc123", "123456789012", 4100) - - sessionStore := newMockSessionStore(t). - withPaySession(r). - withExpiredPaySession(r, w) - - now := time.Now() - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{{}}, nil) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything). - Return(expectedError) - - logger := newMockLogger(t) - logger. - On("Print", fmt.Sprintf("error tagging evidence: %s", expectedError)) - - err := PaymentConfirmation(logger, nil, payClient, nil, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{ - FeeType: pay.HalfFee, - CertificateProvider: actor.CertificateProvider{ - Email: "certificateprovider@example.com", - }, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - -func TestGetPaymentConfirmationHalfFeeWhenDocumentStorePutError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil) - - payClient := newMockPayClient(t). - withASuccessfulPayment("abc123", "123456789012", 4100) - - sessionStore := newMockSessionStore(t). - withPaySession(r). - withExpiredPaySession(r, w) - - now := time.Now() - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{{}}, nil) - documentStore. - On("Put", r.Context(), mock.Anything). - Return(expectedError) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything). - Return(nil) - - err := PaymentConfirmation(nil, nil, payClient, nil, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{ - FeeType: pay.HalfFee, - CertificateProvider: actor.CertificateProvider{ - Email: "certificateprovider@example.com", - }, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - func TestGetPaymentConfirmationHalfFeeWhenDonorStorePutError(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil) @@ -335,31 +205,16 @@ func TestGetPaymentConfirmationHalfFeeWhenDonorStorePutError(t *testing.T) { withPaySession(r). withExpiredPaySession(r, w) - now := time.Now() - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), mock.Anything). Return(expectedError) - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{{}}, nil) - documentStore. - On("Put", r.Context(), mock.Anything). - Return(nil) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything). - Return(nil) - logger := newMockLogger(t) logger. On("Print", fmt.Sprintf("unable to update lpa in donorStore: %s", expectedError)) - err := PaymentConfirmation(logger, nil, payClient, donorStore, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{ + err := PaymentConfirmation(logger, nil, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{ FeeType: pay.HalfFee, CertificateProvider: actor.CertificateProvider{ Email: "certificateprovider@example.com", diff --git a/internal/page/donor/register.go b/internal/page/donor/register.go index 6b85c548c0..497084d260 100644 --- a/internal/page/donor/register.go +++ b/internal/page/donor/register.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/gorilla/sessions" "github.com/ministryofjustice/opg-go-common/template" @@ -172,15 +171,12 @@ func Register( documentStore DocumentStore, ) { payer := &payHelper{ - logger: logger, - sessionStore: sessionStore, - donorStore: donorStore, - payClient: payClient, - appPublicURL: appPublicURL, - randomString: random.String, - evidenceS3Client: evidenceS3Client, - now: time.Now, - documentStore: documentStore, + logger: logger, + sessionStore: sessionStore, + donorStore: donorStore, + payClient: payClient, + appPublicURL: appPublicURL, + randomString: random.String, } handleRoot := makeHandle(rootMux, sessionStore, None, errorHandler) @@ -327,7 +323,7 @@ func Register( handleWithLpa(page.Paths.FeeDenied, None, FeeDenied(tmpls.Get("fee_denied.gohtml"), payer)) handleWithLpa(page.Paths.PaymentConfirmation, None, - PaymentConfirmation(logger, tmpls.Get("payment_confirmation.gohtml"), payClient, donorStore, sessionStore, evidenceS3Client, time.Now, documentStore)) + PaymentConfirmation(logger, tmpls.Get("payment_confirmation.gohtml"), payClient, donorStore, sessionStore)) handleWithLpa(page.Paths.EvidenceSuccessfullyUploaded, None, Guidance(tmpls.Get("evidence_successfully_uploaded.gohtml"))) handleWithLpa(page.Paths.WhatHappensNextPostEvidence, None, @@ -468,15 +464,12 @@ func makeLpaHandle(mux *http.ServeMux, store sesh.Store, defaultOptions handleOp } type payHelper struct { - logger Logger - sessionStore sessions.Store - donorStore DonorStore - payClient PayClient - appPublicURL string - randomString func(int) string - evidenceS3Client S3Client - now func() time.Time - documentStore DocumentStore + logger Logger + sessionStore sessions.Store + donorStore DonorStore + payClient PayClient + appPublicURL string + randomString func(int) string } func (p *payHelper) Pay(appData page.AppData, w http.ResponseWriter, r *http.Request, lpa *page.Lpa) error { @@ -490,27 +483,6 @@ func (p *payHelper) Pay(appData page.AppData, w http.ResponseWriter, r *http.Req return appData.Redirect(w, r, lpa, page.Paths.WhatHappensNextPostEvidence.Format(lpa.ID)) } - documents, err := p.documentStore.GetAll(r.Context()) - if err != nil { - return err - } - - for _, document := range documents { - if document.Sent.IsZero() { - if err := p.evidenceS3Client.PutObjectTagging(r.Context(), document.Key, []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }); err != nil { - p.logger.Print(fmt.Sprintf("error tagging evidence: %s", err.Error())) - return err - } - - document.Sent = p.now() - if err := p.documentStore.Put(r.Context(), document); err != nil { - return err - } - } - } - return appData.Redirect(w, r, lpa, page.Paths.EvidenceSuccessfullyUploaded.Format(lpa.ID)) } diff --git a/internal/page/donor/register_test.go b/internal/page/donor/register_test.go index 78f693593a..38647a70c8 100644 --- a/internal/page/donor/register_test.go +++ b/internal/page/donor/register_test.go @@ -2,15 +2,11 @@ package donor import ( "context" - "fmt" "log" "net/http" "net/http/httptest" "testing" - "time" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/gorilla/sessions" "github.com/ministryofjustice/opg-go-common/template" "github.com/ministryofjustice/opg-modernising-lpa/internal/actor" @@ -436,21 +432,6 @@ func TestPayHelperPayWhenPaymentNotRequired(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodPost, "/", nil) - now := time.Now() - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{ - {Key: "lpa-uid/evidence/a-uid", Filename: "dummy.pdf", Sent: now}, - {Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png"}, - }, nil) - documentStore. - On("Put", r.Context(), page.Document{ - Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png", Sent: now, - }). - Return(nil) - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), &page.Lpa{ @@ -461,18 +442,8 @@ func TestPayHelperPayWhenPaymentNotRequired(t *testing.T) { }). Return(nil) - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", r.Context(), "lpa-uid/evidence/another-uid", []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }). - Return(nil) - err := (&payHelper{ - donorStore: donorStore, - now: func() time.Time { return now }, - evidenceS3Client: s3Client, - documentStore: documentStore, + donorStore: donorStore, }).Pay(testAppData, w, r, &page.Lpa{ ID: "lpa-id", FeeType: feeType, @@ -498,8 +469,6 @@ func TestPayHelperPayWhenPostingEvidence(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodPost, "/", nil) - now := time.Now() - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), &page.Lpa{ @@ -512,7 +481,6 @@ func TestPayHelperPayWhenPostingEvidence(t *testing.T) { err := (&payHelper{ donorStore: donorStore, - now: func() time.Time { return now }, }).Pay(testAppData, w, r, &page.Lpa{ ID: "lpa-id", FeeType: feeType, @@ -531,21 +499,6 @@ func TestPayHelperPayWhenMoreEvidenceProvided(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodPost, "/", nil) - now := time.Now() - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{ - {Key: "lpa-uid/evidence/a-uid", Filename: "dummy.pdf", Sent: now}, - {Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png"}, - }, nil) - documentStore. - On("Put", r.Context(), page.Document{ - Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png", Sent: now, - }). - Return(nil) - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), &page.Lpa{ @@ -556,18 +509,8 @@ func TestPayHelperPayWhenMoreEvidenceProvided(t *testing.T) { }). Return(nil) - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", r.Context(), "lpa-uid/evidence/another-uid", []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }). - Return(nil) - err := (&payHelper{ - donorStore: donorStore, - now: func() time.Time { return now }, - evidenceS3Client: s3Client, - documentStore: documentStore, + donorStore: donorStore, }).Pay(testAppData, w, r, &page.Lpa{ ID: "lpa-id", FeeType: pay.HalfFee, @@ -581,128 +524,10 @@ func TestPayHelperPayWhenMoreEvidenceProvided(t *testing.T) { assert.Equal(t, page.Paths.EvidenceSuccessfullyUploaded.Format("lpa-id"), resp.Header.Get("Location")) } -func TestPayHelperPayWhenPaymentNotRequiredWhenDocumentStoreGetAllError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodPost, "/", nil) - - donorStore := newMockDonorStore(t) - donorStore. - On("Put", r.Context(), mock.Anything). - Return(nil) - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{}, expectedError) - - err := (&payHelper{documentStore: documentStore, donorStore: donorStore}).Pay(testAppData, w, r, &page.Lpa{ - ID: "lpa-id", - FeeType: pay.NoFee, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - -func TestPayHelperPayWhenPaymentNotRequiredWhenS3ClientPutObjectTaggingError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodPost, "/", nil) - - now := time.Now() - - donorStore := newMockDonorStore(t) - donorStore. - On("Put", r.Context(), mock.Anything). - Return(nil) - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{ - {Key: "lpa-uid/evidence/a-uid", Filename: "dummy.pdf", Sent: now}, - {Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png"}, - }, nil) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", r.Context(), "lpa-uid/evidence/another-uid", []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }). - Return(expectedError) - - logger := newMockLogger(t) - logger. - On("Print", fmt.Sprintf("error tagging evidence: %s", expectedError)) - - err := (&payHelper{ - logger: logger, - now: func() time.Time { return now }, - donorStore: donorStore, - evidenceS3Client: s3Client, - documentStore: documentStore, - }).Pay(testAppData, w, r, &page.Lpa{ - ID: "lpa-id", - FeeType: pay.NoFee, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - -func TestPayHelperPayWhenPaymentNotRequiredWhenDocumentStorePutError(t *testing.T) { - w := httptest.NewRecorder() - r, _ := http.NewRequest(http.MethodPost, "/", nil) - - now := time.Now() - - donorStore := newMockDonorStore(t) - donorStore. - On("Put", r.Context(), mock.Anything). - Return(nil) - - documentStore := newMockDocumentStore(t) - documentStore. - On("GetAll", r.Context()). - Return(page.Documents{ - {Key: "lpa-uid/evidence/a-uid", Filename: "dummy.pdf", Sent: now}, - {Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png"}, - }, nil) - documentStore. - On("Put", r.Context(), page.Document{ - Key: "lpa-uid/evidence/another-uid", Filename: "dummy.png", Sent: now, - }). - Return(expectedError) - - s3Client := newMockS3Client(t) - s3Client. - On("PutObjectTagging", r.Context(), "lpa-uid/evidence/another-uid", []types.Tag{ - {Key: aws.String("replicate"), Value: aws.String("true")}, - }). - Return(nil) - - err := (&payHelper{ - now: func() time.Time { return now }, - donorStore: donorStore, - evidenceS3Client: s3Client, - documentStore: documentStore, - }).Pay(testAppData, w, r, &page.Lpa{ - ID: "lpa-id", - FeeType: pay.NoFee, - }) - resp := w.Result() - - assert.Equal(t, expectedError, err) - assert.Equal(t, http.StatusOK, resp.StatusCode) -} - func TestPayHelperPayWhenPaymentNotRequiredWhenDonorStorePutError(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodPost, "/", nil) - now := time.Now() - donorStore := newMockDonorStore(t) donorStore. On("Put", r.Context(), &page.Lpa{ @@ -714,7 +539,6 @@ func TestPayHelperPayWhenPaymentNotRequiredWhenDonorStorePutError(t *testing.T) err := (&payHelper{ donorStore: donorStore, - now: func() time.Time { return now }, }).Pay(testAppData, w, r, &page.Lpa{ ID: "lpa-id", FeeType: pay.NoFee,