Skip to content
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

Subscription feature tests #3352

Merged
merged 21 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Core/DailyPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class DailyPixel {

}

private static let storage: UserDefaults = UserDefaults(suiteName: Constant.dailyPixelStorageIdentifier)!
public static let storage: UserDefaults = UserDefaults(suiteName: Constant.dailyPixelStorageIdentifier)!

/// Sends a given Pixel once per day.
/// This is useful in situations where pixels receive spikes in volume, as the daily pixel can be used to determine how many users are actually affected.
Expand Down
22 changes: 20 additions & 2 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
1E4DCF4A27B6A38000961E25 /* DownloadListRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4DCF4927B6A38000961E25 /* DownloadListRepresentable.swift */; };
1E4DCF4C27B6A4CB00961E25 /* URLFileExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4DCF4B27B6A4CB00961E25 /* URLFileExtension.swift */; };
1E4DCF4E27B6A69600961E25 /* DownloadsListHostingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4DCF4D27B6A69600961E25 /* DownloadsListHostingController.swift */; };
1E4E6C552C775B400059C0FA /* Subscription.storekit in Resources */ = {isa = PBXBuildFile; fileRef = D664C7952B289AA000CBFA76 /* Subscription.storekit */; };
1E4E6C572C78B8540059C0FA /* StorePurchaseManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4E6C562C78B8540059C0FA /* StorePurchaseManagerTests.swift */; };
1E4F4A5A297193DE00625985 /* MainViewController+CookiesManaged.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4F4A59297193DE00625985 /* MainViewController+CookiesManaged.swift */; };
1E4FAA6427D8DFB900ADC5B3 /* OngoingDownloadRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4FAA6327D8DFB900ADC5B3 /* OngoingDownloadRowViewModel.swift */; };
1E4FAA6627D8DFC800ADC5B3 /* CompleteDownloadRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4FAA6527D8DFC800ADC5B3 /* CompleteDownloadRowViewModel.swift */; };
Expand Down Expand Up @@ -100,6 +102,7 @@
1E9529A12C4E748B006E80D4 /* UINavigationControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9529A02C4E748B006E80D4 /* UINavigationControllerExtension.swift */; };
1EA51376286596A000493C6A /* PrivacyIconLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EA51375286596A000493C6A /* PrivacyIconLogic.swift */; };
1EA513782866039400493C6A /* TrackerAnimationLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EA513772866039400493C6A /* TrackerAnimationLogic.swift */; };
1EAABE712C99FC75003F5137 /* SubscriptionFeatureAvailabilityMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E25D5312C92126B004400F0 /* SubscriptionFeatureAvailabilityMock.swift */; };
1EC458462948932500CB2B13 /* UIHostingControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EC458452948932500CB2B13 /* UIHostingControllerExtension.swift */; };
1EDE39D22705D4A200C99C72 /* FileSizeDebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EDE39D12705D4A100C99C72 /* FileSizeDebugViewController.swift */; };
1EE411F12857C3640003FE64 /* TrackerAnimationImageProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EE411F02857C3640003FE64 /* TrackerAnimationImageProvider.swift */; };
Expand Down Expand Up @@ -1355,11 +1358,13 @@
1E1D8B6929953CE300C96994 /* autoconsent-test-page-banner.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "autoconsent-test-page-banner.html"; sourceTree = "<group>"; };
1E24295D293F57FA00584836 /* LottieView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LottieView.swift; sourceTree = "<group>"; };
1E24295F293F585300584836 /* cookie-icon-animated-40-light.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "cookie-icon-animated-40-light.json"; sourceTree = "<group>"; };
1E25D5312C92126B004400F0 /* SubscriptionFeatureAvailabilityMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionFeatureAvailabilityMock.swift; sourceTree = "<group>"; };
1E4DCF4527B6A33600961E25 /* DownloadsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadsListViewModel.swift; sourceTree = "<group>"; };
1E4DCF4727B6A35400961E25 /* DownloadsListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadsListModel.swift; sourceTree = "<group>"; };
1E4DCF4927B6A38000961E25 /* DownloadListRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadListRepresentable.swift; sourceTree = "<group>"; };
1E4DCF4B27B6A4CB00961E25 /* URLFileExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLFileExtension.swift; sourceTree = "<group>"; };
1E4DCF4D27B6A69600961E25 /* DownloadsListHostingController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadsListHostingController.swift; sourceTree = "<group>"; };
1E4E6C562C78B8540059C0FA /* StorePurchaseManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorePurchaseManagerTests.swift; sourceTree = "<group>"; };
1E4F4A59297193DE00625985 /* MainViewController+CookiesManaged.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MainViewController+CookiesManaged.swift"; sourceTree = "<group>"; };
1E4FAA6327D8DFB900ADC5B3 /* OngoingDownloadRowViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OngoingDownloadRowViewModel.swift; sourceTree = "<group>"; };
1E4FAA6527D8DFC800ADC5B3 /* CompleteDownloadRowViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompleteDownloadRowViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3312,6 +3317,14 @@
name = Autoconsent;
sourceTree = "<group>";
};
1E25D5302C921246004400F0 /* Mocks */ = {
isa = PBXGroup;
children = (
1E25D5312C92126B004400F0 /* SubscriptionFeatureAvailabilityMock.swift */,
);
name = Mocks;
sourceTree = "<group>";
};
1E4DCF4227B6A29D00961E25 /* View */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -5296,7 +5309,6 @@
D60170BB2BA32DD6001911B5 /* Subscription.swift */,
1E53508E2C7C9A1F00818DAA /* DefaultSubscriptionManager+AccountManagerKeychainAccessDelegate.swift */,
D6D95CE42B6DA3F200960317 /* AsyncHeadlessWebview */,
D664C7952B289AA000CBFA76 /* Subscription.storekit */,
D664C7932B289AA000CBFA76 /* ViewModel */,
D664C7AC2B289AA000CBFA76 /* Views */,
D664C7B02B289AA000CBFA76 /* UserScripts */,
Expand Down Expand Up @@ -6082,10 +6094,13 @@
F1BDDBFC2C340D9C00459306 /* Subscription */ = {
isa = PBXGroup;
children = (
1E25D5302C921246004400F0 /* Mocks */,
D664C7952B289AA000CBFA76 /* Subscription.storekit */,
BDE219E92C457B46005D5884 /* PrivacyProDataReporterTests.swift */,
F1BDDBF92C340D9C00459306 /* SubscriptionContainerViewModelTests.swift */,
F1BDDBFA2C340D9C00459306 /* SubscriptionFlowViewModelTests.swift */,
F1BDDBFB2C340D9C00459306 /* SubscriptionPagesUseSubscriptionFeatureTests.swift */,
1E4E6C562C78B8540059C0FA /* StorePurchaseManagerTests.swift */,
);
path = Subscription;
sourceTree = "<group>";
Expand Down Expand Up @@ -7018,6 +7033,7 @@
files = (
EA39B7E2268A1A35000C62CD /* privacy-reference-tests in Resources */,
8524092D2C77EF7A00CB28FC /* mobile_segments_test_cases.json in Resources */,
1E4E6C552C775B400059C0FA /* Subscription.storekit in Resources */,
F17843E91F36226700390DCD /* MockFiles in Resources */,
8572298A2BBEF0C800E2E802 /* AppRatingPrompt_v1 in Resources */,
);
Expand Down Expand Up @@ -7899,6 +7915,7 @@
6FF915822B88E07A0042AC87 /* AdAttributionFetcherTests.swift in Sources */,
9F4CC51B2C48C0C7006A96EB /* MockTabDelegate.swift in Sources */,
85E065C22C73ADE600D73E2A /* UsageSegmentationTests.swift in Sources */,
1E4E6C572C78B8540059C0FA /* StorePurchaseManagerTests.swift in Sources */,
1E722729292EB24D003B5F53 /* AppSettingsMock.swift in Sources */,
8536A1C8209AF2410050739E /* MockVariantManager.swift in Sources */,
C1B7B53428944EFA0098FD6A /* CoreDataTestUtilities.swift in Sources */,
Expand Down Expand Up @@ -7949,6 +7966,7 @@
BDFF03262BA3DA4900F324C9 /* NetworkProtectionFeatureVisibilityTests.swift in Sources */,
0283A2042C6E572F00508FBD /* BrokenSitePromptLimiterTests.swift in Sources */,
D62EC3BA2C246A7000FC9D04 /* YoutublePlayerNavigationHandlerTests.swift in Sources */,
1EAABE712C99FC75003F5137 /* SubscriptionFeatureAvailabilityMock.swift in Sources */,
8341D807212D5E8D000514C2 /* HashExtensionTest.swift in Sources */,
C1D21E2F293A599C006E5A05 /* AutofillLoginSessionTests.swift in Sources */,
85D2187924BF6B8B004373D2 /* FaviconSourcesProviderTests.swift in Sources */,
Expand Down Expand Up @@ -10909,7 +10927,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 196.1.0;
version = 196.2.0;
};
};
9F8FE9472BAE50E50071E372 /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "f7083a3c74a4aa1f6a0f4ab65265eb2f422a2cf0",
"version" : "196.1.0"
"revision" : "32a2ec64385543ccfbaaafbfe9545543a2c06aac",
"version" : "196.2.0"
}
},
{
Expand Down Expand Up @@ -138,7 +138,7 @@
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct NetworkProtectionFeatureVisibilityMocks: NetworkProtectionFeatureVisibili
init(with options: Options) {
self.options = options

let subscriptionAppGroup = Bundle.main.appGroup(bundle: .subs)
let subscriptionAppGroup = "NetworkProtectionFeatureVisibilityTests"
let subscriptionUserDefaults = UserDefaults(suiteName: subscriptionAppGroup)!
let subscriptionEnvironment = DefaultSubscriptionManager.getSavedOrDefaultEnvironment(userDefaults: subscriptionUserDefaults)
let entitlementsCache = UserDefaultsCache<[Entitlement]>(userDefaults: subscriptionUserDefaults,
Expand Down
190 changes: 190 additions & 0 deletions DuckDuckGoTests/Subscription/StorePurchaseManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//
// StorePurchaseManagerTests.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest
@testable import Subscription
import SubscriptionTestingUtilities
import StoreKitTest

final class StorePurchaseManagerTests: XCTestCase {

private struct Constants {
static let externalID = UUID().uuidString
static let monthlySubscriptionID = "ios.subscription.1month"
static let yearlySubscriptionID = "ios.subscription.1year"
}

var session: SKTestSession!
var storePurchaseManager: StorePurchaseManager!

override func setUpWithError() throws {
let path = Bundle.main.url(forResource: "Subscription", withExtension: "storekit")

session = try SKTestSession(contentsOf: path!)
session.resetToDefaultState()
session.disableDialogs = true
session.clearTransactions()

storePurchaseManager = DefaultStorePurchaseManager()
}

override func tearDownWithError() throws {
storePurchaseManager = nil
session = nil
}

func testSubscriptionOptionsWhenNoCachedProducts() async throws {
// When
let subscriptionOptions = await storePurchaseManager.subscriptionOptions()
miasma13 marked this conversation as resolved.
Show resolved Hide resolved

// Then
XCTAssertNil(subscriptionOptions)
XCTAssertFalse(storePurchaseManager.areProductsAvailable)
}

func testSubscriptionOptionsWhenAvailableProductsWereUpdated() async throws {
// Given
await storePurchaseManager.updateAvailableProducts()

// When
guard let subscriptionOptions = await storePurchaseManager.subscriptionOptions() else {
XCTFail("Expected subscription options")
return
}

// Then
XCTAssertEqual(subscriptionOptions.options.count, 2)
XCTAssertEqual(subscriptionOptions.features.count, SubscriptionFeatureName.allCases.count)
XCTAssertTrue(storePurchaseManager.areProductsAvailable)

let optionIDs = subscriptionOptions.options.map { $0.id }
XCTAssertTrue(optionIDs.contains(Constants.monthlySubscriptionID))
XCTAssertTrue(optionIDs.contains(Constants.yearlySubscriptionID))
}

func testHasActiveSubscriptionIsFalseWithoutPurchase() async throws {
// When
let hasActiveSubscription = await storePurchaseManager.hasActiveSubscription()

// Then
XCTAssertFalse(hasActiveSubscription)
}

func testPurchaseSubscription() async throws {
// Given
await storePurchaseManager.updateAvailableProducts()

XCTAssertEqual(storePurchaseManager.purchasedProductIDs, [])

// When
let result = await storePurchaseManager.purchaseSubscription(with: Constants.yearlySubscriptionID, externalID: Constants.externalID)

// Then
switch result {
case .success:
XCTAssertTrue(storePurchaseManager.purchaseQueue.isEmpty)
XCTAssertEqual(storePurchaseManager.purchasedProductIDs, [Constants.yearlySubscriptionID])

let transactions = await StoreKitHelpers.currentEntitlements()
XCTAssertEqual(transactions.count, 1)
XCTAssertEqual(transactions.first!.appAccountToken?.uuidString, Constants.externalID)

let hasActiveSubscription = await storePurchaseManager.hasActiveSubscription()
XCTAssertTrue(hasActiveSubscription)
case .failure:
XCTFail("Unexpected failure")
}
}

func testPurchaseSubscriptionFailureWithoutValidProductID() async throws {
// Given
await storePurchaseManager.updateAvailableProducts()

// When
let result = await storePurchaseManager.purchaseSubscription(with: "", externalID: Constants.externalID)

// Then
switch result {
case .success:
XCTFail("Unexpected success")
case .failure(let error):
XCTAssertEqual(error, StorePurchaseManagerError.productNotFound)
}
}

func testPurchaseSubscriptionFailureWithoutValidUUID() async throws {
// Given
await storePurchaseManager.updateAvailableProducts()

let invalidUUID = "a"
XCTAssertNil(UUID(uuidString: invalidUUID))

// When
let result = await storePurchaseManager.purchaseSubscription(with: Constants.yearlySubscriptionID, externalID: invalidUUID)

// Then
switch result {
case .success:
XCTFail("Unexpected success")
case .failure(let error):
XCTAssertEqual(error, StorePurchaseManagerError.externalIDisNotAValidUUID)
}
}

@available(iOS 17.0, *)
func testPurchaseSubscriptionFailure() async throws {
// Given
try? await session.setSimulatedError(SKTestFailures.Purchase.purchase(.productUnavailable),
forAPI: StoreKitPurchaseAPI.purchase)

await storePurchaseManager.updateAvailableProducts()

// When
let result = await storePurchaseManager.purchaseSubscription(with: Constants.yearlySubscriptionID, externalID: Constants.externalID)

// Then
switch result {
case .success:
XCTFail("Unexpected success")
case .failure(let error):
XCTAssertEqual(error, StorePurchaseManagerError.purchaseFailed)
}
}
}

private final class StoreKitHelpers {

static func currentEntitlements() async -> [Transaction] {
return await Transaction.currentEntitlements.compactMap { result in
try? checkVerified(result)
}.reduce(into: [], { $0.append($1) })
}

static func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
// Check whether the JWS passes StoreKit verification.
switch result {
case .unverified:
// StoreKit parses the JWS, but it fails verification.
throw StoreError.failedVerification
case .verified(let safe):
// The result is verified. Return the unwrapped value.
return safe
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// SubscriptionFeatureAvailabilityMock.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
@testable import BrowserServicesKit

public final class SubscriptionFeatureAvailabilityMock: SubscriptionFeatureAvailability {
public var isFeatureAvailable: Bool
public var isSubscriptionPurchaseAllowed: Bool
public var usesUnifiedFeedbackForm: Bool

public init(isFeatureAvailable: Bool, isSubscriptionPurchaseAllowed: Bool, usesUnifiedFeedbackForm: Bool) {
self.isFeatureAvailable = isFeatureAvailable
self.isSubscriptionPurchaseAllowed = isSubscriptionPurchaseAllowed
self.usesUnifiedFeedbackForm = usesUnifiedFeedbackForm
}

}
Loading
Loading