|
| 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 FirebaseAppCheck |
| 16 | +import FirebaseCore |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class AppCheckE2ETests: XCTestCase { |
| 20 | + let appName = "test_app_name" |
| 21 | + var app: FirebaseApp! |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + AppCheck.setAppCheckProviderFactory(TestAppCheckProviderFactory()) |
| 25 | + let options = FirebaseOptions( |
| 26 | + googleAppID: "1:123456789:ios:abc123", |
| 27 | + gcmSenderID: "123456789" |
| 28 | + ) |
| 29 | + options.projectID = "test_project_id" |
| 30 | + options.apiKey = "test_api_key" |
| 31 | + FirebaseApp.configure(name: appName, options: options) |
| 32 | + |
| 33 | + app = FirebaseApp.app(name: appName) |
| 34 | + } |
| 35 | + |
| 36 | + override func tearDown() { |
| 37 | + let semaphore = DispatchSemaphore(value: 0) |
| 38 | + app.delete { _ in |
| 39 | + semaphore.signal() |
| 40 | + } |
| 41 | + semaphore.wait() |
| 42 | + } |
| 43 | + |
| 44 | + func testInitAppCheck() throws { |
| 45 | + let appCheck = AppCheck.appCheck(app: app) |
| 46 | + |
| 47 | + XCTAssertNotNil(appCheck) |
| 48 | + } |
| 49 | + |
| 50 | + func testInitAppCheckDebugProvider() throws { |
| 51 | + let debugProvider = AppCheckDebugProvider(app: app) |
| 52 | + |
| 53 | + XCTAssertNotNil(debugProvider) |
| 54 | + } |
| 55 | + |
| 56 | + func testInitAppCheckDebugProviderFactory() throws { |
| 57 | + let debugProvider = AppCheckDebugProviderFactory().createProvider(with: app) |
| 58 | + |
| 59 | + XCTAssertNotNil(debugProvider) |
| 60 | + } |
| 61 | + |
| 62 | + @available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, watchOS 9.0, *) |
| 63 | + func testInitDeviceCheckProvider() throws { |
| 64 | + let deviceCheckProvider = DeviceCheckProvider(app: app) |
| 65 | + |
| 66 | + XCTAssertNotNil(deviceCheckProvider) |
| 67 | + } |
| 68 | + |
| 69 | + @available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, watchOS 9.0, *) |
| 70 | + func testDeviceCheckProviderFactoryCreate() throws { |
| 71 | + let deviceCheckProvider = DeviceCheckProviderFactory().createProvider(with: app) |
| 72 | + |
| 73 | + XCTAssertNotNil(deviceCheckProvider) |
| 74 | + } |
| 75 | + |
| 76 | + @available(iOS 14.0, macOS 11.3, macCatalyst 14.5, tvOS 15.0, watchOS 9.0, *) |
| 77 | + func testInitAppAttestProvider() throws { |
| 78 | + let appAttestProvider = AppAttestProvider(app: app) |
| 79 | + |
| 80 | + XCTAssertNotNil(appAttestProvider) |
| 81 | + } |
| 82 | + |
| 83 | + // The following test is disabled on macOS since `token(forcingRefresh:handler:)` requires a |
| 84 | + // provisioning profile to access the keychain to cache tokens. |
| 85 | + // See go/firebase-macos-keychain-popups for more details. |
| 86 | + #if !os(macOS) && !targetEnvironment(macCatalyst) |
| 87 | + func testGetToken() throws { |
| 88 | + guard let appCheck = AppCheck.appCheck(app: app) else { |
| 89 | + XCTFail("AppCheck instance is nil.") |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + let expectation = XCTestExpectation() |
| 94 | + appCheck.token(forcingRefresh: true) { token, error in |
| 95 | + XCTAssertNil(error) |
| 96 | + XCTAssertNotNil(token) |
| 97 | + XCTAssertEqual(token?.token, TestAppCheckProvider.tokenValue) |
| 98 | + expectation.fulfill() |
| 99 | + } |
| 100 | + |
| 101 | + wait(for: [expectation], timeout: 0.5) |
| 102 | + } |
| 103 | + #endif // !os(macOS) && !targetEnvironment(macCatalyst) |
| 104 | +} |
| 105 | + |
| 106 | +class TestAppCheckProvider: NSObject, AppCheckProvider { |
| 107 | + static let tokenValue = "TestToken" |
| 108 | + |
| 109 | + func getToken(completion handler: @escaping (AppCheckToken?, Error?) -> Void) { |
| 110 | + let token = AppCheckToken( |
| 111 | + token: TestAppCheckProvider.tokenValue, |
| 112 | + expirationDate: Date.distantFuture |
| 113 | + ) |
| 114 | + handler(token, nil) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +class TestAppCheckProviderFactory: NSObject, AppCheckProviderFactory { |
| 119 | + func createProvider(with app: FirebaseApp) -> AppCheckProvider? { |
| 120 | + return TestAppCheckProvider() |
| 121 | + } |
| 122 | +} |
0 commit comments