From a7254a4d98c3e24e0e161820a684beecf84f9109 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Mon, 22 Jul 2024 12:16:34 +0200 Subject: [PATCH] Allow activating subscription for internal users via debug menu (#3003) Task/Issue URL: https://app.asana.com/0/72649045549333/1207811184536020/f **Description**: Add to debug menu option to start the subscription activation flow. This option is to allow access to the flow even when the purchase is disallowed in given region (App Store build for non-US user). Additional changes are renaming and cleanup of obsolete items in the "Subscription" debug menu. **Steps to test this PR**: 1. Remove subscription if any present. 2. Open menu select: Debug -> Subscription -> I Have a Subscription 3. A new tab should open for activating subscription via email 4. Activate subscription via email OTP flow Test the above for the setup where purchase platform is set to App Store and no subscription products are available so there are no purchase entry points shown in the app. This can be achieved by altering MAIN_BUNDLE_IDENTIFIER_PREFIX for a given build (so it does not matches one we have subscriptions defined in the App Store) and after launching the build ensuring that the purchase environment is set to App Store). After these changes the "Privacy Pro" should not be visible in the more options menu not in the settings but the activation flow from the debug menu should work. **Definition of Done**: * [ ] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)? --- ###### Internal references: [Pull Request Review Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f) [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943) [Pull Request Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f) --- .../Utilities/UserDefaultsWrapper.swift | 1 - DuckDuckGo/Menus/MainMenu.swift | 4 +- .../DebugMenu/SubscriptionDebugMenu.swift | 67 ++++--------------- 3 files changed, 15 insertions(+), 57 deletions(-) diff --git a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift index 845406318e..5b928018e5 100644 --- a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift +++ b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift @@ -223,7 +223,6 @@ public struct UserDefaultsWrapper { // Subscription - case subscriptionInternalTesting = "subscription.internal-testing-enabled" case subscriptionEnvironment = "subscription.environment" } diff --git a/DuckDuckGo/Menus/MainMenu.swift b/DuckDuckGo/Menus/MainMenu.swift index d2eb8ea9e4..72f9a9ef40 100644 --- a/DuckDuckGo/Menus/MainMenu.swift +++ b/DuckDuckGo/Menus/MainMenu.swift @@ -636,7 +636,6 @@ final class MainMenu: NSMenu { NSMenuItem(title: "C++ exception", action: #selector(MainViewController.crashOnCxxException)) } - let isInternalTestingWrapper = UserDefaultsWrapper(key: .subscriptionInternalTesting, defaultValue: false) let subscriptionAppGroup = Bundle.main.appGroup(bundle: .subs) let subscriptionUserDefaults = UserDefaults(suiteName: subscriptionAppGroup)! @@ -653,9 +652,8 @@ final class MainMenu: NSMenu { SubscriptionDebugMenu(currentEnvironment: currentEnvironment, updateServiceEnvironment: updateServiceEnvironment, updatePurchasingPlatform: updatePurchasingPlatform, - isInternalTestingEnabled: { isInternalTestingWrapper.wrappedValue }, - updateInternalTestingFlag: { isInternalTestingWrapper.wrappedValue = $0 }, currentViewController: { WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController }, + openSubscriptionTab: { WindowControllersManager.shared.showTab(with: .subscription($0)) }, subscriptionManager: Application.appDelegate.subscriptionManager) NSMenuItem(title: "Privacy Pro Survey") { diff --git a/LocalPackages/SubscriptionUI/Sources/SubscriptionUI/DebugMenu/SubscriptionDebugMenu.swift b/LocalPackages/SubscriptionUI/Sources/SubscriptionUI/DebugMenu/SubscriptionDebugMenu.swift index f672fa995d..128765c197 100644 --- a/LocalPackages/SubscriptionUI/Sources/SubscriptionUI/DebugMenu/SubscriptionDebugMenu.swift +++ b/LocalPackages/SubscriptionUI/Sources/SubscriptionUI/DebugMenu/SubscriptionDebugMenu.swift @@ -24,9 +24,7 @@ public final class SubscriptionDebugMenu: NSMenuItem { var currentEnvironment: SubscriptionEnvironment var updateServiceEnvironment: (SubscriptionEnvironment.ServiceEnvironment) -> Void var updatePurchasingPlatform: (SubscriptionEnvironment.PurchasePlatform) -> Void - - var isInternalTestingEnabled: () -> Bool - var updateInternalTestingFlag: (Bool) -> Void + var openSubscriptionTab: (URL) -> Void private var purchasePlatformItem: NSMenuItem? @@ -53,16 +51,14 @@ public final class SubscriptionDebugMenu: NSMenuItem { public init(currentEnvironment: SubscriptionEnvironment, updateServiceEnvironment: @escaping (SubscriptionEnvironment.ServiceEnvironment) -> Void, updatePurchasingPlatform: @escaping (SubscriptionEnvironment.PurchasePlatform) -> Void, - isInternalTestingEnabled: @escaping () -> Bool, - updateInternalTestingFlag: @escaping (Bool) -> Void, currentViewController: @escaping () -> NSViewController?, + openSubscriptionTab: @escaping (URL) -> Void, subscriptionManager: SubscriptionManager) { self.currentEnvironment = currentEnvironment self.updateServiceEnvironment = updateServiceEnvironment self.updatePurchasingPlatform = updatePurchasingPlatform - self.isInternalTestingEnabled = isInternalTestingEnabled - self.updateInternalTestingFlag = updateInternalTestingFlag self.currentViewController = currentViewController + self.openSubscriptionTab = openSubscriptionTab self.subscriptionManager = subscriptionManager super.init(title: "Subscription", action: nil, keyEquivalent: "") self.submenu = makeSubmenu() @@ -71,20 +67,19 @@ public final class SubscriptionDebugMenu: NSMenuItem { private func makeSubmenu() -> NSMenu { let menu = NSMenu(title: "") - menu.addItem(NSMenuItem(title: "Simulate Subscription Active State (fake token)", action: #selector(simulateSubscriptionActiveState), target: self)) - menu.addItem(NSMenuItem(title: "Clear Subscription Authorization Data", action: #selector(signOut), target: self)) - menu.addItem(NSMenuItem(title: "Show account details", action: #selector(showAccountDetails), target: self)) + menu.addItem(NSMenuItem(title: "I Have a Subscription", action: #selector(activateSubscription), target: self)) + menu.addItem(NSMenuItem(title: "Remove Subscription From This Device", action: #selector(signOut), target: self)) + menu.addItem(NSMenuItem(title: "Show Account Details", action: #selector(showAccountDetails), target: self)) menu.addItem(.separator()) menu.addItem(NSMenuItem(title: "Validate Token", action: #selector(validateToken), target: self)) menu.addItem(NSMenuItem(title: "Check Entitlements", action: #selector(checkEntitlements), target: self)) - menu.addItem(NSMenuItem(title: "Get Subscription Info", action: #selector(getSubscriptionDetails), target: self)) - menu.addItem(NSMenuItem(title: "Restore Subscription from App Store transaction", action: #selector(restorePurchases), target: self)) - menu.addItem(NSMenuItem(title: "Post didSignIn notification", action: #selector(postDidSignInNotification), target: self)) - menu.addItem(NSMenuItem(title: "Post subscriptionDidChange notification", action: #selector(postSubscriptionDidChangeNotification), target: self)) - menu.addItem(.separator()) + menu.addItem(NSMenuItem(title: "Get Subscription Details", action: #selector(getSubscriptionDetails), target: self)) + if #available(macOS 12.0, *) { + menu.addItem(.separator()) menu.addItem(NSMenuItem(title: "Sync App Store AppleID Account (re- sign-in)", action: #selector(syncAppleIDAccount), target: self)) menu.addItem(NSMenuItem(title: "Purchase Subscription from App Store", action: #selector(showPurchaseView), target: self)) + menu.addItem(NSMenuItem(title: "Restore Subscription from App Store transaction", action: #selector(restorePurchases), target: self)) } menu.addItem(.separator()) @@ -97,12 +92,6 @@ public final class SubscriptionDebugMenu: NSMenuItem { environmentItem.submenu = makeEnvironmentSubmenu() menu.addItem(environmentItem) - menu.addItem(.separator()) - - let internalTestingItem = NSMenuItem(title: "Internal testing", action: #selector(toggleInternalTesting), target: self) - internalTestingItem.state = isInternalTestingEnabled() ? .on : .off - menu.addItem(internalTestingItem) - menu.delegate = self return menu @@ -170,8 +159,9 @@ public final class SubscriptionDebugMenu: NSMenuItem { } @objc - func simulateSubscriptionActiveState() { - accountManager.storeAccount(token: "fake-token", email: "fake@email.com", externalID: "123") + func activateSubscription() { + let url = subscriptionManager.url(for: .activateViaEmail) + openSubscriptionTab(url) } @objc @@ -307,16 +297,6 @@ public final class SubscriptionDebugMenu: NSMenuItem { // MARK: - - @objc - func postDidSignInNotification(_ sender: Any?) { - NotificationCenter.default.post(name: .accountDidSignIn, object: self, userInfo: nil) - } - - @objc - func postSubscriptionDidChangeNotification(_ sender: Any?) { - NotificationCenter.default.post(name: .subscriptionDidChange, object: self, userInfo: nil) - } - @objc func restorePurchases(_ sender: Any?) { if #available(macOS 12.0, *) { @@ -330,26 +310,6 @@ public final class SubscriptionDebugMenu: NSMenuItem { } } - @objc - func toggleInternalTesting(_ sender: Any?) { - Task { @MainActor in - let currentValue = isInternalTestingEnabled() - let shouldShowAlert = currentValue == false - - if shouldShowAlert { - let alert = makeAlert(title: "Are you sure you want to enable internal testing", - message: "Only enable this option if you are participating in internal testing and have been requested to do so.", - buttonNames: ["Yes", "No"]) - let response = alert.runModal() - - guard case .alertFirstButtonReturn = response else { return } - } - - updateInternalTestingFlag(!currentValue) - self.refreshSubmenu() - } - } - private func showAlert(title: String, message: String? = nil) { Task { @MainActor in let alert = makeAlert(title: title, message: message) @@ -367,6 +327,7 @@ public final class SubscriptionDebugMenu: NSMenuItem { for buttonName in buttonNames { alert.addButton(withTitle: buttonName) } + alert.accessoryView = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 0)) return alert } }