2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// IAPManagerTests.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/12/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
import Combine
|
2024-11-20 17:05:47 +00:00
|
|
|
@testable import CommonIAP
|
2024-11-03 12:16:13 +00:00
|
|
|
@testable import CommonLibrary
|
2024-11-05 17:55:57 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import XCTest
|
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
@MainActor
|
2024-09-23 13:02:26 +00:00
|
|
|
final class IAPManagerTests: XCTestCase {
|
|
|
|
private let olderBuildNumber = 500
|
|
|
|
|
|
|
|
private let defaultBuildNumber = 1000
|
|
|
|
|
|
|
|
private let newerBuildNumber = 1500
|
2024-11-21 17:57:32 +00:00
|
|
|
|
|
|
|
private var subscriptions: Set<AnyCancellable> = []
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
// MARK: - Actions
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
extension IAPManagerTests {
|
2024-11-21 17:57:32 +00:00
|
|
|
func test_givenProducts_whenFetchAppProducts_thenReturnsCorrespondingInAppProducts() async throws {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
let appProducts: [AppProduct] = [
|
|
|
|
.Full.iOS,
|
|
|
|
.Donations.huge
|
|
|
|
]
|
|
|
|
let inAppProducts = try await sut.purchasableProducts(for: appProducts)
|
|
|
|
inAppProducts.enumerated().forEach {
|
|
|
|
XCTAssertEqual($0.element.productIdentifier, appProducts[$0.offset].rawValue)
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
func test_givenProducts_whenPurchase_thenIsAddedToPurchasedProducts() async throws {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, products: [])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
let appleTV: AppProduct = .Features.appleTV
|
|
|
|
XCTAssertFalse(sut.purchasedProducts.contains(appleTV))
|
|
|
|
do {
|
|
|
|
let purchasable = try await sut.purchasableProducts(for: [appleTV])
|
|
|
|
let purchasableAppleTV = try XCTUnwrap(purchasable.first)
|
|
|
|
let result = try await sut.purchase(purchasableAppleTV)
|
|
|
|
if result == .done {
|
|
|
|
XCTAssertTrue(sut.purchasedProducts.contains(appleTV))
|
|
|
|
} else {
|
|
|
|
XCTFail("Unexpected purchase() result: \(result)")
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
XCTFail("Unexpected purchase() failure: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Build products
|
|
|
|
|
|
|
|
extension IAPManagerTests {
|
2024-09-23 13:02:26 +00:00
|
|
|
func test_givenBuildProducts_whenOlder_thenFullVersion() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-06 12:20:12 +00:00
|
|
|
await reader.setReceipt(withBuild: olderBuildNumber, identifiers: [])
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(receiptReader: reader) { build in
|
|
|
|
if build <= self.defaultBuildNumber {
|
|
|
|
return [.Full.allPlatforms]
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenBuildProducts_whenNewer_thenFreeVersion() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: newerBuildNumber, products: [])
|
|
|
|
let sut = IAPManager(receiptReader: reader) { build in
|
|
|
|
if build <= self.defaultBuildNumber {
|
|
|
|
return [.Full.allPlatforms]
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-21 17:57:32 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
// MARK: - Eligibility
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
extension IAPManagerTests {
|
2024-09-23 13:02:26 +00:00
|
|
|
func test_givenPurchasedFeature_whenReloadReceipt_thenIsEligible() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.allPlatforms])
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPurchasedFeatures_thenIsOnlyEligibleForFeatures() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [
|
|
|
|
.Features.networkSettings
|
|
|
|
])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-10-03 10:13:03 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: .dns))
|
|
|
|
XCTAssertTrue(sut.isEligible(for: .httpProxy))
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: .onDemand))
|
2024-10-03 10:13:03 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: .routing))
|
2024-11-09 14:20:59 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: .sharing))
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPurchasedAndCancelledFeature_thenIsNotEligible() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(
|
|
|
|
withBuild: defaultBuildNumber,
|
|
|
|
products: [.Full.allPlatforms],
|
|
|
|
cancelledProducts: [.Full.allPlatforms]
|
|
|
|
)
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFreeVersion_thenIsNotEligibleForAnyFeature() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertFalse(sut.userLevel.isFullVersion)
|
2024-11-11 19:10:27 +00:00
|
|
|
AppFeature.fullV2Features.forEach {
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFreeVersion_thenIsNotEligibleForAppleTV() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertFalse(sut.isEligible(for: .appleTV))
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
func test_givenFullV2Version_thenIsEligibleForAnyFeatureExceptExcluded() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.allPlatforms])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
let excluded: Set<AppFeature> = [
|
|
|
|
.appleTV,
|
|
|
|
.interactiveLogin
|
|
|
|
]
|
|
|
|
AppFeature.allCases.forEach {
|
|
|
|
if AppFeature.fullV2Features.contains($0) {
|
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
} else {
|
|
|
|
XCTAssertTrue(excluded.contains($0))
|
|
|
|
XCTAssertFalse(sut.isEligible(for: $0))
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
func test_givenAppleTV_thenIsEligibleForAppleTVAndSharing() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Features.appleTV])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.isEligible(for: .appleTV))
|
2024-11-21 17:57:32 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: .sharing))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPlatformVersion_thenIsFullVersionForPlatform() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.macOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.iOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPlatformVersion_thenIsNotFullVersionForOtherPlatform() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.iOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.macOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.fullV2Features))
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
func test_givenUser_thenIsNotEligibleForFeedback() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
XCTAssertFalse(sut.isEligibleForFeedback())
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenBeta_thenIsEligibleForFeedback() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, identifiers: [])
|
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.isEligibleForFeedback())
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPayingUser_thenIsEligibleForFeedback() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, products: [.Full.iOS])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.isEligibleForFeedback())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - App level
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
extension IAPManagerTests {
|
2024-09-23 13:02:26 +00:00
|
|
|
func test_givenBetaApp_thenIsRestricted() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-21 17:57:32 +00:00
|
|
|
XCTAssertTrue(sut.isRestricted)
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertTrue(sut.userLevel.isRestricted)
|
|
|
|
}
|
|
|
|
|
2024-11-10 15:53:01 +00:00
|
|
|
func test_givenBetaApp_thenIsNotEligibleForAllFeatures() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-10 15:53:01 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allCases))
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenBetaApp_thenIsEligibleForUserLevelFeatures() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-10 15:53:01 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
|
|
|
|
let eligible = AppUserLevel.beta.features
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.isEligible(for: eligible))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
func test_givenBetaApp_thenIsEligibleForUnrestrictedFeature() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-10-02 14:05:40 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader, unrestrictedFeatures: [.onDemand])
|
|
|
|
|
2024-11-10 15:53:01 +00:00
|
|
|
var eligible = AppUserLevel.beta.features
|
|
|
|
eligible.append(.onDemand)
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
await sut.reloadReceipt()
|
2024-11-10 15:53:01 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: eligible))
|
2024-10-02 14:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
func test_givenFullV2App_thenIsFullVersion() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-11 19:10:27 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullV2, receiptReader: reader)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.userLevel.isFullVersion)
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
func test_givenSubscriberApp_thenIsFullVersion() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-11 19:10:27 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .subscriber, receiptReader: reader)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.userLevel.isFullVersion)
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
func test_givenFullV2App_thenIsEligibleForAnyFeatureExceptExcluded() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-11 19:10:27 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullV2, receiptReader: reader)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
let excluded: Set<AppFeature> = [
|
|
|
|
.appleTV,
|
|
|
|
.interactiveLogin
|
|
|
|
]
|
|
|
|
AppFeature.allCases.forEach {
|
|
|
|
if AppFeature.fullV2Features.contains($0) {
|
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
} else {
|
|
|
|
XCTAssertTrue(excluded.contains($0))
|
|
|
|
XCTAssertFalse(sut.isEligible(for: $0))
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:10:27 +00:00
|
|
|
func test_givenSubscriberApp_thenIsEligibleForAnyFeature() async {
|
2024-11-20 17:05:47 +00:00
|
|
|
let reader = FakeAppReceiptReader()
|
2024-11-11 19:10:27 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .subscriber, receiptReader: reader)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-11 19:10:27 +00:00
|
|
|
AppFeature.fullV2Features.forEach {
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
XCTAssertTrue(sut.isEligible(for: .appleTV))
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 17:55:57 +00:00
|
|
|
|
2024-11-21 17:57:32 +00:00
|
|
|
// MARK: - Beta
|
|
|
|
|
|
|
|
extension IAPManagerTests {
|
|
|
|
func test_givenChecker_whenReloadReceipt_thenIsBeta() async {
|
|
|
|
let betaChecker = MockBetaChecker()
|
|
|
|
betaChecker.isBeta = true
|
|
|
|
let sut = IAPManager(receiptReader: FakeAppReceiptReader(), betaChecker: betaChecker)
|
|
|
|
XCTAssertEqual(sut.userLevel, .undefined)
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertEqual(sut.userLevel, .beta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Receipts
|
|
|
|
|
|
|
|
extension IAPManagerTests {
|
|
|
|
func test_givenReceipts_whenReloadReceipt_thenPublishesEligibleFeatures() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, products: [
|
|
|
|
.Features.appleTV,
|
|
|
|
.Features.trustedNetworks
|
|
|
|
])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
let exp = expectation(description: "Eligible features")
|
|
|
|
sut
|
|
|
|
.$eligibleFeatures
|
|
|
|
.dropFirst()
|
|
|
|
.sink { _ in
|
|
|
|
exp.fulfill()
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
await fulfillment(of: [exp], timeout: 0.1)
|
|
|
|
|
|
|
|
XCTAssertEqual(sut.eligibleFeatures, [
|
|
|
|
.appleTV,
|
|
|
|
.onDemand,
|
|
|
|
.sharing // implied by Apple TV purchase
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenInvalidReceipts_whenReloadReceipt_thenSkipsInvalid() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, products: [])
|
|
|
|
await reader.addPurchase(with: "foobar")
|
|
|
|
await reader.addPurchase(with: .Features.allProviders, expirationDate: Date().addingTimeInterval(-10))
|
|
|
|
await reader.addPurchase(with: .Features.appleTV)
|
|
|
|
await reader.addPurchase(with: .Features.networkSettings, expirationDate: Date().addingTimeInterval(10))
|
|
|
|
await reader.addPurchase(with: .Full.iOS, cancellationDate: Date().addingTimeInterval(-60))
|
|
|
|
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
|
|
|
|
XCTAssertEqual(sut.eligibleFeatures, [
|
|
|
|
.appleTV,
|
|
|
|
.dns,
|
|
|
|
.httpProxy,
|
|
|
|
.routing,
|
|
|
|
.sharing
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Observation
|
|
|
|
|
|
|
|
extension IAPManagerTests {
|
|
|
|
func test_givenManager_whenObserveObjects_thenReloadsReceipt() async {
|
|
|
|
let reader = FakeAppReceiptReader()
|
|
|
|
await reader.setReceipt(withBuild: .max, products: [.Full.allPlatforms])
|
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
|
|
|
XCTAssertEqual(sut.userLevel, .undefined)
|
|
|
|
XCTAssertTrue(sut.eligibleFeatures.isEmpty)
|
|
|
|
|
|
|
|
let exp = expectation(description: "Reload receipt")
|
|
|
|
sut
|
|
|
|
.$eligibleFeatures
|
|
|
|
.dropFirst()
|
|
|
|
.sink { _ in
|
|
|
|
exp.fulfill()
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
sut.observeObjects()
|
|
|
|
await fulfillment(of: [exp], timeout: 0.1)
|
|
|
|
|
|
|
|
XCTAssertNotEqual(sut.userLevel, .undefined)
|
|
|
|
XCTAssertFalse(sut.eligibleFeatures.isEmpty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
2024-11-05 17:55:57 +00:00
|
|
|
private extension IAPManager {
|
|
|
|
convenience init(
|
|
|
|
customUserLevel: AppUserLevel? = nil,
|
2024-11-21 17:57:32 +00:00
|
|
|
inAppHelper: (any AppProductHelper)? = nil,
|
2024-11-06 12:20:12 +00:00
|
|
|
receiptReader: AppReceiptReader,
|
2024-11-21 17:57:32 +00:00
|
|
|
betaChecker: BetaChecker? = nil,
|
2024-11-05 17:55:57 +00:00
|
|
|
unrestrictedFeatures: Set<AppFeature> = [],
|
|
|
|
productsAtBuild: BuildProducts<AppProduct>? = nil
|
|
|
|
) {
|
|
|
|
self.init(
|
|
|
|
customUserLevel: customUserLevel,
|
2024-11-21 17:57:32 +00:00
|
|
|
inAppHelper: inAppHelper ?? FakeAppProductHelper(),
|
2024-11-05 17:55:57 +00:00
|
|
|
receiptReader: receiptReader,
|
2024-11-21 17:57:32 +00:00
|
|
|
betaChecker: betaChecker ?? TestFlightChecker(),
|
2024-11-05 17:55:57 +00:00
|
|
|
unrestrictedFeatures: unrestrictedFeatures,
|
|
|
|
productsAtBuild: productsAtBuild
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|