|
| 1 | +import XCTest |
| 2 | +import TestKit |
| 3 | +import StoreKitTest |
| 4 | + |
| 5 | +@testable import Yosemite |
| 6 | +@testable import Networking |
| 7 | + |
| 8 | +final class InAppPurchaseStoreTests: XCTestCase { |
| 9 | + |
| 10 | + /// Mock Network: Allows us to inject predefined responses! |
| 11 | + /// |
| 12 | + private var network: MockNetwork! |
| 13 | + |
| 14 | + /// Mock Storage: InMemory |
| 15 | + /// |
| 16 | + private var storageManager: MockStorageManager! |
| 17 | + |
| 18 | + private var storeKitSession = try! SKTestSession(configurationFileNamed: "WooCommerceTest") |
| 19 | + |
| 20 | + /// Testing SiteID |
| 21 | + /// |
| 22 | + private let sampleSiteID: Int64 = 123 |
| 23 | + |
| 24 | + /// Testing Product ID |
| 25 | + /// Should match the product ID in WooCommerce.storekit |
| 26 | + /// |
| 27 | + private let sampleProductID: String = "debug.woocommerce.ecommerce.monthly" |
| 28 | + |
| 29 | + /// Testing Order ID |
| 30 | + /// Should match the order ID in iap-order-create.json |
| 31 | + /// |
| 32 | + private let sampleOrderID: Int64 = 12345 |
| 33 | + |
| 34 | + var store: InAppPurchaseStore! |
| 35 | + |
| 36 | + |
| 37 | + override func setUp() { |
| 38 | + network = MockNetwork(useResponseQueue: true) |
| 39 | + storageManager = MockStorageManager() |
| 40 | + store = InAppPurchaseStore(dispatcher: Dispatcher(), storageManager: storageManager, network: network) |
| 41 | + storeKitSession.disableDialogs = true |
| 42 | + } |
| 43 | + |
| 44 | + override func tearDown() { |
| 45 | + storeKitSession.resetToDefaultState() |
| 46 | + storeKitSession.clearTransactions() |
| 47 | + } |
| 48 | + |
| 49 | + func test_iap_supported_in_us() throws { |
| 50 | + // Given |
| 51 | + storeKitSession.storefront = "USA" |
| 52 | + |
| 53 | + // When |
| 54 | + let result = waitFor { promise in |
| 55 | + let action = InAppPurchaseAction.inAppPurchasesAreSupported { result in |
| 56 | + promise(result) |
| 57 | + } |
| 58 | + self.store.onAction(action) |
| 59 | + } |
| 60 | + |
| 61 | + // Then |
| 62 | + XCTAssertTrue(result) |
| 63 | + } |
| 64 | + |
| 65 | + func test_iap_supported_in_canada() throws { |
| 66 | + // Given |
| 67 | + storeKitSession.storefront = "CAN" |
| 68 | + |
| 69 | + // When |
| 70 | + let result = waitFor { promise in |
| 71 | + let action = InAppPurchaseAction.inAppPurchasesAreSupported { result in |
| 72 | + promise(result) |
| 73 | + } |
| 74 | + self.store.onAction(action) |
| 75 | + } |
| 76 | + |
| 77 | + // Then |
| 78 | + XCTAssertFalse(result) |
| 79 | + } |
| 80 | + |
| 81 | + func test_load_products_loads_products_response() throws { |
| 82 | + // Given |
| 83 | + network.simulateResponse(requestUrlSuffix: "iap/products", filename: "iap-products") |
| 84 | + |
| 85 | + // When |
| 86 | + let result = waitFor { promise in |
| 87 | + let action = InAppPurchaseAction.loadProducts { result in |
| 88 | + promise(result) |
| 89 | + } |
| 90 | + self.store.onAction(action) |
| 91 | + } |
| 92 | + |
| 93 | + // Then |
| 94 | + let products = try XCTUnwrap(result.get()) |
| 95 | + XCTAssertFalse(products.isEmpty) |
| 96 | + XCTAssertEqual(products.first?.id, sampleProductID) |
| 97 | + } |
| 98 | + |
| 99 | + func test_load_products_fails_if_iap_unsupported() throws { |
| 100 | + // Given |
| 101 | + storeKitSession.storefront = "CAN" |
| 102 | + network.simulateResponse(requestUrlSuffix: "iap/products", filename: "iap-products") |
| 103 | + |
| 104 | + // When |
| 105 | + let result = waitFor { promise in |
| 106 | + let action = InAppPurchaseAction.loadProducts { result in |
| 107 | + promise(result) |
| 108 | + } |
| 109 | + self.store.onAction(action) |
| 110 | + } |
| 111 | + |
| 112 | + // Then |
| 113 | + XCTAssert(result.isFailure) |
| 114 | + } |
| 115 | + |
| 116 | + func test_purchase_product_completes_purchase() throws { |
| 117 | + // Given |
| 118 | + network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "iap-order-create") |
| 119 | + |
| 120 | + // When |
| 121 | + let result = waitFor { promise in |
| 122 | + let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in |
| 123 | + promise(result) |
| 124 | + } |
| 125 | + self.store.onAction(action) |
| 126 | + } |
| 127 | + |
| 128 | + // Then |
| 129 | + let purchaseResult = try XCTUnwrap(result.get()) |
| 130 | + guard case let .success(verificationResult) = purchaseResult, |
| 131 | + case let .verified(transaction) = verificationResult else { |
| 132 | + return XCTFail() |
| 133 | + } |
| 134 | + XCTAssertEqual(transaction.productID, sampleProductID) |
| 135 | + XCTAssertNotNil(transaction.appAccountToken) |
| 136 | + } |
| 137 | + |
| 138 | + @available(iOS 16.0, *) |
| 139 | + func test_purchase_product_ensure_xcode_environment() throws { |
| 140 | + // Given |
| 141 | + network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "iap-order-create") |
| 142 | + |
| 143 | + // When |
| 144 | + let result = waitFor { promise in |
| 145 | + let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in |
| 146 | + promise(result) |
| 147 | + } |
| 148 | + self.store.onAction(action) |
| 149 | + } |
| 150 | + |
| 151 | + // Then |
| 152 | + let purchaseResult = try XCTUnwrap(result.get()) |
| 153 | + guard case let .success(verificationResult) = purchaseResult, |
| 154 | + case let .verified(transaction) = verificationResult else { |
| 155 | + return XCTFail() |
| 156 | + } |
| 157 | + XCTAssertEqual(transaction.environment, .xcode) |
| 158 | + } |
| 159 | + |
| 160 | + func test_purchase_product_handles_api_errors() throws { |
| 161 | + // Given |
| 162 | + network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "error-wp-rest-forbidden") |
| 163 | + |
| 164 | + // When |
| 165 | + let result = waitFor { promise in |
| 166 | + let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in |
| 167 | + promise(result) |
| 168 | + } |
| 169 | + self.store.onAction(action) |
| 170 | + } |
| 171 | + |
| 172 | + // Then |
| 173 | + XCTAssert(result.isFailure) |
| 174 | + let error = try XCTUnwrap(result.failure) |
| 175 | + XCTAssert(error is WordPressApiError) |
| 176 | + } |
| 177 | + |
| 178 | + func test_user_is_entitled_to_product_returns_false_when_not_entitled() throws { |
| 179 | + // Given |
| 180 | + |
| 181 | + // When |
| 182 | + let result = waitFor { promise in |
| 183 | + let action = InAppPurchaseAction.userIsEntitledToProduct(productID: self.sampleProductID) { result in |
| 184 | + promise(result) |
| 185 | + } |
| 186 | + self.store.onAction(action) |
| 187 | + } |
| 188 | + |
| 189 | + // Then |
| 190 | + let isEntitled = try XCTUnwrap(result.get()) |
| 191 | + XCTAssertFalse(isEntitled) |
| 192 | + } |
| 193 | + |
| 194 | + func test_user_is_entitled_to_product_returns_true_when_entitled() throws { |
| 195 | + // Given |
| 196 | + try storeKitSession.buyProduct(productIdentifier: sampleProductID) |
| 197 | + |
| 198 | + // When |
| 199 | + let result = waitFor { promise in |
| 200 | + let action = InAppPurchaseAction.userIsEntitledToProduct(productID: self.sampleProductID) { result in |
| 201 | + promise(result) |
| 202 | + } |
| 203 | + self.store.onAction(action) |
| 204 | + } |
| 205 | + |
| 206 | + // Then |
| 207 | + let isEntitled = try XCTUnwrap(result.get()) |
| 208 | + XCTAssertTrue(isEntitled) |
| 209 | + } |
| 210 | +} |
0 commit comments