HTTPSCallableTests.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2021 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 Combine
  16. import FirebaseCore
  17. @testable import FirebaseFunctions
  18. import FirebaseFunctionsCombineSwift
  19. import FirebaseAuthInterop
  20. import FirebaseAppCheckInterop
  21. import FirebaseMessagingInterop
  22. import GTMSessionFetcherCore
  23. import XCTest
  24. // hardcoded in HTTPSCallable.swift
  25. private let timeoutInterval: TimeInterval = 70.0
  26. private let expectationTimeout: TimeInterval = 2
  27. class MockFunctions: Functions {
  28. let mockCallFunction: () throws -> HTTPSCallableResult
  29. var verifyParameters: ((_ name: String, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
  30. override func callFunction(name: String,
  31. withObject data: Any?,
  32. options: HTTPSCallableOptions?,
  33. timeout: TimeInterval,
  34. completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
  35. do {
  36. try verifyParameters?(name, data, timeout)
  37. let result = try mockCallFunction()
  38. completion(.success(result))
  39. } catch {
  40. completion(.failure(error))
  41. }
  42. }
  43. init(mockCallFunction: @escaping () throws -> HTTPSCallableResult) {
  44. self.mockCallFunction = mockCallFunction
  45. super.init(
  46. projectID: "dummy-project",
  47. region: "",
  48. customDomain: nil,
  49. auth: nil,
  50. messaging: nil,
  51. appCheck: nil,
  52. fetcherService: GTMSessionFetcherService()
  53. )
  54. }
  55. }
  56. public class HTTPSCallableResultFake: HTTPSCallableResult {
  57. let fakeData: String
  58. init(data: String) {
  59. fakeData = data
  60. super.init(data: data)
  61. }
  62. }
  63. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
  64. class HTTPSCallableTests: XCTestCase {
  65. func testCallWithoutParametersSuccess() {
  66. // given
  67. var cancellables = Set<AnyCancellable>()
  68. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  69. let functionWasCalledExpectation = expectation(description: "Function was called")
  70. let expectedResult = "mockResult w/o parameters"
  71. let functions = MockFunctions {
  72. httpsFunctionWasCalledExpectation.fulfill()
  73. return HTTPSCallableResultFake(data: expectedResult)
  74. }
  75. let dummyFunction = functions.httpsCallable("dummyFunction")
  76. // when
  77. dummyFunction.call()
  78. .sink { completion in
  79. switch completion {
  80. case .finished:
  81. print("Finished")
  82. case let .failure(error):
  83. XCTFail("💥 Something went wrong: \(error)")
  84. }
  85. } receiveValue: { functionResult in
  86. guard let result = functionResult.data as? String else {
  87. XCTFail("Expected String data")
  88. return
  89. }
  90. XCTAssertEqual(result, expectedResult)
  91. functionWasCalledExpectation.fulfill()
  92. }
  93. .store(in: &cancellables)
  94. // then
  95. wait(
  96. for: [functionWasCalledExpectation, httpsFunctionWasCalledExpectation],
  97. timeout: expectationTimeout
  98. )
  99. }
  100. func testCallWithParametersSuccess() {
  101. // given
  102. var cancellables = Set<AnyCancellable>()
  103. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  104. let functionWasCalledExpectation = expectation(description: "Function was called")
  105. let inputParameter = "input parameter"
  106. let expectedResult = "mockResult w/ parameters: \(inputParameter)"
  107. let functions = MockFunctions {
  108. httpsFunctionWasCalledExpectation.fulfill()
  109. return HTTPSCallableResultFake(data: expectedResult)
  110. }
  111. functions.verifyParameters = { name, data, timeout in
  112. XCTAssertEqual(name as String, "dummyFunction")
  113. XCTAssertEqual(data as? String, inputParameter)
  114. XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
  115. }
  116. let dummyFunction = functions.httpsCallable("dummyFunction")
  117. // when
  118. dummyFunction.call(inputParameter)
  119. .sink { completion in
  120. switch completion {
  121. case .finished:
  122. print("Finished")
  123. case let .failure(error):
  124. XCTFail("💥 Something went wrong: \(error)")
  125. }
  126. } receiveValue: { functionResult in
  127. guard let result = functionResult.data as? String else {
  128. XCTFail("Expected String data")
  129. return
  130. }
  131. XCTAssertEqual(result, expectedResult)
  132. functionWasCalledExpectation.fulfill()
  133. }
  134. .store(in: &cancellables)
  135. // then
  136. wait(
  137. for: [httpsFunctionWasCalledExpectation, functionWasCalledExpectation],
  138. timeout: expectationTimeout
  139. )
  140. }
  141. func testCallWithParametersFailure() {
  142. // given
  143. var cancellables = Set<AnyCancellable>()
  144. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  145. let functionCallFailedExpectation = expectation(description: "Function call failed")
  146. let inputParameter = "input parameter"
  147. let functions = MockFunctions {
  148. httpsFunctionWasCalledExpectation.fulfill()
  149. throw NSError(domain: FunctionsErrorDomain,
  150. code: FunctionsErrorCode.internal.rawValue,
  151. userInfo: [NSLocalizedDescriptionKey: "Response is missing data field."])
  152. }
  153. functions.verifyParameters = { name, data, timeout in
  154. XCTAssertEqual(name as String, "dummyFunction")
  155. XCTAssertEqual(data as? String, inputParameter)
  156. XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
  157. }
  158. let dummyFunction = functions.httpsCallable("dummyFunction")
  159. // when
  160. dummyFunction.call(inputParameter)
  161. .sink { completion in
  162. if case let .failure(error as NSError) = completion {
  163. // Verify user mismatch error.
  164. XCTAssertEqual(error.code, FunctionsErrorCode.internal.rawValue)
  165. functionCallFailedExpectation.fulfill()
  166. }
  167. } receiveValue: { functionResult in
  168. XCTFail("💥 result unexpected")
  169. }
  170. .store(in: &cancellables)
  171. // then
  172. wait(
  173. for: [functionCallFailedExpectation, httpsFunctionWasCalledExpectation],
  174. timeout: expectationTimeout
  175. )
  176. }
  177. }