From 2fcaf4fa40733c6f7972a0ec61ce52b8380866d0 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Fri, 29 Jul 2022 18:19:26 -0300
Subject: [PATCH] Fixed comments on MR and added unit test

---
 Package.swift                                 |  7 ++++++
 Sources/App/AppDelegate.swift                 | 18 +++++++++++----
 .../ViewModels/SearchLeftViewModel.swift      | 22 ++++++++-----------
 Tests/AppTests/General/InvitationTests.swift  | 21 ++++++++++++++++++
 4 files changed, 51 insertions(+), 17 deletions(-)
 create mode 100644 Tests/AppTests/General/InvitationTests.swift

diff --git a/Package.swift b/Package.swift
index bcec957c..08f6fef2 100644
--- a/Package.swift
+++ b/Package.swift
@@ -722,6 +722,13 @@ let package = Package(
                 dependencies: ["DependencyInjection"]
             ),
 
+        // MARK: - AppTests
+
+            .testTarget(
+                name: "AppTests",
+                dependencies: ["App"]
+            ),
+
         // MARK: - ProfileFeatureTests
 
             .testTarget(
diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift
index fdf6090e..b20f3d6c 100644
--- a/Sources/App/AppDelegate.swift
+++ b/Sources/App/AppDelegate.swift
@@ -143,10 +143,8 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {
         open url: URL,
         options: [UIApplication.OpenURLOptionsKey : Any] = [:]
     ) -> Bool {
-        if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
-           let invitation = components.queryItems?.first(where: { $0.name == "invitation" }),
-            let username = invitation.value {
-            self.invitation = username
+        if let invitationUsername = getUsernameFromInvitationDeepLink(url) {
+            self.invitation = invitationUsername
             return true
         } else {
             return dropboxService.handleOpenUrl(url)
@@ -154,6 +152,18 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {
     }
 }
 
+func getUsernameFromInvitationDeepLink(_ url: URL) -> String? {
+    if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
+       components.scheme == "xxnetwork",
+       components.host == "messenger",
+       let queryItem = components.queryItems?.first(where: { $0.name == "invitation" }),
+       let username = queryItem.value {
+        return username
+    }
+
+    return nil
+}
+
 // MARK: Notifications
 
 extension AppDelegate: UNUserNotificationCenterDelegate {
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index e9c6e68d..cab4842c 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -50,19 +50,15 @@ final class SearchLeftViewModel {
 
             networkMonitor.statusPublisher
                 .first { $0 == .available }
-                .map { [unowned self] _ in
-                    session.waitForNodes(timeout: 5).first()
-                        .sink {
-                            if case .failure(let error) = $0 {
-                                self.hudSubject.send(.error(.init(with: error)))
-                            }
-                            networkCancellable.removeAll()
-                        } receiveValue: { _ in
-                            self.didStartSearching()
-                            networkCancellable.removeAll()
-                        }.store(in: &networkCancellable)
-                }.sink(receiveValue: { _ in })
-                .store(in: &networkCancellable)
+                .eraseToAnyPublisher()
+                .flatMap { _ in self.session.waitForNodes(timeout: 5) }
+                .sink {
+                    if case .failure(let error) = $0 {
+                        self.hudSubject.send(.error(.init(with: error)))
+                    }
+                } receiveValue: {
+                    self.didStartSearching()
+                }.store(in: &networkCancellable)
         }
     }
 
diff --git a/Tests/AppTests/General/InvitationTests.swift b/Tests/AppTests/General/InvitationTests.swift
new file mode 100644
index 00000000..bbd80d6e
--- /dev/null
+++ b/Tests/AppTests/General/InvitationTests.swift
@@ -0,0 +1,21 @@
+import XCTest
+
+@testable import App
+
+final class AppDelegateTests: XCTestCase {
+    func test_invitationDeeplink() {
+        let host = "messenger"
+        let query = "invitation"
+        let scheme = "xxnetwork"
+        let username = "john_doe"
+
+        let url = URL(string: "\(scheme)://\(host)?\(query)=\(username)")!
+        XCTAssertEqual(getUsernameFromInvitationDeepLink(url), username)
+
+        let malformedURL = URL(string: "\(scheme)\(host)\(query)\(username)")!
+        XCTAssertNil(getUsernameFromInvitationDeepLink(malformedURL))
+
+        let urlAnotherUsername = URL(string: "\(scheme)://\(host)?\(query)=asdfg")!
+        XCTAssertNotEqual(getUsernameFromInvitationDeepLink(urlAnotherUsername), username)
+    }
+}
-- 
GitLab