From 7bf49e4667537fbec5e9935e38a911b88feb6668 Mon Sep 17 00:00:00 2001 From: Joshua Hawxwell Date: Tue, 11 Jun 2024 09:32:08 +0100 Subject: [PATCH] Only emit notification-sent event for non-simulated notify sends --- cypress/e2e/donor/check-your-lpa.cy.js | 9 ++--- internal/notify/client.go | 39 +++++++++++++++------ internal/notify/client_test.go | 48 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/cypress/e2e/donor/check-your-lpa.cy.js b/cypress/e2e/donor/check-your-lpa.cy.js index bdc11725c8..9c9585265e 100644 --- a/cypress/e2e/donor/check-your-lpa.cy.js +++ b/cypress/e2e/donor/check-your-lpa.cy.js @@ -1,8 +1,4 @@ describe('Check the LPA', () => { - beforeEach(() => { - cy.visit('/fixtures?redirect=/check-your-lpa&progress=peopleToNotifyAboutYourLpa'); - }); - it('cannot change when personal welfare LPA can be used', () => { cy.visit('/fixtures?redirect=/check-your-lpa&progress=peopleToNotifyAboutYourLpa&lpa-type=personal-welfare'); @@ -12,6 +8,8 @@ describe('Check the LPA', () => { }); it("can submit the completed LPA", () => { + cy.visit('/fixtures?redirect=/check-your-lpa&progress=peopleToNotifyAboutYourLpa&certificateProviderEmail=test@example.com'); + cy.contains('h1', "Check your LPA") cy.checkA11yApp(); @@ -47,6 +45,8 @@ describe('Check the LPA', () => { }); it('does not allow checking when no changes', () => { + cy.visit('/fixtures?redirect=/check-your-lpa&progress=peopleToNotifyAboutYourLpa'); + cy.get('#f-checked-and-happy').check({ force: true }) cy.contains('button', 'Confirm').click(); @@ -186,6 +186,7 @@ describe('Check the LPA', () => { }) it("errors when not selected", () => { + cy.visit('/fixtures?redirect=/check-your-lpa&progress=peopleToNotifyAboutYourLpa'); cy.contains('button', 'Confirm').click(); cy.get('.govuk-error-summary').within(() => { diff --git a/internal/notify/client.go b/internal/notify/client.go index cdede43627..28e7a32135 100644 --- a/internal/notify/client.go +++ b/internal/notify/client.go @@ -9,6 +9,7 @@ import ( "errors" "log/slog" "net/http" + "slices" "strings" "time" @@ -16,7 +17,19 @@ import ( "github.com/ministryofjustice/opg-modernising-lpa/internal/event" ) -var allowResendAfter = 10 * time.Minute +var ( + allowResendAfter = 10 * time.Minute + simulatedEmails = []string{ + "simulate-delivered@notifications.service.gov.uk", + "simulate-delivered-2@notifications.service.gov.uk", + "simulate-delivered-3@notifications.service.gov.uk", + } + simulatedPhones = []string{ + "07700900000", + "07700900111", + "07700900222", + } +) type Logger interface { ErrorContext(ctx context.Context, msg string, args ...any) @@ -140,11 +153,13 @@ func (c *Client) SendActorEmail(ctx context.Context, to, lpaUID string, email Em return err } - if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{ - UID: lpaUID, - NotificationID: resp.ID, - }); err != nil { - return err + if !slices.Contains(simulatedEmails, to) { + if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{ + UID: lpaUID, + NotificationID: resp.ID, + }); err != nil { + return err + } } return nil @@ -171,11 +186,13 @@ func (c *Client) SendActorSMS(ctx context.Context, to, lpaUID string, sms SMS) e return err } - if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{ - UID: lpaUID, - NotificationID: resp.ID, - }); err != nil { - return err + if !slices.Contains(simulatedPhones, to) { + if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{ + UID: lpaUID, + NotificationID: resp.ID, + }); err != nil { + return err + } } return nil diff --git a/internal/notify/client_test.go b/internal/notify/client_test.go index f6903a9a7d..8075ee9a52 100644 --- a/internal/notify/client_test.go +++ b/internal/notify/client_test.go @@ -155,6 +155,33 @@ func TestSendActorEmail(t *testing.T) { } } +func TestSendActorEmailWhenToSimulated(t *testing.T) { + assert := assert.New(t) + ctx := context.Background() + + for _, email := range simulatedEmails { + doer := newMockDoer(t) + doer.EXPECT(). + Do(mock.Anything). + Return(&http.Response{ + Body: io.NopCloser(strings.NewReader(`{"notifications":[]}`)), + }, nil). + Once() + doer.EXPECT(). + Do(mock.Anything). + Return(&http.Response{ + Body: io.NopCloser(strings.NewReader(`{"id":"xyz"}`)), + }, nil). + Once() + + client, _ := New(nil, true, "", "my_client-f33517ff-2a88-4f6e-b855-c550268ce08a-740e5834-3a29-46b4-9a6f-16142fde533a", doer, nil) + client.now = func() time.Time { return time.Date(2020, time.January, 2, 3, 4, 5, 6, time.UTC) } + + err := client.SendActorEmail(ctx, email, "lpa-uid", testEmail{A: "value"}) + assert.Nil(err) + } +} + func TestSendActorEmailWhenAlreadyRecentlyCreated(t *testing.T) { testcases := map[string]string{ "previously created": `{"notifications":[{"status":"delivered","created_at":"2020-01-02T02:54:06Z"}]}`, @@ -202,6 +229,7 @@ func TestSendActorEmailWhenReferenceExistsError(t *testing.T) { err := client.SendActorEmail(context.Background(), "me@example.com", "lpa-uid", testEmail{A: "value"}) assert.Equal(t, expectedError, err) } + func TestSendActorEmailWhenError(t *testing.T) { assert := assert.New(t) ctx := context.Background() @@ -447,6 +475,26 @@ func TestSendActorSMS(t *testing.T) { assert.Nil(err) } +func TestSendActorSMSWhenToSimulated(t *testing.T) { + assert := assert.New(t) + ctx := context.Background() + + for _, phone := range simulatedPhones { + doer := newMockDoer(t) + doer.EXPECT(). + Do(mock.Anything). + Return(&http.Response{ + Body: io.NopCloser(strings.NewReader(`{"id":"xyz"}`)), + }, nil) + + client, _ := New(nil, true, "", "my_client-f33517ff-2a88-4f6e-b855-c550268ce08a-740e5834-3a29-46b4-9a6f-16142fde533a", doer, nil) + client.now = func() time.Time { return time.Date(2020, time.January, 2, 3, 4, 5, 6, time.UTC) } + + err := client.SendActorSMS(ctx, phone, "lpa-uid", testSMS{A: "value"}) + assert.Nil(err) + } +} + func TestSendActorSMSWhenError(t *testing.T) { assert := assert.New(t) ctx := context.Background()