storj/satellite/console/emailreminders/chore_test.go
Malcolm Bouzi 087e57d037 web/satellite: create chore that will resend verification emails to unverified users
We want to remind unverified users to verify their emails:
once after 24 hours has passed and again after 5 days has passed.

Add mailservice.Service to satellite core because it is needed by the
chore for sending emails. To add the mailservice.Service to the core,
we create a helper function in satellite/peer.go to avoid duplicating
the code in both api.go and core.go. In addition to the chore, this
change adds methods to users.DB to get unverified users in need of
reminder.

Change-Id: I4e515bdf43f922788b4f965b2efb34fa32288bd1
2022-05-18 08:08:33 +00:00

168 lines
4.6 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package emailreminders_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
)
func TestEmailChoreUpdatesVerificationReminders(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.EmailReminders.FirstVerificationReminder = 0
config.EmailReminders.SecondVerificationReminder = 0
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
users := planet.Satellites[0].DB.Console().Users()
chore := planet.Satellites[0].Core.Mail.EmailReminders
chore.Loop.Pause()
// Overwrite link address in chore so the links don't work
// and we can test that the correct number of reminders are sent.
chore.TestSetLinkAddress("")
id1 := testrand.UUID()
_, err := users.Insert(ctx, &console.User{
ID: id1,
FullName: "test",
Email: "userone@mail.test",
PasswordHash: []byte("123a123"),
})
require.NoError(t, err)
id2 := testrand.UUID()
_, err = users.Insert(ctx, &console.User{
ID: id2,
FullName: "test",
Email: "usertwo@mail.test",
PasswordHash: []byte("123a123"),
})
require.NoError(t, err)
id3 := testrand.UUID()
_, err = users.Insert(ctx, &console.User{
ID: id3,
FullName: "test",
Email: "userthree@mail.test",
PasswordHash: []byte("123a123"),
})
require.NoError(t, err)
// This user will verify immediately and should not get reminders.
user1, err := users.Get(ctx, id1)
require.NoError(t, err)
require.Zero(t, user1.VerificationReminders)
// This user will get one reminder and then verify and should not get a second.
user2, err := users.Get(ctx, id2)
require.NoError(t, err)
require.Zero(t, user2.VerificationReminders)
// This user will not verify at all and should get 2 reminders and no more.
user3, err := users.Get(ctx, id3)
require.NoError(t, err)
require.Zero(t, user3.VerificationReminders)
user1.Status = 1
err = users.Update(ctx, user1)
require.NoError(t, err)
chore.Loop.TriggerWait()
user1, err = users.Get(ctx, id1)
require.NoError(t, err)
require.Zero(t, user1.VerificationReminders)
user2, err = users.Get(ctx, id2)
require.NoError(t, err)
require.Equal(t, 1, user2.VerificationReminders)
user3, err = users.Get(ctx, id3)
require.NoError(t, err)
require.Equal(t, 1, user3.VerificationReminders)
user2.Status = 1
err = users.Update(ctx, user2)
require.NoError(t, err)
chore.Loop.TriggerWait()
user1, err = users.Get(ctx, id1)
require.NoError(t, err)
require.Zero(t, user1.VerificationReminders)
user2, err = users.Get(ctx, id2)
require.NoError(t, err)
require.Equal(t, 1, user2.VerificationReminders)
user3, err = users.Get(ctx, id3)
require.NoError(t, err)
require.Equal(t, 2, user3.VerificationReminders)
// Check user is not reminded again after 2
chore.Loop.TriggerWait()
user1, err = users.Get(ctx, id1)
require.NoError(t, err)
require.Zero(t, user1.VerificationReminders)
user2, err = users.Get(ctx, id2)
require.NoError(t, err)
require.Equal(t, 1, user2.VerificationReminders)
user3, err = users.Get(ctx, id3)
require.NoError(t, err)
require.Equal(t, 2, user3.VerificationReminders)
})
}
func TestEmailChoreLinkActivatesAccount(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.EmailReminders.FirstVerificationReminder = 0
config.EmailReminders.SecondVerificationReminder = 0
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
users := planet.Satellites[0].DB.Console().Users()
chore := planet.Satellites[0].Core.Mail.EmailReminders
chore.Loop.Pause()
chore.TestUseBlockingSend()
id := testrand.UUID()
_, err := users.Insert(ctx, &console.User{
ID: id,
FullName: "test",
Email: "userone@mail.test",
PasswordHash: []byte("123a123"),
})
require.NoError(t, err)
u, err := users.Get(ctx, id)
require.NoError(t, err)
require.Equal(t, console.UserStatus(0), u.Status)
chore.Loop.TriggerWait()
u, err = users.Get(ctx, id)
require.NoError(t, err)
require.Equal(t, console.UserStatus(1), u.Status)
})
}