Skip to content

Commit

Permalink
Allow activating subscription for internal users via debug menu (#3003)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
miasma13 authored Jul 22, 2024
1 parent 0a259e9 commit a7254a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 57 deletions.
1 change: 0 additions & 1 deletion DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public struct UserDefaultsWrapper<T> {

// Subscription

case subscriptionInternalTesting = "subscription.internal-testing-enabled"
case subscriptionEnvironment = "subscription.environment"
}

Expand Down
4 changes: 1 addition & 3 deletions DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!

Expand All @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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()
Expand All @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, *) {
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down

0 comments on commit a7254a4

Please sign in to comment.