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"
|
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
"storj.io/storj/internal/currency"
|
2019-06-04 20:17:01 +01:00
|
|
|
"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",
|
2019-07-01 20:16:49 +01:00
|
|
|
AwardCredit: currency.Cents(100),
|
|
|
|
InviteeCredit: currency.Cents(50),
|
2019-06-04 20:17:01 +01:00
|
|
|
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",
|
2019-07-30 14:21:00 +01:00
|
|
|
AwardCredit: currency.Cents(0),
|
2019-07-01 20:16:49 +01:00
|
|
|
InviteeCredit: currency.Cents(50),
|
2019-07-30 14:21:00 +01:00
|
|
|
AwardCreditDurationDays: 0,
|
2019-06-04 20:17:01 +01:00
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * 1),
|
2019-07-12 15:19:38 +01:00
|
|
|
Status: rewards.Active,
|
2019-06-24 21:51:54 +01:00
|
|
|
Type: rewards.FreeCredit,
|
2019-06-04 20:17:01 +01:00
|
|
|
},
|
2019-07-25 23:06:23 +01:00
|
|
|
{
|
2019-08-01 18:46:33 +01:00
|
|
|
Name: "Zenko",
|
|
|
|
Description: "partner offer",
|
2019-07-25 23:06:23 +01:00
|
|
|
AwardCredit: currency.Cents(0),
|
|
|
|
InviteeCredit: currency.Cents(50),
|
|
|
|
AwardCreditDurationDays: 0,
|
|
|
|
InviteeCreditDurationDays: 30,
|
|
|
|
RedeemableCap: 50,
|
|
|
|
ExpiresAt: time.Now().UTC().Add(time.Hour * 1),
|
|
|
|
Status: rewards.Active,
|
|
|
|
Type: rewards.Partner,
|
|
|
|
},
|
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-08-01 18:46:33 +01:00
|
|
|
offers, err := planet.Satellites[0].DB.Rewards().GetActiveOffersByType(ctx, new.Type)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var pID string
|
|
|
|
if new.Type == rewards.Partner {
|
2019-11-05 12:58:09 +00:00
|
|
|
partner, err := planet.Satellites[0].API.Marketing.PartnersService.PartnerByName(ctx, new.Name)
|
2019-08-01 18:46:33 +01:00
|
|
|
require.NoError(t, err)
|
2019-11-05 12:58:09 +00:00
|
|
|
pID = partner.ID
|
2019-08-01 18:46:33 +01:00
|
|
|
}
|
2019-11-05 12:58:09 +00:00
|
|
|
c, err := planet.Satellites[0].API.Marketing.PartnersService.GetActiveOffer(ctx, offers, new.Type, pID)
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, new, c)
|
|
|
|
|
2019-07-12 15:19:38 +01:00
|
|
|
err = planet.Satellites[0].DB.Rewards().Finish(ctx, all[i].ID)
|
2019-06-04 20:17:01 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-12 16:53:19 +01:00
|
|
|
|
2019-07-12 15:19:38 +01:00
|
|
|
updated, err := planet.Satellites[0].DB.Rewards().ListAll(ctx)
|
2019-06-12 16:53:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-12 15:19:38 +01:00
|
|
|
require.Equal(t, rewards.Done, updated[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",
|
2019-07-30 14:21:00 +01:00
|
|
|
AwardCredit: currency.Cents(0),
|
2019-07-01 20:16:49 +01:00
|
|
|
InviteeCredit: currency.Cents(50),
|
2019-07-30 14:21:00 +01:00
|
|
|
AwardCreditDurationDays: 0,
|
2019-06-04 20:17:01 +01:00
|
|
|
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",
|
2019-07-01 20:16:49 +01:00
|
|
|
AwardCredit: currency.Cents(100),
|
|
|
|
InviteeCredit: currency.Cents(50),
|
2019-06-04 20:17:01 +01:00
|
|
|
AwardCreditDurationDays: 60,
|
|
|
|
InviteeCreditDurationDays: 30,
|
2019-07-16 00:30:00 +01:00
|
|
|
RedeemableCap: 0,
|
2019-06-04 20:17:01 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|