TestsBase.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseAuth
  17. import Foundation
  18. import XCTest
  19. class TestsBase: XCTestCase {
  20. static let kExpectationsTimeout = 10.0
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. func signInAnonymouslyAsync() async throws {
  23. let auth = Auth.auth()
  24. try await auth.signInAnonymously()
  25. }
  26. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  27. func deleteCurrentUserAsync() async throws {
  28. let auth = Auth.auth()
  29. try await auth.currentUser?.delete()
  30. }
  31. func signInAnonymously() {
  32. let auth = Auth.auth()
  33. let expectation = self.expectation(description: "Anonymous sign-in finished.")
  34. auth.signInAnonymously { result, error in
  35. if let error = error {
  36. print("Anonymous sign in error: \(error)")
  37. }
  38. expectation.fulfill()
  39. }
  40. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  41. }
  42. func signOut() {
  43. let auth = Auth.auth()
  44. do {
  45. try auth.signOut()
  46. } catch {
  47. print("Error signing out: \(error)")
  48. }
  49. }
  50. func deleteCurrentUser() {
  51. let auth = Auth.auth()
  52. let expectation = self.expectation(description: "Delete current user finished.")
  53. auth.currentUser?.delete { error in
  54. if let error = error {
  55. print("Anonymous sign in error: \(error)")
  56. }
  57. expectation.fulfill()
  58. }
  59. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  60. }
  61. }