InAppReceiptTests.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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, isUpgraded: 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, isUpgraded: isUpgraded)
  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. self.isUpgraded = isUpgraded
  47. }
  48. var receiptInfo: NSDictionary {
  49. var result: [String: AnyObject] = [
  50. "product_id": productId as NSString,
  51. "quantity": String(quantity) as NSString,
  52. "purchase_date_ms": purchaseDate.timeIntervalSince1970.millisecondsNSString,
  53. "original_purchase_date_ms": originalPurchaseDate.timeIntervalSince1970.millisecondsNSString,
  54. "is_trial_period": (isTrialPeriod ? "1" : "0") as NSString,
  55. "transaction_id": transactionId as NSString,
  56. "original_transaction_id": originalTransactionId as NSString
  57. ]
  58. if let subscriptionExpirationDate = subscriptionExpirationDate {
  59. result["expires_date_ms"] = subscriptionExpirationDate.timeIntervalSince1970.millisecondsNSString
  60. }
  61. if let cancellationDate = cancellationDate {
  62. result["cancellation_date_ms"] = cancellationDate.timeIntervalSince1970.millisecondsNSString
  63. result["cancellation_date"] = cancellationDate as NSDate
  64. }
  65. return NSDictionary(dictionary: result)
  66. }
  67. public static func == (lhs: ReceiptItem, rhs: ReceiptItem) -> Bool {
  68. return
  69. lhs.productId == rhs.productId &&
  70. lhs.quantity == rhs.quantity &&
  71. lhs.purchaseDate == rhs.purchaseDate &&
  72. lhs.originalPurchaseDate == rhs.originalPurchaseDate &&
  73. lhs.subscriptionExpirationDate == rhs.subscriptionExpirationDate &&
  74. lhs.cancellationDate == rhs.cancellationDate &&
  75. lhs.isTrialPeriod == rhs.isTrialPeriod
  76. }
  77. }
  78. extension VerifySubscriptionResult: Equatable {
  79. public static func == (lhs: VerifySubscriptionResult, rhs: VerifySubscriptionResult) -> Bool {
  80. switch (lhs, rhs) {
  81. case (.notPurchased, .notPurchased): return true
  82. case (.purchased(let lhsExpiryDate, let lhsReceiptItem), .purchased(let rhsExpiryDate, let rhsReceiptItem)):
  83. return lhsExpiryDate == rhsExpiryDate && lhsReceiptItem == rhsReceiptItem
  84. case (.expired(let lhsExpiryDate, let lhsReceiptItem), .expired(let rhsExpiryDate, let rhsReceiptItem)):
  85. return lhsExpiryDate == rhsExpiryDate && lhsReceiptItem == rhsReceiptItem
  86. default: return false
  87. }
  88. }
  89. }
  90. extension VerifyPurchaseResult: Equatable {
  91. public static func == (lhs: VerifyPurchaseResult, rhs: VerifyPurchaseResult) -> Bool {
  92. switch (lhs, rhs) {
  93. case (.notPurchased, .notPurchased): return true
  94. case (.purchased(let lhsReceiptItem), .purchased(let rhsReceiptItem)):
  95. return lhsReceiptItem == rhsReceiptItem
  96. default: return false
  97. }
  98. }
  99. }
  100. // swiftlint: disable file_length
  101. class InAppReceiptTests: XCTestCase {
  102. // MARK: Verify Purchase
  103. func testVerifyPurchase_when_noPurchases_then_resultIsNotPurchased() {
  104. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  105. let productId = "product1"
  106. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  107. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  108. XCTAssertEqual(verifyPurchaseResult, .notPurchased)
  109. }
  110. func testVerifyPurchase_when_onePurchase_then_resultIsPurchased() {
  111. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  112. let productId = "product1"
  113. let item = ReceiptItem(productId: productId, purchaseDate: receiptRequestDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: false)
  114. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  115. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  116. XCTAssertEqual(verifyPurchaseResult, .purchased(item: item))
  117. }
  118. func testVerifyPurchase_when_oneCancelledPurchase_then_resultIsNotPurchased() {
  119. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  120. let productId = "product1"
  121. let item = ReceiptItem(productId: productId, purchaseDate: receiptRequestDate, subscriptionExpirationDate: nil, cancellationDate: receiptRequestDate, isTrialPeriod: false)
  122. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  123. let verifyPurchaseResult = SwiftyStoreKit.verifyPurchase(productId: productId, inReceipt: receipt)
  124. XCTAssertEqual(verifyPurchaseResult, .notPurchased)
  125. }
  126. // MARK: Verify Subscription, single receipt item tests
  127. // auto-renewable, not purchased
  128. func testVerifyAutoRenewableSubscription_when_noSubscriptions_then_resultIsNotPurchased() {
  129. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  130. let productId = "product1"
  131. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  132. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  133. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  134. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  135. }
  136. // auto-renewable, expired
  137. func testVerifyAutoRenewableSubscription_when_oneExpiredSubscription_then_resultIsExpired() {
  138. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  139. let productId = "product1"
  140. let isTrialPeriod = false
  141. let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  142. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  143. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  144. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  145. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  146. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
  147. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  148. }
  149. // auto-renewable, purchased
  150. func testVerifyAutoRenewableSubscription_when_oneNonExpiredSubscription_then_resultIsPurchased() {
  151. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  152. let productId = "product1"
  153. let isTrialPeriod = false
  154. let purchaseDate = receiptRequestDate
  155. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  156. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  157. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  158. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  159. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: expirationDate, items: [item])
  160. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  161. }
  162. // auto-renewable, cancelled
  163. func testVerifyAutoRenewableSubscription_when_oneCancelledSubscription_then_resultIsNotPurchased() {
  164. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  165. let productId = "product1"
  166. let isTrialPeriod = false
  167. let purchaseDate = receiptRequestDate
  168. let expirationDate = purchaseDate.addingTimeInterval(60 * 60)
  169. let cancelledDate = purchaseDate.addingTimeInterval(30 * 60)
  170. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: expirationDate, cancellationDate: cancelledDate, isTrialPeriod: isTrialPeriod)
  171. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  172. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  173. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  174. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  175. }
  176. // non-renewing, non purchased
  177. func testVerifyNonRenewingSubscription_when_noSubscriptions_then_resultIsNotPurchased() {
  178. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  179. let productId = "product1"
  180. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  181. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: 60 * 60), productId: productId, inReceipt: receipt)
  182. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  183. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  184. }
  185. // non-renewing, expired
  186. func testVerifyNonRenewingSubscription_when_oneExpiredSubscription_then_resultIsExpired() {
  187. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  188. let productId = "product1"
  189. let isTrialPeriod = false
  190. let purchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  191. let duration: TimeInterval = 60 * 60
  192. let expirationDate = purchaseDate.addingTimeInterval(duration)
  193. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  194. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  195. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  196. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: expirationDate, items: [item])
  197. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  198. }
  199. // non-renewing, purchased
  200. func testVerifyNonRenewingSubscription_when_oneNonExpiredSubscription_then_resultIsPurchased() {
  201. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  202. let productId = "product1"
  203. let isTrialPeriod = false
  204. let purchaseDate = receiptRequestDate
  205. let duration: TimeInterval = 60 * 60
  206. let expirationDate = purchaseDate.addingTimeInterval(duration)
  207. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: nil, isTrialPeriod: isTrialPeriod)
  208. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  209. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  210. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: expirationDate, items: [item])
  211. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  212. }
  213. // non-renewing, cancelled
  214. func testVerifyNonRenewingSubscription_when_oneCancelledSubscription_then_resultIsNotPurchased() {
  215. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  216. let productId = "product1"
  217. let isTrialPeriod = false
  218. let purchaseDate = receiptRequestDate
  219. let duration: TimeInterval = 60 * 60
  220. let cancelledDate = purchaseDate.addingTimeInterval(30 * 60)
  221. let item = ReceiptItem(productId: productId, purchaseDate: purchaseDate, subscriptionExpirationDate: nil, cancellationDate: cancelledDate, isTrialPeriod: isTrialPeriod)
  222. let receipt = makeReceipt(items: [item], requestDate: receiptRequestDate)
  223. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .nonRenewing(validDuration: duration), productId: productId, inReceipt: receipt)
  224. let expectedSubscriptionResult = VerifySubscriptionResult.notPurchased
  225. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  226. }
  227. // MARK: Verify Subscription, multiple receipt item tests
  228. func testVerifyAutoRenewableSubscription_when_twoSubscriptions_sameProductId_mostRecentNonExpired_then_resultIsPurchased_itemsSorted() {
  229. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  230. let productId = "product1"
  231. let isTrialPeriod = false
  232. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  233. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  234. let olderItem = ReceiptItem(productId: productId,
  235. purchaseDate: olderPurchaseDate,
  236. subscriptionExpirationDate: olderExpirationDate,
  237. cancellationDate: nil,
  238. isTrialPeriod: isTrialPeriod)
  239. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  240. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  241. let newerItem = ReceiptItem(productId: productId,
  242. purchaseDate: newerPurchaseDate,
  243. subscriptionExpirationDate: newerExpirationDate,
  244. cancellationDate: nil,
  245. isTrialPeriod: isTrialPeriod)
  246. let receipt = makeReceipt(items: [olderItem, newerItem], requestDate: receiptRequestDate)
  247. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  248. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  249. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  250. }
  251. func testVerifyAutoRenewableSubscription_when_twoSubscriptions_sameProductId_bothExpired_then_resultIsExpired_itemsSorted() {
  252. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  253. let productId = "product1"
  254. let isTrialPeriod = false
  255. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  256. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  257. let olderItem = ReceiptItem(productId: productId,
  258. purchaseDate: olderPurchaseDate,
  259. subscriptionExpirationDate: olderExpirationDate,
  260. cancellationDate: nil,
  261. isTrialPeriod: isTrialPeriod)
  262. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 13)
  263. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  264. let newerItem = ReceiptItem(productId: productId,
  265. purchaseDate: newerPurchaseDate,
  266. subscriptionExpirationDate: newerExpirationDate,
  267. cancellationDate: nil,
  268. isTrialPeriod: isTrialPeriod)
  269. let receipt = makeReceipt(items: [olderItem, newerItem], requestDate: receiptRequestDate)
  270. let verifySubscriptionResult = SwiftyStoreKit.verifySubscription(ofType: .autoRenewable, productId: productId, inReceipt: receipt)
  271. let expectedSubscriptionResult = VerifySubscriptionResult.expired(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  272. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  273. }
  274. // MARK: Verify Subscriptions, multiple receipt item tests
  275. func testVerifyAutoRenewableSubscriptions_when_threeSubscriptions_twoMatchingProductIds_mostRecentNonExpired_then_resultIsPurchased_itemsSorted() {
  276. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  277. let productId1 = "product1"
  278. let productId2 = "product2"
  279. let productIds = Set([ productId1, productId2 ])
  280. let isTrialPeriod = false
  281. let olderPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 12)
  282. let olderExpirationDate = olderPurchaseDate.addingTimeInterval(60 * 60)
  283. let olderItem = ReceiptItem(productId: productId1,
  284. purchaseDate: olderPurchaseDate,
  285. subscriptionExpirationDate: olderExpirationDate,
  286. cancellationDate: nil,
  287. isTrialPeriod: isTrialPeriod)
  288. let newerPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  289. let newerExpirationDate = newerPurchaseDate.addingTimeInterval(60 * 60)
  290. let newerItem = ReceiptItem(productId: productId2,
  291. purchaseDate: newerPurchaseDate,
  292. subscriptionExpirationDate: newerExpirationDate,
  293. cancellationDate: nil,
  294. isTrialPeriod: isTrialPeriod)
  295. let otherPurchaseDate = makeDateAtMidnight(year: 2017, month: 5, day: 15)
  296. let otherExpirationDate = otherPurchaseDate.addingTimeInterval(60 * 60)
  297. let otherItem = ReceiptItem(productId: "otherProduct",
  298. purchaseDate: otherPurchaseDate,
  299. subscriptionExpirationDate: otherExpirationDate,
  300. cancellationDate: nil,
  301. isTrialPeriod: isTrialPeriod)
  302. let receipt = makeReceipt(items: [olderItem, newerItem, otherItem], requestDate: receiptRequestDate)
  303. let verifySubscriptionResult = SwiftyStoreKit.verifySubscriptions(ofType: .autoRenewable, productIds: productIds, inReceipt: receipt)
  304. let expectedSubscriptionResult = VerifySubscriptionResult.purchased(expiryDate: newerExpirationDate, items: [newerItem, olderItem])
  305. XCTAssertEqual(verifySubscriptionResult, expectedSubscriptionResult)
  306. }
  307. // MARK: Get Distinct Purchase Identifiers, empty receipt item tests
  308. func testGetDistinctPurchaseIds_when_noReceipt_then_resultIsNil() {
  309. let receiptRequestDate = makeDateAtMidnight(year: 2017, month: 5, day: 14)
  310. let receipt = makeReceipt(items: [], requestDate: receiptRequestDate)
  311. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  312. XCTAssertNil(getdistinctProductIdsResult)
  313. }
  314. // MARK: Get Distinct Purchase Identifiers, multiple receipt item tests
  315. func testGetDistinctPurchaseIds_when_Receipt_then_resultIsNotNil() {
  316. let receiptRequestDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 20)
  317. let purchaseDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 1)
  318. let purchaseDateTwo = makeDateAtMidnight(year: 2020, month: 1, day: 1)
  319. let productId1 = "product1"
  320. let productId2 = "product2"
  321. let product1 = ReceiptItem(productId: productId1, purchaseDate: purchaseDateOne)
  322. let product2 = ReceiptItem(productId: productId2, purchaseDate: purchaseDateTwo)
  323. let receipt = makeReceipt(items: [product1, product2], requestDate: receiptRequestDateOne)
  324. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  325. XCTAssertNotNil(getdistinctProductIdsResult)
  326. }
  327. // MARK: Get Distinct Purchase Identifiers, multiple non unique product identifiers tests
  328. func testGetDistinctPurchaseIds_when_nonUniqueIdentifiers_then_resultIsUnique() {
  329. let receiptRequestDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 20)
  330. let purchaseDateOne = makeDateAtMidnight(year: 2020, month: 2, day: 1)
  331. let purchaseDateTwo = makeDateAtMidnight(year: 2020, month: 2, day: 2)
  332. let purchaseDateThree = makeDateAtMidnight(year: 2020, month: 2, day: 3)
  333. let purchaseDateFour = makeDateAtMidnight(year: 2020, month: 2, day: 4)
  334. let productId1 = "product1"
  335. let productId2 = "product2"
  336. let productId3 = "product1"
  337. let productId4 = "product2"
  338. let product1 = ReceiptItem(productId: productId1, purchaseDate: purchaseDateOne)
  339. let product2 = ReceiptItem(productId: productId2, purchaseDate: purchaseDateTwo)
  340. let product3 = ReceiptItem(productId: productId3, purchaseDate: purchaseDateThree)
  341. let product4 = ReceiptItem(productId: productId4, purchaseDate: purchaseDateFour)
  342. let receipt = makeReceipt(items: [product1, product2, product3, product4], requestDate: receiptRequestDateOne)
  343. let getdistinctProductIdsResult = SwiftyStoreKit.getDistinctPurchaseIds(ofType: .autoRenewable, inReceipt: receipt)
  344. let expectedProductIdsResult = Set([productId1, productId2, productId3, productId4])
  345. XCTAssertEqual(getdistinctProductIdsResult, expectedProductIdsResult)
  346. }
  347. // MARK: Helper methods
  348. func makeReceipt(items: [ReceiptItem], requestDate: Date) -> [String: AnyObject] {
  349. let receiptInfos = items.map { $0.receiptInfo }
  350. // Creating this with NSArray results in __NSSingleObjectArrayI which fails the cast to [String: AnyObject]
  351. let array = NSMutableArray()
  352. array.addObjects(from: receiptInfos)
  353. return [
  354. //"latest_receipt": [:],
  355. "status": "200" as NSString,
  356. "environment": "Sandbox" as NSString,
  357. "receipt": NSDictionary(dictionary: [
  358. "request_date_ms": requestDate.timeIntervalSince1970.millisecondsNSString,
  359. "in_app": array // non renewing
  360. ]),
  361. "latest_receipt_info": array // autoRenewable
  362. ]
  363. }
  364. func makeDateAtMidnight(year: Int, month: Int, day: Int) -> Date {
  365. var dateComponents = DateComponents()
  366. dateComponents.day = day
  367. dateComponents.month = month
  368. dateComponents.year = year
  369. let calendar = Calendar(identifier: .gregorian)
  370. return calendar.date(from: dateComponents)!
  371. }
  372. }