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-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
|
|
|
|
|
|
|
|
final class IAPManagerTests: XCTestCase {
|
|
|
|
// private let inApp = MockAppProductHelper()
|
|
|
|
|
|
|
|
private let olderBuildNumber = 500
|
|
|
|
|
|
|
|
private let defaultBuildNumber = 1000
|
|
|
|
|
|
|
|
private let newerBuildNumber = 1500
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
extension IAPManagerTests {
|
|
|
|
|
|
|
|
// MARK: Build products
|
|
|
|
|
|
|
|
func test_givenBuildProducts_whenOlder_thenFullVersion() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenBuildProducts_whenNewer_thenFreeVersion() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Eligibility
|
|
|
|
|
|
|
|
func test_givenPurchasedFeature_whenReloadReceipt_thenIsEligible() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(receiptReader: reader)
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.allPlatforms])
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPurchasedFeatures_thenIsOnlyEligibleForFeatures() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [
|
|
|
|
.Features.siriShortcuts,
|
|
|
|
.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))
|
|
|
|
XCTAssertTrue(sut.isEligible(for: .siri))
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPurchasedAndCancelledFeature_thenIsNotEligible() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFreeVersion_thenIsNotEligibleForAnyFeature() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
AppFeature.allButAppleTV.forEach {
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFreeVersion_thenIsNotEligibleForAppleTV() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFullVersion_thenIsEligibleForAnyFeatureExceptAppleTV() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
AppFeature.allButAppleTV.forEach {
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
XCTAssertFalse(sut.isEligible(for: .appleTV))
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenAppleTV_thenIsEligibleForAppleTV() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPlatformVersion_thenIsFullVersionForPlatform() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.iOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenPlatformVersion_thenIsNotFullVersionForOtherPlatform() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
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-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
|
|
|
await reader.setReceipt(withBuild: defaultBuildNumber, products: [.Full.macOS, .Features.networkSettings])
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-10-01 08:17:20 +00:00
|
|
|
// MARK: Purchasable
|
|
|
|
|
2024-10-04 08:30:14 +00:00
|
|
|
// FIXME: #570, test app library
|
2024-10-04 07:58:42 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
// MARK: App level
|
|
|
|
|
|
|
|
func test_givenBetaApp_thenIsRestricted() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.userLevel.isRestricted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenBetaApp_thenIsNotEligibleForAnyFeature() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
XCTAssertFalse(sut.isEligible(for: AppFeature.allButAppleTV))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
func test_givenBetaApp_thenIsEligibleForUnrestrictedFeature() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-10-02 14:05:40 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .beta, receiptReader: reader, unrestrictedFeatures: [.onDemand])
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
AppFeature.allButAppleTV.forEach {
|
2024-10-02 14:05:40 +00:00
|
|
|
if $0 == .onDemand {
|
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
} else {
|
|
|
|
XCTAssertFalse(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
func test_givenFullApp_thenIsFullVersion() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullVersion, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.userLevel.isFullVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFullPlusTVApp_thenIsFullVersion() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullVersionPlusTV, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
|
|
|
XCTAssertTrue(sut.userLevel.isFullVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFullApp_thenIsEligibleForAnyFeatureExceptAppleTV() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullVersion, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
AppFeature.allButAppleTV.forEach {
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertTrue(sut.isEligible(for: $0))
|
|
|
|
}
|
|
|
|
XCTAssertFalse(sut.isEligible(for: .appleTV))
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenFullPlusTVApp_thenIsEligibleForAnyFeature() async {
|
2024-11-01 22:32:35 +00:00
|
|
|
let reader = MockAppReceiptReader()
|
2024-09-23 13:02:26 +00:00
|
|
|
let sut = IAPManager(customUserLevel: .fullVersionPlusTV, receiptReader: reader)
|
|
|
|
|
|
|
|
await sut.reloadReceipt()
|
2024-11-06 12:20:12 +00:00
|
|
|
AppFeature.allButAppleTV.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
|
|
|
|
|
|
|
private extension IAPManager {
|
|
|
|
convenience init(
|
|
|
|
customUserLevel: AppUserLevel? = nil,
|
2024-11-06 12:20:12 +00:00
|
|
|
receiptReader: AppReceiptReader,
|
2024-11-05 17:55:57 +00:00
|
|
|
unrestrictedFeatures: Set<AppFeature> = [],
|
|
|
|
productsAtBuild: BuildProducts<AppProduct>? = nil
|
|
|
|
) {
|
|
|
|
self.init(
|
|
|
|
customUserLevel: customUserLevel,
|
|
|
|
inAppHelper: MockAppProductHelper(),
|
|
|
|
receiptReader: receiptReader,
|
|
|
|
unrestrictedFeatures: unrestrictedFeatures,
|
|
|
|
productsAtBuild: productsAtBuild
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|