diff --git a/FirebaseAuth/Sources/Swift/Auth/Auth.swift b/FirebaseAuth/Sources/Swift/Auth/Auth.swift index b8a1276bf60..74e795ebee4 100644 --- a/FirebaseAuth/Sources/Swift/Auth/Auth.swift +++ b/FirebaseAuth/Sources/Swift/Auth/Auth.swift @@ -1688,8 +1688,8 @@ extension Auth: AuthInterop { // Using reflection here to avoid build errors in extensions. let sel = NSSelectorFromString("sharedApplication") guard UIApplication.responds(to: sel), - let application = UIApplication.perform(sel).takeUnretainedValue() as? UIApplication - else { + let rawApplication = UIApplication.perform(sel), + let application = rawApplication.takeUnretainedValue() as? UIApplication else { return } diff --git a/FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift b/FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift index a2555f21c67..599415e6a2b 100644 --- a/FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift +++ b/FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift @@ -186,7 +186,10 @@ import FirebaseCore guard phoneNumber.count > 0 else { throw AuthErrorUtils.missingPhoneNumberError(message: nil) } - guard await auth.notificationManager.checkNotificationForwarding() else { + guard let manager = auth.notificationManager else { + throw AuthErrorUtils.notificationNotForwardedError() + } + guard await manager.checkNotificationForwarding() else { throw AuthErrorUtils.notificationNotForwardedError() } return try await verifyClAndSendVerificationCode(toPhoneNumber: phoneNumber, diff --git a/FirebaseAuth/Sources/Swift/Utilities/AuthDefaultUIDelegate.swift b/FirebaseAuth/Sources/Swift/Utilities/AuthDefaultUIDelegate.swift index 207db960012..55d8a880801 100644 --- a/FirebaseAuth/Sources/Swift/Utilities/AuthDefaultUIDelegate.swift +++ b/FirebaseAuth/Sources/Swift/Utilities/AuthDefaultUIDelegate.swift @@ -42,8 +42,8 @@ // Using reflection here to avoid build errors in extensions. let sel = NSSelectorFromString("sharedApplication") guard UIApplication.responds(to: sel), - let application = UIApplication.perform(sel).takeUnretainedValue() as? UIApplication - else { + let rawApplication = UIApplication.perform(sel), + let application = rawApplication.takeUnretainedValue() as? UIApplication else { return nil } var topViewController: UIViewController? diff --git a/FirebaseAuth/Tests/Unit/AuthTests.swift b/FirebaseAuth/Tests/Unit/AuthTests.swift index 9eb771e5feb..e71ddc419bf 100644 --- a/FirebaseAuth/Tests/Unit/AuthTests.swift +++ b/FirebaseAuth/Tests/Unit/AuthTests.swift @@ -1858,45 +1858,50 @@ class AuthTests: RPCBaseTests { XCTAssertEqual(AuthTests.kNewAccessToken, auth.currentUser?.rawAccessToken()) } - #if os(iOS) - /** @fn testAutoRefreshAppForegroundedNotification - @brief Tests that app foreground notification triggers the scheduling of an automatic token - refresh task. - */ - func testAutoRefreshAppForegroundedNotification() throws { - try auth.signOut() - // Enable auto refresh - enableAutoTokenRefresh() - - // Sign in a user. - try waitForSignInWithAccessToken() - - // Post "UIApplicationDidBecomeActiveNotification" to trigger scheduling token refresh task. - NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) - - setFakeSecureTokenService(fakeAccessToken: AuthTests.kNewAccessToken) + #if COCOAPODS // This test depends on a non-nil UIApplication.shared + #if os(iOS) + /** @fn testAutoRefreshAppForegroundedNotification + @brief Tests that app foreground notification triggers the scheduling of an automatic token + refresh task. + */ + func testAutoRefreshAppForegroundedNotification() throws { + try auth.signOut() + // Enable auto refresh + enableAutoTokenRefresh() + + // Sign in a user. + try waitForSignInWithAccessToken() + + // Post "UIApplicationDidBecomeActiveNotification" to trigger scheduling token refresh task. + NotificationCenter.default.post( + name: UIApplication.didBecomeActiveNotification, + object: nil + ) + + setFakeSecureTokenService(fakeAccessToken: AuthTests.kNewAccessToken) + + // Verify that the current user's access token is the "old" access token before automatic + // token refresh. + XCTAssertEqual(AuthTests.kAccessToken, auth.currentUser?.rawAccessToken()) + + // Execute saved token refresh task. + let expectation = self.expectation(description: #function) + kAuthGlobalWorkQueue.async { + XCTAssertNotNil(self.authDispatcherCallback) + self.authDispatcherCallback?() + expectation.fulfill() + } + waitForExpectations(timeout: 5) + waitForAuthGlobalWorkQueueDrain() - // Verify that the current user's access token is the "old" access token before automatic - // token refresh. - XCTAssertEqual(AuthTests.kAccessToken, auth.currentUser?.rawAccessToken()) + // Time for callback to run. + RPCBaseTests.waitSleep() - // Execute saved token refresh task. - let expectation = self.expectation(description: #function) - kAuthGlobalWorkQueue.async { - XCTAssertNotNil(self.authDispatcherCallback) - self.authDispatcherCallback?() - expectation.fulfill() + // Verify that current user's access token is the "new" access token provided in the mock + // secure token response during automatic token refresh. + XCTAssertEqual(AuthTests.kNewAccessToken, auth.currentUser?.rawAccessToken()) } - waitForExpectations(timeout: 5) - waitForAuthGlobalWorkQueueDrain() - - // Time for callback to run. - RPCBaseTests.waitSleep() - - // Verify that current user's access token is the "new" access token provided in the mock - // secure token response during automatic token refresh. - XCTAssertEqual(AuthTests.kNewAccessToken, auth.currentUser?.rawAccessToken()) - } + #endif #endif // MARK: Application Delegate tests. diff --git a/FirebaseAuth/Tests/Unit/OAuthProviderTests.swift b/FirebaseAuth/Tests/Unit/OAuthProviderTests.swift index 92b14d48b9f..df097e02bd4 100644 --- a/FirebaseAuth/Tests/Unit/OAuthProviderTests.swift +++ b/FirebaseAuth/Tests/Unit/OAuthProviderTests.swift @@ -298,7 +298,7 @@ import FirebaseCore let projectConfigExpectation = self.expectation(description: "projectConfiguration") rpcIssuer?.projectConfigRequester = { request in // 3. Validate the created Request instance. - XCTAssertEqual(request.apiKey, PhoneAuthProviderTests.kFakeAPIKey) + XCTAssertEqual(request.apiKey, OAuthProviderTests.kFakeAPIKey) XCTAssertEqual(request.endpoint, "getProjectConfig") // 4. Fulfill the expectation. projectConfigExpectation.fulfill() diff --git a/FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift b/FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift index 6f03c13d838..f6cbd7bd305 100644 --- a/FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift +++ b/FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift @@ -348,7 +348,7 @@ let expectation = self.expectation(description: function) // Fake push notification. - auth.appCredentialManager.fakeCredential = AuthAppCredential( + auth.appCredentialManager?.fakeCredential = AuthAppCredential( receipt: kTestReceipt, secret: kTestSecret ) @@ -438,7 +438,7 @@ let expectation = self.expectation(description: function) // Fake push notification. - auth.appCredentialManager.fakeCredential = AuthAppCredential( + auth.appCredentialManager?.fakeCredential = AuthAppCredential( receipt: kTestReceipt, secret: reCAPTCHAfallback ? nil : kTestSecret ) @@ -556,8 +556,8 @@ if !reCAPTCHAfallback { // Fake out appCredentialManager flow. - auth.appCredentialManager.credential = AuthAppCredential(receipt: kTestReceipt, - secret: kTestSecret) + auth.appCredentialManager?.credential = AuthAppCredential(receipt: kTestReceipt, + secret: kTestSecret) } else { // 1. Intercept, handle, and test the projectConfiguration RPC calls. let projectConfigExpectation = self.expectation(description: "projectConfiguration") @@ -673,7 +673,7 @@ settings.appVerificationDisabledForTesting = true auth.settings = settings } - auth.notificationManager.immediateCallbackForTestFaking = { forwardingNotification } + auth.notificationManager?.immediateCallbackForTestFaking = { forwardingNotification } auth.mainBundleUrlTypes = [["CFBundleURLSchemes": [scheme]]] if fakeToken { @@ -681,7 +681,7 @@ XCTFail("Failed to encode data for fake token") return } - auth.tokenManager.tokenStore = AuthAPNSToken(withData: data, type: .prod) + auth.tokenManager?.tokenStore = AuthAPNSToken(withData: data, type: .prod) } else { // Skip APNS token fetching. auth.tokenManager = FakeTokenManager(withApplication: UIApplication.shared) diff --git a/Package.swift b/Package.swift index cfc647cf0d1..4cacedb4da0 100644 --- a/Package.swift +++ b/Package.swift @@ -447,7 +447,11 @@ let package = Package( dependencies: [ "FirebaseAuth", ], - path: "FirebaseAuth/Tests/Unit" + path: "FirebaseAuth/Tests/Unit", + exclude: [ + "PhoneAuthProviderTests.swift", // TODO: these tests rely on a non-zero UIApplication.shared + "AuthNotificationManagerTests.swift", + ] ), .target( name: "FirebaseAuthCombineSwift",