Skip to content

Commit

Permalink
Merge 6a769b6 into 6e22948
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Mar 26, 2024
2 parents 6e22948 + 6a769b6 commit 3c4dd3f
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cmd/event-received/cloud_watch_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (h *cloudWatchEventHandler) Handle(ctx context.Context, cloudWatchEvent eve

return handleDonorSubmissionCompleted(ctx, h.dynamoClient, cloudWatchEvent, shareCodeSender, appData, lpaStoreClient)

case "certificate-provider-submission-completed":
return handleCertificateProviderSubmissionCompleted(ctx, cloudWatchEvent, h.factory)

default:
return fmt.Errorf("unknown cloudwatch event")
}
Expand Down Expand Up @@ -217,8 +220,43 @@ func handleDonorSubmissionCompleted(ctx context.Context, client dynamodbClient,

if donor.CertificateProvider.CarryOutBy.IsOnline() {
if err := shareCodeSender.SendCertificateProviderInvite(ctx, appData, donor); err != nil {
return fmt.Errorf("failed to send share code to certificate provider: %w", err)
}
}

return nil
}

func handleCertificateProviderSubmissionCompleted(ctx context.Context, event events.CloudWatchEvent, factory factory) error {
var v uidEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}

lpaStoreClient, err := factory.LpaStoreClient()
if err != nil {
return err
}

donor, err := lpaStoreClient.Lpa(ctx, v.UID)
if err != nil {
return fmt.Errorf("failed to retrieve lpa: %w", err)
}

if donor.CertificateProvider.CarryOutBy.IsPaper() {
shareCodeSender, err := factory.ShareCodeSender(ctx)
if err != nil {
return err
}

appData, err := factory.AppData()
if err != nil {
return err
}

if err := shareCodeSender.SendAttorneys(ctx, appData, donor); err != nil {
return fmt.Errorf("failed to send share codes to attorneys: %w", err)
}
}

return nil
Expand Down
168 changes: 168 additions & 0 deletions cmd/event-received/cloud_watch_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,5 +607,173 @@ func TestHandleDonorSubmissionCompletedWhenShareCodeSenderError(t *testing.T) {
Return(expectedError)

err := handleDonorSubmissionCompleted(ctx, client, event, shareCodeSender, appData, lpaStoreClient)
assert.Equal(t, fmt.Errorf("failed to send share code to certificate provider: %w", expectedError), err)
}

var certificateProviderSubmissionCompletedEvent = events.CloudWatchEvent{
DetailType: "certificate-provider-submission-completed",
Detail: json.RawMessage(`{"uid":"M-1111-2222-3333"}`),
}

func TestHandleCertificateProviderSubmissionCompleted(t *testing.T) {
appData := page.AppData{}

donor := &actor.DonorProvidedDetails{
CertificateProvider: actor.CertificateProvider{
CarryOutBy: actor.Paper,
},
}

lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(donor, nil)

shareCodeSender := newMockShareCodeSender(t)
shareCodeSender.EXPECT().
SendAttorneys(ctx, appData, donor).
Return(nil)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)
factory.EXPECT().
ShareCodeSender(ctx).
Return(shareCodeSender, nil)
factory.EXPECT().
AppData().
Return(appData, nil)

err := handleCertificateProviderSubmissionCompleted(ctx, certificateProviderSubmissionCompletedEvent, factory)
assert.Nil(t, err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenOnline(t *testing.T) {
donor := &actor.DonorProvidedDetails{
CertificateProvider: actor.CertificateProvider{
CarryOutBy: actor.Online,
},
}

lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(donor, nil)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Nil(t, err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenLpaStoreFactoryErrors(t *testing.T) {
factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(nil, expectedError)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Equal(t, expectedError, err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenLpaStoreErrors(t *testing.T) {
lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(nil, expectedError)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Equal(t, fmt.Errorf("failed to retrieve lpa: %w", expectedError), err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenShareCodeSenderErrors(t *testing.T) {
lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(&actor.DonorProvidedDetails{
CertificateProvider: actor.CertificateProvider{
CarryOutBy: actor.Paper,
},
}, nil)

shareCodeSender := newMockShareCodeSender(t)
shareCodeSender.EXPECT().
SendAttorneys(ctx, mock.Anything, mock.Anything).
Return(expectedError)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)
factory.EXPECT().
ShareCodeSender(ctx).
Return(shareCodeSender, nil)
factory.EXPECT().
AppData().
Return(page.AppData{}, nil)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Equal(t, fmt.Errorf("failed to send share codes to attorneys: %w", expectedError), err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenShareCodeSenderFactoryErrors(t *testing.T) {
lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(&actor.DonorProvidedDetails{
CertificateProvider: actor.CertificateProvider{
CarryOutBy: actor.Paper,
},
}, nil)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)
factory.EXPECT().
ShareCodeSender(ctx).
Return(nil, expectedError)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Equal(t, expectedError, err)
}

func TestHandleCertificateProviderSubmissionCompletedWhenAppDataFactoryErrors(t *testing.T) {
lpaStoreClient := newMockLpaStoreClient(t)
lpaStoreClient.EXPECT().
Lpa(ctx, "M-1111-2222-3333").
Return(&actor.DonorProvidedDetails{
CertificateProvider: actor.CertificateProvider{
CarryOutBy: actor.Paper,
},
}, nil)

factory := newMockFactory(t)
factory.EXPECT().
LpaStoreClient().
Return(lpaStoreClient, nil)
factory.EXPECT().
ShareCodeSender(ctx).
Return(nil, nil)
factory.EXPECT().
AppData().
Return(page.AppData{}, expectedError)

handler := &cloudWatchEventHandler{factory: factory}
err := handler.Handle(ctx, certificateProviderSubmissionCompletedEvent)
assert.Equal(t, expectedError, err)
}
1 change: 1 addition & 0 deletions cmd/event-received/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type SecretsClient interface {
type ShareCodeSender interface {
SendCertificateProviderInvite(context.Context, page.AppData, *actor.DonorProvidedDetails) error
SendCertificateProviderPrompt(context.Context, page.AppData, *actor.DonorProvidedDetails) error
SendAttorneys(context.Context, page.AppData, *actor.DonorProvidedDetails) error
}

type UidStore interface {
Expand Down
48 changes: 48 additions & 0 deletions cmd/event-received/mock_shareCodeSender_test.go

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

0 comments on commit 3c4dd3f

Please sign in to comment.