2019-01-31 13:01:13 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console_test
|
|
|
|
|
|
|
|
import (
|
2019-07-17 21:53:14 +01:00
|
|
|
"context"
|
2021-11-18 18:54:39 +00:00
|
|
|
"database/sql"
|
2019-01-31 13:01:13 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-01 00:13:45 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-06 19:46:53 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2021-11-01 15:27:32 +00:00
|
|
|
"storj.io/common/memory"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2022-01-06 19:46:53 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-01-31 13:01:13 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
const (
|
2021-10-26 14:30:19 +01:00
|
|
|
lastName = "lastName"
|
|
|
|
email = "email@mail.test"
|
|
|
|
passValid = "123456"
|
|
|
|
name = "name"
|
|
|
|
newName = "newName"
|
|
|
|
newLastName = "newLastName"
|
|
|
|
newEmail = "newEmail@mail.test"
|
|
|
|
newPass = "newPass1234567890123456789012345"
|
|
|
|
position = "position"
|
|
|
|
companyName = "companyName"
|
|
|
|
employeeCount = "0"
|
|
|
|
workingOn = "workingOn"
|
|
|
|
isProfessional = true
|
|
|
|
mfaSecretKey = "mfaSecretKey"
|
|
|
|
signupPromoCode = "STORJ50"
|
2019-07-17 21:53:14 +01:00
|
|
|
)
|
|
|
|
|
2019-01-31 13:01:13 +00:00
|
|
|
func TestUserRepository(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-01-31 13:01:13 +00:00
|
|
|
repository := db.Console().Users()
|
2019-07-17 21:53:14 +01:00
|
|
|
|
|
|
|
user := &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2019-07-17 21:53:14 +01:00
|
|
|
FullName: name,
|
|
|
|
ShortName: lastName,
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte(passValid),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
testUsers(ctx, t, repository, user)
|
2021-02-04 15:00:15 +00:00
|
|
|
|
|
|
|
// test professional user
|
|
|
|
user = &console.User{
|
2021-10-26 14:30:19 +01:00
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: name,
|
|
|
|
ShortName: lastName,
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte(passValid),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
IsProfessional: isProfessional,
|
|
|
|
Position: position,
|
|
|
|
CompanyName: companyName,
|
|
|
|
EmployeeCount: employeeCount,
|
|
|
|
WorkingOn: workingOn,
|
|
|
|
SignupPromoCode: signupPromoCode,
|
2021-02-04 15:00:15 +00:00
|
|
|
}
|
|
|
|
testUsers(ctx, t, repository, user)
|
2019-07-17 21:53:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:00:33 +01:00
|
|
|
func TestUserEmailCase(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-09-10 15:00:33 +01:00
|
|
|
for _, testCase := range []struct {
|
2019-11-14 08:31:30 +00:00
|
|
|
email string
|
2019-09-10 15:00:33 +01:00
|
|
|
}{
|
|
|
|
{email: "prettyandsimple@example.com"},
|
|
|
|
{email: "firstname.lastname@domain.com "},
|
|
|
|
{email: "email@subdomain.domain.com "},
|
|
|
|
{email: "firstname+lastname@domain.com "},
|
|
|
|
{email: "email@[123.123.123.123] "},
|
|
|
|
{email: "\"email\"@domain.com"},
|
|
|
|
{email: "_______@domain.com "},
|
|
|
|
} {
|
|
|
|
newUser := &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2019-09-10 15:00:33 +01:00
|
|
|
FullName: newName,
|
|
|
|
ShortName: newLastName,
|
|
|
|
Email: testCase.email,
|
|
|
|
Status: console.Active,
|
|
|
|
PasswordHash: []byte(newPass),
|
|
|
|
}
|
|
|
|
|
|
|
|
createdUser, err := db.Console().Users().Insert(ctx, newUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.email, createdUser.Email)
|
|
|
|
|
|
|
|
createdUser.Status = console.Active
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
err = db.Console().Users().Update(ctx, createdUser.ID, console.UpdateUserRequest{
|
|
|
|
Status: &createdUser.Status,
|
|
|
|
})
|
2019-09-10 15:00:33 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedUser, err := db.Console().Users().GetByEmail(ctx, testCase.email)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.email, retrievedUser.Email)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-01 00:13:45 +01:00
|
|
|
func TestUserUpdatePaidTier(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
email := "testemail@mail.test"
|
|
|
|
fullName := "first name last name"
|
|
|
|
shortName := "short name"
|
|
|
|
password := "password"
|
2021-11-01 15:27:32 +00:00
|
|
|
projectBandwidthLimit := memory.Size(50000000000)
|
|
|
|
storageStorageLimit := memory.Size(50000000000)
|
2022-02-28 21:37:46 +00:00
|
|
|
projectLimit := 3
|
2021-12-06 19:06:50 +00:00
|
|
|
segmentLimit := int64(100)
|
2021-07-01 00:13:45 +01:00
|
|
|
newUser := &console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: fullName,
|
|
|
|
ShortName: shortName,
|
|
|
|
Email: email,
|
|
|
|
Status: console.Active,
|
|
|
|
PasswordHash: []byte(password),
|
|
|
|
}
|
|
|
|
|
|
|
|
createdUser, err := db.Console().Users().Insert(ctx, newUser)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, email, createdUser.Email)
|
|
|
|
require.Equal(t, fullName, createdUser.FullName)
|
|
|
|
require.Equal(t, shortName, createdUser.ShortName)
|
|
|
|
require.False(t, createdUser.PaidTier)
|
|
|
|
|
2022-02-28 21:37:46 +00:00
|
|
|
err = db.Console().Users().UpdatePaidTier(ctx, createdUser.ID, true, projectBandwidthLimit, storageStorageLimit, segmentLimit, projectLimit)
|
2021-07-01 00:13:45 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedUser, err := db.Console().Users().Get(ctx, createdUser.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, email, retrievedUser.Email)
|
|
|
|
require.Equal(t, fullName, retrievedUser.FullName)
|
|
|
|
require.Equal(t, shortName, retrievedUser.ShortName)
|
|
|
|
require.True(t, retrievedUser.PaidTier)
|
|
|
|
|
2022-02-28 21:37:46 +00:00
|
|
|
err = db.Console().Users().UpdatePaidTier(ctx, createdUser.ID, false, projectBandwidthLimit, storageStorageLimit, segmentLimit, projectLimit)
|
2021-07-01 00:13:45 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedUser, err = db.Console().Users().Get(ctx, createdUser.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, retrievedUser.PaidTier)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
func testUsers(ctx context.Context, t *testing.T, repository console.Users, user *console.User) {
|
|
|
|
|
|
|
|
t.Run("User insertion success", func(t *testing.T) {
|
|
|
|
|
|
|
|
insertedUser, err := repository.Insert(ctx, user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
insertedUser.Status = console.Active
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
err = repository.Update(ctx, insertedUser.ID, console.UpdateUserRequest{
|
|
|
|
Status: &insertedUser.Status,
|
|
|
|
})
|
2019-07-17 21:53:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get user success", func(t *testing.T) {
|
|
|
|
userByEmail, err := repository.GetByEmail(ctx, email)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, name, userByEmail.FullName)
|
|
|
|
assert.Equal(t, lastName, userByEmail.ShortName)
|
2021-10-26 14:30:19 +01:00
|
|
|
assert.Equal(t, user.SignupPromoCode, userByEmail.SignupPromoCode)
|
2021-06-29 22:41:44 +01:00
|
|
|
assert.False(t, user.PaidTier)
|
2021-07-13 18:21:16 +01:00
|
|
|
assert.False(t, user.MFAEnabled)
|
|
|
|
assert.Empty(t, user.MFASecretKey)
|
|
|
|
assert.Empty(t, user.MFARecoveryCodes)
|
2021-10-12 14:54:05 +01:00
|
|
|
|
2021-02-04 15:00:15 +00:00
|
|
|
if user.IsProfessional {
|
|
|
|
assert.Equal(t, workingOn, userByEmail.WorkingOn)
|
|
|
|
assert.Equal(t, position, userByEmail.Position)
|
|
|
|
assert.Equal(t, companyName, userByEmail.CompanyName)
|
2021-02-10 15:55:38 +00:00
|
|
|
assert.Equal(t, employeeCount, userByEmail.EmployeeCount)
|
2021-02-04 15:00:15 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, "", userByEmail.WorkingOn)
|
|
|
|
assert.Equal(t, "", userByEmail.Position)
|
|
|
|
assert.Equal(t, "", userByEmail.CompanyName)
|
2021-02-10 15:55:38 +00:00
|
|
|
assert.Equal(t, "", userByEmail.EmployeeCount)
|
2021-02-04 15:00:15 +00:00
|
|
|
}
|
2019-07-17 21:53:14 +01:00
|
|
|
|
|
|
|
userByID, err := repository.Get(ctx, userByEmail.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, name, userByID.FullName)
|
|
|
|
assert.Equal(t, lastName, userByID.ShortName)
|
2021-10-26 14:30:19 +01:00
|
|
|
assert.Equal(t, user.SignupPromoCode, userByID.SignupPromoCode)
|
2021-07-13 18:21:16 +01:00
|
|
|
assert.False(t, user.MFAEnabled)
|
|
|
|
assert.Empty(t, user.MFASecretKey)
|
|
|
|
assert.Empty(t, user.MFARecoveryCodes)
|
2019-07-17 21:53:14 +01:00
|
|
|
|
2021-02-04 15:00:15 +00:00
|
|
|
if user.IsProfessional {
|
|
|
|
assert.Equal(t, workingOn, userByID.WorkingOn)
|
|
|
|
assert.Equal(t, position, userByID.Position)
|
|
|
|
assert.Equal(t, companyName, userByID.CompanyName)
|
2021-02-10 15:55:38 +00:00
|
|
|
assert.Equal(t, employeeCount, userByID.EmployeeCount)
|
2021-02-04 15:00:15 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, "", userByID.WorkingOn)
|
|
|
|
assert.Equal(t, "", userByID.Position)
|
|
|
|
assert.Equal(t, "", userByID.CompanyName)
|
2021-02-10 15:55:38 +00:00
|
|
|
assert.Equal(t, "", userByID.EmployeeCount)
|
2021-02-04 15:00:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
assert.Equal(t, userByID.ID, userByEmail.ID)
|
|
|
|
assert.Equal(t, userByID.FullName, userByEmail.FullName)
|
|
|
|
assert.Equal(t, userByID.ShortName, userByEmail.ShortName)
|
|
|
|
assert.Equal(t, userByID.Email, userByEmail.Email)
|
|
|
|
assert.Equal(t, userByID.PasswordHash, userByEmail.PasswordHash)
|
|
|
|
assert.Equal(t, userByID.CreatedAt, userByEmail.CreatedAt)
|
2021-02-04 15:00:15 +00:00
|
|
|
assert.Equal(t, userByID.IsProfessional, userByEmail.IsProfessional)
|
|
|
|
assert.Equal(t, userByID.WorkingOn, userByEmail.WorkingOn)
|
|
|
|
assert.Equal(t, userByID.Position, userByEmail.Position)
|
|
|
|
assert.Equal(t, userByID.CompanyName, userByEmail.CompanyName)
|
2021-02-10 15:55:38 +00:00
|
|
|
assert.Equal(t, userByID.EmployeeCount, userByEmail.EmployeeCount)
|
2021-10-12 14:54:05 +01:00
|
|
|
assert.Equal(t, userByID.SignupPromoCode, userByEmail.SignupPromoCode)
|
2019-07-17 21:53:14 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update user success", func(t *testing.T) {
|
|
|
|
oldUser, err := repository.GetByEmail(ctx, email)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-07-13 18:21:16 +01:00
|
|
|
newUserInfo := &console.User{
|
2023-01-03 20:55:57 +00:00
|
|
|
ID: oldUser.ID,
|
|
|
|
FullName: newName,
|
|
|
|
ShortName: newLastName,
|
|
|
|
Email: newEmail,
|
|
|
|
Status: console.Active,
|
|
|
|
PaidTier: true,
|
|
|
|
MFAEnabled: true,
|
|
|
|
MFASecretKey: mfaSecretKey,
|
|
|
|
MFARecoveryCodes: []string{"1", "2"},
|
|
|
|
PasswordHash: []byte(newPass),
|
2019-07-17 21:53:14 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
shortNamePtr := &newUserInfo.ShortName
|
|
|
|
secretKeyPtr := &newUserInfo.MFASecretKey
|
|
|
|
|
|
|
|
err = repository.Update(ctx, newUserInfo.ID, console.UpdateUserRequest{
|
2023-01-03 20:55:57 +00:00
|
|
|
FullName: &newUserInfo.FullName,
|
|
|
|
ShortName: &shortNamePtr,
|
|
|
|
Email: &newUserInfo.Email,
|
|
|
|
Status: &newUserInfo.Status,
|
|
|
|
PaidTier: &newUserInfo.PaidTier,
|
|
|
|
MFAEnabled: &newUserInfo.MFAEnabled,
|
|
|
|
MFASecretKey: &secretKeyPtr,
|
|
|
|
MFARecoveryCodes: &newUserInfo.MFARecoveryCodes,
|
|
|
|
PasswordHash: newUserInfo.PasswordHash,
|
2022-06-01 22:15:37 +01:00
|
|
|
})
|
2019-07-17 21:53:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-07-13 18:21:16 +01:00
|
|
|
newUser, err := repository.Get(ctx, oldUser.ID)
|
2019-07-17 21:53:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, oldUser.ID, newUser.ID)
|
|
|
|
assert.Equal(t, newName, newUser.FullName)
|
|
|
|
assert.Equal(t, newLastName, newUser.ShortName)
|
|
|
|
assert.Equal(t, newEmail, newUser.Email)
|
|
|
|
assert.Equal(t, []byte(newPass), newUser.PasswordHash)
|
2021-06-29 22:41:44 +01:00
|
|
|
assert.True(t, newUser.PaidTier)
|
2021-07-13 18:21:16 +01:00
|
|
|
assert.True(t, newUser.MFAEnabled)
|
|
|
|
assert.Equal(t, mfaSecretKey, newUser.MFASecretKey)
|
|
|
|
assert.Equal(t, newUserInfo.MFARecoveryCodes, newUser.MFARecoveryCodes)
|
2019-07-17 21:53:14 +01:00
|
|
|
assert.Equal(t, oldUser.CreatedAt, newUser.CreatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete user success", func(t *testing.T) {
|
|
|
|
oldUser, err := repository.GetByEmail(ctx, newEmail)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = repository.Delete(ctx, oldUser.ID)
|
|
|
|
assert.NoError(t, err)
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
_, err = repository.Get(ctx, oldUser.ID)
|
|
|
|
assert.Error(t, err)
|
2019-01-31 13:01:13 +00:00
|
|
|
})
|
|
|
|
}
|
2021-11-18 18:54:39 +00:00
|
|
|
|
|
|
|
func TestGetUserByEmail(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
usersRepo := db.Console().Users()
|
|
|
|
email := "test@mail.test"
|
|
|
|
|
|
|
|
inactiveUser := console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "Inactive User",
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte("123a123"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := usersRepo.Insert(ctx, &inactiveUser)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = usersRepo.GetByEmail(ctx, email)
|
|
|
|
require.ErrorIs(t, sql.ErrNoRows, err)
|
|
|
|
|
|
|
|
verified, unverified, err := usersRepo.GetByEmailWithUnverified(ctx, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, verified)
|
|
|
|
require.Equal(t, inactiveUser.ID, unverified[0].ID)
|
|
|
|
|
|
|
|
activeUser := console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "Active User",
|
|
|
|
Email: email,
|
|
|
|
Status: console.Active,
|
|
|
|
PasswordHash: []byte("123a123"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = usersRepo.Insert(ctx, &activeUser)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Required to set the active status.
|
2022-06-01 22:15:37 +01:00
|
|
|
err = usersRepo.Update(ctx, activeUser.ID, console.UpdateUserRequest{
|
|
|
|
Status: &activeUser.Status,
|
|
|
|
})
|
2021-11-18 18:54:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dbUser, err := usersRepo.GetByEmail(ctx, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, activeUser.ID, dbUser.ID)
|
|
|
|
})
|
|
|
|
}
|
2022-01-06 19:46:53 +00:00
|
|
|
|
2023-10-23 17:27:00 +01:00
|
|
|
func TestGetUsersByStatus(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
usersRepo := db.Console().Users()
|
|
|
|
|
|
|
|
inactiveUser := console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "Inactive User",
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte("123a123"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := usersRepo.Insert(ctx, &inactiveUser)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
activeUser := console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "Active User",
|
|
|
|
Email: email,
|
|
|
|
Status: console.Active,
|
|
|
|
PasswordHash: []byte("123a123"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = usersRepo.Insert(ctx, &activeUser)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Required to set the active status.
|
|
|
|
err = usersRepo.Update(ctx, activeUser.ID, console.UpdateUserRequest{
|
|
|
|
Status: &activeUser.Status,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cursor := console.UserCursor{
|
|
|
|
Limit: 50,
|
|
|
|
Page: 1,
|
|
|
|
}
|
|
|
|
usersPage, err := usersRepo.GetByStatus(ctx, console.Inactive, cursor)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Lenf(t, usersPage.Users, 1, "expected 1 inactive user")
|
|
|
|
require.Equal(t, inactiveUser.ID, usersPage.Users[0].ID)
|
|
|
|
|
|
|
|
usersPage, err = usersRepo.GetByStatus(ctx, console.Active, cursor)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Lenf(t, usersPage.Users, 1, "expected 1 active user")
|
|
|
|
require.Equal(t, activeUser.ID, usersPage.Users[0].ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-06 19:46:53 +00:00
|
|
|
func TestGetUnverifiedNeedingReminder(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.EmailReminders.FirstVerificationReminder = 24 * time.Hour
|
|
|
|
config.EmailReminders.SecondVerificationReminder = 120 * time.Hour
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SatelliteCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
var sentFirstReminder bool
|
|
|
|
var sentSecondReminder bool
|
|
|
|
|
|
|
|
config := planet.Satellites[0].Config.EmailReminders
|
|
|
|
db := planet.Satellites[0].DB.Console().Users()
|
|
|
|
|
|
|
|
id := testrand.UUID()
|
|
|
|
_, err := db.Insert(ctx, &console.User{
|
|
|
|
ID: id,
|
|
|
|
FullName: "unverified user one",
|
|
|
|
Email: "userone@mail.test",
|
|
|
|
PasswordHash: []byte("123a123"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// We expect two reminders in total - one after a day of account creation,
|
|
|
|
// and one after five. This test will check to ensure that both reminders occur.
|
|
|
|
// Each iteration advances time by i*24 hours from `now`.
|
|
|
|
for i := 0; i <= 6; i++ {
|
|
|
|
u, err := db.Get(ctx, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Intuitively it would be better to test this by setting `created_at` to some point in the past.
|
|
|
|
// Since we have no control over `created_at` (it's autoinserted) we will instead pass in a future time
|
|
|
|
// as the `now` argument to `GetUnverifiedNeedingReminder`
|
|
|
|
futureTime := now.Add(time.Duration(i*24) * time.Hour)
|
2022-06-08 19:30:29 +01:00
|
|
|
needReminder, err := db.GetUnverifiedNeedingReminder(ctx, futureTime.Add(-config.FirstVerificationReminder), futureTime.Add(-config.SecondVerificationReminder), now.Add(-time.Hour))
|
2022-01-06 19:46:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// These are the conditions in the SQL query which selects users needing reminder
|
|
|
|
if u.VerificationReminders == 0 && u.CreatedAt.Before(futureTime.Add(-config.FirstVerificationReminder)) {
|
|
|
|
require.NotEmpty(t, needReminder)
|
|
|
|
require.Equal(t, u.ID, needReminder[0].ID)
|
|
|
|
require.NoError(t, db.UpdateVerificationReminders(ctx, u.ID))
|
|
|
|
sentFirstReminder = true
|
|
|
|
} else if u.VerificationReminders == 1 && u.CreatedAt.Before(futureTime.Add(-config.SecondVerificationReminder)) {
|
|
|
|
require.NotEmpty(t, needReminder)
|
|
|
|
require.Equal(t, u.ID, needReminder[0].ID)
|
|
|
|
require.NoError(t, db.UpdateVerificationReminders(ctx, u.ID))
|
|
|
|
sentSecondReminder = true
|
|
|
|
} else {
|
|
|
|
require.Empty(t, needReminder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, sentFirstReminder)
|
|
|
|
require.True(t, sentSecondReminder)
|
|
|
|
})
|
|
|
|
}
|