This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSubscriptionAppStoreRestorerTests.swift
329 lines (260 loc) · 15.9 KB
/
SubscriptionAppStoreRestorerTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// SubscriptionAppStoreRestorerTests.swift
//
// 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
@testable import DuckDuckGo_Privacy_Browser
@testable import PixelKit
import PixelKitTestingUtilities
import Common
import enum StoreKit.StoreKitError
@available(macOS 12.0, *)
final class SubscriptionAppStoreRestorerTests: XCTestCase {
private struct Constants {
static let userDefaultsSuiteName = "SubscriptionAppStoreRestorerTests"
static let authToken = UUID().uuidString
static let accessToken = UUID().uuidString
static let externalID = UUID().uuidString
static let email = "dax@duck.com"
}
var userDefaults: UserDefaults!
var pixelKit: PixelKit!
var uiHandler: SubscriptionUIHandlerMock!
var accountManager: AccountManagerMock!
var subscriptionService: SubscriptionEndpointServiceMock!
var authService: AuthEndpointServiceMock!
var storePurchaseManager: StorePurchaseManagerMock!
var subscriptionFeatureMappingCache: SubscriptionFeatureMappingCacheMock!
var subscriptionEnvironment: SubscriptionEnvironment!
var subscriptionManager: SubscriptionManagerMock!
var appStoreRestoreFlow: AppStoreRestoreFlowMock!
var subscriptionAppStoreRestorer: SubscriptionAppStoreRestorer!
var pixelsFired = Set<String>()
var uiEventsHappened: [SubscriptionUIHandlerMock.UIHandlerMockPerformedAction] = []
override func setUp() async throws {
userDefaults = UserDefaults(suiteName: Constants.userDefaultsSuiteName)!
userDefaults.removePersistentDomain(forName: Constants.userDefaultsSuiteName)
pixelKit = PixelKit(dryRun: false,
appVersion: "1.0.0",
defaultHeaders: [:],
defaults: userDefaults) { pixelName, _, _, _, _, _ in
self.pixelsFired.insert(pixelName)
}
pixelKit.clearFrequencyHistoryForAllPixels()
PixelKit.setSharedForTesting(pixelKit: pixelKit)
uiHandler = await SubscriptionUIHandlerMock(didPerformActionCallback: { action in
self.uiEventsHappened.append(action)
})
accountManager = AccountManagerMock()
subscriptionService = SubscriptionEndpointServiceMock()
authService = AuthEndpointServiceMock()
storePurchaseManager = StorePurchaseManagerMock()
subscriptionFeatureMappingCache = SubscriptionFeatureMappingCacheMock()
subscriptionEnvironment = SubscriptionEnvironment(serviceEnvironment: .production,
purchasePlatform: .appStore)
subscriptionManager = SubscriptionManagerMock(accountManager: accountManager,
subscriptionEndpointService: subscriptionService,
authEndpointService: authService,
storePurchaseManager: storePurchaseManager,
currentEnvironment: subscriptionEnvironment,
canPurchase: true,
subscriptionFeatureMappingCache: subscriptionFeatureMappingCache)
appStoreRestoreFlow = AppStoreRestoreFlowMock()
subscriptionAppStoreRestorer = DefaultSubscriptionAppStoreRestorer(subscriptionManager: subscriptionManager,
appStoreRestoreFlow: appStoreRestoreFlow,
uiHandler: uiHandler)
}
override func tearDown() async throws {
userDefaults = nil
PixelKit.tearDown()
pixelKit.clearFrequencyHistoryForAllPixels()
pixelsFired.removeAll()
uiEventsHappened.removeAll()
accountManager = nil
subscriptionService = nil
authService = nil
storePurchaseManager = nil
subscriptionEnvironment = nil
subscriptionManager = nil
appStoreRestoreFlow = nil
uiHandler = nil
subscriptionAppStoreRestorer = nil
}
// MARK: - Tests for restoreAppStoreSubscription
func testRestoreAppStoreSubscriptionSuccess() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .success(())
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreSuccess.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreSuccess.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenUserCancelsSyncAppleID() async throws {
// Given
storePurchaseManager.syncAppleIDAccountResultError = StoreKitError.userCancelled
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController])
XCTAssertTrue(pixelsFired.isEmpty)
}
func testRestoreAppStoreSubscriptionSuccessWhenSyncAppleIDFailsButUserProceedsRegardeless() async throws {
// Given
storePurchaseManager.syncAppleIDAccountResultError = StoreKitError.unknown
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .success(())
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.appleIDSyncFailed),
.didPresentProgressViewController,
.didDismissProgressViewController])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreSuccess.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreSuccess.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
// MARK: - Tests for different restore failures
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToMissingAccountOrTransactions() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.missingAccountOrTransactions)
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.subscriptionNotFound),
.didShowTab(.subscription(subscriptionManager.url(for: .purchase)))])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureNotFound.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureNotFound.name + "_c",
PrivacyProPixel.privacyProPurchaseFailureStoreError.name + "_d",
PrivacyProPixel.privacyProPurchaseFailureStoreError.name + "_c",
PrivacyProPixel.privacyProOfferScreenImpression.name])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToPastTransactionAuthenticationError() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.pastTransactionAuthenticationError)
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.somethingWentWrong)])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_c",
PrivacyProPixel.privacyProPurchaseFailure.name + "_d",
PrivacyProPixel.privacyProPurchaseFailure.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToFailedToObtainAccessToken() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.failedToObtainAccessToken)
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.somethingWentWrong)])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_c",
PrivacyProPixel.privacyProPurchaseFailure.name + "_d",
PrivacyProPixel.privacyProPurchaseFailure.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToFailedToFetchAccountDetails() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.failedToFetchAccountDetails)
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.somethingWentWrong)])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_c",
PrivacyProPixel.privacyProPurchaseFailure.name + "_d",
PrivacyProPixel.privacyProPurchaseFailure.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToFailedToFetchSubscriptionDetails() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.failedToFetchSubscriptionDetails)
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.somethingWentWrong)])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_c",
PrivacyProPixel.privacyProPurchaseFailure.name + "_d",
PrivacyProPixel.privacyProPurchaseFailure.name + "_c"])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
func testRestoreAppStoreSubscriptionWhenRestoreFailsDueToSubscriptionBeingExpired() async throws {
// Given
appStoreRestoreFlow.restoreAccountFromPastPurchaseResult = .failure(.subscriptionExpired(accountDetails: .init(authToken: Constants.authToken,
accessToken: Constants.accessToken,
externalID: Constants.externalID,
email: Constants.email)) )
await uiHandler.setAlertResponse(alertResponse: .alertFirstButtonReturn)
// When
await subscriptionAppStoreRestorer.restoreAppStoreSubscription()
// Then
XCTAssertEqual(uiEventsHappened, [.didPresentProgressViewController,
.didDismissProgressViewController,
.didShowAlert(.subscriptionInactive),
.didShowTab(.subscription(subscriptionManager.url(for: .purchase)))])
let expectedPixels = Set([PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_d",
PrivacyProPixel.privacyProRestorePurchaseStoreFailureOther.name + "_c",
PrivacyProPixel.privacyProPurchaseFailureStoreError.name + "_d",
PrivacyProPixel.privacyProPurchaseFailureStoreError.name + "_c",
PrivacyProPixel.privacyProOfferScreenImpression.name])
XCTAssertTrue(expectedPixels.isSubset(of: pixelsFired))
XCTAssertTrue(assertNoOtherPrivacyProPixelsExcept(expectedPixels), "Unexpected Privacy Pro pixels fired")
}
private func assertNoOtherPrivacyProPixelsExcept(_ expectedPixels: Set<String>) -> Bool {
#if APPSTORE
let appDistribution = "store"
#else
let appDistribution = "direct"
#endif
let privacyProPixelPrefix = "m_mac_\(appDistribution)_privacy-pro"
let otherPixels = pixelsFired.subtracting(expectedPixels)
return !otherPixels.contains { $0.hasPrefix(privacyProPixelPrefix) }
}
}