Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpearce-digital authored Oct 3, 2023
2 parents 8e8762b + 755481e commit 80824a9
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 161 deletions.
22 changes: 2 additions & 20 deletions internal/actor/certificate_provider_provided.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package actor

import (
"fmt"
"time"

"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/identity"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
)

// CertificateProviderProvidedDetails contains details about the certificate provider, provided by the certificate provider
Expand All @@ -16,20 +14,8 @@ type CertificateProviderProvidedDetails struct {
LpaID string
// Tracking when CertificateProviderProvidedDetails is updated
UpdatedAt time.Time
// First names of the certificate provider
FirstNames string
// Last name of the certificate provider
LastName string
// Email of the certificate provider
Email string
// Address of the certificate provider
Address place.Address
// Mobile number of the certificate provider
Mobile string
// Date of birth of the certificate provider
DateOfBirth date.Date
// The full name provided by the certificate provider. Only requested if the certificate provider indicates their name as provided by the applicant is incorrect
DeclaredFullName string
// The method by which the certificate provider will complete identity checks
IdentityOption identity.Option
// Data returned from an identity check service
Expand All @@ -40,13 +26,9 @@ type CertificateProviderProvidedDetails struct {
Tasks CertificateProviderTasks
}

func (c CertificateProviderProvidedDetails) FullName() string {
return fmt.Sprintf("%s %s", c.FirstNames, c.LastName)
}

func (c *CertificateProviderProvidedDetails) CertificateProviderIdentityConfirmed() bool {
func (c *CertificateProviderProvidedDetails) CertificateProviderIdentityConfirmed(firstNames, lastName string) bool {
return c.IdentityUserData.OK && c.IdentityUserData.Provider != identity.UnknownOption &&
c.IdentityUserData.MatchName(c.FirstNames, c.LastName) &&
c.IdentityUserData.MatchName(firstNames, lastName) &&
c.IdentityUserData.DateOfBirth.Equals(c.DateOfBirth)
}

Expand Down
26 changes: 11 additions & 15 deletions internal/actor/certificate_provider_provided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCertificateProviderProvidedFullName(t *testing.T) {
p := CertificateProviderProvidedDetails{FirstNames: "Bob Alan George", LastName: "Smith Jones-Doe"}

assert.Equal(t, "Bob Alan George Smith Jones-Doe", p.FullName())
}

func TestCertificateProviderProvidedIdentityConfirmed(t *testing.T) {
testCases := map[string]struct {
cp *CertificateProviderProvidedDetails
expected bool
cp *CertificateProviderProvidedDetails
firstNames string
lastName string
expected bool
}{
"set": {
cp: &CertificateProviderProvidedDetails{
FirstNames: "a",
LastName: "b",
IdentityUserData: identity.UserData{OK: true, Provider: identity.OneLogin, FirstNames: "a", LastName: "b"},
},
expected: true,
firstNames: "a",
lastName: "b",
expected: true,
},
"missing provider": {
cp: &CertificateProviderProvidedDetails{
Expand All @@ -40,11 +36,11 @@ func TestCertificateProviderProvidedIdentityConfirmed(t *testing.T) {
},
"no match": {
cp: &CertificateProviderProvidedDetails{
FirstNames: "a",
LastName: "b",
IdentityUserData: identity.UserData{Provider: identity.OneLogin},
},
expected: false,
firstNames: "a",
lastName: "b",
expected: false,
},
"none": {
cp: &CertificateProviderProvidedDetails{},
Expand All @@ -54,7 +50,7 @@ func TestCertificateProviderProvidedIdentityConfirmed(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.cp.CertificateProviderIdentityConfirmed())
assert.Equal(t, tc.expected, tc.cp.CertificateProviderIdentityConfirmed(tc.firstNames, tc.lastName))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ type identityWithOneLoginCallbackData struct {
CouldNotConfirm bool
}

func IdentityWithOneLoginCallback(tmpl template.Template, oneLoginClient OneLoginClient, sessionStore sesh.Store, certificateProviderStore CertificateProviderStore) page.Handler {
func IdentityWithOneLoginCallback(tmpl template.Template, oneLoginClient OneLoginClient, sessionStore sesh.Store, certificateProviderStore CertificateProviderStore, donorStore DonorStore) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
certificateProvider, err := certificateProviderStore.Get(r.Context())
if err != nil {
return err
}

lpa, err := donorStore.GetAny(r.Context())
if err != nil {
return err
}

if r.Method == http.MethodPost {
if certificateProvider.CertificateProviderIdentityConfirmed() {
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
return appData.Redirect(w, r, nil, page.Paths.CertificateProvider.ReadTheLpa.Format(certificateProvider.LpaID))
} else {
return appData.Redirect(w, r, nil, page.Paths.CertificateProvider.SelectYourIdentityOptions1.Format(certificateProvider.LpaID))
Expand All @@ -39,7 +44,7 @@ func IdentityWithOneLoginCallback(tmpl template.Template, oneLoginClient OneLogi

data := &identityWithOneLoginCallbackData{App: appData}

if certificateProvider.CertificateProviderIdentityConfirmed() {
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
data.FirstNames = certificateProvider.IdentityUserData.FirstNames
data.LastName = certificateProvider.IdentityUserData.LastName
data.DateOfBirth = certificateProvider.IdentityUserData.DateOfBirth
Expand Down Expand Up @@ -77,7 +82,7 @@ func IdentityWithOneLoginCallback(tmpl template.Template, oneLoginClient OneLogi
certificateProvider.IdentityUserData = userData
certificateProvider.Tasks.ConfirmYourIdentity = actor.TaskCompleted

if certificateProvider.CertificateProviderIdentityConfirmed() {
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
data.FirstNames = userData.FirstNames
data.LastName = userData.LastName
data.DateOfBirth = userData.DateOfBirth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ func TestGetIdentityWithOneLoginCallback(t *testing.T) {
certificateProviderStore := newMockCertificateProviderStore(t)
certificateProviderStore.
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{FirstNames: "John", LastName: "Doe"}, nil)
Return(&actor.CertificateProviderProvidedDetails{}, nil)
certificateProviderStore.
On("Put", r.Context(), &actor.CertificateProviderProvidedDetails{
FirstNames: "John",
LastName: "Doe",
IdentityUserData: userData,
Tasks: actor.CertificateProviderTasks{
ConfirmYourIdentity: actor.TaskCompleted,
},
}).
Return(nil)

donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{FirstNames: "John", LastName: "Doe"}}, nil)

sessionStore := newMockSessionStore(t)
sessionStore.
On("Get", mock.Anything, "params").
Expand Down Expand Up @@ -69,7 +72,7 @@ func TestGetIdentityWithOneLoginCallback(t *testing.T) {
}).
Return(nil)

err := IdentityWithOneLoginCallback(template.Execute, oneLoginClient, sessionStore, certificateProviderStore)(testAppData, w, r)
err := IdentityWithOneLoginCallback(template.Execute, oneLoginClient, sessionStore, certificateProviderStore, donorStore)(testAppData, w, r)
resp := w.Result()

assert.Nil(t, err)
Expand Down Expand Up @@ -221,11 +224,16 @@ func TestGetIdentityWithOneLoginCallbackWhenIdentityNotConfirmed(t *testing.T) {
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{}, nil)

donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{}}, nil)

sessionStore := tc.sessionStore(t)
oneLoginClient := tc.oneLoginClient(t)
template := tc.template(t, w)

err := IdentityWithOneLoginCallback(template.Execute, oneLoginClient, sessionStore, certificateProviderStore)(testAppData, w, r)
err := IdentityWithOneLoginCallback(template.Execute, oneLoginClient, sessionStore, certificateProviderStore, donorStore)(testAppData, w, r)
resp := w.Result()

assert.Equal(t, tc.error, err)
Expand All @@ -234,7 +242,7 @@ func TestGetIdentityWithOneLoginCallbackWhenIdentityNotConfirmed(t *testing.T) {
}
}

func TestGetIdentityWithOneLoginCallbackWhenGetDataCertificateProviderStoreError(t *testing.T) {
func TestGetIdentityWithOneLoginCallbackWhenGetCertificateProviderStoreError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/?code=a-code", nil)

Expand All @@ -243,7 +251,26 @@ func TestGetIdentityWithOneLoginCallbackWhenGetDataCertificateProviderStoreError
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{}, expectedError)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore)(testAppData, w, r)
err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore, nil)(testAppData, w, r)

assert.Equal(t, expectedError, err)
}

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

certificateProviderStore := newMockCertificateProviderStore(t)
certificateProviderStore.
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{}, nil)

donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{}}, expectedError)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore, donorStore)(testAppData, w, r)

assert.Equal(t, expectedError, err)
}
Expand All @@ -261,6 +288,11 @@ func TestGetIdentityWithOneLoginCallbackWhenPutCertificateProviderStoreError(t *
On("Put", r.Context(), mock.Anything).
Return(expectedError)

donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{}}, nil)

sessionStore := newMockSessionStore(t)
sessionStore.
On("Get", mock.Anything, "params").
Expand All @@ -281,7 +313,7 @@ func TestGetIdentityWithOneLoginCallbackWhenPutCertificateProviderStoreError(t *
On("ParseIdentityClaim", mock.Anything, mock.Anything).
Return(identity.UserData{OK: true, Provider: identity.OneLogin}, nil)

err := IdentityWithOneLoginCallback(nil, oneLoginClient, sessionStore, certificateProviderStore)(testAppData, w, r)
err := IdentityWithOneLoginCallback(nil, oneLoginClient, sessionStore, certificateProviderStore, donorStore)(testAppData, w, r)

assert.Equal(t, expectedError, err)
}
Expand All @@ -296,11 +328,14 @@ func TestGetIdentityWithOneLoginCallbackWhenReturning(t *testing.T) {
certificateProviderStore.
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{
FirstNames: "first-names",
LastName: "last-name",
IdentityUserData: userData,
}, nil)

donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{FirstNames: "first-names", LastName: "last-name"}}, nil)

template := newMockTemplate(t)
template.
On("Execute", w, &identityWithOneLoginCallbackData{
Expand All @@ -311,7 +346,7 @@ func TestGetIdentityWithOneLoginCallbackWhenReturning(t *testing.T) {
}).
Return(nil)

err := IdentityWithOneLoginCallback(template.Execute, nil, nil, certificateProviderStore)(testAppData, w, r)
err := IdentityWithOneLoginCallback(template.Execute, nil, nil, certificateProviderStore, donorStore)(testAppData, w, r)
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -330,7 +365,12 @@ func TestPostIdentityWithOneLoginCallback(t *testing.T) {
IdentityUserData: identity.UserData{OK: true, Provider: identity.OneLogin},
}, nil)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore)(testAppData, w, r)
donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{}}, nil)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore, donorStore)(testAppData, w, r)
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -347,7 +387,12 @@ func TestPostIdentityWithOneLoginCallbackNotConfirmed(t *testing.T) {
On("Get", r.Context()).
Return(&actor.CertificateProviderProvidedDetails{LpaID: "lpa-id"}, nil)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore)(testAppData, w, r)
donorStore := newMockDonorStore(t)
donorStore.
On("GetAny", r.Context()).
Return(&page.Lpa{CertificateProvider: actor.CertificateProvider{}}, nil)

err := IdentityWithOneLoginCallback(nil, nil, nil, certificateProviderStore, donorStore)(testAppData, w, r)
resp := w.Result()

assert.Nil(t, err)
Expand Down
11 changes: 8 additions & 3 deletions internal/page/certificateprovider/identity_with_todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ type identityWithTodoData struct {
IdentityOption identity.Option
}

func IdentityWithTodo(tmpl template.Template, now func() time.Time, identityOption identity.Option, certificateProviderStore CertificateProviderStore) page.Handler {
func IdentityWithTodo(tmpl template.Template, now func() time.Time, identityOption identity.Option, certificateProviderStore CertificateProviderStore, donorStore DonorStore) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
certificateProvider, err := certificateProviderStore.Get(r.Context())
if err != nil {
return err
}

lpa, err := donorStore.GetAny(r.Context())
if err != nil {
return err
}

if r.Method == http.MethodPost {
return appData.Redirect(w, r, nil, page.Paths.CertificateProvider.ReadTheLpa.Format(certificateProvider.LpaID))
}

certificateProvider.IdentityUserData = identity.UserData{
OK: true,
Provider: identityOption,
FirstNames: certificateProvider.FirstNames,
LastName: certificateProvider.LastName,
FirstNames: lpa.CertificateProvider.FirstNames,
LastName: lpa.CertificateProvider.LastName,
DateOfBirth: certificateProvider.DateOfBirth,
RetrievedAt: now(),
}
Expand Down
Loading

0 comments on commit 80824a9

Please sign in to comment.