Skip to content

Commit

Permalink
Remove unnecessary compiler checks for Concurrency support (#12232)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 authored Jan 3, 2024
1 parent 5ac4a05 commit 0fe8385
Show file tree
Hide file tree
Showing 26 changed files with 2,078 additions and 2,214 deletions.
58 changes: 26 additions & 32 deletions FirebaseAppCheck/Tests/Unit/Swift/AppCheckAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ final class AppCheckAPITests {
}

// Get token (async/await)
#if compiler(>=5.5.2) && canImport(_Concurrency)
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
try await AppCheck.appCheck().token(forcingRefresh: false)
} catch {
// ...
}
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
try await AppCheck.appCheck().token(forcingRefresh: false)
} catch {
// ...
}
}
#endif // compiler(>=5.5.2) && canImport(_Concurrency)
}

// Set `AppCheckProviderFactory`
AppCheck.setAppCheckProviderFactory(DummyAppCheckProviderFactory())
Expand All @@ -97,18 +95,16 @@ final class AppCheckAPITests {
}

// Get token (async/await)
#if compiler(>=5.5.2) && canImport(_Concurrency)
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
_ = try await debugProvider.getToken()
} catch {
// ...
}
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
_ = try await debugProvider.getToken()
} catch {
// ...
}
}
#endif // compiler(>=5.5.2) && canImport(_Concurrency)
}

_ = debugProvider.localDebugToken()
_ = debugProvider.currentDebugToken()
Expand Down Expand Up @@ -170,20 +166,18 @@ final class AppCheckAPITests {
}
}
// Get token (async/await)
#if compiler(>=5.5.2) && canImport(_Concurrency)
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
_ = try await deviceCheckProvider.getToken()
} catch AppCheckErrorCode.unsupported {
// ...
} catch {
// ...
}
if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is a Swift 5.5+ feature available on iOS 15+
Task {
do {
_ = try await deviceCheckProvider.getToken()
} catch AppCheckErrorCode.unsupported {
// ...
} catch {
// ...
}
}
#endif // compiler(>=5.5.2) && canImport(_Concurrency)
}
}
}
#endif // !os(watchOS)
Expand Down
44 changes: 21 additions & 23 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/AccountInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,27 @@ class AccountInfoTests: TestsBase {
waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, watchOS 7, *)
func testUpdatingUsersEmailAsync() async throws {
let auth = Auth.auth()
do {
_ = try await auth.createUser(withEmail: kOldUserEmail, password: "password")
XCTFail("Did not get error for recreating a user")
} catch {
XCTAssertEqual((error as NSError).code,
AuthErrorCode.emailAlreadyInUse.rawValue,
"Created a user despite it already exiting.")
}
@available(iOS 13, tvOS 13, macOS 10.15, watchOS 7, *)
func testUpdatingUsersEmailAsync() async throws {
let auth = Auth.auth()
do {
_ = try await auth.createUser(withEmail: kOldUserEmail, password: "password")
XCTFail("Did not get error for recreating a user")
} catch {
XCTAssertEqual((error as NSError).code,
AuthErrorCode.emailAlreadyInUse.rawValue,
"Created a user despite it already exiting.")
}

let user = try await auth.signIn(withEmail: kOldUserEmail, password: "password")
XCTAssertEqual(user.user.email, kOldUserEmail)
XCTAssertEqual(auth.currentUser?.email,
kOldUserEmail,
"Signed user does not match request.")
let user = try await auth.signIn(withEmail: kOldUserEmail, password: "password")
XCTAssertEqual(user.user.email, kOldUserEmail)
XCTAssertEqual(auth.currentUser?.email,
kOldUserEmail,
"Signed user does not match request.")

try await auth.currentUser?.updateEmail(to: kNewUserEmail)
XCTAssertEqual(auth.currentUser?.email,
kNewUserEmail,
"Signed user does not match change.")
}
#endif
try await auth.currentUser?.updateEmail(to: kNewUserEmail)
XCTAssertEqual(auth.currentUser?.email,
kNewUserEmail,
"Signed user does not match change.")
}
}
20 changes: 9 additions & 11 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/AnonymousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ class AnonymousTests: TestsBase {
deleteCurrentUser()
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testUpdatingUsersEmailAsync() async throws {
try await signInAnonymouslyAsync()
if let isAnonymous = Auth.auth().currentUser?.isAnonymous {
XCTAssertTrue(isAnonymous)
} else {
XCTFail("Missing currentUser after anonymous sign in")
}
try await deleteCurrentUserAsync()
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testUpdatingUsersEmailAsync() async throws {
try await signInAnonymouslyAsync()
if let isAnonymous = Auth.auth().currentUser?.isAnonymous {
XCTAssertTrue(isAnonymous)
} else {
XCTFail("Missing currentUser after anonymous sign in")
}
#endif
try await deleteCurrentUserAsync()
}
}
34 changes: 15 additions & 19 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/EmailPasswordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ class EmailPasswordTests: TestsBase {
deleteCurrentUser()
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testCreateAccountWithEmailAndPasswordAsync() async throws {
let auth = Auth.auth()
try await auth.createUser(withEmail: kNewEmailToCreateUser, password: "password")
XCTAssertEqual(auth.currentUser?.email, kNewEmailToCreateUser, "Expected email doesn't match")
try await deleteCurrentUserAsync()
}
#endif
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testCreateAccountWithEmailAndPasswordAsync() async throws {
let auth = Auth.auth()
try await auth.createUser(withEmail: kNewEmailToCreateUser, password: "password")
XCTAssertEqual(auth.currentUser?.email, kNewEmailToCreateUser, "Expected email doesn't match")
try await deleteCurrentUserAsync()
}

func testSignInExistingUserWithEmailAndPassword() {
let auth = Auth.auth()
Expand All @@ -63,14 +61,12 @@ class EmailPasswordTests: TestsBase {
waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testSignInExistingUserWithEmailAndPasswordAsync() async throws {
let auth = Auth.auth()
try await auth.signIn(withEmail: kExistingEmailToSignIn, password: "password")
XCTAssertEqual(auth.currentUser?.email,
kExistingEmailToSignIn,
"Signed user does not match request.")
}
#endif
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testSignInExistingUserWithEmailAndPasswordAsync() async throws {
let auth = Auth.auth()
try await auth.signIn(withEmail: kExistingEmailToSignIn, password: "password")
XCTAssertEqual(auth.currentUser?.email,
kExistingEmailToSignIn,
"Signed user does not match request.")
}
}
8 changes: 0 additions & 8 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/FacebookTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import XCTest
// deleteFacebookTestingAccountbyID(facebookAccountID)
// }
//
// #if compiler(>=5.5.2) && canImport(_Concurrency)
// @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
// func testSignInWithFacebookAsync() async throws {
// let auth = Auth.auth()
Expand All @@ -60,7 +59,6 @@ import XCTest
// try await deleteCurrentUserAsync()
// try await deleteFacebookTestingAccountbyIDAsync(facebookAccountID)
// }
// #endif
//
// func testLinkAnonymousAccountToFacebookAccount() throws {
// let auth = Auth.auth()
Expand Down Expand Up @@ -90,7 +88,6 @@ import XCTest
// deleteFacebookTestingAccountbyID(facebookAccountID)
// }
//
// #if compiler(>=5.5.2) && canImport(_Concurrency)
// @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
// func testLinkAnonymousAccountToFacebookAccountAsync() async throws {
// let auth = Auth.auth()
Expand All @@ -111,7 +108,6 @@ import XCTest
// try await deleteCurrentUserAsync()
// try await deleteFacebookTestingAccountbyIDAsync(facebookAccountID)
// }
// #endif
//
// /// Creates a Facebook testing account using Facebook Graph API and return a dictionary that
// /// constrains "id", "access_token", "login_url", "email" and "password" of the created account.
Expand Down Expand Up @@ -151,7 +147,6 @@ import XCTest
// return returnValue
// }
//
// #if compiler(>=5.5.2) && canImport(_Concurrency)
// @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
// /// Creates a Facebook testing account using Facebook Graph API and return a dictionary that
// /// constains "id", "access_token", "login_url", "email" and "password" of the created
Expand All @@ -174,7 +169,6 @@ import XCTest
// }
// return returnValue
// }
// #endif
//
// // ** Delete a Facebook testing account by account Id using Facebook Graph API. */
// func deleteFacebookTestingAccountbyID(_ accountID: String) {
Expand All @@ -195,7 +189,6 @@ import XCTest
// waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
// }
//
// #if compiler(>=5.5.2) && canImport(_Concurrency)
// @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
// // ** Delete a Facebook testing account by account Id using Facebook Graph API. */
// func deleteFacebookTestingAccountbyIDAsync(_ accountID: String) async throws {
Expand All @@ -208,5 +201,4 @@ import XCTest
// fetcher.setRequestValue("text/plain", forHTTPHeaderField: "Content-Type")
// try await fetcher.beginFetch()
// }
// #endif
// }
74 changes: 35 additions & 39 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/GoogleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ class GoogleTests: TestsBase {
waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testSignInWithGoogleAsync() async throws {
let auth = Auth.auth()
let userInfoDict = try await getGoogleAccessTokenAsync()
let googleAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
let googleIDToken: String = try XCTUnwrap(userInfoDict["id_token"] as? String)
let credential = GoogleAuthProvider.credential(withIDToken: googleIDToken,
accessToken: googleAccessToken)
_ = try await auth.signIn(with: credential)
}
#endif
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func testSignInWithGoogleAsync() async throws {
let auth = Auth.auth()
let userInfoDict = try await getGoogleAccessTokenAsync()
let googleAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
let googleIDToken: String = try XCTUnwrap(userInfoDict["id_token"] as? String)
let credential = GoogleAuthProvider.credential(withIDToken: googleIDToken,
accessToken: googleAccessToken)
_ = try await auth.signIn(with: credential)
}

/// Sends http request to Google OAuth2 token server to use refresh token to exchange for Google
/// access token.
Expand Down Expand Up @@ -84,32 +82,30 @@ class GoogleTests: TestsBase {
return returnValue
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
/// Sends http request to Google OAuth2 token server to use refresh token to exchange for Google
/// access token.
/// Returns a dictionary that constains "access_token", "token_type", "expires_in" and sometimes
/// the "id_token". (The id_token is not guaranteed to be returned during a refresh exchange;
/// see https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokenResponse)
func getGoogleAccessTokenAsync() async throws -> [String: Any] {
let googleOauth2TokenServerUrl = "https://www.googleapis.com/oauth2/v4/token"
let bodyString = "client_id=\(Credentials.kGoogleClientID)&grant_type=refresh_token" +
"&refresh_token=\(Credentials.kGoogleTestAccountRefreshToken)"
let postData = bodyString.data(using: .utf8)
let service = GTMSessionFetcherService()
let fetcher = service.fetcher(withURLString: googleOauth2TokenServerUrl)
fetcher.bodyData = postData
fetcher.setRequestValue(
"application/x-www-form-urlencoded",
forHTTPHeaderField: "Content-Type"
)
let data = try await fetcher.beginFetch()
guard let returnValue = try JSONSerialization.jsonObject(with: data, options: [])
as? [String: Any] else {
XCTFail("Failed to serialize userInfo as a Dictionary")
fatalError()
}
return returnValue
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
/// Sends http request to Google OAuth2 token server to use refresh token to exchange for Google
/// access token.
/// Returns a dictionary that constains "access_token", "token_type", "expires_in" and sometimes
/// the "id_token". (The id_token is not guaranteed to be returned during a refresh exchange;
/// see https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokenResponse)
func getGoogleAccessTokenAsync() async throws -> [String: Any] {
let googleOauth2TokenServerUrl = "https://www.googleapis.com/oauth2/v4/token"
let bodyString = "client_id=\(Credentials.kGoogleClientID)&grant_type=refresh_token" +
"&refresh_token=\(Credentials.kGoogleTestAccountRefreshToken)"
let postData = bodyString.data(using: .utf8)
let service = GTMSessionFetcherService()
let fetcher = service.fetcher(withURLString: googleOauth2TokenServerUrl)
fetcher.bodyData = postData
fetcher.setRequestValue(
"application/x-www-form-urlencoded",
forHTTPHeaderField: "Content-Type"
)
let data = try await fetcher.beginFetch()
guard let returnValue = try JSONSerialization.jsonObject(with: data, options: [])
as? [String: Any] else {
XCTFail("Failed to serialize userInfo as a Dictionary")
fatalError()
}
#endif
return returnValue
}
}
22 changes: 10 additions & 12 deletions FirebaseAuth/Tests/Sample/SwiftApiTests/TestsBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ import XCTest
class TestsBase: XCTestCase {
static let kExpectationsTimeout = 10.0

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func signInAnonymouslyAsync() async throws {
let auth = Auth.auth()
try await auth.signInAnonymously()
}
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func signInAnonymouslyAsync() async throws {
let auth = Auth.auth()
try await auth.signInAnonymously()
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func deleteCurrentUserAsync() async throws {
let auth = Auth.auth()
try await auth.currentUser?.delete()
}
#endif
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
func deleteCurrentUserAsync() async throws {
let auth = Auth.auth()
try await auth.currentUser?.delete()
}

func signInAnonymously() {
let auth = Auth.auth()
Expand Down
Loading

0 comments on commit 0fe8385

Please sign in to comment.