-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
592 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package actor | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMemberInviteHasExpired(t *testing.T) { | ||
testcases := map[bool]time.Time{ | ||
true: time.Now().Add(-time.Hour * 48), | ||
false: time.Now().Add(-time.Hour * 47), | ||
} | ||
|
||
for hasExpired, createdAt := range testcases { | ||
t.Run(fmt.Sprintf("%v", hasExpired), func(t *testing.T) { | ||
assert.Equal(t, hasExpired, MemberInvite{CreatedAt: createdAt}.HasExpired()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,19 +38,19 @@ func TestOrganisationStoreCreate(t *testing.T) { | |
} | ||
|
||
func TestOrganisationStoreCreateWithSessionMissing(t *testing.T) { | ||
ctx := context.Background() | ||
organisationStore := &organisationStore{dynamoClient: nil, now: testNowFn} | ||
|
||
err := organisationStore.Create(ctx, "A name") | ||
assert.Equal(t, page.SessionMissingError{}, err) | ||
} | ||
testcases := map[string]context.Context{ | ||
"no session id": page.ContextWithSessionData(context.Background(), &page.SessionData{}), | ||
"no session data": context.Background(), | ||
} | ||
|
||
func TestOrganisationStoreCreateWithMissingSessionID(t *testing.T) { | ||
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{}) | ||
organisationStore := &organisationStore{dynamoClient: nil, now: testNowFn} | ||
for name, ctx := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
organisationStore := &organisationStore{} | ||
|
||
err := organisationStore.Create(ctx, "A name") | ||
assert.Error(t, err) | ||
err := organisationStore.Create(ctx, "A name") | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestOrganisationStoreCreateWhenErrors(t *testing.T) { | ||
|
@@ -160,3 +160,37 @@ func TestOrganisationStoreGetWhenErrors(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestOrganisationStoreCreateMemberInvite(t *testing.T) { | ||
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"}) | ||
|
||
dynamoClient := newMockDynamoClient(t) | ||
dynamoClient.EXPECT(). | ||
Create(ctx, &actor.MemberInvite{ | ||
PK: "MEMBERINVITE#abcde", | ||
SK: "MEMBERINVITE#abcde", | ||
CreatedAt: testNow, | ||
OrganisationID: "a-uuid", | ||
Email: "[email protected]", | ||
}). | ||
Return(nil) | ||
|
||
organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn} | ||
|
||
err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{ID: "a-uuid"}, "[email protected]", "abcde") | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestOrganisationStoreCreateMemberInviteWhenErrors(t *testing.T) { | ||
ctx := page.ContextWithSessionData(context.Background(), &page.SessionData{SessionID: "an-id"}) | ||
|
||
dynamoClient := newMockDynamoClient(t) | ||
dynamoClient.EXPECT(). | ||
Create(ctx, mock.Anything). | ||
Return(expectedError) | ||
|
||
organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn} | ||
|
||
err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{}, "[email protected]", "abcde") | ||
assert.ErrorIs(t, err, expectedError) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package supporter | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ministryofjustice/opg-go-common/template" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/page" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation" | ||
) | ||
|
||
type inviteMemberData struct { | ||
App page.AppData | ||
Errors validation.List | ||
Form *inviteMemberForm | ||
} | ||
|
||
func InviteMember(tmpl template.Template, organisationStore OrganisationStore, notifyClient NotifyClient, randomString func(int) string) Handler { | ||
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error { | ||
data := &inviteMemberData{ | ||
App: appData, | ||
Form: &inviteMemberForm{}, | ||
} | ||
|
||
if r.Method == http.MethodPost { | ||
data.Form = readInviteMemberForm(r) | ||
data.Errors = data.Form.Validate() | ||
|
||
if !data.Errors.Any() { | ||
organisation, err := organisationStore.Get(r.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
inviteCode := randomString(12) | ||
if err := organisationStore.CreateMemberInvite(r.Context(), organisation, data.Form.Email, inviteCode); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := notifyClient.SendEmail(r.Context(), data.Form.Email, notify.MemberInviteEmail{ | ||
OrganisationName: organisation.Name, | ||
InviteCode: inviteCode, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return page.Paths.Supporter.Dashboard.Redirect(w, r, appData) | ||
} | ||
} | ||
|
||
return tmpl(w, data) | ||
} | ||
} | ||
|
||
type inviteMemberForm struct { | ||
Email string | ||
} | ||
|
||
func readInviteMemberForm(r *http.Request) *inviteMemberForm { | ||
return &inviteMemberForm{ | ||
Email: page.PostFormString(r, "email"), | ||
} | ||
} | ||
|
||
func (f *inviteMemberForm) Validate() validation.List { | ||
var errors validation.List | ||
|
||
errors.String("email", "email", f.Email, | ||
validation.Empty(), | ||
validation.Email()) | ||
|
||
return errors | ||
} |
Oops, something went wrong.