Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Refine subscription validation coverage

Neeraj Guptaa29d1b6bf120
diff --git a/server/pkg/controller/appstore.go b/server/pkg/controller/appstore.go
index 663f4d76fe..44f622aa48 100644
--- a/server/pkg/controller/appstore.go
+++ b/server/pkg/controller/appstore.go
@@ -40,8 +40,8 @@ type AppStoreController struct {
}
type appStoreTransaction struct {
- receiptInfo appstore.InApp
- expiryTime int64
+ receiptInfo appstore.InApp
+ expiryTimeMicros int64
}
func NewAppStoreController(
@@ -138,7 +138,7 @@ func (c *AppStoreController) HandleNotification(ctx *gin.Context, notification a
return err
}
- if latestReceiptInfo.ProductID == subscription.ProductID && transaction.expiryTime < subscription.ExpiryTime {
+ if latestReceiptInfo.ProductID == subscription.ProductID && transaction.expiryTimeMicros < subscription.ExpiryTime {
// Outdated notification, no-op
} else {
if latestReceiptInfo.ProductID != subscription.ProductID {
@@ -158,7 +158,7 @@ func (c *AppStoreController) HandleNotification(ctx *gin.Context, notification a
}
newSubscription := ente.Subscription{
Storage: newStorage,
- ExpiryTime: transaction.expiryTime,
+ ExpiryTime: transaction.expiryTimeMicros,
ProductID: latestReceiptInfo.ProductID,
PaymentProvider: ente.AppStore,
OriginalTransactionID: latestReceiptInfo.OriginalTransactionID,
@@ -183,7 +183,7 @@ func (c *AppStoreController) HandleNotification(ctx *gin.Context, notification a
return stacktrace.Propagate(err, "")
}
}
- err = c.BillingRepo.UpdateSubscriptionExpiryTime(subscription.ID, transaction.expiryTime)
+ err = c.BillingRepo.UpdateSubscriptionExpiryTime(subscription.ID, transaction.expiryTimeMicros)
if err != nil {
return stacktrace.Propagate(err, "")
}
@@ -226,7 +226,7 @@ func (c *AppStoreController) GetVerifiedSubscription(userID int64, productID str
ProductID: transaction.receiptInfo.ProductID,
Storage: storage,
OriginalTransactionID: transaction.receiptInfo.OriginalTransactionID,
- ExpiryTime: transaction.expiryTime,
+ ExpiryTime: transaction.expiryTimeMicros,
PaymentProvider: ente.AppStore,
Attributes: ente.SubscriptionAttributes{
LatestVerificationData: verificationData,
@@ -254,20 +254,20 @@ func (c *AppStoreController) verifyAppStoreSubscription(verificationData string)
func getCurrentAppStoreTransaction(receiptInfo []appstore.InApp) (appStoreTransaction, error) {
var selected appstore.InApp
- var selectedPurchaseTime int64
+ var selectedPurchaseTimeMillis int64
found := false
ambiguous := false
for _, candidate := range receiptInfo {
- purchaseTime, err := strconv.ParseInt(candidate.PurchaseDateMS, 10, 64)
+ purchaseTimeMillis, err := strconv.ParseInt(candidate.PurchaseDateMS, 10, 64)
if err != nil {
return appStoreTransaction{}, stacktrace.Propagate(ente.ErrBadRequest, "invalid App Store purchase time")
}
- if !found || purchaseTime > selectedPurchaseTime {
+ if !found || purchaseTimeMillis > selectedPurchaseTimeMillis {
selected = candidate
- selectedPurchaseTime = purchaseTime
+ selectedPurchaseTimeMillis = purchaseTimeMillis
found = true
ambiguous = false
- } else if purchaseTime == selectedPurchaseTime && candidate.TransactionID != selected.TransactionID {
+ } else if purchaseTimeMillis == selectedPurchaseTimeMillis && candidate.TransactionID != selected.TransactionID {
ambiguous = true
}
}
@@ -277,13 +277,13 @@ func getCurrentAppStoreTransaction(receiptInfo []appstore.InApp) (appStoreTransa
if !found || selected.ProductID == "" || selected.TransactionID == "" || selected.OriginalTransactionID == "" || selected.IsUpgraded == "true" {
return appStoreTransaction{}, stacktrace.Propagate(ente.ErrBadRequest, "no current App Store entitlement")
}
- expiryTime, err := strconv.ParseInt(selected.ExpiresDateMS, 10, 64)
+ expiryTimeMillis, err := strconv.ParseInt(selected.ExpiresDateMS, 10, 64)
if err != nil {
return appStoreTransaction{}, stacktrace.Propagate(ente.ErrBadRequest, "invalid App Store expiry time")
}
return appStoreTransaction{
- receiptInfo: selected,
- expiryTime: expiryTime * 1000,
+ receiptInfo: selected,
+ expiryTimeMicros: expiryTimeMillis * 1000,
}, nil
}
diff --git a/server/pkg/controller/appstore_test.go b/server/pkg/controller/appstore_test.go
index 1e0b1dfa1f..68810faf39 100644
--- a/server/pkg/controller/appstore_test.go
+++ b/server/pkg/controller/appstore_test.go
@@ -21,8 +21,8 @@ func TestGetCurrentAppStoreTransactionUsesLatestPurchase(t *testing.T) {
if transaction.receiptInfo.ProductID != "small" {
t.Fatalf("product ID = %q, want small", transaction.receiptInfo.ProductID)
}
- if transaction.expiryTime != 400_000 {
- t.Fatalf("expiry time = %d, want 400000", transaction.expiryTime)
+ if transaction.expiryTimeMicros != 400_000 {
+ t.Fatalf("expiry time in microseconds = %d, want 400000", transaction.expiryTimeMicros)
}
}
@@ -54,6 +54,14 @@ func TestGetCurrentAppStoreTransactionRejectsInvalidCurrentPurchase(t *testing.T
name: "missing product",
receipts: []appstore.InApp{appStoreReceipt("", "new", "original", "200", "400", false, false)},
},
+ {
+ name: "missing transaction",
+ receipts: []appstore.InApp{appStoreReceipt("small", "", "original", "200", "400", false, false)},
+ },
+ {
+ name: "missing original transaction",
+ receipts: []appstore.InApp{appStoreReceipt("small", "new", "", "200", "400", false, false)},
+ },
{
name: "invalid purchase time",
receipts: []appstore.InApp{appStoreReceipt("small", "new", "original", "invalid", "400", false, false)},
@@ -80,7 +88,7 @@ func TestGetCurrentAppStoreTransactionRejectsInvalidCurrentPurchase(t *testing.T
}
}
-func TestGetAppStoreStorageUsesAllConfiguredPlans(t *testing.T) {
+func TestGetAppStoreStorageUsesAllConfiguredAccountsAndCountries(t *testing.T) {
controller := &AppStoreController{
BillingPlansPerAccount: ente.BillingPlansPerAccount{
ente.StripeUS: {"EU": {{IOSID: "current", Storage: 1000}}},
@@ -108,10 +116,6 @@ func TestGetAppStoreStorageRejectsInvalidConfiguration(t *testing.T) {
controller: appStoreControllerWithPlans(ente.BillingPlan{IOSID: "known", Storage: 100}),
productID: "unknown",
},
- {
- name: "missing product",
- controller: appStoreControllerWithPlans(ente.BillingPlan{Storage: 100}),
- },
{
name: "inconsistent storage",
controller: &AppStoreController{BillingPlansPerAccount: ente.BillingPlansPerAccount{
@@ -201,8 +205,8 @@ func appStoreReceipt(
productID string,
transactionID string,
originalTransactionID string,
- purchaseTime string,
- expiryTime string,
+ purchaseTimeMillis string,
+ expiryTimeMillis string,
isUpgraded bool,
isCancelled bool,
) appstore.InApp {
@@ -210,8 +214,8 @@ func appStoreReceipt(
ProductID: productID,
TransactionID: transactionID,
OriginalTransactionID: originalTransactionID,
- PurchaseDate: appstore.PurchaseDate{PurchaseDateMS: purchaseTime},
- ExpiresDate: appstore.ExpiresDate{ExpiresDateMS: expiryTime},
+ PurchaseDate: appstore.PurchaseDate{PurchaseDateMS: purchaseTimeMillis},
+ ExpiresDate: appstore.ExpiresDate{ExpiresDateMS: expiryTimeMillis},
}
if isUpgraded {
receipt.IsUpgraded = "true"