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

Allow activating subscription for internal users via debug menu #3117

Merged
merged 7 commits into from
Jul 22, 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
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
1E908BF129827C480008C8F3 /* AutoconsentUserScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E908BEE29827C480008C8F3 /* AutoconsentUserScript.swift */; };
1E908BF229827C480008C8F3 /* autoconsent-bundle.js in Resources */ = {isa = PBXBuildFile; fileRef = 1E908BEF29827C480008C8F3 /* autoconsent-bundle.js */; };
1E908BF329827C480008C8F3 /* AutoconsentManagement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E908BF029827C480008C8F3 /* AutoconsentManagement.swift */; };
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 */; };
1EC458462948932500CB2B13 /* UIHostingControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EC458452948932500CB2B13 /* UIHostingControllerExtension.swift */; };
Expand Down Expand Up @@ -1247,6 +1248,7 @@
1E908BEE29827C480008C8F3 /* AutoconsentUserScript.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoconsentUserScript.swift; sourceTree = "<group>"; };
1E908BEF29827C480008C8F3 /* autoconsent-bundle.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "autoconsent-bundle.js"; sourceTree = "<group>"; };
1E908BF029827C480008C8F3 /* AutoconsentManagement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoconsentManagement.swift; sourceTree = "<group>"; };
1E9529A02C4E748B006E80D4 /* UINavigationControllerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UINavigationControllerExtension.swift; sourceTree = "<group>"; };
1EA51375286596A000493C6A /* PrivacyIconLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyIconLogic.swift; sourceTree = "<group>"; };
1EA513772866039400493C6A /* TrackerAnimationLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackerAnimationLogic.swift; sourceTree = "<group>"; };
1EC458452948932500CB2B13 /* UIHostingControllerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIHostingControllerExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5678,6 +5680,7 @@
319A37162829C8AD0079FBCE /* UITableViewExtension.swift */,
854A01322A558B3A00FCC628 /* UIView+Constraints.swift */,
F143C32C1E4A9A4800CFDE3A /* UIViewControllerExtension.swift */,
1E9529A02C4E748B006E80D4 /* UINavigationControllerExtension.swift */,
F1DE78591E5CD2A70058895A /* UIViewExtension.swift */,
F1F5337B1F26A9EF00D80D4F /* UserText.swift */,
4BBBBA912B03291700D965DA /* VPNWaitlistUserText.swift */,
Expand Down Expand Up @@ -6855,6 +6858,7 @@
1E8AD1D127C000AB00ABA377 /* OngoingDownloadRow.swift in Sources */,
1DEAADF02BA46E0700E25A97 /* PrivateSearchView.swift in Sources */,
85058366219AE9EA00ED4EDB /* HomePageConfiguration.swift in Sources */,
1E9529A12C4E748B006E80D4 /* UINavigationControllerExtension.swift in Sources */,
EE0153E12A6EABE0002A8B26 /* NetworkProtectionConvenienceInitialisers.swift in Sources */,
C17B595B2A03AAD30055F2D1 /* PasswordGenerationPromptView.swift in Sources */,
98AA92B32456FBE100ED4B9E /* SearchFieldContainerView.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Debug.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
<rect key="frame" x="0.0" y="0.0" width="383.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Subscriptions" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="2HN-ZT-QyE">
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Subscription" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="2HN-ZT-QyE">
<rect key="frame" x="20" y="0.0" width="355.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
Expand Down
8 changes: 8 additions & 0 deletions DuckDuckGo/MainViewController+Segues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ extension MainViewController {
}
}

func segueToSubscriptionRestoreFlow() {
os_log(#function, log: .generalLog, type: .debug)
hideAllHighlightsIfNeeded()
launchSettings {
$0.triggerDeepLinkNavigation(to: .restoreFlow)
}
}

func segueToDebugSettings() {
os_log(#function, log: .generalLog, type: .debug)
hideAllHighlightsIfNeeded()
Expand Down
11 changes: 11 additions & 0 deletions DuckDuckGo/SettingsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ struct SettingsRootView: View {
}
}
})

.onReceive(subscriptionNavigationCoordinator.$shouldPopToAppSettings) { shouldDismiss in
if shouldDismiss {
shouldDisplayDeepLinkSheet = false
shouldDisplayDeepLinkPush = false
}
}
}

// MARK: DeepLink Views
Expand All @@ -117,6 +124,10 @@ struct SettingsRootView: View {
SubscriptionContainerViewFactory.makeSubscribeFlow(origin: origin,
navigationCoordinator: subscriptionNavigationCoordinator,
subscriptionManager: AppDependencyProvider.shared.subscriptionManager)
case .restoreFlow:
SubscriptionContainerViewFactory.makeEmailFlow(navigationCoordinator: subscriptionNavigationCoordinator,
subscriptionManager: AppDependencyProvider.shared.subscriptionManager,
onDisappear: {})
case .duckPlayer:
SettingsDuckPlayerView().environmentObject(viewModel)
default:
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ extension SettingsViewModel {
case dbp
case itr
case subscriptionFlow(origin: String? = nil)
case restoreFlow
case duckPlayer
// Add other cases as needed

Expand All @@ -643,6 +644,7 @@ extension SettingsViewModel {
case .dbp: return "dbp"
case .itr: return "itr"
case .subscriptionFlow: return "subscriptionFlow"
case .restoreFlow: return "restoreFlow"
case .duckPlayer: return "duckPlayer"
// Ensure all cases are covered
}
Expand Down
87 changes: 51 additions & 36 deletions DuckDuckGo/SubscriptionDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ import NetworkProtection

private let titles = [
Sections.authorization: "Authentication",
Sections.subscription: "Subscription",
Sections.api: "Make API Call",
Sections.appstore: "App Store",
Sections.environment: "Environment",
]

enum Sections: Int, CaseIterable {
case authorization
case subscription
case api
case appstore
case environment
}

enum AuthorizationRows: Int, CaseIterable {
case showAccountDetails
case restoreSubscription
case clearAuthData
case injectCredentials
case showAccountDetails
}

enum SubscriptionRows: Int, CaseIterable {
case validateToken
case getEntitlements
case checkEntitlements
case getSubscription
}

Expand Down Expand Up @@ -87,18 +87,29 @@ import NetworkProtection

case .authorization:
switch AuthorizationRows(rawValue: indexPath.row) {
case .restoreSubscription:
cell.textLabel?.text = "I Have a Subscription"
case .clearAuthData:
cell.textLabel?.text = "Clear Authorization Data (Sign out)"
cell.textLabel?.text = "Remove Subscription From This Device"
case .showAccountDetails:
cell.textLabel?.text = "Show Account Details"
case .injectCredentials:
cell.textLabel?.text = "Simulate Authentication (Inject Fake token)"
case .none:
break
}

case.none:
break
case .api:
switch SubscriptionRows(rawValue: indexPath.row) {
case .validateToken:
cell.textLabel?.text = "Validate Token"
case .checkEntitlements:
cell.textLabel?.text = "Check Entitlements"
case .getSubscription:
cell.textLabel?.text = "Get Subscription Details"

case .none:
break
}


case .appstore:
switch AppStoreRows(rawValue: indexPath.row) {
Expand All @@ -107,18 +118,6 @@ import NetworkProtection
case .none:
break
}

case .subscription:
switch SubscriptionRows(rawValue: indexPath.row) {
case .validateToken:
cell.textLabel?.text = "Validate Token"
case .getSubscription:
cell.textLabel?.text = "Get subscription details"
case .getEntitlements:
cell.textLabel?.text = "Get Entitlements"
case .none:
break
}

case .environment:
let currentEnv = subscriptionManager.currentEnvironment.serviceEnvironment
Expand All @@ -132,14 +131,18 @@ import NetworkProtection
case .none:
break
}

case.none:
break
}

return cell
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Sections(rawValue: section) {
case .authorization: return AuthorizationRows.allCases.count
case .subscription: return SubscriptionRows.allCases.count
case .api: return SubscriptionRows.allCases.count
case .appstore: return AppStoreRows.allCases.count
case .environment: return EnvironmentRows.allCases.count
case .none: return 0
Expand All @@ -151,21 +154,21 @@ import NetworkProtection
switch Sections(rawValue: indexPath.section) {
case .authorization:
switch AuthorizationRows(rawValue: indexPath.row) {
case .restoreSubscription: openSubscriptionRestoreFlow()
case .clearAuthData: clearAuthData()
case .showAccountDetails: showAccountDetails()
case .injectCredentials: injectCredentials()
default: break
}
case .appstore:
switch AppStoreRows(rawValue: indexPath.row) {
case .syncAppStoreAccount: syncAppleIDAccount()
default: break
}
case .subscription:
case .api:
switch SubscriptionRows(rawValue: indexPath.row) {
case .validateToken: validateToken()
case .getSubscription: getSubscription()
case .getEntitlements: getEntitlements()
case .checkEntitlements: checkEntitlements()
case .getSubscription: getSubscriptionDetails()
default: break
}
case .environment:
Expand Down Expand Up @@ -221,18 +224,30 @@ import NetworkProtection
// func showAlert(title: String, message: String, alternativeAction)

// MARK: Account Status Actions

private func openSubscriptionRestoreFlow() {
guard let mainVC = view.window?.rootViewController as? MainViewController else { return }


if let navigationController = mainVC.presentedViewController as? UINavigationController {

navigationController.popToRootViewController {
if navigationController.viewControllers.first is SettingsHostingController {
mainVC.segueToSubscriptionRestoreFlow()
} else {
navigationController.dismiss(animated: true, completion: {
mainVC.segueToSubscriptionRestoreFlow()
})
}
}
}
}

private func clearAuthData() {
subscriptionManager.accountManager.signOut()
showAlert(title: "Data cleared!")
}

private func injectCredentials() {
subscriptionManager.accountManager.storeAccount(token: "a-fake-token",
email: "[email protected]",
externalID: "666")
showAccountDetails()
}

private func showAccountDetails() {
let title = subscriptionManager.accountManager.isUserAuthenticated ? "Authenticated" : "Not Authenticated"
let message = subscriptionManager.accountManager.isUserAuthenticated ?
Expand Down Expand Up @@ -271,7 +286,7 @@ import NetworkProtection
}
}

private func getSubscription() {
private func getSubscriptionDetails() {
Task {
guard let token = subscriptionManager.accountManager.accessToken else {
showAlert(title: "Not authenticated", message: "No authenticated user found! - Subscription not available")
Expand All @@ -287,7 +302,7 @@ import NetworkProtection
}
}

private func getEntitlements() {
private func checkEntitlements() {
Task {
var results: [String] = []
guard subscriptionManager.accountManager.accessToken != nil else {
Expand Down
45 changes: 45 additions & 0 deletions DuckDuckGo/UINavigationControllerExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// UINavigationControllerExtension.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 UIKit

extension UINavigationController {

func popToRootViewController(animated: Bool = true, completion: @escaping () -> Void) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.popToRootViewController(animated: animated)
CATransaction.commit()
}

func popViewControllerWithHandler(animated: Bool = true, completion: @escaping () -> Void) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.popViewController(animated: animated)
CATransaction.commit()
}

func pushViewController(viewController: UIViewController, animated: Bool = true, completion: @escaping () -> Void) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.pushViewController(viewController, animated: animated)
CATransaction.commit()
}

}
Loading