2019-06-18 16:55:47 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/satellite"
|
2019-06-24 21:51:54 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/rewards"
|
2019-06-18 16:55:47 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUsercredits(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
consoleDB := db.Console()
|
|
|
|
|
|
|
|
user, referrer, offer := setupData(ctx, t, db)
|
|
|
|
randomID, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// test foreign key constraint for inserting a new user credit entry with randomID
|
|
|
|
var invalidUserCredits = []console.UserCredit{
|
|
|
|
{
|
|
|
|
UserID: *randomID,
|
|
|
|
OfferID: offer.ID,
|
|
|
|
ReferredBy: referrer.ID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: user.ID,
|
|
|
|
OfferID: 10,
|
|
|
|
ReferredBy: referrer.ID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: user.ID,
|
|
|
|
OfferID: offer.ID,
|
|
|
|
ReferredBy: *randomID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 1, 0),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ivc := range invalidUserCredits {
|
|
|
|
_, err := consoleDB.UserCredits().Create(ctx, ivc)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
2019-06-19 21:49:04 +01:00
|
|
|
remainingCharge int
|
|
|
|
usage console.UserCreditUsage
|
|
|
|
hasErr bool
|
2019-06-18 16:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var validUserCredits = []struct {
|
|
|
|
userCredit console.UserCredit
|
|
|
|
chargedCredits int
|
|
|
|
expected result
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
userCredit: console.UserCredit{
|
|
|
|
UserID: user.ID,
|
|
|
|
OfferID: offer.ID,
|
|
|
|
ReferredBy: referrer.ID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 1, 0),
|
|
|
|
},
|
|
|
|
chargedCredits: 120,
|
|
|
|
expected: result{
|
2019-06-19 21:49:04 +01:00
|
|
|
remainingCharge: 20,
|
|
|
|
usage: console.UserCreditUsage{
|
|
|
|
AvailableCredits: 0,
|
|
|
|
UsedCredits: 100,
|
|
|
|
Referred: 0,
|
|
|
|
},
|
|
|
|
hasErr: false,
|
2019-06-18 16:55:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// simulate a credit that's already expired
|
|
|
|
userCredit: console.UserCredit{
|
|
|
|
UserID: user.ID,
|
|
|
|
OfferID: offer.ID,
|
|
|
|
ReferredBy: referrer.ID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 0, -5),
|
|
|
|
},
|
|
|
|
chargedCredits: 60,
|
|
|
|
expected: result{
|
2019-06-19 21:49:04 +01:00
|
|
|
remainingCharge: 60,
|
|
|
|
usage: console.UserCreditUsage{
|
|
|
|
AvailableCredits: 0,
|
|
|
|
UsedCredits: 100,
|
|
|
|
Referred: 0,
|
|
|
|
},
|
|
|
|
hasErr: true,
|
2019-06-18 16:55:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// simulate a credit that's not expired
|
|
|
|
userCredit: console.UserCredit{
|
|
|
|
UserID: user.ID,
|
|
|
|
OfferID: offer.ID,
|
|
|
|
ReferredBy: referrer.ID,
|
|
|
|
CreditsEarnedInCents: 100,
|
|
|
|
ExpiresAt: time.Now().UTC().AddDate(0, 0, 5),
|
|
|
|
},
|
|
|
|
chargedCredits: 80,
|
|
|
|
expected: result{
|
2019-06-19 21:49:04 +01:00
|
|
|
remainingCharge: 0,
|
|
|
|
usage: console.UserCreditUsage{
|
|
|
|
AvailableCredits: 20,
|
|
|
|
UsedCredits: 180,
|
|
|
|
Referred: 0,
|
|
|
|
},
|
|
|
|
hasErr: false,
|
2019-06-18 16:55:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, vc := range validUserCredits {
|
|
|
|
_, err = consoleDB.UserCredits().Create(ctx, vc.userCredit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
{
|
|
|
|
remainingCharge, err := consoleDB.UserCredits().UpdateAvailableCredits(ctx, vc.chargedCredits, vc.userCredit.UserID, time.Now().UTC())
|
|
|
|
if vc.expected.hasErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, vc.expected.remainingCharge, remainingCharge)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-19 21:49:04 +01:00
|
|
|
usage, err := consoleDB.UserCredits().GetCreditUsage(ctx, vc.userCredit.UserID, time.Now().UTC())
|
2019-06-18 16:55:47 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-19 21:49:04 +01:00
|
|
|
require.Equal(t, vc.expected.usage, *usage)
|
|
|
|
}
|
2019-06-18 16:55:47 +01:00
|
|
|
|
2019-06-19 21:49:04 +01:00
|
|
|
{
|
|
|
|
referred, err := consoleDB.UserCredits().GetCreditUsage(ctx, referrer.ID, time.Now().UTC())
|
2019-06-18 16:55:47 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-19 21:49:04 +01:00
|
|
|
require.Equal(t, int64(i+1), referred.Referred)
|
2019-06-18 16:55:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
func setupData(ctx context.Context, t *testing.T, db satellite.DB) (user *console.User, referrer *console.User, offer *rewards.Offer) {
|
2019-06-18 16:55:47 +01:00
|
|
|
consoleDB := db.Console()
|
2019-06-24 21:51:54 +01:00
|
|
|
offersDB := db.Rewards()
|
2019-06-18 16:55:47 +01:00
|
|
|
// create user
|
|
|
|
var userPassHash [8]byte
|
|
|
|
_, err := rand.Read(userPassHash[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var referrerPassHash [8]byte
|
|
|
|
_, err = rand.Read(referrerPassHash[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// create an user
|
|
|
|
user, err = consoleDB.Users().Insert(ctx, &console.User{
|
|
|
|
FullName: "John Doe",
|
|
|
|
Email: "john@mail.test",
|
|
|
|
PasswordHash: userPassHash[:],
|
|
|
|
Status: console.Active,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//create an user as referrer
|
|
|
|
referrer, err = consoleDB.Users().Insert(ctx, &console.User{
|
|
|
|
FullName: "referrer",
|
|
|
|
Email: "referrer@mail.test",
|
|
|
|
PasswordHash: referrerPassHash[:],
|
|
|
|
Status: console.Active,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// create offer
|
2019-06-24 21:51:54 +01:00
|
|
|
offer, err = offersDB.Create(ctx, &rewards.NewOffer{
|
2019-06-18 16:55:47 +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-18 16:55:47 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return user, referrer, offer
|
|
|
|
}
|