Files
purchases-ios/Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift
T
NachoSoto 948314cf50 StoreKitConfigTestCase: delete Bundle.appStoreReceiptURL after every test (#1700)
Equivalent to #1671 but for unit tests.

In iOS 16 (as of beta 1), the receipt persists across tests, despite the use of `SKTestSession.resetToDefaultState`.
This might also help with flaky tests pre-iOS 16 too.

Also note that I fixed this method in `BaseBackendIntegrationTests`. Using `URL.absoluteString` was incorrect there, so the file was probably never deleted.

I filed a feedback (FB10252944) for this:
![Screen Shot 2022-06-14 at 11 27 13](https://user-images.githubusercontent.com/685609/173662482-edae8bb9-48a2-432a-9823-c07a46e8861a.png)
2022-06-14 15:02:08 -07:00

97 lines
2.9 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// BaseBackendIntegrationTests.swift
//
// Created by Nacho Soto on 4/1/22.
@testable import RevenueCat
import XCTest
final class TestPurchaseDelegate: NSObject, PurchasesDelegate {
var customerInfo: CustomerInfo?
var customerInfoUpdateCount = 0
func purchases(_ purchases: Purchases, receivedUpdated customerInfo: CustomerInfo) {
self.customerInfo = customerInfo
customerInfoUpdateCount += 1
}
}
class BaseBackendIntegrationTests: XCTestCase {
private var userDefaults: UserDefaults!
// swiftlint:disable:next weak_delegate
private(set) var purchasesDelegate: TestPurchaseDelegate!
class var storeKit2Setting: StoreKit2Setting {
return .default
}
override func setUp() async throws {
try await super.setUp()
// Avoid continuing with potentially bad data after a failed assertion
self.continueAfterFailure = false
guard Constants.apiKey != "REVENUECAT_API_KEY", Constants.proxyURL != "REVENUECAT_PROXY_URL" else {
XCTFail("Must set configuration in `Constants.swift`")
throw ErrorCode.configurationError
}
userDefaults = UserDefaults(suiteName: Constants.userDefaultsSuiteName)
userDefaults?.removePersistentDomain(forName: Constants.userDefaultsSuiteName)
if !Constants.proxyURL.isEmpty {
Purchases.proxyURL = URL(string: Constants.proxyURL)
}
self.clearReceiptIfExists()
self.configurePurchases()
}
override func tearDown() {
Purchases.clearSingleton()
super.tearDown()
}
}
private extension BaseBackendIntegrationTests {
func clearReceiptIfExists() {
let manager = FileManager.default
guard let url = Bundle.main.appStoreReceiptURL, manager.fileExists(atPath: url.path) else { return }
do {
try manager.removeItem(at: url)
} catch {
Logger.appleWarning("Error attempting to remove receipt URL '\(url)': \(error)")
}
}
func configurePurchases() {
purchasesDelegate = TestPurchaseDelegate()
Purchases.configure(withAPIKey: Constants.apiKey,
appUserID: nil,
observerMode: false,
userDefaults: userDefaults,
storeKit2Setting: Self.storeKit2Setting,
storeKitTimeout: Configuration.storeKitRequestTimeoutDefault,
networkTimeout: Configuration.networkTimeoutDefault,
dangerousSettings: nil)
Purchases.logLevel = .debug
Purchases.shared.delegate = purchasesDelegate
}
}