StorageUpdateMetadataTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2022 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. @testable import FirebaseStorage
  15. import Foundation
  16. import GTMSessionFetcherCore
  17. import XCTest
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class StorageUpdateMetadataTests: StorageTestHelpers {
  20. var fetcherService: GTMSessionFetcherService?
  21. var dispatchQueue: DispatchQueue?
  22. var metadata: StorageMetadata?
  23. override func setUp() {
  24. super.setUp()
  25. metadata = StorageMetadata(dictionary: ["bucket": "bucket", "name": "path/to/object"])
  26. fetcherService = GTMSessionFetcherService()
  27. dispatchQueue = DispatchQueue(label: "Test dispatch queue")
  28. }
  29. override func tearDown() {
  30. fetcherService = nil
  31. super.tearDown()
  32. }
  33. func testFetcherConfiguration() {
  34. let expectation = self.expectation(description: #function)
  35. fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!,
  36. response: GTMSessionFetcherTestResponse) in
  37. XCTAssertEqual(fetcher.request?.url, self.objectURL())
  38. XCTAssertEqual(fetcher.request?.httpMethod, "PATCH")
  39. let httpResponse = HTTPURLResponse(
  40. url: (fetcher.request?.url)!,
  41. statusCode: 200,
  42. httpVersion: "HTTP/1.1",
  43. headerFields: nil
  44. )
  45. response(httpResponse, nil, nil)
  46. }
  47. let path = objectPath()
  48. let ref = StorageReference(storage: storage(), path: path)
  49. StorageUpdateMetadataTask.updateMetadataTask(
  50. reference: ref,
  51. fetcherService: fetcherService!.self,
  52. queue: dispatchQueue!.self,
  53. metadata: metadata!
  54. ) { metadata, error in
  55. expectation.fulfill()
  56. }
  57. waitForExpectation(test: self)
  58. }
  59. func testSuccessfulFetch() {
  60. let expectation = self.expectation(description: #function)
  61. fetcherService!.testBlock = successBlock(withMetadata: metadata)
  62. let path = objectPath()
  63. let ref = StorageReference(storage: storage(), path: path)
  64. StorageUpdateMetadataTask.updateMetadataTask(
  65. reference: ref,
  66. fetcherService: fetcherService!.self,
  67. queue: dispatchQueue!.self,
  68. metadata: metadata!
  69. ) { metadata, error in
  70. XCTAssertNil(error)
  71. XCTAssertEqual(self.metadata?.bucket, metadata?.bucket)
  72. XCTAssertEqual(self.metadata?.name, metadata?.name)
  73. expectation.fulfill()
  74. }
  75. waitForExpectation(test: self)
  76. }
  77. func testSuccessfulFetchWithEmulator() {
  78. let expectation = self.expectation(description: #function)
  79. let storage = self.storage()
  80. storage.useEmulator(withHost: "localhost", port: 8080)
  81. fetcherService?.allowLocalhostRequest = true
  82. fetcherService!
  83. .testBlock = successBlock(
  84. withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")!
  85. )
  86. let path = objectPath()
  87. let ref = StorageReference(storage: storage, path: path)
  88. StorageUpdateMetadataTask.updateMetadataTask(
  89. reference: ref,
  90. fetcherService: fetcherService!.self,
  91. queue: dispatchQueue!.self,
  92. metadata: metadata!
  93. ) { metadata, error in
  94. expectation.fulfill()
  95. }
  96. waitForExpectation(test: self)
  97. }
  98. func testUnsuccessfulFetchUnauthenticated() {
  99. let expectation = self.expectation(description: #function)
  100. fetcherService!.testBlock = unauthenticatedBlock()
  101. let path = objectPath()
  102. let ref = StorageReference(storage: storage(), path: path)
  103. StorageUpdateMetadataTask.updateMetadataTask(
  104. reference: ref,
  105. fetcherService: fetcherService!.self,
  106. queue: dispatchQueue!.self,
  107. metadata: metadata!
  108. ) { metadata, error in
  109. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthenticated.rawValue)
  110. expectation.fulfill()
  111. }
  112. waitForExpectation(test: self)
  113. }
  114. func testUnsuccessfulFetchUnauthorized() {
  115. let expectation = self.expectation(description: #function)
  116. fetcherService!.testBlock = unauthorizedBlock()
  117. let path = objectPath()
  118. let ref = StorageReference(storage: storage(), path: path)
  119. StorageUpdateMetadataTask.updateMetadataTask(
  120. reference: ref,
  121. fetcherService: fetcherService!.self,
  122. queue: dispatchQueue!.self,
  123. metadata: metadata!
  124. ) { metadata, error in
  125. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthorized.rawValue)
  126. expectation.fulfill()
  127. }
  128. waitForExpectation(test: self)
  129. }
  130. func testUnsuccessfulFetchObjectDoesntExist() {
  131. let expectation = self.expectation(description: #function)
  132. fetcherService!.testBlock = notFoundBlock()
  133. let path = objectPath()
  134. let ref = StorageReference(storage: storage(), path: path)
  135. StorageUpdateMetadataTask.updateMetadataTask(
  136. reference: ref,
  137. fetcherService: fetcherService!.self,
  138. queue: dispatchQueue!.self,
  139. metadata: metadata!
  140. ) { metadata, error in
  141. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
  142. expectation.fulfill()
  143. }
  144. waitForExpectation(test: self)
  145. }
  146. func testUnsuccessfulFetchBadJSON() {
  147. let expectation = self.expectation(description: #function)
  148. fetcherService!.testBlock = invalidJSONBlock()
  149. let path = objectPath()
  150. let ref = StorageReference(storage: storage(), path: path)
  151. StorageUpdateMetadataTask.updateMetadataTask(
  152. reference: ref,
  153. fetcherService: fetcherService!.self,
  154. queue: dispatchQueue!.self,
  155. metadata: metadata!
  156. ) { metadata, error in
  157. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unknown.rawValue)
  158. expectation.fulfill()
  159. }
  160. waitForExpectation(test: self)
  161. }
  162. }