StorageIntegrationCommon.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 FirebaseAuth
  15. import FirebaseCore
  16. import FirebaseStorage
  17. import FirebaseStorageSwift
  18. import XCTest
  19. class StorageIntegrationCommon: XCTestCase {
  20. var app: FirebaseApp!
  21. var auth: Auth!
  22. var storage: Storage!
  23. static var configured = false
  24. static var once = false
  25. static var signedIn = false
  26. override class func setUp() {
  27. if !StorageIntegrationCommon.configured {
  28. StorageIntegrationCommon.configured = true
  29. FirebaseApp.configure()
  30. }
  31. }
  32. override func setUp() {
  33. super.setUp()
  34. app = FirebaseApp.app()
  35. auth = Auth.auth(app: app)
  36. storage = Storage.storage(app: app!)
  37. if !StorageIntegrationCommon.signedIn {
  38. signInAndWait()
  39. }
  40. if !StorageIntegrationCommon.once {
  41. StorageIntegrationCommon.once = true
  42. let setupExpectation = expectation(description: "setUp")
  43. let largeFiles = ["ios/public/1mb", "ios/public/1mb2"]
  44. let emptyFiles =
  45. ["ios/public/empty", "ios/public/list/a", "ios/public/list/b", "ios/public/list/prefix/c"]
  46. setupExpectation.expectedFulfillmentCount = largeFiles.count + emptyFiles.count
  47. do {
  48. let bundle = Bundle(for: StorageIntegrationCommon.self)
  49. let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
  50. "Failed to get filePath")
  51. let data = try XCTUnwrap(try Data(contentsOf: URL(fileURLWithPath: filePath)),
  52. "Failed to load file")
  53. for largeFile in largeFiles {
  54. let ref = storage.reference().child(largeFile)
  55. ref.putData(data) { result in
  56. self.assertResultSuccess(result)
  57. setupExpectation.fulfill()
  58. }
  59. }
  60. for emptyFile in emptyFiles {
  61. let ref = storage.reference().child(emptyFile)
  62. ref.putData(data) { result in
  63. self.assertResultSuccess(result)
  64. setupExpectation.fulfill()
  65. }
  66. }
  67. waitForExpectations()
  68. } catch {
  69. XCTFail("Error thrown setting up files in setUp")
  70. }
  71. }
  72. }
  73. override func tearDown() {
  74. app = nil
  75. storage = nil
  76. super.tearDown()
  77. }
  78. private func signInAndWait() {
  79. let expectation = self.expectation(description: #function)
  80. auth.signIn(withEmail: Credentials.kUserName,
  81. password: Credentials.kPassword) { result, error in
  82. XCTAssertNil(error)
  83. StorageIntegrationCommon.signedIn = true
  84. print("Successfully signed in")
  85. expectation.fulfill()
  86. }
  87. waitForExpectations()
  88. }
  89. private func waitForExpectations() {
  90. let kTestTimeout = 60.0
  91. waitForExpectations(timeout: kTestTimeout,
  92. handler: { (error) -> Void in
  93. if let error = error {
  94. print(error)
  95. }
  96. })
  97. }
  98. private func assertResultSuccess<T>(_ result: Result<T, Error>,
  99. file: StaticString = #file, line: UInt = #line) {
  100. switch result {
  101. case let .success(value):
  102. XCTAssertNotNil(value, file: file, line: line)
  103. case let .failure(error):
  104. XCTFail("Unexpected error \(error)")
  105. }
  106. }
  107. }