2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-18 02:34:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-26 17:58:51 +00:00
|
|
|
|
2020-01-18 02:34:06 +00:00
|
|
|
"storj.io/common/memory"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-26 17:58:51 +00:00
|
|
|
"storj.io/storj/satellite"
|
2020-01-18 02:34:06 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-11-26 17:58:51 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCouponRepository(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2021-03-30 00:37:46 +01:00
|
|
|
duration := 2
|
2019-11-26 17:58:51 +00:00
|
|
|
couponsRepo := db.StripeCoinPayments().Coupons()
|
|
|
|
coupon := payments.Coupon{
|
2021-03-30 00:37:46 +01:00
|
|
|
Duration: &duration,
|
2019-11-26 17:58:51 +00:00
|
|
|
Amount: 10,
|
|
|
|
Status: payments.CouponActive,
|
|
|
|
Description: "description",
|
2020-04-02 15:18:08 +01:00
|
|
|
UserID: testrand.UUID(),
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
t.Run("insert", func(t *testing.T) {
|
2020-05-19 11:36:13 +01:00
|
|
|
_, err := couponsRepo.Insert(ctx, coupon)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 17:58:51 +00:00
|
|
|
|
|
|
|
coupons, err := couponsRepo.List(ctx, payments.CouponActive)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(coupons))
|
2019-11-26 17:58:51 +00:00
|
|
|
coupon = coupons[0]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update", func(t *testing.T) {
|
2020-05-19 11:36:13 +01:00
|
|
|
_, err := couponsRepo.Update(ctx, coupon.ID, payments.CouponUsed)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 17:58:51 +00:00
|
|
|
|
|
|
|
coupons, err := couponsRepo.List(ctx, payments.CouponUsed)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, payments.CouponUsed, coupons[0].Status)
|
2019-11-26 17:58:51 +00:00
|
|
|
coupon = coupons[0]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get latest on empty table return stripecoinpayments.ErrNoCouponUsages", func(t *testing.T) {
|
|
|
|
_, err := couponsRepo.GetLatest(ctx, coupon.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, true, stripecoinpayments.ErrNoCouponUsages.Has(err))
|
2019-11-26 17:58:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("total on empty table returns 0", func(t *testing.T) {
|
|
|
|
total, err := couponsRepo.TotalUsage(ctx, coupon.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), total)
|
2019-11-26 17:58:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add usage", func(t *testing.T) {
|
|
|
|
err := couponsRepo.AddUsage(ctx, stripecoinpayments.CouponUsage{
|
|
|
|
CouponID: coupon.ID,
|
|
|
|
Amount: 1,
|
2020-01-07 10:41:19 +00:00
|
|
|
Period: now,
|
2019-11-26 17:58:51 +00:00
|
|
|
})
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 17:58:51 +00:00
|
|
|
date, err := couponsRepo.GetLatest(ctx, coupon.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-16 21:10:33 +00:00
|
|
|
// go and postgres has different precision. go - nanoseconds, postgres micro
|
2020-06-28 04:56:29 +01:00
|
|
|
require.Equal(t, date.UTC(), now.Truncate(time.Microsecond))
|
2019-11-26 17:58:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("total usage", func(t *testing.T) {
|
|
|
|
amount, err := couponsRepo.TotalUsage(ctx, coupon.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, amount, int64(1))
|
2019-11-26 17:58:51 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
// TestPopulatePromotionalCoupons is a test for PopulatePromotionalCoupons function
|
|
|
|
// that creates coupons with predefined values for each of user (from arguments) that have a project
|
2021-04-27 22:20:53 +01:00
|
|
|
// and that don't have a valid promotional coupon yet. Also it updates limits of selected projects to 1TB.
|
2020-01-18 02:34:06 +00:00
|
|
|
// Because the coupon should be added to a project, we select the first project of the user.
|
2021-04-27 22:20:53 +01:00
|
|
|
// In this test we have the following test cases:
|
2020-01-18 02:34:06 +00:00
|
|
|
// 1. Activated user, 2 projects, without coupon. For this case we should add new coupon to his first project.
|
2021-04-27 22:20:53 +01:00
|
|
|
// 2. Activated user, 1 project, without coupon. Coupon should be added.
|
2020-01-18 02:34:06 +00:00
|
|
|
// 3. Activated user without project. Coupon should not be added.
|
|
|
|
// 4. User with inactive account. Coupon should not be added.
|
|
|
|
// 5. Activated user with project and coupon. Coupon should not be added.
|
2021-04-27 22:20:53 +01:00
|
|
|
// 6. Activated user with project and expired coupon. Coupon should be added.
|
|
|
|
// 7. Activated user with project and fully consumed, non-expired coupon. Coupon should be added.
|
|
|
|
// 8. Next step - is populating coupons for all 7 users. 4 coupons should be added.
|
|
|
|
// 9. Creating new user with project.
|
|
|
|
// 10. Populating coupons again. For 8 users above. Only 1 new coupon should be added.
|
|
|
|
// Five new coupons total should be added by 2 runs of PopulatePromotionalCoupons method.
|
2020-01-18 02:34:06 +00:00
|
|
|
func TestPopulatePromotionalCoupons(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2020-01-18 02:34:06 +00:00
|
|
|
usersRepo := db.Console().Users()
|
|
|
|
projectsRepo := db.Console().Projects()
|
|
|
|
couponsRepo := db.StripeCoinPayments().Coupons()
|
|
|
|
usageRepo := db.ProjectAccounting()
|
|
|
|
|
|
|
|
// creating test users with different status.
|
|
|
|
|
|
|
|
// activated user with 2 project. New coupon should be added to the first project.
|
|
|
|
user1, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
FullName: "user1",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test1@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user1.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// activated user with proj. New coupon should be added.
|
|
|
|
user2, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
FullName: "user2",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test2@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user2.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// activated user without proj. New coupon should not be added.
|
|
|
|
user3, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
FullName: "user3",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test3@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user3.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// inactive user. New coupon should not be added.
|
|
|
|
user4, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
FullName: "user4",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test4@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// activated user with proj and coupon. New coupon should not be added.
|
|
|
|
user5, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
FullName: "user5",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test5@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user5.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
// activated user with proj and expired coupon. New coupon should be added.
|
|
|
|
user6, err := usersRepo.Insert(ctx, &console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "user6",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test6@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user6.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user6)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// activated user with proj and non-expired but consumed coupon. New coupon should be added.
|
|
|
|
user7, err := usersRepo.Insert(ctx, &console.User{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
FullName: "user7",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "test7@example.com",
|
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user7.Status = console.Active
|
|
|
|
|
|
|
|
err = usersRepo.Update(ctx, user7)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-18 02:34:06 +00:00
|
|
|
// creating projects for users above.
|
|
|
|
proj1, err := projectsRepo.Insert(ctx, &console.Project{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
Name: "proj 1 of user 1",
|
|
|
|
Description: "descr 1",
|
|
|
|
OwnerID: user1.ID,
|
|
|
|
})
|
2020-01-29 00:57:15 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
// should not be processed as we takes only first project of the user.
|
|
|
|
proj2, err := projectsRepo.Insert(ctx, &console.Project{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
Name: "proj 2 of user 1",
|
|
|
|
Description: "descr 2",
|
|
|
|
OwnerID: user1.ID,
|
|
|
|
})
|
2020-01-29 00:57:15 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj3, err := projectsRepo.Insert(ctx, &console.Project{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-18 02:34:06 +00:00
|
|
|
Name: "proj 1 of user 2",
|
|
|
|
Description: "descr 3",
|
|
|
|
OwnerID: user2.ID,
|
|
|
|
})
|
2020-01-29 00:57:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
_, err = projectsRepo.Insert(ctx, &console.Project{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
Name: "proj 1 of user 5",
|
|
|
|
Description: "descr 4",
|
|
|
|
OwnerID: user5.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = projectsRepo.Insert(ctx, &console.Project{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
Name: "proj 1 of user 6",
|
|
|
|
Description: "descr 5",
|
|
|
|
OwnerID: user6.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = projectsRepo.Insert(ctx, &console.Project{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
Name: "proj 1 of user 7",
|
|
|
|
Description: "descr 5",
|
|
|
|
OwnerID: user7.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// add coupons to users who should have them before PopulatePromotionalCoupons
|
2021-03-30 00:37:46 +01:00
|
|
|
duration := 2
|
2020-04-02 15:18:08 +01:00
|
|
|
couponID := testrand.UUID()
|
2020-05-19 11:36:13 +01:00
|
|
|
_, err = couponsRepo.Insert(ctx, payments.Coupon{
|
2020-01-18 02:34:06 +00:00
|
|
|
ID: couponID,
|
|
|
|
UserID: user5.ID,
|
|
|
|
Amount: 5500,
|
2021-03-30 00:37:46 +01:00
|
|
|
Duration: &duration,
|
2020-01-18 02:34:06 +00:00
|
|
|
Description: "qw",
|
|
|
|
Type: payments.CouponTypePromotional,
|
|
|
|
Status: payments.CouponActive,
|
|
|
|
})
|
2020-01-29 00:57:15 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
couponID = testrand.UUID()
|
|
|
|
_, err = couponsRepo.Insert(ctx, payments.Coupon{
|
|
|
|
ID: couponID,
|
|
|
|
UserID: user6.ID,
|
|
|
|
Amount: 1000,
|
|
|
|
Duration: &duration,
|
|
|
|
Description: "qw",
|
|
|
|
Type: payments.CouponTypePromotional,
|
|
|
|
Status: payments.CouponExpired,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
couponID = testrand.UUID()
|
|
|
|
_, err = couponsRepo.Insert(ctx, payments.Coupon{
|
|
|
|
ID: couponID,
|
|
|
|
UserID: user7.ID,
|
|
|
|
Amount: 1000,
|
|
|
|
Duration: nil,
|
|
|
|
Description: "qw",
|
|
|
|
Type: payments.CouponTypePromotional,
|
|
|
|
Status: payments.CouponUsed,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-18 02:34:06 +00:00
|
|
|
// creating new users and projects to test that multiple execution of populate method wont generate extra coupons.
|
2021-04-27 22:20:53 +01:00
|
|
|
user8, err := usersRepo.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2021-04-27 22:20:53 +01:00
|
|
|
FullName: "user8",
|
2020-01-18 02:34:06 +00:00
|
|
|
ShortName: "",
|
2021-04-27 22:20:53 +01:00
|
|
|
Email: "test8@example.com",
|
2020-01-18 02:34:06 +00:00
|
|
|
PasswordHash: []byte("123qwe"),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
user8.Status = console.Active
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
err = usersRepo.Update(ctx, user8)
|
2020-01-18 02:34:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
proj4, err := projectsRepo.Insert(ctx, &console.Project{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2021-04-27 22:20:53 +01:00
|
|
|
Name: "proj 1 of user 8",
|
|
|
|
Description: "descr 7",
|
|
|
|
OwnerID: user8.ID,
|
2020-01-18 02:34:06 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("first population", func(t *testing.T) {
|
|
|
|
var usersIds = []uuid.UUID{
|
|
|
|
user1.ID,
|
|
|
|
user2.ID,
|
|
|
|
user3.ID,
|
|
|
|
user4.ID,
|
|
|
|
user5.ID,
|
2021-04-27 22:20:53 +01:00
|
|
|
user6.ID,
|
|
|
|
user7.ID,
|
2020-01-18 02:34:06 +00:00
|
|
|
}
|
2021-03-30 00:37:46 +01:00
|
|
|
duration := 2
|
|
|
|
err := couponsRepo.PopulatePromotionalCoupons(ctx, usersIds, &duration, 5500, memory.TB)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user1Coupons, err := couponsRepo.ListByUserID(ctx, user1.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user1Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj1Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj1.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-06 00:02:12 +01:00
|
|
|
require.Equal(t, memory.TB.Int64(), *proj1Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj2Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj2.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-06 13:50:29 +01:00
|
|
|
require.Nil(t, proj2Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user2Coupons, err := couponsRepo.ListByUserID(ctx, user2.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user2Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj3Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj3.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-06 00:02:12 +01:00
|
|
|
require.Equal(t, memory.TB.Int64(), *proj3Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user3Coupons, err := couponsRepo.ListByUserID(ctx, user3.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(user3Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user4Coupons, err := couponsRepo.ListByUserID(ctx, user4.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(user4Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user5Coupons, err := couponsRepo.ListByUserID(ctx, user5.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user5Coupons))
|
2021-04-27 22:20:53 +01:00
|
|
|
|
|
|
|
user6Coupons, err := couponsRepo.ListByUserID(ctx, user6.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(user6Coupons))
|
|
|
|
|
|
|
|
user7Coupons, err := couponsRepo.ListByUserID(ctx, user7.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(user7Coupons))
|
|
|
|
|
|
|
|
user8Coupons, err := couponsRepo.ListByUserID(ctx, user8.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(user8Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("second population", func(t *testing.T) {
|
|
|
|
var usersIds = []uuid.UUID{
|
|
|
|
user1.ID,
|
|
|
|
user2.ID,
|
|
|
|
user3.ID,
|
|
|
|
user4.ID,
|
|
|
|
user5.ID,
|
|
|
|
user6.ID,
|
2021-04-27 22:20:53 +01:00
|
|
|
user7.ID,
|
|
|
|
user8.ID,
|
2020-01-18 02:34:06 +00:00
|
|
|
}
|
2021-03-30 00:37:46 +01:00
|
|
|
duration := 2
|
|
|
|
err := couponsRepo.PopulatePromotionalCoupons(ctx, usersIds, &duration, 5500, memory.TB)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user1Coupons, err := couponsRepo.ListByUserID(ctx, user1.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user1Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj1Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj1.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-06 00:02:12 +01:00
|
|
|
require.Equal(t, memory.TB.Int64(), *proj1Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj2Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj2.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-06 13:50:29 +01:00
|
|
|
require.Nil(t, proj2Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user2Coupons, err := couponsRepo.ListByUserID(ctx, user2.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user2Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
proj3Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj3.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-06 00:02:12 +01:00
|
|
|
require.Equal(t, memory.TB.Int64(), *proj3Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user3Coupons, err := couponsRepo.ListByUserID(ctx, user3.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(user3Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user4Coupons, err := couponsRepo.ListByUserID(ctx, user4.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(user4Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user5Coupons, err := couponsRepo.ListByUserID(ctx, user5.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user5Coupons))
|
|
|
|
require.Equal(t, "qw", user5Coupons[0].Description)
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
user6Coupons, err := couponsRepo.ListByUserID(ctx, user6.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2021-04-27 22:20:53 +01:00
|
|
|
require.Equal(t, 2, len(user6Coupons))
|
|
|
|
|
|
|
|
user7Coupons, err := couponsRepo.ListByUserID(ctx, user7.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(user7Coupons))
|
|
|
|
|
|
|
|
user8Coupons, err := couponsRepo.ListByUserID(ctx, user8.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(user8Coupons))
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2021-04-27 22:20:53 +01:00
|
|
|
proj4Usage, err := usageRepo.GetProjectStorageLimit(ctx, proj4.ID)
|
2020-01-20 12:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2021-04-27 22:20:53 +01:00
|
|
|
require.Equal(t, memory.TB.Int64(), *proj4Usage)
|
2020-01-18 02:34:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|