|
| 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 | + |
| 15 | +import Foundation |
| 16 | +import XCTest |
| 17 | + |
| 18 | +@testable import FirebaseAuth |
| 19 | +import FirebaseCore |
| 20 | + |
| 21 | +class AuthLifecycleTests: XCTestCase { |
| 22 | + private let kFakeAPIKey = "FAKE_API_KEY" |
| 23 | + private let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000", |
| 24 | + gcmSenderID: "00000000000000000-00000000000-000000000") |
| 25 | + |
| 26 | + override func setUp() { |
| 27 | + options.apiKey = kFakeAPIKey |
| 28 | + FirebaseApp.resetApps() |
| 29 | + FirebaseApp.configure(options: options) |
| 30 | + } |
| 31 | + |
| 32 | + /** @fn testSingleton |
| 33 | + @brief Verifies the @c auth method behaves like a singleton. |
| 34 | + */ |
| 35 | + func testSingleton() { |
| 36 | + let auth1 = Auth.auth() |
| 37 | + XCTAssertNotNil(auth1) |
| 38 | + let auth2 = Auth.auth() |
| 39 | + XCTAssertTrue(auth1 === auth2) |
| 40 | + } |
| 41 | + |
| 42 | + /** @fn testDefaultAuth |
| 43 | + @brief Verifies the @c auth method associates with the default Firebase app. |
| 44 | + */ |
| 45 | + func testDefaultAuth() throws { |
| 46 | + let auth1 = Auth.auth() |
| 47 | + let defaultApp = try XCTUnwrap(FirebaseApp.app()) |
| 48 | + let auth2 = Auth.auth(app: defaultApp) |
| 49 | + XCTAssertTrue(auth1 === auth2) |
| 50 | + XCTAssertTrue(auth1.app === defaultApp) |
| 51 | + } |
| 52 | + |
| 53 | + /** @fn testAppAPIkey |
| 54 | + @brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth . |
| 55 | + */ |
| 56 | + func testAppAPIKey() { |
| 57 | + let auth = Auth.auth() |
| 58 | + XCTAssertEqual(auth.requestConfiguration.apiKey, kFakeAPIKey) |
| 59 | + } |
| 60 | + |
| 61 | + /** @fn testAppAssociation |
| 62 | + @brief Verifies each @c FIRApp instance associates with a @c FIRAuth . |
| 63 | + */ |
| 64 | + func testAppAssociation() throws { |
| 65 | + let app1 = FirebaseApp(instanceWithName: "app1", options: options) |
| 66 | + let auth1 = Auth(app: app1) |
| 67 | + XCTAssertNotNil(auth1) |
| 68 | + XCTAssertEqual(auth1.app, app1) |
| 69 | + |
| 70 | + let app2 = FirebaseApp(instanceWithName: "app2", options: options) |
| 71 | + let auth2 = Auth(app: app2) |
| 72 | + XCTAssertNotNil(auth2) |
| 73 | + XCTAssertEqual(auth2.app, app2) |
| 74 | + |
| 75 | + XCTAssert(auth1 !== auth2) |
| 76 | + } |
| 77 | + |
| 78 | + /** @fn testLifeCycle |
| 79 | + @brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp . |
| 80 | + */ |
| 81 | + func testLifecycle() { |
| 82 | + let expectation = self.expectation(description: #function) |
| 83 | + weak var app: FirebaseApp? |
| 84 | + weak var auth: Auth? |
| 85 | + autoreleasepool { |
| 86 | + let app1 = FirebaseApp(instanceWithName: "app1", options: options) |
| 87 | + app = app1 |
| 88 | + let auth1 = Auth(app: app1) |
| 89 | + auth = auth1 |
| 90 | + // Verify that neither the app nor the auth is released yet, i.e., the app owns the auth |
| 91 | + // because nothing else retains the auth. |
| 92 | + XCTAssertNotNil(app) |
| 93 | + XCTAssertNotNil(auth) |
| 94 | + } |
| 95 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { |
| 96 | + XCTAssertNil(app) |
| 97 | + XCTAssertNil(auth) |
| 98 | + expectation.fulfill() |
| 99 | + } |
| 100 | + waitForExpectations(timeout: 1) |
| 101 | + } |
| 102 | +} |
0 commit comments