-
Notifications
You must be signed in to change notification settings - Fork 121
[In-app Purchases] Add unit tests for InAppPurchaseStore #8067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| [ | ||
| "woocommerce_entry_monthly", | ||
| "woocommerce_entry_yearly" | ||
| "debug.woocommerce.ecommerce.monthly" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "identifier" : "28D2E099", | ||
| "nonRenewingSubscriptions" : [ | ||
|
|
||
| ], | ||
| "products" : [ | ||
|
|
||
| ], | ||
| "settings" : { | ||
| "_applicationInternalID" : "1389130815", | ||
| "_developerTeamID" : "PZYM8XX95Q", | ||
| "_lastSynchronizedDate" : 689685986.85126996 | ||
| }, | ||
| "subscriptionGroups" : [ | ||
| { | ||
| "id" : "21032762", | ||
| "localizations" : [ | ||
|
|
||
| ], | ||
| "name" : "test_subscription_group", | ||
| "subscriptions" : [ | ||
| { | ||
| "adHocOffers" : [ | ||
|
|
||
| ], | ||
| "codeOffers" : [ | ||
|
|
||
| ], | ||
| "displayPrice" : "69.99", | ||
| "familyShareable" : false, | ||
| "groupNumber" : 1, | ||
| "internalID" : "1650562345", | ||
| "introductoryOffer" : null, | ||
| "localizations" : [ | ||
| { | ||
| "description" : "1 Month of Debug Woo", | ||
| "displayName" : "Debug Monthly", | ||
| "locale" : "en_US" | ||
| } | ||
| ], | ||
| "productID" : "debug.woocommerce.ecommerce.monthly", | ||
| "recurringSubscriptionPeriod" : "P1M", | ||
| "referenceName" : "Debug Monthly", | ||
| "subscriptionGroupID" : "21032762", | ||
| "type" : "RecurringSubscription" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "version" : { | ||
| "major" : 2, | ||
| "minor" : 0 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
WooCommerce/WooCommerceTests/Yosemite/InAppPurchaseStoreTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import XCTest | ||
| import TestKit | ||
| import StoreKitTest | ||
|
|
||
| @testable import Yosemite | ||
| @testable import Networking | ||
|
|
||
| final class InAppPurchaseStoreTests: XCTestCase { | ||
|
|
||
| /// Mock Network: Allows us to inject predefined responses! | ||
| /// | ||
| private var network: MockNetwork! | ||
|
|
||
| /// Mock Storage: InMemory | ||
| /// | ||
| private var storageManager: MockStorageManager! | ||
|
|
||
| private var storeKitSession = try! SKTestSession(configurationFileNamed: "WooCommerce") | ||
|
|
||
| /// Testing SiteID | ||
| /// | ||
| private let sampleSiteID: Int64 = 123 | ||
|
|
||
| /// Testing Product ID | ||
| /// Should match the product ID in WooCommerce.storekit | ||
| /// | ||
| private let sampleProductID: String = "debug.woocommerce.ecommerce.monthly" | ||
|
|
||
| /// Testing Order ID | ||
| /// Should match the order ID in iap-order-create.json | ||
| /// | ||
| private let sampleOrderID: Int64 = 12345 | ||
|
|
||
| var store: InAppPurchaseStore! | ||
|
|
||
|
|
||
| override func setUp() { | ||
| network = MockNetwork(useResponseQueue: true) | ||
| storageManager = MockStorageManager() | ||
| store = InAppPurchaseStore(dispatcher: Dispatcher(), storageManager: storageManager, network: network) | ||
| storeKitSession.disableDialogs = true | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| storeKitSession.resetToDefaultState() | ||
| storeKitSession.clearTransactions() | ||
| } | ||
|
|
||
| func test_iap_supported_in_us() throws { | ||
| // Given | ||
| storeKitSession.storefront = "USA" | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.inAppPurchasesAreSupported { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| XCTAssertTrue(result) | ||
| } | ||
|
|
||
| func test_iap_supported_in_canada() throws { | ||
| // Given | ||
| storeKitSession.storefront = "CAN" | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.inAppPurchasesAreSupported { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| XCTAssertFalse(result) | ||
| } | ||
|
|
||
| func test_load_products_loads_products_response() throws { | ||
| // Given | ||
| network.simulateResponse(requestUrlSuffix: "iap/products", filename: "iap-products") | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.loadProducts { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| let products = try XCTUnwrap(result.get()) | ||
| XCTAssertFalse(products.isEmpty) | ||
| XCTAssertEqual(products.first?.id, sampleProductID) | ||
| } | ||
|
|
||
| func test_load_products_fails_if_iap_unsupported() throws { | ||
| // Given | ||
| storeKitSession.storefront = "CAN" | ||
| network.simulateResponse(requestUrlSuffix: "iap/products", filename: "iap-products") | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.loadProducts { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| XCTAssert(result.isFailure) | ||
| } | ||
|
|
||
| func test_purchase_product_completes_purchase() throws { | ||
| // Given | ||
| network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "iap-order-create") | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| let purchaseResult = try XCTUnwrap(result.get()) | ||
| guard case let .success(verificationResult) = purchaseResult, | ||
| case let .verified(transaction) = verificationResult else { | ||
| return XCTFail() | ||
| } | ||
| XCTAssertEqual(transaction.productID, sampleProductID) | ||
| XCTAssertNotNil(transaction.appAccountToken) | ||
| } | ||
|
|
||
| @available(iOS 16.0, *) | ||
| func test_purchase_product_ensure_xcode_environment() throws { | ||
| // Given | ||
| network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "iap-order-create") | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| let purchaseResult = try XCTUnwrap(result.get()) | ||
| guard case let .success(verificationResult) = purchaseResult, | ||
| case let .verified(transaction) = verificationResult else { | ||
| return XCTFail() | ||
| } | ||
| XCTAssertEqual(transaction.environment, .xcode) | ||
| } | ||
|
|
||
| func test_purchase_product_handles_api_errors() throws { | ||
| // Given | ||
| network.simulateResponse(requestUrlSuffix: "iap/orders", filename: "error-wp-rest-forbidden") | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.purchaseProduct(siteID: self.sampleSiteID, productID: self.sampleProductID) { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| XCTAssert(result.isFailure) | ||
| let error = try XCTUnwrap(result.failure) | ||
| XCTAssert(error is WordPressApiError) | ||
| } | ||
|
|
||
| func test_user_is_entitled_to_product_returns_false_when_not_entitled() throws { | ||
| // Given | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.userIsEntitledToProduct(productID: self.sampleProductID) { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| let isEntitled = try XCTUnwrap(result.get()) | ||
| XCTAssertFalse(isEntitled) | ||
| } | ||
|
|
||
| func test_user_is_entitled_to_product_returns_true_when_entitled() throws { | ||
| // Given | ||
| try storeKitSession.buyProduct(productIdentifier: sampleProductID) | ||
|
|
||
| // When | ||
| let result = waitFor { promise in | ||
| let action = InAppPurchaseAction.userIsEntitledToProduct(productID: self.sampleProductID) { result in | ||
| promise(result) | ||
| } | ||
| self.store.onAction(action) | ||
| } | ||
|
|
||
| // Then | ||
| let isEntitled = try XCTUnwrap(result.get()) | ||
| XCTAssertTrue(isEntitled) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having second thoughts about this. I added the StoreKit configuration with sync enabled, but if we're only going to use this for testing, maybe I should convert to local and ensure it only has the debug product? I'm not sure we want the whole product catalog synced to a public repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if it's only for unit testing I would convert it to local and only add the debug product, no need to have the whole product catalog even if I don't see huge security issues with that.