Skip to content

Commit

Permalink
MLPAB-1739 Add supporter start page and login (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Jan 18, 2024
1 parent 2f360ca commit aff31d5
Show file tree
Hide file tree
Showing 21 changed files with 1,390 additions and 5 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ packages:
github.com/ministryofjustice/opg-modernising-lpa/internal/page/attorney:
github.com/ministryofjustice/opg-modernising-lpa/internal/page/certificateprovider:
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/place:
github.com/ministryofjustice/opg-modernising-lpa/internal/s3:
Expand Down
44 changes: 43 additions & 1 deletion cmd/mlpa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
html "html/template"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -112,7 +114,17 @@ func main() {
}
}

tmpls, err := template.Parse(webDir+"/template", templatefn.All(Tag, region))
layouts, err := parseLayoutTemplates(webDir+"/template/layout", templatefn.All(Tag, region))
if err != nil {
logger.Fatal(err)
}

tmpls, err := parseTemplates(webDir+"/template", layouts)
if err != nil {
logger.Fatal(err)
}

supporterTmpls, err := parseTemplates(webDir+"/template/supporter", layouts)
if err != nil {
logger.Fatal(err)
}
Expand Down Expand Up @@ -235,6 +247,7 @@ func main() {
bundle.For(localize.Cy),
localize.Cy,
tmpls,
supporterTmpls,
sessionStore,
lpasDynamoClient,
appPublicURL,
Expand All @@ -255,6 +268,7 @@ func main() {
bundle.For(localize.En),
localize.En,
tmpls,
supporterTmpls,
sessionStore,
lpasDynamoClient,
appPublicURL,
Expand Down Expand Up @@ -323,3 +337,31 @@ func awsRegion(metadataURL string) (string, error) {

return parts[3], nil
}

func parseLayoutTemplates(layoutDir string, funcs html.FuncMap) (*html.Template, error) {
return html.New("").Funcs(funcs).ParseGlob(filepath.Join(layoutDir, "*.*"))
}

func parseTemplates(templateDir string, layouts *html.Template) (template.Templates, error) {
files, err := filepath.Glob(filepath.Join(templateDir, "*.*"))
if err != nil {
return nil, err
}

tmpls := map[string]*html.Template{}
for _, file := range files {
clone, err := layouts.Clone()
if err != nil {
return nil, err
}

tmpl, err := clone.ParseFiles(file)
if err != nil {
return nil, err
}

tmpls[filepath.Base(file)] = tmpl
}

return tmpls, nil
}
18 changes: 18 additions & 0 deletions cypress/e2e/supporter/start.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
describe('Start', () => {
beforeEach(() => {
cy.visit('/supporter-start');
});

it('can be started', () => {
cy.contains("Help someone to make a lasting power of attorney");
cy.contains('a', 'Start').click();

if (Cypress.config().baseUrl.includes('localhost')) {
cy.url().should('contain', '/authorize')
} else {
cy.origin('https://signin.integration.account.gov.uk', () => {
cy.url().should('contain', '/')
})
}
});
});
13 changes: 13 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/page/certificateprovider"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page/fixtures"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page/supporter"
"github.com/ministryofjustice/opg-modernising-lpa/internal/pay"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
Expand Down Expand Up @@ -72,6 +73,7 @@ func App(
localizer page.Localizer,
lang localize.Lang,
tmpls template.Templates,
supporterTmpls template.Templates,
sessionStore SessionStore,
lpaDynamoClient DynamoClient,
appPublicURL string,
Expand Down Expand Up @@ -132,13 +134,24 @@ func App(
page.Guidance(tmpls.Get("certificate_provider_start.gohtml")))
handleRoot(page.Paths.Attorney.Start, None,
page.Guidance(tmpls.Get("attorney_start.gohtml")))
handleRoot(page.Paths.Supporter.Start, None,
page.Guidance(supporterTmpls.Get("start.gohtml")))
handleRoot(page.Paths.Dashboard, RequireSession,
page.Dashboard(tmpls.Get("dashboard.gohtml"), donorStore, dashboardStore))
handleRoot(page.Paths.LpaDeleted, RequireSession,
page.Guidance(tmpls.Get("lpa_deleted.gohtml")))
handleRoot(page.Paths.LpaWithdrawn, RequireSession,
page.Guidance(tmpls.Get("lpa_withdrawn.gohtml")))

supporter.Register(
rootMux,
supporterTmpls,
oneLoginClient,
sessionStore,
notFoundHandler,
errorHandler,
)

certificateprovider.Register(
rootMux,
logger,
Expand Down
2 changes: 1 addition & 1 deletion internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestApp(t *testing.T) {
app := App(&logging.Logger{}, &localize.Localizer{}, localize.En, template.Templates{}, nil, nil, "http://public.url", &pay.Client{}, &notify.Client{}, &place.Client{}, page.RumConfig{}, "?%3fNEI0t9MN", page.Paths, &onelogin.Client{}, "http://onelogin.url", nil, nil, nil)
app := App(&logging.Logger{}, &localize.Localizer{}, localize.En, template.Templates{}, template.Templates{}, nil, nil, "http://public.url", &pay.Client{}, &notify.Client{}, &place.Client{}, page.RumConfig{}, "?%3fNEI0t9MN", page.Paths, &onelogin.Client{}, "http://onelogin.url", nil, nil, nil)

assert.Implements(t, (*http.Handler)(nil), app)
}
Expand Down
32 changes: 32 additions & 0 deletions internal/page/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func (p CertificateProviderPath) Redirect(w http.ResponseWriter, r *http.Request
return nil
}

type SupporterPath string

func (p SupporterPath) String() string {
return string(p)
}

func (p SupporterPath) Format() string {
return "/supporter" + string(p)
}

func (p SupporterPath) Redirect(w http.ResponseWriter, r *http.Request, appData AppData) error {
http.Redirect(w, r, appData.Lang.URL(p.Format()), http.StatusFound)
return nil
}

type AttorneyPaths struct {
EnterReferenceNumber Path
Login Path
Expand Down Expand Up @@ -148,9 +163,18 @@ type HealthCheckPaths struct {
Dependency Path
}

type SupporterPaths struct {
Start Path
Login Path
LoginCallback Path

YourOrganisation SupporterPath
}

type AppPaths struct {
Attorney AttorneyPaths
CertificateProvider CertificateProviderPaths
Supporter SupporterPaths
HealthCheck HealthCheckPaths

AttorneyFixtures Path
Expand Down Expand Up @@ -305,6 +329,14 @@ var Paths = AppPaths{
YourPreferredLanguage: "/your-preferred-language",
},

Supporter: SupporterPaths{
Start: "/supporter-start",
Login: "/supporter-login",
LoginCallback: "/supporter-login-callback",

YourOrganisation: "/your-organisation",
},

HealthCheck: HealthCheckPaths{
Service: "/health-check/service",
Dependency: "/health-check/dependency",
Expand Down
21 changes: 21 additions & 0 deletions internal/page/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,24 @@ func TestCertificateProviderPathRedirect(t *testing.T) {
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, p.Format("lpa-id"), resp.Header.Get("Location"))
}

func TestSupporterPathString(t *testing.T) {
assert.Equal(t, "/anything", SupporterPath("/anything").String())
}

func TestSupporterPathFormat(t *testing.T) {
assert.Equal(t, "/supporter/anything", SupporterPath("/anything").Format())
}

func TestSupporterPathRedirect(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
p := SupporterPath("/something")

err := p.Redirect(w, r, AppData{Lang: localize.En})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, p.Format(), resp.Header.Get("Location"))
}
44 changes: 44 additions & 0 deletions internal/page/supporter/login_callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package supporter

import (
"context"
"net/http"

"github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
)

type LoginCallbackOneLoginClient interface {
Exchange(ctx context.Context, code, nonce string) (idToken, accessToken string, err error)
UserInfo(ctx context.Context, accessToken string) (onelogin.UserInfo, error)
}

func LoginCallback(oneLoginClient LoginCallbackOneLoginClient, sessionStore sesh.Store) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
oneLoginSession, err := sesh.OneLogin(sessionStore, r)
if err != nil {
return err
}

idToken, accessToken, err := oneLoginClient.Exchange(r.Context(), r.FormValue("code"), oneLoginSession.Nonce)
if err != nil {
return err
}

userInfo, err := oneLoginClient.UserInfo(r.Context(), accessToken)
if err != nil {
return err
}

if err := sesh.SetLoginSession(sessionStore, r, w, &sesh.LoginSession{
IDToken: idToken,
Sub: userInfo.Sub,
Email: userInfo.Email,
}); err != nil {
return err
}

return page.Paths.Supporter.YourOrganisation.Redirect(w, r, appData)
}
}
Loading

0 comments on commit aff31d5

Please sign in to comment.