storj/satellite/payments/stripecoinpayments/invoices_test.go
Cameron e2cf486fcb satellite/console: add Purchase method to console.Payments
Purchase collects a payment from user using the specified price,
description, and payment method. This is implemented at the lower layers
using the payments.Invoices interface. There are also additions to
stripecoinpayments stripemock to simulate errors returned from stripe
invoices New and Pay. If an invoice exists with the same description, if
it is a draft, it is deleted and a new one is created and paid. If it is
open, pay it and don't create a new one.

Change-Id: Ic3147434bc44a0777ecbedb3a4ed4c768eb02ea3
2023-02-21 20:10:42 +00:00

63 lines
2.3 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/v72"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite/payments/stripecoinpayments"
)
func TestInvoices(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellite := planet.Satellites[0]
userID := planet.Uplinks[0].Projects[0].Owner.ID
price := int64(1000)
desc := "test payment intent description"
t.Run("Create with unknown user fails", func(t *testing.T) {
pi, err := satellite.API.Payments.Accounts.Invoices().Create(ctx, testrand.UUID(), price, desc)
require.Error(t, err)
require.Nil(t, pi)
})
t.Run("Create returns error when underlying StripeInvoices.Create returns error", func(t *testing.T) {
pi, err := satellite.API.Payments.Accounts.Invoices().Create(ctx, testrand.UUID(), price, stripecoinpayments.MockInvoicesNewFailure)
require.Error(t, err)
require.Nil(t, pi)
})
t.Run("Pay returns error with unknown invoice ID", func(t *testing.T) {
confirmedPI, err := satellite.API.Payments.Accounts.Invoices().Pay(ctx, "unknown_id", "test_payment_method")
require.Error(t, err)
require.Nil(t, confirmedPI)
})
t.Run("Create and Pay success", func(t *testing.T) {
pi, err := satellite.API.Payments.Accounts.Invoices().Create(ctx, userID, price, desc)
require.NoError(t, err)
require.NotNil(t, pi)
confirmedPI, err := satellite.API.Payments.Accounts.Invoices().Pay(ctx, pi.ID, "test_payment_method")
require.NoError(t, err)
require.Equal(t, pi.ID, confirmedPI.ID)
require.Equal(t, string(stripe.InvoiceStatusPaid), confirmedPI.Status)
})
t.Run("Pay returns error when underlying StripeInvoices.Pay returns error", func(t *testing.T) {
pi, err := satellite.API.Payments.Accounts.Invoices().Create(ctx, userID, price, desc)
require.NoError(t, err)
require.NotNil(t, pi)
confirmedPI, err := satellite.API.Payments.Accounts.Invoices().Pay(ctx, pi.ID, stripecoinpayments.MockInvoicesPayFailure)
require.Error(t, err)
require.Nil(t, confirmedPI)
})
})
}