StorageReferenceTests.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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) = completion {
  48. putFileExpectation.fulfill()
  49. XCTAssertTrue(String(describing: error).starts(with: "unknown"))
  50. }
  51. } receiveValue: { metadata in
  52. XCTFail("💥 result unexpected")
  53. }
  54. .store(in: &cancellables)
  55. // then
  56. wait(for: [putFileExpectation], timeout: expectationTimeout)
  57. }
  58. }