InAppReceiptTests.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // InAppReceiptTests.swift
  3. // SwiftyStoreKit
  4. //
  5. // Created by Andrea Bizzotto on 08/05/2017.
  6. // Copyright (c) 2017 Andrea Bizzotto (bizz84@gmail.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. import XCTest
  26. import SwiftyStoreKit
  27. private extension TimeInterval {
  28. var millisecondsNSString: NSString {
  29. return String(format: "%.0f", self * 1000) as NSString
  30. }
  31. }
  32. extension ReceiptItem: Equatable {
  33. init(productId: String, purchaseDate: Date, subscriptionExpirationDate: Date? = nil, cancellationDate: Date? = nil, isTrialPeriod: Bool = false, isInIntroOfferPeriod: Bool = false) {
  34. self.init(productId: productId, quantity: 1, transactionId: UUID().uuidString, originalTransactionId: UUID().uuidString, purchaseDate: purchaseDate, originalPurchaseDate: purchaseDate, webOrderLineItemId: UUID().uuidString, subscriptionExpirationDate: subscriptionExpirationDate, cancellationDate: cancellationDate, isTrialPeriod: isTrialPeriod, isInIntroOfferPeriod: isInIntroOfferPeriod)
  35. self.productId = productId
  36. self.quantity = 1
  37. self.purchaseDate = purchaseDate
  38. self.originalPurchaseDate = purchaseDate
  39. self.subscriptionExpirationDate = subscriptionExpirationDate
  40. self.cancellationDate = cancellationDate
  41. self.transactionId = UUID().uuidString
  42. self.originalTransactionId = UUID().uuidString
  43. self.webOrderLineItemId = UUID().uuidString
  44. self.isTrialPeriod = isTrialPeriod
  45. self.isInIntroOfferPeriod = isInIntroOfferPeriod
  46. }
  47. var receiptInfo: NSDictionary {
  48. var result: [String: AnyObject] = [
  49. "product_id": productId as NSString,
  50. "quantity": String(quantity) as NSString,
  51. "purchase_date_ms": purchaseDate.timeIntervalSince1970.millisecondsNSString,
  52. "original_purchase_date_ms": originalPurchaseDate.timeIntervalSince1970.millisecondsNSString,
  53. "is_trial_period": (isTrialPeriod ? "1" : "0") as NSString,
  54. "transaction_id": transactionId as NSString,
  55. "original_transaction_id": originalTransactionId as NSString
  56. ]
  57. if let subscriptionExpirationDate = subscriptionExpirationDate {
  58. result["expires_date_ms"] = subscriptionExpirationDate.timeIntervalSince1970.millisecondsNSString
  59. }
  60. if let cancellationDate = cancellationDate {
  61. result["cancellation_date_ms"] = cancellationDate.timeIntervalSince1970.millisecondsNSString
  62. result["cancellation_date"] = cancellationDate as NSDate
  63. }
  64. return NSDictionary(dictionary: result)
  65. }
  66. public static func == (lhs: ReceiptItem, rhs: ReceiptItem) -> Bool {
  67. return
  68. lhs.productId == rhs.productId &&
  69. lhs.quantity == rhs.quantity &&
  70. lhs.purchaseDate == rhs.purchaseDate &&
  71. lhs.originalPurchaseDate == rhs.originalPurchaseDate &&
  72. lhs.subscriptionExpirationDate == rhs.subscriptionExpirationDate &&
  73. lhs.cancellationDate == rhs.cancellationDate &&
  74. lhs.isTrialPeriod == rhs.isTrialPeriod
  75. }
  76. }
  77. extension VerifySubscriptionResult: Equatable {
  78. public static func == (lhs: VerifySubscriptionResult, rhs: VerifySubscriptionResult) -> Bool {
  79. switch (lhs, rhs) {
  80. case (.notPurchased, .notPurchased): return true
  81. case (.purchased(let lhsExpiryDate, let lhsReceiptItem), .purchased(let rhsExpiryDate, let rhsReceiptItem)):
  82. return lhsExpiryDate == rhsExpiryDate && lhsReceiptItem == rhsReceiptItem
  83. case (.expired(let lhsExpiryDate, let lhsReceiptItem), .expired(let rhsExpiryDate, let rhsReceiptItem)):
  84. return lhsExpiryDate == rhsExpiryDate && lhsReceiptItem == rhsReceiptItem
  85. default: return false
  86. }
  87. }
  88. }
  89. extension VerifyPurchaseResult: Equatable {
  90. public static func == (lhs: VerifyPurchaseResult, rhs: VerifyPurchaseResult) -> Bool {
  91. switch (lhs, rhs) {
  92. case (.notPurchased, .notPurchased): return true
  93. case (.purchased(let lhsReceiptItem), .purchased(let rhsReceiptItem)):
  94. return lhsReceiptItem == rhsReceiptItem
  95. default: return false
  96. }
  97. }
  98. }
  99. // swiftlint: disable file_length
  100. class InAppReceiptTests: XCTestCase {
  101. // MARK: Verify Purchase
  102. func testVerifyPurchase_when_noPurchases_then_resultIsNotPurchased() {
  103. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  104. let productId = "product1"
  105. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  106. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  107. XCTAssertEqual(verifyPurchaseResult, .notPurchased)
  108. }
  109. func testVerifyPurchase_when_onePurchase_then_resultIsPurchased() {
  110. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  111. let productId = "product1"
  112. let item = ReceiptItem(productId: productId, purchaseDate: receiptRequestDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: false)
  113. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  114. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  115. XCTAssertEqual(verifyPurchaseResult, .purchased(item: item))
  116. }
  117. func testVerifyPurchase_when_oneCancelledPurchase_then_resultIsNotPurchased() {
  118. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  119. let productId = "product1"
  120. let item = ReceiptItem(productId: productId, purchaseDate: receiptRequestDate, subscriptionExpirationDate: nil, cancellationDate: receiptRequestDate, isTrialPeriod: false)
  121. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  122. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  123. XCTAssertEqual(verifyPurchaseResult, .notPurchased)
  124. }
  125. // MARK: Verify Subscription, single receipt item tests
  126. // auto-renewable, not purchased
  127. func testVerifyAutoRenewableSubscription_when_noSubscriptions_then_resultIsNotPurchased() {
  128. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  129. let productId = "product1"
  130. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  131. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  132. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  133. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  134. }
  135. // auto-renewable, expired
  136. func testVerifyAutoRenewableSubscription_when_oneExpiredSubscription_then_resultIsExpired() {
  137. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  138. let productId = "product1"
  139. let isTrialPeriod = false
  140. let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  141. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  142. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  143. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  144. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  145. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
  146. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  147. }
  148. // auto-renewable, purchased
  149. func testVerifyAutoRenewableSubscription_when_oneNonExpiredSubscription_then_resultIsPurchased() {
  150. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  151. let productId = "product1"
  152. let isTrialPeriod = false
  153. let purchaseDate = receiptRequestDate
  154. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  155. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  156. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  157. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  158. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: expirationDate, items: [item])
  159. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  160. }
  161. // auto-renewable, cancelled
  162. func testVerifyAutoRenewableSubscription_when_oneCancelledSubscription_then_resultIsNotPurchased() {
  163. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  164. let productId = "product1"
  165. let isTrialPeriod = false
  166. let purchaseDate = receiptRequestDate
  167. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  168. let cancelledDate = purchaseDate.addingTimeInterval(30 * 60)
  169. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: cancelledDate, isTrialPeriod: isTrialPeriod)
  170. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  171. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  172. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  173. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  174. }
  175. // non-renewing, non purchased
  176. func testVerifyNonRenewingSubscription_when_noSubscriptions_then_resultIsNotPurchased() {
  177. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  178. let productId = "product1"
  179. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  180. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: 60 * 60), productId: productId, inReceipt: receipt)
  181. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  182. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  183. }
  184. // non-renewing, expired
  185. func testVerifyNonRenewingSubscription_when_oneExpiredSubscription_then_resultIsExpired() {
  186. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  187. let productId = "product1"
  188. let isTrialPeriod = false
  189. let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  190. let duration: TimeInterval = 60 * 60
  191. let expirationDate = purchaseDate.addingTimeInterval(duration)
  192. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  193. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  194. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  195. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
  196. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  197. }
  198. // non-renewing, purchased
  199. func testVerifyNonRenewingSubscription_when_oneNonExpiredSubscription_then_resultIsPurchased() {
  200. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  201. let productId = "product1"
  202. let isTrialPeriod = false
  203. let purchaseDate = receiptRequestDate
  204. let duration: TimeInterval = 60 * 60
  205. let expirationDate = purchaseDate.addingTimeInterval(duration)
  206. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  207. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  208. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  209. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: expirationDate, items: [item])
  210. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  211. }
  212. // non-renewing, cancelled
  213. func testVerifyNonRenewingSubscription_when_oneCancelledSubscription_then_resultIsNotPurchased() {
  214. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  215. let productId = "product1"
  216. let isTrialPeriod = false
  217. let purchaseDate = receiptRequestDate
  218. let duration: TimeInterval = 60 * 60
  219. let cancelledDate = purchaseDate.addingTimeInterval(30 * 60)
  220. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: cancelledDate, isTrialPeriod: isTrialPeriod)
  221. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  222. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  223. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  224. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  225. }
  226. // MARK: Verify Subscription, multiple receipt item tests
  227. func testVerifyAutoRenewableSubscription_when_twoSubscriptions_sameProductId_mostRecentNonExpired_then_resultIsPurchased_itemsSorted() {
  228. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  229. let productId = "product1"
  230. let isTrialPeriod = false
  231. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  232. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  233. let olderItem = ReceiptItem(productId: productId,
  234. purchaseDate: olderPurchaseDate,
  235. subscriptionExpirationDate: olderExpirationDate,
  236. cancellationDate: nil,
  237. isTrialPeriod: isTrialPeriod)
  238. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  239. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  240. let newerItem = ReceiptItem(productId: productId,
  241. purchaseDate: newerPurchaseDate,
  242. subscriptionExpirationDate: newerExpirationDate,
  243. cancellationDate: nil,
  244. isTrialPeriod: isTrialPeriod)
  245. let receipt = makeReceipt(items: [olderItem, newerItem], requestDate: receiptRequestDate)
  246. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  247. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  248. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  249. }
  250. func testVerifyAutoRenewableSubscription_when_twoSubscriptions_sameProductId_bothExpired_then_resultIsExpired_itemsSorted() {
  251. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  252. let productId = "product1"
  253. let isTrialPeriod = false
  254. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  255. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  256. let olderItem = ReceiptItem(productId: productId,
  257. purchaseDate: olderPurchaseDate,
  258. subscriptionExpirationDate: olderExpirationDate,
  259. cancellationDate: nil,
  260. isTrialPeriod: isTrialPeriod)
  261. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 13)
  262. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  263. let newerItem = ReceiptItem(productId: productId,
  264. purchaseDate: newerPurchaseDate,
  265. subscriptionExpirationDate: newerExpirationDate,
  266. cancellationDate: nil,
  267. isTrialPeriod: isTrialPeriod)
  268. let receipt = makeReceipt(items: [olderItem, newerItem], requestDate: receiptRequestDate)
  269. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  270. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  271. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  272. }
  273. // MARK: Verify Subscriptions, multiple receipt item tests
  274. func testVerifyAutoRenewableSubscriptions_when_threeSubscriptions_twoMatchingProductIds_mostRecentNonExpired_then_resultIsPurchased_itemsSorted() {
  275. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  276. let productId1 = "product1"
  277. let productId2 = "product2"
  278. let productIds = Set([ productId1, productId2 ])
  279. let isTrialPeriod = false
  280. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  281. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  282. let olderItem = ReceiptItem(productId: productId1,
  283. purchaseDate: olderPurchaseDate,
  284. subscriptionExpirationDate: olderExpirationDate,
  285. cancellationDate: nil,
  286. isTrialPeriod: isTrialPeriod)
  287. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  288. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  289. let newerItem = ReceiptItem(productId: productId2,
  290. purchaseDate: newerPurchaseDate,
  291. subscriptionExpirationDate: newerExpirationDate,
  292. cancellationDate: nil,
  293. isTrialPeriod: isTrialPeriod)
  294. let otherPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  295. let otherExpirationDate = otherPurchaseDate.addingTimeInterval(60 * 60)
  296. let otherItem = ReceiptItem(productId: "otherProduct",
  297. purchaseDate: otherPurchaseDate,
  298. subscriptionExpirationDate: otherExpirationDate,
  299. cancellationDate: nil,
  300. isTrialPeriod: isTrialPeriod)
  301. let receipt = makeReceipt(items: [olderItem, newerItem, otherItem], requestDate: receiptRequestDate)
  302. let verifySubscriptionResult = SwiftyStoreKit.verifySubscriptions(ofType: .autoRenewable, productIds: productIds, inReceipt: receipt)
  303. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  304. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  305. }
  306. // MARK: Get Distinct Purchase Identifiers, empty receipt item tests
  307. func testGetDistinctPurchaseIds_when_noReceipt_then_resultIsNil() {
  308. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  309. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  310. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  311. XCTAssertNil(getdistinctProductIdsResult)
  312. }
  313. // MARK: Get Distinct Purchase Identifiers, multiple receipt item tests
  314. func testGetDistinctPurchaseIds_when_Receipt_then_resultIsNotNil() {
  315. let receiptRequestDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 20)
  316. let purchaseDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 1)
  317. let purchaseDateTwo = makeDateAtMidnight(year: 2020, month: 1, day: 1)
  318. let productId1 = "product1"
  319. let productId2 = "product2"
  320. let product1 = ReceiptItem(productId: productId1, purchaseDate: purchaseDateOne)
  321. let product2 = ReceiptItem(productId: productId2, purchaseDate: purchaseDateTwo)
  322. let receipt = makeReceipt(items: [product1, product2], requestDate: receiptRequestDateOne)
  323. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  324. XCTAssertNotNil(getdistinctProductIdsResult)
  325. }
  326. // MARK: Get Distinct Purchase Identifiers, multiple non unique product identifiers tests
  327. func testGetDistinctPurchaseIds_when_nonUniqueIdentifiers_then_resultIsUnique() {
  328. let receiptRequestDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 20)
  329. let purchaseDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 1)
  330. let purchaseDateTwo = makeDateAtMidnight(year: 2020, month: 2, day: 2)
  331. let purchaseDateThree = makeDateAtMidnight(year: 2020, month: 2, day: 3)
  332. let purchaseDateFour = makeDateAtMidnight(year: 2020, month: 2, day: 4)
  333. let productId1 = "product1"
  334. let productId2 = "product2"
  335. let productId3 = "product1"
  336. let productId4 = "product2"
  337. let product1 = ReceiptItem(productId: productId1, purchaseDate: purchaseDateOne)
  338. let product2 = ReceiptItem(productId: productId2, purchaseDate: purchaseDateTwo)
  339. let product3 = ReceiptItem(productId: productId3, purchaseDate: purchaseDateThree)
  340. let product4 = ReceiptItem(productId: productId4, purchaseDate: purchaseDateFour)
  341. let receipt = makeReceipt(items: [product1, product2, product3, product4], requestDate: receiptRequestDateOne)
  342. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  343. let expectedProductIdsResult = Set([productId1, productId2, productId3, productId4])
  344. XCTAssertEqual(getdistinctProductIdsResult, expectedProductIdsResult)
  345. }
  346. // MARK: Helper methods
  347. func makeReceipt(items: [ReceiptItem], requestDate: Date) -> [String: AnyObject] {
  348. let receiptInfos = items.map { $0.receiptInfo }
  349. // Creating this with NSArray results in __NSSingleObjectArrayI which fails the cast to [String: AnyObject]
  350. let array = NSMutableArray()
  351. array.addObjects(from: receiptInfos)
  352. return [
  353. //"latest_receipt": [:],
  354. "status": "200" as NSString,
  355. "environment": "Sandbox" as NSString,
  356. "receipt": NSDictionary(dictionary: [
  357. "request_date_ms": requestDate.timeIntervalSince1970.millisecondsNSString,
  358. "in_app": array // non renewing
  359. ]),
  360. "latest_receipt_info": array // autoRenewable
  361. ]
  362. }
  363. func makeDateAtMidnight(year: Int, month: Int, day: Int) -> Date {
  364. var dateComponents = DateComponents()
  365. dateComponents.day = day
  366. dateComponents.month = month
  367. dateComponents.year = year
  368. let calendar = Calendar(identifier: .gregorian)
  369. return calendar.date(from: dateComponents)!
  370. }
  371. }