2019-06-04 20:17:01 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
package rewards_test
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-06-24 21:51:54 +01:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2019-06-04 20:17:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOffer_Database(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
// Happy path
|
2019-06-24 21:51:54 +01:00
|
|
|
validOffers := []rewards.NewOffer{
|
2019-06-04 20:17:01 +01:00
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test offer 1",
|
|
|
|
AwardCreditInCents: 100,
|
|
|
|
InviteeCreditInCents: 50,
|
|
|
|
AwardCreditDurationDays: 60,
|
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * 1),
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.Active,
|
|
|
|
Type: rewards.Referral,
|
2019-06-04 20:17:01 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test offer 2",
|
|
|
|
AwardCreditInCents: 100,
|
|
|
|
InviteeCreditInCents: 50,
|
|
|
|
AwardCreditDurationDays: 60,
|
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * 1),
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.Default,
|
|
|
|
Type: rewards.FreeCredit,
|
2019-06-04 20:17:01 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range validOffers {
|
2019-06-24 21:51:54 +01:00
|
|
|
new, err := planet.Satellites[0].DB.Rewards().Create(ctx, &validOffers[i])
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
all, err := planet.Satellites[0].DB.Rewards().ListAll(ctx)
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, all, *new)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
c, err := planet.Satellites[0].DB.Rewards().GetCurrentByType(ctx, new.Type)
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, new, c)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
update := &rewards.UpdateOffer{
|
2019-06-12 16:53:19 +01:00
|
|
|
ID: new.ID,
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.Done,
|
2019-06-12 16:53:19 +01:00
|
|
|
ExpiresAt: time.Now(),
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
2019-06-12 16:53:19 +01:00
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
isDefault := update.Status == rewards.Default
|
|
|
|
err = planet.Satellites[0].DB.Rewards().Redeem(ctx, update.ID, isDefault)
|
2019-06-12 16:53:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
err = planet.Satellites[0].DB.Rewards().Finish(ctx, update.ID)
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-12 16:53:19 +01:00
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
current, err := planet.Satellites[0].DB.Rewards().ListAll(ctx)
|
2019-06-12 16:53:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-24 21:51:54 +01:00
|
|
|
if new.Status == rewards.Default {
|
2019-06-12 16:53:19 +01:00
|
|
|
require.Equal(t, new.NumRedeemed, current[i].NumRedeemed)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, new.NumRedeemed+1, current[i].NumRedeemed)
|
|
|
|
}
|
2019-06-24 21:51:54 +01:00
|
|
|
require.Equal(t, rewards.Done, current[i].Status)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// create with expired offer
|
2019-06-24 21:51:54 +01:00
|
|
|
expiredOffers := []rewards.NewOffer{
|
2019-06-04 20:17:01 +01:00
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test offer",
|
|
|
|
AwardCreditInCents: 100,
|
|
|
|
InviteeCreditInCents: 50,
|
|
|
|
AwardCreditDurationDays: 60,
|
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * -1),
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.Active,
|
|
|
|
Type: rewards.FreeCredit,
|
2019-06-04 20:17:01 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test offer",
|
|
|
|
AwardCreditInCents: 100,
|
|
|
|
InviteeCreditInCents: 50,
|
|
|
|
AwardCreditDurationDays: 60,
|
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * -1),
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.Default,
|
|
|
|
Type: rewards.Referral,
|
2019-06-04 20:17:01 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range expiredOffers {
|
2019-06-24 21:51:54 +01:00
|
|
|
output, err := planet.Satellites[0].DB.Rewards().Create(ctx, &expiredOffers[i])
|
2019-06-04 20:17:01 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, output)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|