Skip to content

Commit

Permalink
Don't crash on nil application and disable related SPM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Jul 28, 2023
1 parent 5119d73 commit bb2d5b9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 49 deletions.
4 changes: 2 additions & 2 deletions FirebaseAuth/Sources/Swift/Auth/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
77 changes: 41 additions & 36 deletions FirebaseAuth/Tests/Unit/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuth/Tests/Unit/OAuthProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -673,15 +673,15 @@
settings.appVerificationDisabledForTesting = true
auth.settings = settings
}
auth.notificationManager.immediateCallbackForTestFaking = { forwardingNotification }
auth.notificationManager?.immediateCallbackForTestFaking = { forwardingNotification }
auth.mainBundleUrlTypes = [["CFBundleURLSchemes": [scheme]]]

if fakeToken {
guard let data = "!@#$%^".data(using: .utf8) else {
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)
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit bb2d5b9

Please sign in to comment.