AuthLifecycleTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. import FirebaseCore
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class AuthLifecycleTests: XCTestCase {
  20. private let kFakeAPIKey = "FAKE_API_KEY"
  21. private let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  22. gcmSenderID: "00000000000000000-00000000000-000000000")
  23. override func setUp() {
  24. options.apiKey = kFakeAPIKey
  25. FirebaseApp.resetApps()
  26. FirebaseApp.configure(options: options)
  27. }
  28. /** @fn testSingleton
  29. @brief Verifies the @c auth method behaves like a singleton.
  30. */
  31. func testSingleton() {
  32. let auth1 = Auth.auth()
  33. XCTAssertNotNil(auth1)
  34. let auth2 = Auth.auth()
  35. XCTAssertTrue(auth1 === auth2)
  36. }
  37. /** @fn testDefaultAuth
  38. @brief Verifies the @c auth method associates with the default Firebase app.
  39. */
  40. func testDefaultAuth() throws {
  41. let auth1 = Auth.auth()
  42. let defaultApp = try XCTUnwrap(FirebaseApp.app())
  43. let auth2 = Auth.auth(app: defaultApp)
  44. XCTAssertTrue(auth1 === auth2)
  45. XCTAssertTrue(auth1.app === defaultApp)
  46. }
  47. /** @fn testAppAPIkey
  48. @brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth .
  49. */
  50. func testAppAPIKey() {
  51. let auth = Auth.auth()
  52. XCTAssertEqual(auth.requestConfiguration.apiKey, kFakeAPIKey)
  53. }
  54. /** @fn testAppAssociation
  55. @brief Verifies each @c FIRApp instance associates with a @c FIRAuth .
  56. */
  57. func testAppAssociation() throws {
  58. let app1 = FirebaseApp(instanceWithName: "app1", options: options)
  59. let auth1 = Auth(app: app1)
  60. XCTAssertNotNil(auth1)
  61. XCTAssertEqual(auth1.app, app1)
  62. let app2 = FirebaseApp(instanceWithName: "app2", options: options)
  63. let auth2 = Auth(app: app2)
  64. XCTAssertNotNil(auth2)
  65. XCTAssertEqual(auth2.app, app2)
  66. XCTAssert(auth1 !== auth2)
  67. }
  68. /** @fn testLifeCycle
  69. @brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp .
  70. */
  71. func testLifecycle() {
  72. let expectation = self.expectation(description: #function)
  73. weak var app: FirebaseApp?
  74. weak var auth: Auth?
  75. autoreleasepool {
  76. let app1 = FirebaseApp(instanceWithName: "app1", options: options)
  77. app = app1
  78. let auth1 = Auth(app: app1)
  79. auth = auth1
  80. // Verify that neither the app nor the auth is released yet, i.e., the app owns the auth
  81. // because nothing else retains the auth.
  82. XCTAssertNotNil(app)
  83. XCTAssertNotNil(auth)
  84. }
  85. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  86. XCTAssertNil(app)
  87. XCTAssertNil(auth)
  88. expectation.fulfill()
  89. }
  90. waitForExpectations(timeout: 5)
  91. }
  92. }