Files
firebase-ios-sdk/FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift
T
0cbe4f2920 Firebase App Check One-time Use Tokens for Callable Functions SDK (#11270)
* draft pr for callable functions

* Adjust Functions podspec dependency on AppCheckInterop

* Add 'HTTPSCallableOptions' class

* Re-implement with new HTTPSCallableOptions type

* Revert and update Objective-C API tests

* Mark new AppCheckInterop API optional for backwards compatibility

* Preserve existing codable Functions API

* Revert integration tests

* Style

* Fix some test build breakages

* Add changelog entry

* Style

* Fix AppCheck tests

* Cleanup current approach

* Re-organize tests

* Add 'options' param to 'getContext' call

* Add success case unit test

* Shorten expectation waiting timeout

* Add remaining unit tests

* Amend current test

* Rename 'limitedUseTokens' to 'requireLimitedUseTokens'

* Fix build and tests

* Fix some renaming misses

* Fix some renaming misses (1)

* Update FirebaseFunctions/CHANGELOG.md

Co-authored-by: Andrew Heard <andrewheard@google.com>

* Update FirebaseFunctions/Sources/Functions.swift

Co-authored-by: Andrew Heard <andrewheard@google.com>

* Update FirebaseFunctions/Sources/Functions.swift

Co-authored-by: Andrew Heard <andrewheard@google.com>

* Update FirebaseFunctions/Sources/Functions.swift

Co-authored-by: Andrew Heard <andrewheard@google.com>

* Update FirebaseFunctions/Sources/Functions.swift

Co-authored-by: Andrew Heard <andrewheard@google.com>

* Update FirebaseFunctions/Sources/HTTPSCallableOptions.swift

Co-authored-by: Andrew Heard <andrewheard@google.com>

* [App Check] Always return fresh limitedUseToken (#11298)

Updated `limitedUseTokenWithCompletion:` to always return a fresh limited-used token in every invocation (removed the `ongoingLimitedUseTokenPromise` property).

---------

Co-authored-by: Nick Cooke <nickcooke@google.com>
Co-authored-by: Nick Cooke <36927374+ncooke3@users.noreply.github.com>
Co-authored-by: Andrew Heard <andrewheard@google.com>
2023-05-16 18:33:15 -04:00

200 lines
6.5 KiB
Swift

// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import Combine
import FirebaseCore
@testable import FirebaseFunctions
import FirebaseFunctionsCombineSwift
import FirebaseAuthInterop
import FirebaseAppCheckInterop
import FirebaseMessagingInterop
import GTMSessionFetcherCore
import XCTest
// hardcoded in HTTPSCallable.swift
private let timeoutInterval: TimeInterval = 70.0
private let expectationTimeout: TimeInterval = 2
class MockFunctions: Functions {
let mockCallFunction: () throws -> HTTPSCallableResult
var verifyParameters: ((_ name: String, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
override func callFunction(name: String,
withObject data: Any?,
options: HTTPSCallableOptions?,
timeout: TimeInterval,
completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
do {
try verifyParameters?(name, data, timeout)
let result = try mockCallFunction()
completion(.success(result))
} catch {
completion(.failure(error))
}
}
init(mockCallFunction: @escaping () throws -> HTTPSCallableResult) {
self.mockCallFunction = mockCallFunction
super.init(
projectID: "dummy-project",
region: "",
customDomain: nil,
auth: nil,
messaging: nil,
appCheck: nil,
fetcherService: GTMSessionFetcherService()
)
}
}
public class HTTPSCallableResultFake: HTTPSCallableResult {
let fakeData: String
init(data: String) {
fakeData = data
super.init(data: data)
}
}
@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
class HTTPSCallableTests: XCTestCase {
func testCallWithoutParametersSuccess() {
// given
var cancellables = Set<AnyCancellable>()
let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
let functionWasCalledExpectation = expectation(description: "Function was called")
let expectedResult = "mockResult w/o parameters"
let functions = MockFunctions {
httpsFunctionWasCalledExpectation.fulfill()
return HTTPSCallableResultFake(data: expectedResult)
}
let dummyFunction = functions.httpsCallable("dummyFunction")
// when
dummyFunction.call()
.sink { completion in
switch completion {
case .finished:
print("Finished")
case let .failure(error):
XCTFail("💥 Something went wrong: \(error)")
}
} receiveValue: { functionResult in
guard let result = functionResult.data as? String else {
XCTFail("Expected String data")
return
}
XCTAssertEqual(result, expectedResult)
functionWasCalledExpectation.fulfill()
}
.store(in: &cancellables)
// then
wait(
for: [functionWasCalledExpectation, httpsFunctionWasCalledExpectation],
timeout: expectationTimeout
)
}
func testCallWithParametersSuccess() {
// given
var cancellables = Set<AnyCancellable>()
let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
let functionWasCalledExpectation = expectation(description: "Function was called")
let inputParameter = "input parameter"
let expectedResult = "mockResult w/ parameters: \(inputParameter)"
let functions = MockFunctions {
httpsFunctionWasCalledExpectation.fulfill()
return HTTPSCallableResultFake(data: expectedResult)
}
functions.verifyParameters = { name, data, timeout in
XCTAssertEqual(name as String, "dummyFunction")
XCTAssertEqual(data as? String, inputParameter)
XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
}
let dummyFunction = functions.httpsCallable("dummyFunction")
// when
dummyFunction.call(inputParameter)
.sink { completion in
switch completion {
case .finished:
print("Finished")
case let .failure(error):
XCTFail("💥 Something went wrong: \(error)")
}
} receiveValue: { functionResult in
guard let result = functionResult.data as? String else {
XCTFail("Expected String data")
return
}
XCTAssertEqual(result, expectedResult)
functionWasCalledExpectation.fulfill()
}
.store(in: &cancellables)
// then
wait(
for: [httpsFunctionWasCalledExpectation, functionWasCalledExpectation],
timeout: expectationTimeout
)
}
func testCallWithParametersFailure() {
// given
var cancellables = Set<AnyCancellable>()
let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
let functionCallFailedExpectation = expectation(description: "Function call failed")
let inputParameter = "input parameter"
let functions = MockFunctions {
httpsFunctionWasCalledExpectation.fulfill()
throw NSError(domain: FunctionsErrorDomain,
code: FunctionsErrorCode.internal.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Response is missing data field."])
}
functions.verifyParameters = { name, data, timeout in
XCTAssertEqual(name as String, "dummyFunction")
XCTAssertEqual(data as? String, inputParameter)
XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
}
let dummyFunction = functions.httpsCallable("dummyFunction")
// when
dummyFunction.call(inputParameter)
.sink { completion in
if case let .failure(error as NSError) = completion {
// Verify user mismatch error.
XCTAssertEqual(error.code, FunctionsErrorCode.internal.rawValue)
functionCallFailedExpectation.fulfill()
}
} receiveValue: { functionResult in
XCTFail("💥 result unexpected")
}
.store(in: &cancellables)
// then
wait(
for: [functionCallFailedExpectation, httpsFunctionWasCalledExpectation],
timeout: expectationTimeout
)
}
}