IntegrationTests.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 the Objective C
  20. /// 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() throws {
  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. expectation.fulfill()
  90. } catch {
  91. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  92. }
  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() throws {
  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. expectation.fulfill()
  131. } catch {
  132. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  133. }
  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() throws {
  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. expectation.fulfill()
  176. } catch {
  177. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  178. }
  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() throws {
  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. expectation.fulfill()
  214. } catch {
  215. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  216. }
  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() throws {
  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. expectation.fulfill()
  244. } catch {
  245. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  246. }
  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. // No parameters to call should be the same as passing nil.
  263. // If no parameters are required, then the non-typed API
  264. // is more appropriate since it specifically avoids defining
  265. // type.
  266. // func testParameterless() {
  267. // }
  268. //
  269. //
  270. func testMissingResult() throws {
  271. let expectation = expectation(description: #function)
  272. let function = functions.httpsCallable(
  273. "missingResultTest",
  274. requestAs: Int?.self,
  275. responseAs: Int?.self
  276. )
  277. function.call(nil) { result in
  278. do {
  279. _ = try result.get()
  280. } catch {
  281. let error = error as NSError
  282. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  283. XCTAssertEqual("Response is missing data field.", error.localizedDescription)
  284. expectation.fulfill()
  285. }
  286. }
  287. waitForExpectations(timeout: 5)
  288. }
  289. #if compiler(>=5.5) && canImport(_Concurrency)
  290. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  291. func testMissingResultAsync() async {
  292. let function = functions.httpsCallable(
  293. "missingResultTest",
  294. requestAs: Int?.self,
  295. responseAs: Int?.self
  296. )
  297. do {
  298. _ = try await function.call(nil)
  299. XCTFail("Failed to throw error for missing result")
  300. } catch {
  301. let error = error as NSError
  302. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  303. XCTAssertEqual("Response is missing data field.", error.localizedDescription)
  304. }
  305. }
  306. #endif
  307. func testUnhandledError() throws {
  308. let expectation = expectation(description: #function)
  309. let function = functions.httpsCallable(
  310. "unhandledErrorTest",
  311. requestAs: [Int].self,
  312. responseAs: Int.self
  313. )
  314. function.call([]) { result in
  315. do {
  316. _ = try result.get()
  317. } catch {
  318. let error = error as NSError
  319. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  320. XCTAssertEqual("INTERNAL", error.localizedDescription)
  321. expectation.fulfill()
  322. }
  323. }
  324. XCTAssert(true)
  325. waitForExpectations(timeout: 5)
  326. }
  327. #if compiler(>=5.5) && canImport(_Concurrency)
  328. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  329. func testUnhandledErrorAsync() async {
  330. let function = functions.httpsCallable(
  331. "unhandledErrorTest",
  332. requestAs: [Int].self,
  333. responseAs: Int.self
  334. )
  335. do {
  336. _ = try await function.call([])
  337. XCTFail("Failed to throw error for missing result")
  338. } catch {
  339. let error = error as NSError
  340. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  341. XCTAssertEqual("INTERNAL", error.localizedDescription)
  342. }
  343. }
  344. #endif
  345. func testUnknownError() throws {
  346. let expectation = expectation(description: #function)
  347. let function = functions.httpsCallable(
  348. "unknownErrorTest",
  349. requestAs: [Int].self,
  350. responseAs: Int.self
  351. )
  352. function.call([]) { result in
  353. do {
  354. _ = try result.get()
  355. } catch {
  356. let error = error as NSError
  357. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  358. XCTAssertEqual("INTERNAL", error.localizedDescription)
  359. expectation.fulfill()
  360. }
  361. }
  362. waitForExpectations(timeout: 5)
  363. }
  364. #if compiler(>=5.5) && canImport(_Concurrency)
  365. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  366. func testUnknownErrorAsync() async {
  367. let function = functions.httpsCallable(
  368. "unknownErrorTest",
  369. requestAs: [Int].self,
  370. responseAs: Int.self
  371. )
  372. do {
  373. _ = try await function.call([])
  374. XCTAssertFalse(true, "Failed to throw error for missing result")
  375. } catch {
  376. let error = error as NSError
  377. XCTAssertEqual(FunctionsErrorCode.internal.rawValue, error.code)
  378. XCTAssertEqual("INTERNAL", error.localizedDescription)
  379. }
  380. }
  381. #endif
  382. func testExplicitError() throws {
  383. let expectation = expectation(description: #function)
  384. let function = functions.httpsCallable(
  385. "explicitErrorTest",
  386. requestAs: [Int].self,
  387. responseAs: Int.self
  388. )
  389. function.call([]) { result in
  390. do {
  391. _ = try result.get()
  392. } catch {
  393. let error = error as NSError
  394. XCTAssertEqual(FunctionsErrorCode.outOfRange.rawValue, error.code)
  395. XCTAssertEqual("explicit nope", error.localizedDescription)
  396. XCTAssertEqual(["start": 10 as Int32, "end": 20 as Int32, "long": 30],
  397. error.userInfo[FunctionsErrorDetailsKey] as! [String: Int32])
  398. expectation.fulfill()
  399. }
  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() throws {
  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. }
  439. }
  440. waitForExpectations(timeout: 5)
  441. }
  442. #if compiler(>=5.5) && canImport(_Concurrency)
  443. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  444. func testHttpErrorAsync() async {
  445. let function = functions.httpsCallable(
  446. "httpErrorTest",
  447. requestAs: [Int].self,
  448. responseAs: Int.self
  449. )
  450. do {
  451. _ = try await function.call([])
  452. XCTAssertFalse(true, "Failed to throw error for missing result")
  453. } catch {
  454. let error = error as NSError
  455. XCTAssertEqual(FunctionsErrorCode.invalidArgument.rawValue, error.code)
  456. }
  457. }
  458. #endif
  459. func testTimeout() throws {
  460. let expectation = expectation(description: #function)
  461. var function = functions.httpsCallable(
  462. "timeoutTest",
  463. requestAs: [Int].self,
  464. responseAs: Int.self
  465. )
  466. function.timeoutInterval = 0.05
  467. function.call([]) { result in
  468. do {
  469. _ = try result.get()
  470. } catch {
  471. let error = error as NSError
  472. XCTAssertEqual(FunctionsErrorCode.deadlineExceeded.rawValue, error.code)
  473. XCTAssertEqual("DEADLINE EXCEEDED", error.localizedDescription)
  474. XCTAssertNil(error.userInfo[FunctionsErrorDetailsKey])
  475. expectation.fulfill()
  476. }
  477. }
  478. waitForExpectations(timeout: 5)
  479. }
  480. #if compiler(>=5.5) && canImport(_Concurrency)
  481. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  482. func testTimeoutAsync() async {
  483. var function = functions.httpsCallable(
  484. "timeoutTest",
  485. requestAs: [Int].self,
  486. responseAs: Int.self
  487. )
  488. function.timeoutInterval = 0.05
  489. do {
  490. _ = try await function.call([])
  491. XCTAssertFalse(true, "Failed to throw error for missing result")
  492. } catch {
  493. let error = error as NSError
  494. XCTAssertEqual(FunctionsErrorCode.deadlineExceeded.rawValue, error.code)
  495. XCTAssertEqual("DEADLINE EXCEEDED", error.localizedDescription)
  496. XCTAssertNil(error.userInfo[FunctionsErrorDetailsKey])
  497. }
  498. }
  499. #endif
  500. func testCallAsFunction() throws {
  501. let expectation = expectation(description: #function)
  502. let data = DataTestRequest(
  503. bool: true,
  504. int: 2,
  505. long: 9_876_543_210,
  506. string: "four",
  507. array: [5, 6],
  508. null: nil
  509. )
  510. let function = functions.httpsCallable("dataTest",
  511. requestAs: DataTestRequest.self,
  512. responseAs: DataTestResponse.self)
  513. function(data) { result in
  514. do {
  515. let response = try result.get()
  516. let expected = DataTestResponse(
  517. message: "stub response",
  518. long: 420,
  519. code: 42
  520. )
  521. XCTAssertEqual(response, expected)
  522. expectation.fulfill()
  523. } catch {
  524. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  525. }
  526. }
  527. waitForExpectations(timeout: 5)
  528. }
  529. #if compiler(>=5.5) && canImport(_Concurrency)
  530. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  531. func testCallAsFunctionAsync() async throws {
  532. let data = DataTestRequest(
  533. bool: true,
  534. int: 2,
  535. long: 9_876_543_210,
  536. string: "four",
  537. array: [5, 6],
  538. null: nil
  539. )
  540. let function = functions.httpsCallable("dataTest",
  541. requestAs: DataTestRequest.self,
  542. responseAs: DataTestResponse.self)
  543. let response = try await function(data)
  544. let expected = DataTestResponse(
  545. message: "stub response",
  546. long: 420,
  547. code: 42
  548. )
  549. XCTAssertEqual(response, expected)
  550. }
  551. #endif
  552. func testInferredTypes() throws {
  553. let expectation = expectation(description: #function)
  554. let data = DataTestRequest(
  555. bool: true,
  556. int: 2,
  557. long: 9_876_543_210,
  558. string: "four",
  559. array: [5, 6],
  560. null: nil
  561. )
  562. let function: Callable<DataTestRequest, DataTestResponse> = functions.httpsCallable("dataTest")
  563. function(data) { result in
  564. do {
  565. let response = try result.get()
  566. let expected = DataTestResponse(
  567. message: "stub response",
  568. long: 420,
  569. code: 42
  570. )
  571. XCTAssertEqual(response, expected)
  572. expectation.fulfill()
  573. } catch {
  574. XCTAssert(false, "Failed to unwrap the function result: \(error)")
  575. }
  576. }
  577. waitForExpectations(timeout: 5)
  578. }
  579. #if compiler(>=5.5) && canImport(_Concurrency)
  580. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  581. func testInferredTyesAsync() async throws {
  582. let data = DataTestRequest(
  583. bool: true,
  584. int: 2,
  585. long: 9_876_543_210,
  586. string: "four",
  587. array: [5, 6],
  588. null: nil
  589. )
  590. let function: Callable<DataTestRequest, DataTestResponse> = functions
  591. .httpsCallable("dataTest")
  592. let response = try await function(data)
  593. let expected = DataTestResponse(
  594. message: "stub response",
  595. long: 420,
  596. code: 42
  597. )
  598. XCTAssertEqual(response, expected)
  599. }
  600. #endif
  601. }