IntegrationTests.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 FirebaseFunctions
  16. import FirebaseFunctionsSwift
  17. import FirebaseFunctionsTestingSupport
  18. import XCTest
  19. /// This file was intitialized as a direct port of `FirebaseFunctionsSwift/Tests/IntegrationTests.swift`
  20. /// which itself was ported from the Objective C `FirebaseFunctions/Tests/Integration/FIRIntegrationTests.m`
  21. ///
  22. /// The tests require the emulator to be running with `FirebaseFunctions/Backend/start.sh synchronous`
  23. /// The Firebase Functions called in the tests are implemented in `FirebaseFunctions/Backend/index.js`.
  24. struct DataTestRequest: Encodable {
  25. var bool: Bool
  26. var int: Int32
  27. var long: Int64
  28. var string: String
  29. var array: [Int32]
  30. // NOTE: Auto-synthesized Encodable conformance uses 'encodeIfPresent' to
  31. // encode Optional values. To encode Optional.none as null you either need
  32. // to write a manual encodable conformance or use a helper like the
  33. // propertyWrapper here:
  34. @NullEncodable var null: Bool?
  35. }
  36. @propertyWrapper
  37. struct NullEncodable<T>: Encodable where T: Encodable {
  38. var wrappedValue: T?
  39. init(wrappedValue: T?) {
  40. self.wrappedValue = wrappedValue
  41. }
  42. func encode(to encoder: Encoder) throws {
  43. var container = encoder.singleValueContainer()
  44. switch wrappedValue {
  45. case let .some(value): try container.encode(value)
  46. case .none: try container.encodeNil()
  47. }
  48. }
  49. }
  50. struct DataTestResponse: Decodable, Equatable {
  51. var message: String
  52. var long: Int64
  53. var code: Int32
  54. }
  55. class IntegrationTests: XCTestCase {
  56. let functions = FunctionsFake(
  57. projectID: "functions-integration-test",
  58. region: "us-central1",
  59. customDomain: nil,
  60. withToken: nil
  61. )
  62. let projectID = "functions-swift-integration-test"
  63. override func setUp() {
  64. super.setUp()
  65. functions.useLocalhost()
  66. }
  67. func testData() {
  68. let expectation = expectation(description: #function)
  69. let data = DataTestRequest(
  70. bool: true,
  71. int: 2,
  72. long: 9_876_543_210,
  73. string: "four",
  74. array: [5, 6],
  75. null: nil
  76. )
  77. let function = functions.httpsCallable("dataTest",
  78. requestAs: DataTestRequest.self,
  79. responseAs: DataTestResponse.self)
  80. function.call(data) { result in
  81. do {
  82. let response = try result.get()
  83. let expected = DataTestResponse(
  84. message: "stub response",
  85. long: 420,
  86. code: 42
  87. )
  88. XCTAssertEqual(response, expected)
  89. } catch {
  90. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  91. }
  92. expectation.fulfill()
  93. }
  94. waitForExpectations(timeout: 5)
  95. }
  96. #if compiler(>=5.5) && canImport(_Concurrency)
  97. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  98. func testDataAsync() async throws {
  99. let data = DataTestRequest(
  100. bool: true,
  101. int: 2,
  102. long: 9_876_543_210,
  103. string: "four",
  104. array: [5, 6],
  105. null: nil
  106. )
  107. let function = functions.httpsCallable("dataTest",
  108. requestAs: DataTestRequest.self,
  109. responseAs: DataTestResponse.self)
  110. let response = try await function.call(data)
  111. let expected = DataTestResponse(
  112. message: "stub response",
  113. long: 420,
  114. code: 42
  115. )
  116. XCTAssertEqual(response, expected)
  117. }
  118. #endif
  119. func testScalar() {
  120. let expectation = expectation(description: #function)
  121. let function = functions.httpsCallable(
  122. "scalarTest",
  123. requestAs: Int16.self,
  124. responseAs: Int.self
  125. )
  126. function.call(17) { result in
  127. do {
  128. let response = try result.get()
  129. XCTAssertEqual(response, 76)
  130. } catch {
  131. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  132. }
  133. expectation.fulfill()
  134. }
  135. waitForExpectations(timeout: 5)
  136. }
  137. #if compiler(>=5.5) && canImport(_Concurrency)
  138. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  139. func testScalarAsync() async throws {
  140. let function = functions.httpsCallable(
  141. "scalarTest",
  142. requestAs: Int16.self,
  143. responseAs: Int.self
  144. )
  145. let result = try await function.call(17)
  146. XCTAssertEqual(result, 76)
  147. }
  148. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  149. func testScalarAsyncAlternateSignature() async throws {
  150. let function: Callable<Int16, Int> = functions.httpsCallable("scalarTest")
  151. let result = try await function.call(17)
  152. XCTAssertEqual(result, 76)
  153. }
  154. #endif
  155. func testToken() {
  156. // Recreate functions with a token.
  157. let functions = FunctionsFake(
  158. projectID: "functions-integration-test",
  159. region: "us-central1",
  160. customDomain: nil,
  161. withToken: "token"
  162. )
  163. functions.useLocalhost()
  164. let expectation = expectation(description: #function)
  165. let function = functions.httpsCallable(
  166. "FCMTokenTest",
  167. requestAs: [String: Int].self,
  168. responseAs: [String: Int].self
  169. )
  170. XCTAssertNotNil(function)
  171. function.call([:]) { result in
  172. do {
  173. let data = try result.get()
  174. XCTAssertEqual(data, [:])
  175. } catch {
  176. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  177. }
  178. expectation.fulfill()
  179. }
  180. waitForExpectations(timeout: 5)
  181. }
  182. #if compiler(>=5.5) && canImport(_Concurrency)
  183. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  184. func testTokenAsync() async throws {
  185. // Recreate functions with a token.
  186. let functions = FunctionsFake(
  187. projectID: "functions-integration-test",
  188. region: "us-central1",
  189. customDomain: nil,
  190. withToken: "token"
  191. )
  192. functions.useLocalhost()
  193. let function = functions.httpsCallable(
  194. "FCMTokenTest",
  195. requestAs: [String: Int].self,
  196. responseAs: [String: Int].self
  197. )
  198. let data = try await function.call([:])
  199. XCTAssertEqual(data, [:])
  200. }
  201. #endif
  202. func testFCMToken() {
  203. let expectation = expectation(description: #function)
  204. let function = functions.httpsCallable(
  205. "FCMTokenTest",
  206. requestAs: [String: Int].self,
  207. responseAs: [String: Int].self
  208. )
  209. function.call([:]) { result in
  210. do {
  211. let data = try result.get()
  212. XCTAssertEqual(data, [:])
  213. } catch {
  214. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  215. }
  216. expectation.fulfill()
  217. }
  218. waitForExpectations(timeout: 5)
  219. }
  220. #if compiler(>=5.5) && canImport(_Concurrency)
  221. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  222. func testFCMTokenAsync() async throws {
  223. let function = functions.httpsCallable(
  224. "FCMTokenTest",
  225. requestAs: [String: Int].self,
  226. responseAs: [String: Int].self
  227. )
  228. let data = try await function.call([:])
  229. XCTAssertEqual(data, [:])
  230. }
  231. #endif
  232. func testNull() {
  233. let expectation = expectation(description: #function)
  234. let function = functions.httpsCallable(
  235. "nullTest",
  236. requestAs: Int?.self,
  237. responseAs: Int?.self
  238. )
  239. function.call(nil) { result in
  240. do {
  241. let data = try result.get()
  242. XCTAssertEqual(data, nil)
  243. } catch {
  244. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  245. }
  246. expectation.fulfill()
  247. }
  248. waitForExpectations(timeout: 5)
  249. }
  250. #if compiler(>=5.5) && canImport(_Concurrency)
  251. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  252. func testNullAsync() async throws {
  253. let function = functions.httpsCallable(
  254. "nullTest",
  255. requestAs: Int?.self,
  256. responseAs: Int?.self
  257. )
  258. let data = try await function.call(nil)
  259. XCTAssertEqual(data, nil)
  260. }
  261. #endif
  262. func testMissingResult() {
  263. let expectation = expectation(description: #function)
  264. let function = functions.httpsCallable(
  265. "missingResultTest",
  266. requestAs: Int?.self,
  267. responseAs: Int?.self
  268. )
  269. function.call(nil) { result in
  270. do {
  271. _ = try result.get()
  272. } catch {
  273. let error = error as NSError
  274. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  275. XCTAssertEqual("Response is missing data field.", error.localizedDescription)
  276. expectation.fulfill()
  277. return
  278. }
  279. XCTFail("Failed to throw error for missing result")
  280. }
  281. waitForExpectations(timeout: 5)
  282. }
  283. #if compiler(>=5.5) && canImport(_Concurrency)
  284. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  285. func testMissingResultAsync() async {
  286. let function = functions.httpsCallable(
  287. "missingResultTest",
  288. requestAs: Int?.self,
  289. responseAs: Int?.self
  290. )
  291. do {
  292. _ = try await function.call(nil)
  293. XCTFail("Failed to throw error for missing result")
  294. } catch {
  295. let error = error as NSError
  296. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  297. XCTAssertEqual("Response is missing data field.", error.localizedDescription)
  298. }
  299. }
  300. #endif
  301. func testUnhandledError() {
  302. let expectation = expectation(description: #function)
  303. let function = functions.httpsCallable(
  304. "unhandledErrorTest",
  305. requestAs: [Int].self,
  306. responseAs: Int.self
  307. )
  308. function.call([]) { result in
  309. do {
  310. _ = try result.get()
  311. } catch {
  312. let error = error as NSError
  313. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  314. XCTAssertEqual("INTERNAL", error.localizedDescription)
  315. expectation.fulfill()
  316. return
  317. }
  318. XCTFail("Failed to throw error for missing result")
  319. }
  320. XCTAssert(true)
  321. waitForExpectations(timeout: 5)
  322. }
  323. #if compiler(>=5.5) && canImport(_Concurrency)
  324. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  325. func testUnhandledErrorAsync() async {
  326. let function = functions.httpsCallable(
  327. "unhandledErrorTest",
  328. requestAs: [Int].self,
  329. responseAs: Int.self
  330. )
  331. do {
  332. _ = try await function.call([])
  333. XCTFail("Failed to throw error for missing result")
  334. } catch {
  335. let error = error as NSError
  336. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  337. XCTAssertEqual("INTERNAL", error.localizedDescription)
  338. }
  339. }
  340. #endif
  341. func testUnknownError() {
  342. let expectation = expectation(description: #function)
  343. let function = functions.httpsCallable(
  344. "unknownErrorTest",
  345. requestAs: [Int].self,
  346. responseAs: Int.self
  347. )
  348. function.call([]) { result in
  349. do {
  350. _ = try result.get()
  351. } catch {
  352. let error = error as NSError
  353. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  354. XCTAssertEqual("INTERNAL", error.localizedDescription)
  355. expectation.fulfill()
  356. return
  357. }
  358. XCTFail("Failed to throw error for missing result")
  359. }
  360. waitForExpectations(timeout: 5)
  361. }
  362. #if compiler(>=5.5) && canImport(_Concurrency)
  363. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  364. func testUnknownErrorAsync() async {
  365. let function = functions.httpsCallable(
  366. "unknownErrorTest",
  367. requestAs: [Int].self,
  368. responseAs: Int.self
  369. )
  370. do {
  371. _ = try await function.call([])
  372. XCTAssertFalse(true, "Failed to throw error for missing result")
  373. } catch {
  374. let error = error as NSError
  375. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  376. XCTAssertEqual("INTERNAL", error.localizedDescription)
  377. }
  378. }
  379. #endif
  380. func testExplicitError() {
  381. let expectation = expectation(description: #function)
  382. let function = functions.httpsCallable(
  383. "explicitErrorTest",
  384. requestAs: [Int].self,
  385. responseAs: Int.self
  386. )
  387. function.call([]) { result in
  388. do {
  389. _ = try result.get()
  390. } catch {
  391. let error = error as NSError
  392. XCTAssertEqual(FunctionsErrorCode.outOfRange.rawValue, error.code)
  393. XCTAssertEqual("explicit nope", error.localizedDescription)
  394. XCTAssertEqual(["start": 10 as Int32, "end": 20 as Int32, "long": 30],
  395. error.userInfo[FunctionsErrorDetailsKey] as! [String: Int32])
  396. expectation.fulfill()
  397. return
  398. }
  399. XCTFail("Failed to throw error for missing result")
  400. }
  401. waitForExpectations(timeout: 5)
  402. }
  403. #if compiler(>=5.5) && canImport(_Concurrency)
  404. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  405. func testExplicitErrorAsync() async {
  406. let function = functions.httpsCallable(
  407. "explicitErrorTest",
  408. requestAs: [Int].self,
  409. responseAs: Int.self
  410. )
  411. do {
  412. _ = try await function.call([])
  413. XCTAssertFalse(true, "Failed to throw error for missing result")
  414. } catch {
  415. let error = error as NSError
  416. XCTAssertEqual(FunctionsErrorCode.outOfRange.rawValue, error.code)
  417. XCTAssertEqual("explicit nope", error.localizedDescription)
  418. XCTAssertEqual(["start": 10 as Int32, "end": 20 as Int32, "long": 30],
  419. error.userInfo[FunctionsErrorDetailsKey] as! [String: Int32])
  420. }
  421. }
  422. #endif
  423. func testHttpError() {
  424. let expectation = expectation(description: #function)
  425. let function = functions.httpsCallable(
  426. "httpErrorTest",
  427. requestAs: [Int].self,
  428. responseAs: Int.self
  429. )
  430. XCTAssertNotNil(function)
  431. function.call([]) { result in
  432. do {
  433. _ = try result.get()
  434. } catch {
  435. let error = error as NSError
  436. XCTAssertEqual(FunctionsErrorCode.invalidArgument.rawValue, error.code)
  437. expectation.fulfill()
  438. return
  439. }
  440. XCTFail("Failed to throw error for missing result")
  441. }
  442. waitForExpectations(timeout: 5)
  443. }
  444. #if compiler(>=5.5) && canImport(_Concurrency)
  445. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  446. func testHttpErrorAsync() async {
  447. let function = functions.httpsCallable(
  448. "httpErrorTest",
  449. requestAs: [Int].self,
  450. responseAs: Int.self
  451. )
  452. do {
  453. _ = try await function.call([])
  454. XCTAssertFalse(true, "Failed to throw error for missing result")
  455. } catch {
  456. let error = error as NSError
  457. XCTAssertEqual(FunctionsErrorCode.invalidArgument.rawValue, error.code)
  458. }
  459. }
  460. #endif
  461. func testTimeout() {
  462. let expectation = expectation(description: #function)
  463. var function = functions.httpsCallable(
  464. "timeoutTest",
  465. requestAs: [Int].self,
  466. responseAs: Int.self
  467. )
  468. function.timeoutInterval = 0.05
  469. function.call([]) { result in
  470. do {
  471. _ = try result.get()
  472. } catch {
  473. let error = error as NSError
  474. XCTAssertEqual(FunctionsErrorCode.deadlineExceeded.rawValue, error.code)
  475. XCTAssertEqual("DEADLINE EXCEEDED", error.localizedDescription)
  476. XCTAssertNil(error.userInfo[FunctionsErrorDetailsKey])
  477. expectation.fulfill()
  478. return
  479. }
  480. XCTFail("Failed to throw error for missing result")
  481. }
  482. waitForExpectations(timeout: 5)
  483. }
  484. #if compiler(>=5.5) && canImport(_Concurrency)
  485. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  486. func testTimeoutAsync() async {
  487. var function = functions.httpsCallable(
  488. "timeoutTest",
  489. requestAs: [Int].self,
  490. responseAs: Int.self
  491. )
  492. function.timeoutInterval = 0.05
  493. do {
  494. _ = try await function.call([])
  495. XCTAssertFalse(true, "Failed to throw error for missing result")
  496. } catch {
  497. let error = error as NSError
  498. XCTAssertEqual(FunctionsErrorCode.deadlineExceeded.rawValue, error.code)
  499. XCTAssertEqual("DEADLINE EXCEEDED", error.localizedDescription)
  500. XCTAssertNil(error.userInfo[FunctionsErrorDetailsKey])
  501. }
  502. }
  503. #endif
  504. func testCallAsFunction() {
  505. let expectation = expectation(description: #function)
  506. let data = DataTestRequest(
  507. bool: true,
  508. int: 2,
  509. long: 9_876_543_210,
  510. string: "four",
  511. array: [5, 6],
  512. null: nil
  513. )
  514. let function = functions.httpsCallable("dataTest",
  515. requestAs: DataTestRequest.self,
  516. responseAs: DataTestResponse.self)
  517. function(data) { result in
  518. do {
  519. let response = try result.get()
  520. let expected = DataTestResponse(
  521. message: "stub response",
  522. long: 420,
  523. code: 42
  524. )
  525. XCTAssertEqual(response, expected)
  526. expectation.fulfill()
  527. } catch {
  528. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  529. }
  530. }
  531. waitForExpectations(timeout: 5)
  532. }
  533. #if compiler(>=5.5) && canImport(_Concurrency)
  534. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  535. func testCallAsFunctionAsync() async throws {
  536. let data = DataTestRequest(
  537. bool: true,
  538. int: 2,
  539. long: 9_876_543_210,
  540. string: "four",
  541. array: [5, 6],
  542. null: nil
  543. )
  544. let function = functions.httpsCallable("dataTest",
  545. requestAs: DataTestRequest.self,
  546. responseAs: DataTestResponse.self)
  547. let response = try await function(data)
  548. let expected = DataTestResponse(
  549. message: "stub response",
  550. long: 420,
  551. code: 42
  552. )
  553. XCTAssertEqual(response, expected)
  554. }
  555. #endif
  556. func testInferredTypes() {
  557. let expectation = expectation(description: #function)
  558. let data = DataTestRequest(
  559. bool: true,
  560. int: 2,
  561. long: 9_876_543_210,
  562. string: "four",
  563. array: [5, 6],
  564. null: nil
  565. )
  566. let function: Callable<DataTestRequest, DataTestResponse> = functions.httpsCallable("dataTest")
  567. function(data) { result in
  568. do {
  569. let response = try result.get()
  570. let expected = DataTestResponse(
  571. message: "stub response",
  572. long: 420,
  573. code: 42
  574. )
  575. XCTAssertEqual(response, expected)
  576. expectation.fulfill()
  577. } catch {
  578. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  579. }
  580. }
  581. waitForExpectations(timeout: 5)
  582. }
  583. #if compiler(>=5.5) && canImport(_Concurrency)
  584. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  585. func testInferredTyesAsync() async throws {
  586. let data = DataTestRequest(
  587. bool: true,
  588. int: 2,
  589. long: 9_876_543_210,
  590. string: "four",
  591. array: [5, 6],
  592. null: nil
  593. )
  594. let function: Callable<DataTestRequest, DataTestResponse> = functions
  595. .httpsCallable("dataTest")
  596. let response = try await function(data)
  597. let expected = DataTestResponse(
  598. message: "stub response",
  599. long: 420,
  600. code: 42
  601. )
  602. XCTAssertEqual(response, expected)
  603. }
  604. #endif
  605. }