2019-10-10 18:12:23 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments_test
|
|
|
|
|
|
|
|
import (
|
2019-11-05 13:16:02 +00:00
|
|
|
"strconv"
|
2019-10-10 18:12:23 +01:00
|
|
|
"testing"
|
2019-11-05 13:16:02 +00:00
|
|
|
"time"
|
2019-10-10 18:12:23 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-10 18:12:23 +01:00
|
|
|
"storj.io/storj/satellite"
|
2019-11-05 13:16:02 +00:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2019-10-10 18:12:23 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCustomersRepository(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-11-05 13:16:02 +00:00
|
|
|
customers := db.StripeCoinPayments().Customers()
|
2019-10-10 18:12:23 +01:00
|
|
|
|
|
|
|
customerID := "customerID"
|
|
|
|
userID, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("Insert", func(t *testing.T) {
|
2020-03-30 10:08:50 +01:00
|
|
|
err = customers.Insert(ctx, userID, customerID)
|
2019-10-10 18:12:23 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2019-10-11 16:00:35 +01:00
|
|
|
|
|
|
|
t.Run("Can not insert duplicate customerID", func(t *testing.T) {
|
2020-03-30 10:08:50 +01:00
|
|
|
err = customers.Insert(ctx, userID, customerID)
|
2019-10-11 16:00:35 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetCustomerID", func(t *testing.T) {
|
2020-03-30 10:08:50 +01:00
|
|
|
id, err := customers.GetCustomerID(ctx, userID)
|
2019-10-11 16:00:35 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, id, customerID)
|
|
|
|
})
|
2019-10-10 18:12:23 +01:00
|
|
|
})
|
|
|
|
}
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
func TestCustomersRepositoryList(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-11-05 13:16:02 +00:00
|
|
|
customersDB := db.StripeCoinPayments().Customers()
|
|
|
|
|
|
|
|
const custLen = 5
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
for i := 0; i < custLen*2+3; i++ {
|
2019-11-05 13:16:02 +00:00
|
|
|
userID, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cus := stripecoinpayments.Customer{
|
|
|
|
ID: "customerID" + strconv.Itoa(i),
|
2020-03-30 10:08:50 +01:00
|
|
|
UserID: userID,
|
2019-11-05 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = customersDB.Insert(ctx, cus.UserID, cus.ID)
|
|
|
|
require.NoError(t, err)
|
2020-04-29 13:04:38 +01:00
|
|
|
|
|
|
|
// Ensure that every insert gets a different "created at" time.
|
|
|
|
waitForTimeToChange()
|
2019-11-05 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
page, err := customersDB.List(ctx, 0, custLen, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, custLen, len(page.Customers))
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
assert.True(t, page.Next)
|
|
|
|
assert.Equal(t, int64(5), page.NextOffset)
|
|
|
|
|
|
|
|
for i, cus := range page.Customers {
|
|
|
|
assert.Equal(t, "customerID"+strconv.Itoa(12-i), cus.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err = customersDB.List(ctx, page.NextOffset, custLen, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, custLen, len(page.Customers))
|
|
|
|
|
|
|
|
assert.True(t, page.Next)
|
|
|
|
assert.Equal(t, int64(10), page.NextOffset)
|
|
|
|
|
|
|
|
for i, cus := range page.Customers {
|
|
|
|
assert.Equal(t, "customerID"+strconv.Itoa(7-i), cus.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err = customersDB.List(ctx, page.NextOffset, custLen, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(page.Customers))
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
assert.False(t, page.Next)
|
|
|
|
assert.Equal(t, int64(0), page.NextOffset)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
for i, cus := range page.Customers {
|
|
|
|
assert.Equal(t, "customerID"+strconv.Itoa(2-i), cus.ID)
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-29 13:04:38 +01:00
|
|
|
|
|
|
|
func waitForTimeToChange() {
|
|
|
|
t := time.Now()
|
|
|
|
for time.Since(t) == 0 {
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|