StorageIntegrationCommon.swift 3.7 KB

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