HTTPSCallableTests.swift 6.4 KB

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