StorageUpdateMetadataTests.swift 5.9 KB

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