瀏覽代碼

adds tests for a single subscription with pendingRenewalInfo

Justin Schröder 4 年之前
父節點
當前提交
14e9b7a4ee
共有 1 個文件被更改,包括 50 次插入0 次删除
  1. 50 0
      Tests/SwiftyStoreKitTests/InAppReceiptTests.swift

+ 50 - 0
Tests/SwiftyStoreKitTests/InAppReceiptTests.swift

@@ -233,6 +233,56 @@ class InAppReceiptTests: XCTestCase {
         let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
         XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
     }
+    
+    // auto-renewable, in grace period
+    func testVerifyAutoRenewableSubscription_when_oneGracePeriodSubscription_then_resultIsPurchased() {
+        let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
+        let productId = "product1"
+        let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
+        let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
+        let transactionId = UUID().uuidString
+        let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, transactionId: transactionId, isTrialPeriod: false)
+        
+        let gracePeriodExpirationDate = makeDateAtMidnight(year: 2017, month: 5, day: 16)
+        let pendingRenewal = PendingRenewalInfo(productId: productId, expiryDate: gracePeriodExpirationDate, originalTransactionId: transactionId)
+        
+        let receiptNormal = makeReceipt(items: [item], requestDate: receiptRequestDate)
+        let verifySubscriptionResultNormal = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receiptNormal)
+        let expectedSubscriptionResultNormal = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
+        //Sanity Check: Without the pending renewal info the receipt should have been expired.
+        XCTAssertEqual(verifySubscriptionResultNormal, expectedSubscriptionResultNormal)
+        
+        let receiptWithPendingRenewal = makeReceipt(items: [item], requestDate: receiptRequestDate, pendingRenewals: [pendingRenewal])
+        let verifySubscriptionResultWithPendingRenewal = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receiptWithPendingRenewal)
+        let expectedSubscriptionResultWithPendingRenewal = VerifySubscriptionResult.inGracePeriod(endDate: gracePeriodExpirationDate, items: [item], pendingRenewals: [pendingRenewal])
+        //With the pending renewal info, we're in a grace period
+        XCTAssertEqual(verifySubscriptionResultWithPendingRenewal, expectedSubscriptionResultWithPendingRenewal)
+    }
+    
+    // auto-renewable, in expired grace period
+    func testVerifyAutoRenewableSubscription_when_oneExpiredGracePeriodSubscription_then_resultIsExpired() {
+        let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 20)
+        let productId = "product1"
+        let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
+        let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
+        let transactionId = UUID().uuidString
+        let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, transactionId: transactionId, isTrialPeriod: false)
+        
+        let gracePeriodExpirationDate = makeDateAtMidnight(year: 2017, month: 5, day: 19)
+        let pendingRenewal = PendingRenewalInfo(productId: productId, expiryDate: gracePeriodExpirationDate, originalTransactionId: transactionId)
+        
+        let receiptNormal = makeReceipt(items: [item], requestDate: receiptRequestDate)
+        let verifySubscriptionResultNormal = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receiptNormal)
+        let expectedSubscriptionResultNormal = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
+        //Sanity Check: Without the pending renewal info the receipt should have been expired.
+        XCTAssertEqual(verifySubscriptionResultNormal, expectedSubscriptionResultNormal)
+        
+        let receiptWithPendingRenewal = makeReceipt(items: [item], requestDate: receiptRequestDate, pendingRenewals: [pendingRenewal])
+        let verifySubscriptionResultWithPendingRenewal = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receiptWithPendingRenewal)
+        let expectedSubscriptionResultWithPendingRenewal = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
+        //With the pending renewal info, we're still in the expired state as the pending renewal info has expired as well
+        XCTAssertEqual(verifySubscriptionResultWithPendingRenewal, expectedSubscriptionResultWithPendingRenewal)
+    }
 
     // non-renewing, non purchased
     func testVerifyNonRenewingSubscription_when_noSubscriptions_then_resultIsNotPurchased() {