2023-04-26 05:46:48 +01:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb_test
|
|
|
|
|
|
|
|
import (
|
2023-05-09 20:18:07 +01:00
|
|
|
"database/sql"
|
2023-04-26 05:46:48 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectInvitations(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
invitesDB := db.Console().ProjectInvitations()
|
|
|
|
projectsDB := db.Console().Projects()
|
2023-05-09 20:18:07 +01:00
|
|
|
|
|
|
|
inviterID := testrand.UUID()
|
2023-04-26 05:46:48 +01:00
|
|
|
projID := testrand.UUID()
|
|
|
|
projID2 := testrand.UUID()
|
|
|
|
email := "user@mail.test"
|
|
|
|
email2 := "user2@mail.test"
|
|
|
|
|
|
|
|
_, err := projectsDB.Insert(ctx, &console.Project{ID: projID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = projectsDB.Insert(ctx, &console.Project{ID: projID2})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-09 20:18:07 +01:00
|
|
|
invite := &console.ProjectInvitation{
|
|
|
|
ProjectID: projID,
|
|
|
|
Email: email,
|
|
|
|
InviterID: &inviterID,
|
|
|
|
}
|
|
|
|
inviteSameEmail := &console.ProjectInvitation{
|
|
|
|
ProjectID: projID2,
|
|
|
|
Email: email,
|
|
|
|
}
|
|
|
|
inviteSameProject := &console.ProjectInvitation{
|
|
|
|
ProjectID: projID,
|
|
|
|
Email: email2,
|
|
|
|
}
|
2023-04-26 05:46:48 +01:00
|
|
|
|
2023-05-09 20:18:07 +01:00
|
|
|
if !t.Run("insert invitations", func(t *testing.T) {
|
|
|
|
// Expect failure because no user with inviterID exists.
|
2023-06-23 17:34:44 +01:00
|
|
|
_, err = invitesDB.Upsert(ctx, invite)
|
2023-05-09 20:18:07 +01:00
|
|
|
require.Error(t, err)
|
2023-04-26 05:46:48 +01:00
|
|
|
|
2023-05-09 20:18:07 +01:00
|
|
|
_, err = db.Console().Users().Insert(ctx, &console.User{
|
|
|
|
ID: inviterID,
|
|
|
|
PasswordHash: testrand.Bytes(8),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-23 17:34:44 +01:00
|
|
|
invite, err = invitesDB.Upsert(ctx, invite)
|
2023-05-09 20:18:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.WithinDuration(t, time.Now(), invite.CreatedAt, time.Minute)
|
|
|
|
require.Equal(t, projID, invite.ProjectID)
|
|
|
|
require.Equal(t, strings.ToUpper(email), invite.Email)
|
|
|
|
|
2023-06-23 17:34:44 +01:00
|
|
|
inviteSameEmail, err = invitesDB.Upsert(ctx, inviteSameEmail)
|
2023-05-09 20:18:07 +01:00
|
|
|
require.NoError(t, err)
|
2023-06-23 17:34:44 +01:00
|
|
|
inviteSameProject, err = invitesDB.Upsert(ctx, inviteSameProject)
|
2023-05-09 20:18:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}) {
|
|
|
|
// None of the following subtests will pass if invitation insertion failed.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("get invitation", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
2023-06-08 07:34:55 +01:00
|
|
|
other, err := invitesDB.Get(ctx, projID, "nobody@mail.test")
|
|
|
|
require.ErrorIs(t, err, sql.ErrNoRows)
|
|
|
|
require.Nil(t, other)
|
|
|
|
|
|
|
|
other, err = invitesDB.Get(ctx, projID, email)
|
2023-05-09 20:18:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, invite, other)
|
|
|
|
})
|
2023-04-26 05:46:48 +01:00
|
|
|
|
|
|
|
t.Run("get invitations by email", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
invites, err := invitesDB.GetByEmail(ctx, "nobody@mail.test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, invites)
|
|
|
|
|
|
|
|
invites, err = invitesDB.GetByEmail(ctx, "uSeR@mAiL.tEsT")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, invites, []console.ProjectInvitation{*invite, *inviteSameEmail})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get invitations by project ID", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
invites, err := invitesDB.GetByProjectID(ctx, testrand.UUID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, invites)
|
|
|
|
|
|
|
|
invites, err = invitesDB.GetByProjectID(ctx, projID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, invites, []console.ProjectInvitation{*invite, *inviteSameProject})
|
|
|
|
})
|
|
|
|
|
2023-05-09 20:18:07 +01:00
|
|
|
t.Run("ensure inviter removal nullifies inviter ID", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
require.NoError(t, db.Console().Users().Delete(ctx, inviterID))
|
|
|
|
invite, err := invitesDB.Get(ctx, projID, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, invite.InviterID)
|
|
|
|
})
|
|
|
|
|
2023-06-20 06:45:45 +01:00
|
|
|
t.Run("update invitation", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
inviter, err := db.Console().Users().Insert(ctx, &console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
PasswordHash: testrand.Bytes(8),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-23 17:34:44 +01:00
|
|
|
invite.InviterID = &inviter.ID
|
|
|
|
|
|
|
|
oldCreatedAt := invite.CreatedAt
|
|
|
|
|
|
|
|
invite, err = invitesDB.Upsert(ctx, invite)
|
2023-06-20 06:45:45 +01:00
|
|
|
require.NoError(t, err)
|
2023-06-23 17:34:44 +01:00
|
|
|
require.Equal(t, inviter.ID, *invite.InviterID)
|
|
|
|
require.True(t, invite.CreatedAt.After(oldCreatedAt))
|
2023-06-20 06:45:45 +01:00
|
|
|
})
|
|
|
|
|
2023-04-26 05:46:48 +01:00
|
|
|
t.Run("delete invitation", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
require.NoError(t, invitesDB.Delete(ctx, projID, email))
|
|
|
|
|
|
|
|
invites, err := invitesDB.GetByEmail(ctx, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, invites, []console.ProjectInvitation{*inviteSameEmail})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ensure project removal deletes invitations", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
require.NoError(t, projectsDB.Delete(ctx, projID))
|
|
|
|
|
|
|
|
invites, err := invitesDB.GetByProjectID(ctx, projID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, invites)
|
|
|
|
|
|
|
|
invites, err = invitesDB.GetByEmail(ctx, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, invites, []console.ProjectInvitation{*inviteSameEmail})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|