Skip to content

Commit

Permalink
Move donor store to new package and use appcontext everywhere (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Aug 1, 2024
1 parent 7564d07 commit a4ad5ee
Show file tree
Hide file tree
Showing 69 changed files with 2,266 additions and 656 deletions.
3 changes: 2 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ packages:
github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneypage:
github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata:
github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderpage:
github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata:
github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donorpage:
github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo:
github.com/ministryofjustice/opg-modernising-lpa/internal/event:
github.com/ministryofjustice/opg-modernising-lpa/internal/lambda:
github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore:
github.com/ministryofjustice/opg-modernising-lpa/internal/notify:
github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin:
github.com/ministryofjustice/opg-modernising-lpa/internal/page/donor:
github.com/ministryofjustice/opg-modernising-lpa/internal/page/supporter:
github.com/ministryofjustice/opg-modernising-lpa/internal/page:
github.com/ministryofjustice/opg-modernising-lpa/internal/pay:
Expand Down
15 changes: 7 additions & 8 deletions cmd/event-received/mock_lpaStoreClient_test.go

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

15 changes: 7 additions & 8 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.

14 changes: 4 additions & 10 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/google/uuid"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneypage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderpage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donorpage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
Expand Down Expand Up @@ -91,15 +93,7 @@ func App(
) http.Handler {
documentStore := NewDocumentStore(lpaDynamoClient, s3Client, eventClient, random.UuidString, time.Now)

donorStore := &donorStore{
dynamoClient: lpaDynamoClient,
eventClient: eventClient,
logger: logger,
uuidString: uuid.NewString,
newUID: actoruid.New,
now: time.Now,
searchClient: searchClient,
}
donorStore := donordata.NewStore(lpaDynamoClient, eventClient, logger, searchClient)
certificateProviderStore := certificateproviderdata.NewStore(lpaDynamoClient, time.Now)
attorneyStore := attorneydata.NewStore(lpaDynamoClient, time.Now)
shareCodeStore := &shareCodeStore{dynamoClient: lpaDynamoClient, now: time.Now}
Expand Down Expand Up @@ -277,7 +271,7 @@ func makeHandle(mux *http.ServeMux, errorHandler page.ErrorHandler, sessionStore
}

appData.SessionID = loginSession.SessionID()
ctx = page.ContextWithSessionData(ctx, &page.SessionData{SessionID: appData.SessionID})
ctx = page.ContextWithSessionData(ctx, &appcontext.SessionData{SessionID: appData.SessionID})
}

if err := h(appData, w, r.WithContext(page.ContextWithAppData(ctx, appData))); err != nil {
Expand Down
80 changes: 78 additions & 2 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package app

import (
"context"
"encoding/json"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin"
Expand All @@ -18,8 +26,76 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
)

var (
ctx = context.Background()
expectedError = errors.New("err")
testNow = time.Date(2023, time.April, 2, 3, 4, 5, 6, time.UTC)
testNowFn = func() time.Time { return testNow }
testUID = actoruid.New()
testUIDFn = func() actoruid.UID { return testUID }
)

func (m *mockDynamoClient) ExpectOne(ctx, pk, sk, data interface{}, err error) {
m.
On("One", ctx, pk, sk, mock.Anything).
Return(func(ctx context.Context, pk dynamo.PK, partialSk dynamo.SK, v interface{}) error {
b, _ := json.Marshal(data)
json.Unmarshal(b, v)
return err
}).
Once()
}

func (m *mockDynamoClient) ExpectAllBySK(ctx, sk, data interface{}, err error) {
m.
On("AllBySK", ctx, sk, mock.Anything).
Return(func(ctx context.Context, sk dynamo.SK, v interface{}) error {
b, _ := json.Marshal(data)
json.Unmarshal(b, v)
return err
})
}

func (m *mockDynamoClient) ExpectAllByKeys(ctx context.Context, keys []dynamo.Keys, data []map[string]types.AttributeValue, err error) {
m.EXPECT().
AllByKeys(ctx, keys).
Return(data, err)
}

func (m *mockDynamoClient) ExpectOneBySK(ctx, sk, data interface{}, err error) {
m.
On("OneBySK", ctx, sk, mock.Anything).
Return(func(ctx context.Context, sk dynamo.SK, v interface{}) error {
b, _ := json.Marshal(data)
json.Unmarshal(b, v)
return err
})
}

func (m *mockDynamoClient) ExpectAllByPartialSK(ctx, pk, partialSk, data interface{}, err error) {
m.
On("AllByPartialSK", ctx, pk, partialSk, mock.Anything).
Return(func(ctx context.Context, pk dynamo.PK, partialSk dynamo.SK, v interface{}) error {
b, _ := json.Marshal(data)
json.Unmarshal(b, v)
return err
})
}

func (m *mockDynamoClient) ExpectOneByPK(ctx, pk, data interface{}, err error) {
m.
On("OneByPK", ctx, pk, mock.Anything).
Return(func(ctx context.Context, pk dynamo.PK, v interface{}) error {
b, _ := json.Marshal(data)
json.Unmarshal(b, v)
return err
}).
Once()
}

func TestApp(t *testing.T) {
app := App(true, &slog.Logger{}, &localize.Localizer{}, localize.En, template.Templates{}, template.Templates{}, template.Templates{}, template.Templates{}, template.Templates{}, nil, nil, "http://public.url", &pay.Client{}, &notify.Client{}, &place.Client{}, &onelogin.Client{}, nil, nil, nil, &search.Client{})

Expand Down Expand Up @@ -66,8 +142,8 @@ func TestMakeHandleRequireSession(t *testing.T) {
}, appData)
assert.Equal(t, w, hw)

sessionData, _ := page.SessionDataFromContext(hr.Context())
assert.Equal(t, &page.SessionData{SessionID: "cmFuZG9t"}, sessionData)
sessionData, _ := appcontext.SessionDataFromContext(hr.Context())
assert.Equal(t, &appcontext.SessionData{SessionID: "cmFuZG9t"}, sessionData)

hw.WriteHeader(http.StatusTeapot)
return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/app/dashboard_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
Expand Down Expand Up @@ -61,7 +62,7 @@ func (s *dashboardStore) SubExistsForActorType(ctx context.Context, sub string,
}

func (s *dashboardStore) GetAll(ctx context.Context) (donor, attorney, certificateProvider []page.LpaAndActorTasks, err error) {
data, err := page.SessionDataFromContext(ctx)
data, err := appcontext.SessionDataFromContext(ctx)
if err != nil {
return nil, nil, nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions internal/app/dashboard_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestDashboardStoreGetAll(t *testing.T) {

for name, attributeValues := range testCases {
t.Run(name, func(t *testing.T) {
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: sessionID})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: sessionID})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand Down Expand Up @@ -203,7 +204,7 @@ func TestDashboardStoreGetAllSubmittedForAttorneys(t *testing.T) {
IsReplacement: true,
}

ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: sessionID})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: sessionID})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestDashboardStoreGetAllWhenResolveErrors(t *testing.T) {

donor := &actor.DonorProvidedDetails{LpaID: "0", LpaUID: "M", UpdatedAt: aTime, SK: dynamo.LpaOwnerKey(dynamo.DonorKey(sessionID)), PK: dynamo.LpaKey("0")}

ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: sessionID})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: sessionID})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand All @@ -272,7 +273,7 @@ func TestDashboardStoreGetAllWhenResolveErrors(t *testing.T) {
}

func TestDashboardStoreGetAllWhenNone(t *testing.T) {
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: "an-id"})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand All @@ -288,7 +289,7 @@ func TestDashboardStoreGetAllWhenNone(t *testing.T) {
}

func TestDashboardStoreGetAllWhenAllForActorErrors(t *testing.T) {
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: "an-id"})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand All @@ -301,7 +302,7 @@ func TestDashboardStoreGetAllWhenAllForActorErrors(t *testing.T) {
}

func TestDashboardStoreGetAllWhenAllByKeysErrors(t *testing.T) {
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: "an-id"})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand All @@ -317,7 +318,7 @@ func TestDashboardStoreGetAllWhenAllByKeysErrors(t *testing.T) {
}

func TestDashboardStoreGetAllWhenReferenceGetErrors(t *testing.T) {
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"})
ctx := page.ContextWithSessionData(context.Background(), &appcontext.SessionData{SessionID: "an-id"})

dynamoClient := newMockDynamoClient(t)
dynamoClient.ExpectAllBySK(ctx, dynamo.SubKey("an-id"),
Expand Down
10 changes: 9 additions & 1 deletion internal/app/document_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import (

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
)

type EventClient interface {
SendUidRequested(context.Context, event.UidRequested) error
SendApplicationUpdated(context.Context, event.ApplicationUpdated) error
SendPreviousApplicationLinked(context.Context, event.PreviousApplicationLinked) error
SendReducedFeeRequested(context.Context, event.ReducedFeeRequested) error
}

type documentStore struct {
dynamoClient DynamoClient
s3Client S3Client
Expand Down Expand Up @@ -53,7 +61,7 @@ func (s *documentStore) Create(ctx context.Context, donor *actor.DonorProvidedDe
}

func (s *documentStore) GetAll(ctx context.Context) (page.Documents, error) {
data, err := page.SessionDataFromContext(ctx)
data, err := appcontext.SessionDataFromContext(ctx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a4ad5ee

Please sign in to comment.