Skip to content

Commit

Permalink
Add tests for the easy lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Aug 23, 2024
1 parent 11ad7fb commit a90991a
Show file tree
Hide file tree
Showing 17 changed files with 2,115 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ packages:
github.com/ministryofjustice/opg-data-lpa-store/internal/event:
github.com/ministryofjustice/opg-data-lpa-store/internal/objectstore:
github.com/ministryofjustice/opg-data-lpa-store/internal/shared:
github.com/ministryofjustice/opg-data-lpa-store/lambda/create:
github.com/ministryofjustice/opg-data-lpa-store/lambda/get:
github.com/ministryofjustice/opg-data-lpa-store/lambda/getlist:
34 changes: 17 additions & 17 deletions lambda/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ type Lambda struct {
store Store
verifier Verifier
logger Logger
now func() time.Time
}

func (l *Lambda) HandleEvent(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
uid := req.PathParameters["uid"]

_, err := l.verifier.VerifyHeader(req)
if err != nil {
l.logger.Info("Unable to verify JWT from header")
Expand All @@ -60,9 +63,6 @@ func (l *Lambda) HandleEvent(ctx context.Context, req events.APIGatewayProxyRequ

l.logger.Debug("Successfully parsed JWT from event header")

var input shared.LpaInit
uid := req.PathParameters["uid"]

response := events.APIGatewayProxyResponse{
StatusCode: 500,
Body: "{\"code\":\"INTERNAL_SERVER_ERROR\",\"detail\":\"Internal server error\"}",
Expand All @@ -82,16 +82,16 @@ func (l *Lambda) HandleEvent(ctx context.Context, req events.APIGatewayProxyRequ
return problem.Respond()
}

err = json.Unmarshal([]byte(req.Body), &input)
if err != nil {
var input shared.LpaInit
if err := json.Unmarshal([]byte(req.Body), &input); err != nil {
l.logger.Error("error unmarshalling request", slog.Any("err", err))
return shared.ProblemInternalServerError.Respond()
}

// validation
if errs := Validate(input); len(errs) > 0 {
if input.Channel == shared.ChannelPaper {
l.logger.Info("encountered validation errors in lpa", slog.Any("uid", uid))
l.logger.Info("encountered validation errors in lpa", slog.String("uid", uid))
} else {
problem := shared.ProblemInvalidRequest
problem.Errors = errs
Expand All @@ -100,18 +100,19 @@ func (l *Lambda) HandleEvent(ctx context.Context, req events.APIGatewayProxyRequ
}
}

data := shared.Lpa{LpaInit: input}
data.Uid = uid
data.Status = shared.LpaStatusInProgress
data.UpdatedAt = time.Now()
data := shared.Lpa{
LpaInit: input,
Uid: uid,
Status: shared.LpaStatusInProgress,
UpdatedAt: l.now(),
}

if data.Channel == shared.ChannelPaper && len(input.RestrictionsAndConditionsImages) > 0 {
data.RestrictionsAndConditionsImages = make([]shared.File, len(input.RestrictionsAndConditionsImages))
for i, image := range input.RestrictionsAndConditionsImages {
path := fmt.Sprintf("%s/scans/rc_%d_%s", data.Uid, i, image.Filename)

data.RestrictionsAndConditionsImages[i], err = l.staticLpaStorage.UploadFile(ctx, image, path)

if err != nil {
l.logger.Error("error saving restrictions and conditions image", slog.Any("err", err))
return shared.ProblemInternalServerError.Respond()
Expand All @@ -120,26 +121,24 @@ func (l *Lambda) HandleEvent(ctx context.Context, req events.APIGatewayProxyRequ
}

// save
if err = l.store.Put(ctx, data); err != nil {
if err := l.store.Put(ctx, data); err != nil {
l.logger.Error("error saving LPA", slog.Any("err", err))
return shared.ProblemInternalServerError.Respond()
}

// save to static storage as JSON
objectKey := fmt.Sprintf("%s/donor-executed-lpa.json", data.Uid)

if err = l.staticLpaStorage.Put(ctx, objectKey, data); err != nil {
if err := l.staticLpaStorage.Put(ctx, objectKey, data); err != nil {
l.logger.Error("error saving static record", slog.Any("err", err))
return shared.ProblemInternalServerError.Respond()
}

// send lpa-updated event
err = l.eventClient.SendLpaUpdated(ctx, event.LpaUpdated{
if err := l.eventClient.SendLpaUpdated(ctx, event.LpaUpdated{
Uid: uid,
ChangeType: "CREATE",
})

if err != nil {
}); err != nil {
l.logger.Error("unexpected error occurred", slog.Any("err", err))
}

Expand Down Expand Up @@ -179,6 +178,7 @@ func main() {
),
verifier: shared.NewJWTVerifier(cfg, logger),
logger: logger,
now: time.Now,
}

lambda.Start(l.HandleEvent)
Expand Down
Loading

0 comments on commit a90991a

Please sign in to comment.