Skip to content

Commit

Permalink
Merge ba064c9 into 3128f2a
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Mar 4, 2024
2 parents 3128f2a + ba064c9 commit 07d421a
Show file tree
Hide file tree
Showing 64 changed files with 2,134 additions and 1,738 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ packages:
github.com/ministryofjustice/opg-modernising-lpa/internal/s3:
github.com/ministryofjustice/opg-modernising-lpa/internal/search:
github.com/ministryofjustice/opg-modernising-lpa/internal/secrets:
github.com/ministryofjustice/opg-modernising-lpa/internal/sesh:
github.com/ministryofjustice/opg-modernising-lpa/internal/uid:
github.com/ministryofjustice/opg-modernising-lpa/internal/validation:
4 changes: 2 additions & 2 deletions cmd/mlpa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/golang-jwt/jwt/v5"
"github.com/gorilla/handlers"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/env"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/s3"
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/telemetry"
"github.com/ministryofjustice/opg-modernising-lpa/internal/templatefn"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
Expand Down Expand Up @@ -226,7 +226,7 @@ func run(ctx context.Context, logger *slog.Logger) error {
return err
}

sessionStore := sessions.NewCookieStore(sessionKeys...)
sessionStore := sesh.NewStore(sessionKeys)

redirectURL := authRedirectBaseURL + page.Paths.AuthRedirect.Format()

Expand Down
11 changes: 4 additions & 7 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/google/uuid"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
Expand Down Expand Up @@ -65,17 +64,15 @@ type S3Client interface {
}

type SessionStore interface {
Get(r *http.Request, name string) (*sessions.Session, error)
New(r *http.Request, name string) (*sessions.Session, error)
Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error
Login(r *http.Request) (*sesh.LoginSession, error)
}

func App(
logger *slog.Logger,
localizer page.Localizer,
lang localize.Lang,
tmpls, donorTmpls, certificateProviderTmpls, attorneyTmpls, supporterTmpls template.Templates,
sessionStore SessionStore,
sessionStore *sesh.Store,
lpaDynamoClient DynamoClient,
appPublicURL string,
payClient *pay.Client,
Expand Down Expand Up @@ -251,7 +248,7 @@ const (
RequireSession
)

func makeHandle(mux *http.ServeMux, errorHandler page.ErrorHandler, store sesh.Store) func(page.Path, handleOpt, page.Handler) {
func makeHandle(mux *http.ServeMux, errorHandler page.ErrorHandler, sessionStore SessionStore) func(page.Path, handleOpt, page.Handler) {
return func(path page.Path, opt handleOpt, h page.Handler) {
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand All @@ -260,7 +257,7 @@ func makeHandle(mux *http.ServeMux, errorHandler page.ErrorHandler, store sesh.S
appData.Page = path.Format()

if opt&RequireSession != 0 {
loginSession, err := sesh.Login(store, r)
loginSession, err := sessionStore.Login(r)
if err != nil {
http.Redirect(w, r, page.Paths.Start.Format(), http.StatusFound)
return
Expand Down
29 changes: 4 additions & 25 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"testing"

"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
Expand Down Expand Up @@ -55,8 +54,8 @@ func TestMakeHandleRequireSession(t *testing.T) {

sessionStore := newMockSessionStore(t)
sessionStore.EXPECT().
Get(r, "session").
Return(&sessions.Session{Values: map[any]any{"session": &sesh.LoginSession{Sub: "random"}}}, nil)
Login(r).
Return(&sesh.LoginSession{Sub: "random"}, nil)

mux := http.NewServeMux()
handle := makeHandle(mux, nil, sessionStore)
Expand All @@ -80,34 +79,14 @@ func TestMakeHandleRequireSession(t *testing.T) {
assert.Equal(t, http.StatusTeapot, resp.StatusCode)
}

func TestMakeHandleRequireSessionMissing(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/path?a=b", nil)

sessionStore := newMockSessionStore(t)
sessionStore.EXPECT().
Get(r, "session").
Return(&sessions.Session{}, nil)

mux := http.NewServeMux()
handle := makeHandle(mux, nil, sessionStore)
handle("/path", RequireSession, func(_ page.AppData, _ http.ResponseWriter, _ *http.Request) error { return nil })

mux.ServeHTTP(w, r)
resp := w.Result()

assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, page.Paths.Start.Format(), resp.Header.Get("Location"))
}

func TestMakeHandleRequireSessionError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/path?a=b", nil)

sessionStore := newMockSessionStore(t)
sessionStore.EXPECT().
Get(r, "session").
Return(&sessions.Session{}, expectedError)
Login(r).
Return(nil, expectedError)

mux := http.NewServeMux()
handle := makeHandle(mux, nil, sessionStore)
Expand Down
152 changes: 22 additions & 130 deletions internal/app/mock_SessionStore_test.go

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

6 changes: 2 additions & 4 deletions internal/page/attorney/enter_reference_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"net/http"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)

Expand All @@ -20,7 +18,7 @@ type enterReferenceNumberData struct {
Form *enterReferenceNumberForm
}

func EnterReferenceNumber(tmpl template.Template, shareCodeStore ShareCodeStore, sessionStore sessions.Store, attorneyStore AttorneyStore) page.Handler {
func EnterReferenceNumber(tmpl template.Template, shareCodeStore ShareCodeStore, sessionStore SessionStore, attorneyStore AttorneyStore) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
data := enterReferenceNumberData{
App: appData,
Expand All @@ -44,7 +42,7 @@ func EnterReferenceNumber(tmpl template.Template, shareCodeStore ShareCodeStore,
}
}

session, err := sesh.Login(sessionStore, r)
session, err := sessionStore.Login(r)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 07d421a

Please sign in to comment.