StorageReferenceTests.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 XCTest
  17. @testable import FirebaseStorage
  18. class StorageReferenceTests: XCTestCase {
  19. override class func setUp() {
  20. FirebaseApp.configureForTests()
  21. }
  22. override class func tearDown() {
  23. FirebaseApp.app()?.delete { success in
  24. if success {
  25. print("Shut down app successfully.")
  26. } else {
  27. print("💥 There was a problem when shutting down the app..")
  28. }
  29. }
  30. }
  31. var storage: Storage?
  32. override func setUp() {
  33. let app = FirebaseApp.appForStorageUnitTestsWithName(name: "App")
  34. storage = Storage.storage(app: app)
  35. }
  36. func testReferenceWithNonExistentFileFails() {
  37. // Given
  38. var cancellables = Set<AnyCancellable>()
  39. let putFileExpectation = expectation(description: "Put file expectation")
  40. let tempFileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
  41. let tempFilePath = tempFileURL.appendingPathComponent("temp.data").absoluteString
  42. let ref = storage?.reference(withPath: tempFilePath)
  43. let dummyFileURL = URL(fileURLWithPath: "some_non_existing-folder/file.data")
  44. // When
  45. ref?.putFile(from: dummyFileURL, metadata: nil)
  46. .sink { completion in
  47. if case let .failure(error as NSError) = completion {
  48. putFileExpectation.fulfill()
  49. XCTAssertEqual(error.domain, StorageErrorDomain)
  50. XCTAssertEqual(error.code, StorageErrorCode.unknown.rawValue)
  51. let expectedDescription =
  52. "File at URL: \(dummyFileURL.absoluteString) is not reachable. Ensure file URL is not" +
  53. " a directory, symbolic link, or invalid url."
  54. XCTAssertEqual(error.localizedDescription, expectedDescription)
  55. }
  56. } receiveValue: { metadata in
  57. XCTFail("💥 result unexpected")
  58. }
  59. .store(in: &cancellables)
  60. // then
  61. wait(for: [putFileExpectation], timeout: expectationTimeout)
  62. }
  63. }