Skip to content

Commit

Permalink
[MBL-736] Upgrade PerimeterX (#1839)
Browse files Browse the repository at this point in the history
* updated perimeterX integration for version 3

* Added final configuration piece to start PerimeterX

* formatting

* Opted to update the print statement to be inline with the previous print statements for PerimeterX captcha state. Also removed the fatalError.

* removed inline comment
  • Loading branch information
msadoon committed Aug 8, 2023
1 parent 0e82d13 commit 9a1dc93
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 345 deletions.
27 changes: 7 additions & 20 deletions Kickstarter-iOS/AppDelegate.swift
Expand Up @@ -219,6 +219,12 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
.messageBannerViewController?.showBanner(with: success ? .success : .error, message: message)
}

self.viewModel.outputs.configurePerimeterX
.observeValues {
AppEnvironment.current.apiService.perimeterXClient
.start(policyDomains: [AppEnvironment.current.apiService.serverConfig.apiBaseUrl.host ?? ""])
}

NotificationCenter.default
.addObserver(forName: Notification.Name.ksr_sessionStarted, object: nil, queue: nil) { [weak self] _ in
self?.viewModel.inputs.userSessionStarted()
Expand Down Expand Up @@ -268,28 +274,13 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
self?.viewModel.inputs.configUpdatedNotificationObserved()
}

NotificationCenter.default
.addObserver(
forName: Notification.Name.ksr_perimeterXCaptcha,
object: nil,
queue: nil
) { [weak self] note in
self?.viewModel.inputs.perimeterXCaptchaTriggeredWithUserInfo(note.userInfo)
}

self.window?.tintColor = .ksr_create_700

self.viewModel.inputs.applicationDidFinishLaunching(
application: application,
launchOptions: launchOptions
)

self.viewModel.outputs.goToPerimeterXCaptcha
.observeForControllerAction()
.observeValues { response in
self.goToPerimeterXCaptcha(response)
}

UNUserNotificationCenter.current().delegate = self

return self.viewModel.outputs.applicationDidFinishLaunchingReturnValue
Expand Down Expand Up @@ -407,10 +398,6 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
self.rootTabBarController?.switchToMessageThread(messageThread)
}

private func goToPerimeterXCaptcha(_ response: PerimeterXBlockResponseType) {
response.displayCaptcha(on: self.window?.rootViewController)
}

private func goToCreatorMessageThread(_ projectId: Param, _ messageThread: MessageThread) {
self.rootTabBarController?
.switchToCreatorMessageThread(projectId: projectId, messageThread: messageThread)
Expand All @@ -436,7 +423,7 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
_ = AppEnvironment.current.remoteConfigClient?
.addOnConfigUpdateListener { configUpdate, error in
guard let realtimeUpdateError = error else {
print("🔮 Remote Config Keys Update: \(configUpdate?.updatedKeys)")
print("🔮 Remote Config Keys Update: \(String(describing: configUpdate?.updatedKeys))")

return
}
Expand Down
19 changes: 0 additions & 19 deletions Kickstarter-iOS/AppDelegateViewModel.swift
Expand Up @@ -79,9 +79,6 @@ public protocol AppDelegateViewModelInputs {
/// Call when Remote Config configuration has failed
func remoteConfigClientConfigurationFailed()

/// Call when Perimeter X Captcha is triggered
func perimeterXCaptchaTriggeredWithUserInfo(_ userInfo: [AnyHashable: Any]?)

/// Call when the contextual PushNotification dialog should be presented.
func showNotificationDialog(notification: Notification)

Expand Down Expand Up @@ -144,9 +141,6 @@ public protocol AppDelegateViewModelOutputs {
/// Emits a message thread when we should navigate to it.
var goToMessageThread: Signal<MessageThread, Never> { get }

/// Emits when we should display the Perimeter X Captcha view.
var goToPerimeterXCaptcha: Signal<PerimeterXBlockResponseType, Never> { get }

/// Emits when the root view controller should navigate to the user's profile.
var goToProfile: Signal<(), Never> { get }

Expand Down Expand Up @@ -763,13 +757,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
.filter(isTrue)
.mapConst(0)

self.goToPerimeterXCaptcha = self.perimeterXCaptchaTriggeredWithUserInfoProperty.signal.skipNil()
.map { userInfo -> PerimeterXBlockResponseType? in
guard let response = userInfo["response"] as? PerimeterXBlockResponseType else { return nil }
return response
}
.skipNil()

self.configureSegmentWithBraze = self.applicationLaunchOptionsProperty.signal
.skipNil()
.map { _ in
Expand Down Expand Up @@ -948,11 +935,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
self.remoteConfigClientConfigurationFailedProperty.value = ()
}

private let perimeterXCaptchaTriggeredWithUserInfoProperty = MutableProperty<[AnyHashable: Any]?>(nil)
public func perimeterXCaptchaTriggeredWithUserInfo(_ userInfo: [AnyHashable: Any]?) {
self.perimeterXCaptchaTriggeredWithUserInfoProperty.value = userInfo
}

public let applicationIconBadgeNumber: Signal<Int, Never>
public let configureAppCenterWithData: Signal<AppCenterConfigData, Never>
public let configureFirebase: Signal<(), Never>
Expand All @@ -968,7 +950,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
public let goToDiscovery: Signal<DiscoveryParams?, Never>
public let goToLoginWithIntent: Signal<LoginIntent, Never>
public let goToMessageThread: Signal<MessageThread, Never>
public let goToPerimeterXCaptcha: Signal<PerimeterXBlockResponseType, Never>
public let goToProfile: Signal<(), Never>
public let goToProjectActivities: Signal<Param, Never>
public let goToMobileSafari: Signal<URL, Never>
Expand Down
24 changes: 0 additions & 24 deletions Kickstarter-iOS/AppDelegateViewModelTests.swift
Expand Up @@ -26,7 +26,6 @@ final class AppDelegateViewModelTests: TestCase {
private let goToDiscovery = TestObserver<DiscoveryParams?, Never>()
private let goToProjectActivities = TestObserver<Param, Never>()
private let goToLoginWithIntent = TestObserver<LoginIntent, Never>()
private let goToPerimeterXCaptcha = TestObserver<PerimeterXBlockResponseType, Never>()
private let goToProfile = TestObserver<(), Never>()
private let goToMobileSafari = TestObserver<URL, Never>()
private let goToSearch = TestObserver<(), Never>()
Expand Down Expand Up @@ -75,7 +74,6 @@ final class AppDelegateViewModelTests: TestCase {
self.vm.outputs.goToDashboard.observe(self.goToDashboard.observer)
self.vm.outputs.goToDiscovery.observe(self.goToDiscovery.observer)
self.vm.outputs.goToLoginWithIntent.observe(self.goToLoginWithIntent.observer)
self.vm.outputs.goToPerimeterXCaptcha.observe(self.goToPerimeterXCaptcha.observer)
self.vm.outputs.goToProfile.observe(self.goToProfile.observer)
self.vm.outputs.goToMobileSafari.observe(self.goToMobileSafari.observer)
self.vm.outputs.goToProjectActivities.observe(self.goToProjectActivities.observer)
Expand Down Expand Up @@ -2222,28 +2220,6 @@ final class AppDelegateViewModelTests: TestCase {
}
}

func testGoToPerimeterXCaptcha_Captcha() {
self.goToPerimeterXCaptcha.assertDidNotEmitValue()

let response = MockPerimeterXBlockResponse(blockType: .Captcha)

self.vm.inputs.perimeterXCaptchaTriggeredWithUserInfo(["response": response])

self.goToPerimeterXCaptcha.assertValueCount(1)
XCTAssertEqual(self.goToPerimeterXCaptcha.values.last?.type, .Captcha)
}

func testGoToPerimeterXCaptcha_Blocked() {
self.goToPerimeterXCaptcha.assertDidNotEmitValue()

let response = MockPerimeterXBlockResponse(blockType: .Block)

self.vm.inputs.perimeterXCaptchaTriggeredWithUserInfo(["response": response])

self.goToPerimeterXCaptcha.assertValueCount(1)
XCTAssertEqual(self.goToPerimeterXCaptcha.values.last?.type, .Block)
}

func testFeatureFlagsRetainedInConfig_NotRelease() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.beta.rawValue
Expand Down

0 comments on commit 9a1dc93

Please sign in to comment.