Skip to content

Commit 1db0457

Browse files
authored
Added StorageIntegration swift tests (#5640)
* Added StorageIntegration swift tests * Resolved feedback * Resolved Feedback v2
1 parent d935e84 commit 1db0457

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ class StorageIntegration: XCTestCase {
9292
waitForExpectations()
9393
}
9494

95+
func testUnauthenticatedGetMetadataUnauthorized() {
96+
let expectation = self.expectation(description: #function)
97+
let ref = storage.reference().child("ios/private/secretfile.txt")
98+
ref.getMetadata(completion: { (metadata, error) -> Void in
99+
XCTAssertNil(metadata, "Metadata should be nil")
100+
XCTAssertNotNil(error, "Error should not be nil")
101+
XCTAssertEqual((error! as NSError).code, StorageErrorCode.unauthorized.rawValue)
102+
expectation.fulfill()
103+
})
104+
waitForExpectations()
105+
}
106+
95107
func testUnauthenticatedUpdateMetadata() {
96108
let expectation = self.expectation(description: #function)
97109

@@ -129,6 +141,28 @@ class StorageIntegration: XCTestCase {
129141
waitForExpectations()
130142
}
131143

144+
func testUnauthenticatedDeleteNonExistingFile() {
145+
let expectation = self.expectation(description: #function)
146+
let ref = storage.reference(withPath: "ios/public/fileThatDoesNotExist")
147+
ref.delete { error in
148+
XCTAssertNotNil(error, "Error should not be nil")
149+
XCTAssertEqual((error! as NSError).code, StorageErrorCode.objectNotFound.rawValue)
150+
expectation.fulfill()
151+
}
152+
waitForExpectations()
153+
}
154+
155+
func testUnauthenticatedDeleteFileUnauthorized() {
156+
let expectation = self.expectation(description: #function)
157+
let ref = storage.reference(withPath: "ios/private/secretfile.txt")
158+
ref.delete { error in
159+
XCTAssertNotNil(error, "Error should not be nil")
160+
XCTAssertEqual((error! as NSError).code, StorageErrorCode.unauthorized.rawValue)
161+
expectation.fulfill()
162+
}
163+
waitForExpectations()
164+
}
165+
132166
func testDeleteWithNilCompletion() throws {
133167
let expectation = self.expectation(description: #function)
134168
let ref = storage.reference(withPath: "ios/public/fileToDelete")
@@ -290,6 +324,24 @@ class StorageIntegration: XCTestCase {
290324
waitForExpectations()
291325
}
292326

327+
func testUnauthenticatedSimplePutBlankImage() throws {
328+
let expectation = self.expectation(description: #function)
329+
let fileName = "blank.jpg"
330+
let ref = storage.reference(withPath: "ios/public/" + fileName)
331+
let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
332+
let imageURL = tmpDirURL.appendingPathComponent(fileName)
333+
334+
let data = Data()
335+
try data.write(to: imageURL, options: .atomicWrite)
336+
337+
ref.putFile(from: imageURL, metadata: nil, completion: { metadata, error in
338+
XCTAssertNotNil(metadata, "Metadata should not be nil")
339+
XCTAssertNil(error, "Error should be nil")
340+
expectation.fulfill()
341+
})
342+
waitForExpectations()
343+
}
344+
293345
func testUnauthenticatedSimpleGetData() {
294346
let expectation = self.expectation(description: #function)
295347

@@ -357,6 +409,39 @@ class StorageIntegration: XCTestCase {
357409
waitForExpectations()
358410
}
359411

412+
func testUnauthenticatedSimpleGetFileWithCompletion() throws {
413+
let expectation = self.expectation(description: #function)
414+
let ref = storage.reference(withPath: "ios/public/cookie")
415+
let cookieString = "Here's a 🍪, yay!"
416+
let data = try XCTUnwrap(cookieString.data(using: .utf8), "Data construction failed")
417+
418+
ref.putData(data, metadata: nil, completion: { metadata, error in
419+
XCTAssertNotNil(metadata, "Metadata should not be nil")
420+
XCTAssertNil(error, "Error should be nil")
421+
422+
let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
423+
let fileURL = tmpDirURL.appendingPathComponent("cookie.txt")
424+
ref.write(toFile: fileURL) { url, error in
425+
XCTAssertNil(error, "Error should be nil")
426+
427+
guard let url = url else {
428+
XCTFail("Failed to unwrap url")
429+
return
430+
}
431+
XCTAssertEqual(fileURL, url)
432+
433+
do {
434+
let stringData = try String(contentsOf: fileURL, encoding: .utf8)
435+
XCTAssertEqual(stringData, cookieString)
436+
expectation.fulfill()
437+
} catch {
438+
XCTFail("Could not get String contents of fetched data")
439+
}
440+
}
441+
})
442+
waitForExpectations()
443+
}
444+
360445
func testUnauthenticatedSimpleGetFile() throws {
361446
let expectation = self.expectation(description: #function)
362447
let ref = storage.reference(withPath: "ios/public/helloworld")

0 commit comments

Comments
 (0)