TestsBase.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. func signInAnonymouslyAsync() async throws {
  22. let auth = Auth.auth()
  23. try await auth.signInAnonymously()
  24. }
  25. func deleteCurrentUserAsync() async throws {
  26. let auth = Auth.auth()
  27. try await auth.currentUser?.delete()
  28. }
  29. func signInAnonymously() {
  30. let auth = Auth.auth()
  31. let expectation = self.expectation(description: "Anonymous sign-in finished.")
  32. auth.signInAnonymously { result, error in
  33. if let error {
  34. print("Anonymous sign in error: \(error)")
  35. }
  36. expectation.fulfill()
  37. }
  38. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  39. }
  40. func signOut() {
  41. let auth = Auth.auth()
  42. do {
  43. try auth.signOut()
  44. } catch {
  45. print("Error signing out: \(error)")
  46. }
  47. }
  48. func deleteCurrentUser() {
  49. guard let currentUser = Auth.auth().currentUser else { return }
  50. let expectation = self.expectation(description: "Delete current user finished.")
  51. currentUser.delete { error in
  52. if let error {
  53. print("Anonymous sign in error: \(error)")
  54. }
  55. expectation.fulfill()
  56. }
  57. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  58. }
  59. }