| 1 |
//go:build !nopayments |
| 2 |
|
| 3 |
package server |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/mock" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
"github.com/stripe/stripe-go/v74" |
| 13 |
"heckel.io/ntfy/v2/user" |
| 14 |
) |
| 15 |
|
| 16 |
// stripeCheckoutMock wires up a testStripeAPI for a successful checkout of user u, with the given |
| 17 |
// billing email on the session's CustomerDetails. |
| 18 |
func stripeCheckoutMock(u *user.User, billingEmail string) *testStripeAPI { |
| 19 |
m := &testStripeAPI{} |
| 20 |
m.On("GetSession", "SOMETOKEN").Return(&stripe.CheckoutSession{ |
| 21 |
ClientReferenceID: u.ID, |
| 22 |
Customer: &stripe.Customer{ID: "acct_5555"}, |
| 23 |
Subscription: &stripe.Subscription{ID: "sub_1234"}, |
| 24 |
CustomerDetails: &stripe.CheckoutSessionCustomerDetails{Email: billingEmail}, |
| 25 |
}, nil) |
| 26 |
m.On("GetSubscription", "sub_1234").Return(&stripe.Subscription{ |
| 27 |
ID: "sub_1234", |
| 28 |
Status: stripe.SubscriptionStatusActive, |
| 29 |
CurrentPeriodEnd: 123456789, |
| 30 |
Items: &stripe.SubscriptionItemList{ |
| 31 |
Data: []*stripe.SubscriptionItem{ |
| 32 |
{Price: &stripe.Price{ID: "price_1234", Recurring: &stripe.PriceRecurring{Interval: stripe.PriceRecurringIntervalMonth}}}, |
| 33 |
}, |
| 34 |
}, |
| 35 |
}, nil) |
| 36 |
m.On("UpdateCustomer", "acct_5555", mock.Anything).Return(&stripe.Customer{}, nil) |
| 37 |
return m |
| 38 |
} |
| 39 |
|
| 40 |
func newCheckoutEmailTestServer(t *testing.T, databaseURL string) (*Server, *captureMailer, *user.User) { |
| 41 |
c := newTestConfigWithAuthFile(t, databaseURL) |
| 42 |
c.StripeSecretKey = "secret key" |
| 43 |
c.BaseURL = "https://ntfy.example.com" |
| 44 |
c.SMTPSenderAddr = "localhost:25" |
| 45 |
c.SMTPSenderFrom = "[email protected]" |
| 46 |
s := newTestServer(t, c) |
| 47 |
mailer := newCaptureMailer() |
| 48 |
s.mailer = mailer |
| 49 |
require.Nil(t, s.userManager.AddTier(&user.Tier{ |
| 50 |
ID: "ti_123", Code: "starter", StripeMonthlyPriceID: "price_1234", MessageLimit: 100, MessageExpiryDuration: time.Hour, |
| 51 |
})) |
| 52 |
require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false)) |
| 53 |
u, err := s.userManager.User("phil") |
| 54 |
require.Nil(t, err) |
| 55 |
return s, mailer, u |
| 56 |
} |
| 57 |
|
| 58 |
func TestPayments_Checkout_SendsBillingEmailVerification(t *testing.T) { |
| 59 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 60 |
s, mailer, u := newCheckoutEmailTestServer(t, databaseURL) |
| 61 |
defer s.closeDatabases() |
| 62 |
s.stripe = stripeCheckoutMock(u, "[email protected]") |
| 63 |
|
| 64 |
rr := request(t, s, "GET", "/v1/account/billing/subscription/success/SOMETOKEN", "", nil) |
| 65 |
require.Equal(t, 303, rr.Code) |
| 66 |
|
| 67 |
// A verification link was auto-sent to the billing email; clicking it verifies + sets primary |
| 68 |
link := mailer.verifyLinks["[email protected]"] |
| 69 |
require.NotEmpty(t, link) |
| 70 |
token := tokenFromLink(t, link, "https://ntfy.example.com/account/email/verify/") |
| 71 |
require.Equal(t, 200, request(t, s, "POST", "/v1/account/email/verify", fmt.Sprintf(`{"token":"%s"}`, token), nil).Code) |
| 72 |
|
| 73 |
emails, err := s.userManager.Emails(u.ID) |
| 74 |
require.Nil(t, err) |
| 75 |
require.Equal(t, []string{"[email protected]"}, emails.Strings()) |
| 76 |
primary, err := s.userManager.PrimaryEmail(u.ID) |
| 77 |
require.Nil(t, err) |
| 78 |
require.Equal(t, "[email protected]", primary) |
| 79 |
}) |
| 80 |
} |
| 81 |
|
| 82 |
func TestPayments_Checkout_SkipsBillingEmailWhenAlreadyVerified(t *testing.T) { |
| 83 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 84 |
s, mailer, u := newCheckoutEmailTestServer(t, databaseURL) |
| 85 |
defer s.closeDatabases() |
| 86 |
s.stripe = stripeCheckoutMock(u, "[email protected]") |
| 87 |
|
| 88 |
// User already has a verified email -> no auto-send on checkout |
| 89 |
require.Nil(t, s.userManager.AddEmail(u.ID, "[email protected]")) |
| 90 |
|
| 91 |
rr := request(t, s, "GET", "/v1/account/billing/subscription/success/SOMETOKEN", "", nil) |
| 92 |
require.Equal(t, 303, rr.Code) |
| 93 |
require.Empty(t, mailer.verifyLinks) |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
func TestPayments_Checkout_SkipsBillingEmailWhenPrimaryElsewhere(t *testing.T) { |
| 98 |
forEachBackend(t, func(t *testing.T, databaseURL string) { |
| 99 |
s, mailer, u := newCheckoutEmailTestServer(t, databaseURL) |
| 100 |
defer s.closeDatabases() |
| 101 |
s.stripe = stripeCheckoutMock(u, "[email protected]") |
| 102 |
|
| 103 |
// The billing email is already the recovery email on another account -> skip |
| 104 |
require.Nil(t, s.userManager.AddUser("alice", "alice", user.RoleUser, false)) |
| 105 |
alice, err := s.userManager.User("alice") |
| 106 |
require.Nil(t, err) |
| 107 |
require.Nil(t, s.userManager.AddEmail(alice.ID, "[email protected]")) |
| 108 |
require.Nil(t, s.userManager.SetPrimaryEmail(alice.ID, "[email protected]")) |
| 109 |
|
| 110 |
rr := request(t, s, "GET", "/v1/account/billing/subscription/success/SOMETOKEN", "", nil) |
| 111 |
require.Equal(t, 303, rr.Code) |
| 112 |
require.Empty(t, mailer.verifyLinks) |
| 113 |
}) |
| 114 |
} |