satellite/console: only return unexpired project invitations to user

This change causes users to be served only project member invitations
that have not expired. If expired invitations are encountered during
processing of an invitation listing request, they will be removed.

References #5855

Change-Id: I6f621305f4f0a993953eb40a4dbd2375493f02e3
This commit is contained in:
Jeremy Wharton 2023-06-10 22:17:27 -05:00 committed by Storj Robot
parent 8acb1ee5bf
commit d39424993b
2 changed files with 43 additions and 9 deletions

View File

@ -3445,7 +3445,30 @@ func (s *Service) GetUserProjectInvitations(ctx context.Context) (_ []ProjectInv
return nil, Error.Wrap(err)
}
return invites, nil
var active []ProjectInvitation
var deleteErrs []error
var expiredIDs []string
for _, invite := range invites {
if time.Now().After(invite.CreatedAt.Add(s.config.ProjectInvitationExpiration)) {
err := s.store.ProjectInvitations().Delete(ctx, invite.ProjectID, invite.Email)
if err != nil {
deleteErrs = append(deleteErrs, err)
expiredIDs = append(expiredIDs, invite.ProjectID.String())
}
continue
}
active = append(active, invite)
}
if len(deleteErrs) != 0 {
s.log.Warn("error deleting expired project invitations",
zap.Errors("errors", deleteErrs),
zap.String("email", user.Email),
zap.Strings("projectIDs", expiredIDs),
)
}
return active, nil
}
// ProjectInvitationResponse represents a response to a project member invitation.

View File

@ -1975,14 +1975,7 @@ func TestProjectInvitations(t *testing.T) {
return project
}
addInvite := func(t *testing.T, ctx context.Context, project *console.Project, email string, createdAt time.Time) *console.ProjectInvitation {
invite, err := sat.DB.Console().ProjectInvitations().Insert(ctx, &console.ProjectInvitation{
ProjectID: project.ID,
Email: email,
InviterID: &project.OwnerID,
})
require.NoError(t, err)
setInviteDate := func(ctx context.Context, invite *console.ProjectInvitation, createdAt time.Time) *console.ProjectInvitation {
result, err := sat.DB.Testing().RawDB().ExecContext(ctx,
"UPDATE project_invitations SET created_at = $1 WHERE project_id = $2 AND email = $3",
createdAt, invite.ProjectID, strings.ToUpper(invite.Email),
@ -1993,9 +1986,22 @@ func TestProjectInvitations(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, 1, count)
invite, err = sat.DB.Console().ProjectInvitations().Get(ctx, invite.ProjectID, invite.Email)
require.NoError(t, err)
return invite
}
addInvite := func(t *testing.T, ctx context.Context, project *console.Project, email string, createdAt time.Time) *console.ProjectInvitation {
invite, err := sat.DB.Console().ProjectInvitations().Insert(ctx, &console.ProjectInvitation{
ProjectID: project.ID,
Email: email,
InviterID: &project.OwnerID,
})
require.NoError(t, err)
return setInviteDate(ctx, invite, createdAt)
}
t.Run("get invitation", func(t *testing.T) {
user, ctx := getUserAndCtx(t)
@ -2011,6 +2017,11 @@ func TestProjectInvitations(t *testing.T) {
require.Equal(t, invite.Email, invites[0].Email)
require.Equal(t, invite.InviterID, invites[0].InviterID)
require.WithinDuration(t, invite.CreatedAt, invites[0].CreatedAt, time.Second)
setInviteDate(ctx, invite, time.Now().Add(-sat.Config.Console.ProjectInvitationExpiration))
invites, err = service.GetUserProjectInvitations(ctx)
require.NoError(t, err)
require.Empty(t, invites)
})
t.Run("accept invitation", func(t *testing.T) {