Skip to content

Commit

Permalink
unit tests and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Jul 30, 2024
1 parent 34d40f4 commit 272a17e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 188 deletions.
3 changes: 3 additions & 0 deletions FirebaseStorage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [fixed] Fix a potential data race in Storage initialization. (#13369)

# 11.0.0
- [fixed] Updated error handling to support both Swift error enum handling and NSError error
handling. Some of the Swift enums have additional parameters which may be a **breaking** change.
Expand Down
60 changes: 24 additions & 36 deletions FirebaseStorage/Tests/Unit/StorageDeleteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,51 +110,39 @@ class StorageDeleteTests: StorageTestHelpers {
waitForExpectation(test: self)
}

func testUnsuccessfulFetchUnauthenticated() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = unauthenticatedBlock()
func testUnsuccessfulFetchUnauthenticated() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(unauthenticatedBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageDeleteTask.deleteTask(
reference: ref,
queue: dispatchQueue!.self
) { _, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthenticated.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
try await ref.delete()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthenticated.rawValue)
}
waitForExpectation(test: self)
}

func testUnsuccessfulFetchUnauthorized() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = unauthorizedBlock()
func testUnsuccessfulFetchUnauthorized() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(unauthorizedBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageDeleteTask.deleteTask(
reference: ref,
queue: dispatchQueue!.self
) { _, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthorized.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
try await ref.delete()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthorized.rawValue)
}
waitForExpectation(test: self)
}

func testUnsuccessfulFetchObjectDoesntExist() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = notFoundBlock()
func testUnsuccessfulFetchObjectDoesntExist() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(notFoundBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageDeleteTask.deleteTask(
reference: ref,
queue: dispatchQueue!.self
) { _, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
try await ref.delete()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
}
waitForExpectation(test: self)
}
}
60 changes: 24 additions & 36 deletions FirebaseStorage/Tests/Unit/StorageGetMetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,52 +110,40 @@ class StorageGetMetadataTests: StorageTestHelpers {
waitForExpectation(test: self)
}

func testUnsuccessfulFetchUnauthenticated() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = unauthenticatedBlock()
func testUnsuccessfulFetchUnauthenticated() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(unauthenticatedBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageGetMetadataTask.getMetadataTask(
reference: ref,
queue: dispatchQueue!.self
) { metadata, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthenticated.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
let _ = try await ref.getMetadata()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthenticated.rawValue)
}
waitForExpectation(test: self)
}

func testUnsuccessfulFetchUnauthorized() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = unauthorizedBlock()
func testUnsuccessfulFetchUnauthorized() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(unauthorizedBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageGetMetadataTask.getMetadataTask(
reference: ref,
queue: dispatchQueue!.self
) { metadata, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthorized.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
let _ = try await ref.getMetadata()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthorized.rawValue)
}
waitForExpectation(test: self)
}

func testUnsuccessfulFetchObjectDoesntExist() {
let expectation = self.expectation(description: #function)

fetcherService!.testBlock = notFoundBlock()
func testUnsuccessfulFetchObjectDoesntExist() async {
let storage = storage()
await storage.fetcherService.updateTestBlock(notFoundBlock())
let path = objectPath()
let ref = StorageReference(storage: storage(), path: path)
StorageGetMetadataTask.getMetadataTask(
reference: ref,
queue: dispatchQueue!.self
) { metadata, error in
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
expectation.fulfill()
let ref = StorageReference(storage: storage, path: path)
do {
let _ = try await ref.getMetadata()
} catch {
XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
}
waitForExpectation(test: self)
}

func testUnsuccessfulFetchBadJSON() {
Expand Down
89 changes: 28 additions & 61 deletions FirebaseStorage/Tests/Unit/StorageListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,24 @@ class StorageListTests: StorageTestHelpers {
waitForExpectation(test: self)
}

func testDefaultListWithEmulator() {
let expectation = self.expectation(description: #function)
func testDefaultListWithEmulator() async throws {
let storage = self.storage()
storage.useEmulator(withHost: "localhost", port: 8080)
fetcherService?.allowLocalhostRequest = true

fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let url = fetcher.request!.url!
XCTAssertEqual(url.scheme, "http")
XCTAssertEqual(url.host, "localhost")
XCTAssertEqual(url.port, 8080)
XCTAssertEqual(url.path, "/v0/b/bucket/o")
let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
XCTAssertEqual(queryItems.count, 2)
XCTAssertEqual(queryItems.count, 3)
for item in queryItems {
switch item.name {
case "prefix": XCTAssertEqual(item.value, "object/")
case "delimiter": XCTAssertEqual(item.value, "/")
case "maxResults": XCTAssertEqual(item.value, "123")
default: XCTFail("Unexpected URLComponent Query Item")
}
}
Expand All @@ -146,19 +145,9 @@ class StorageListTests: StorageTestHelpers {
headerFields: nil)
response(httpResponse, "{}".data(using: .utf8), nil)
}

let path = objectPath()
let ref = StorageReference(storage: storage, path: path)
StorageListTask.listTask(
reference: ref,
queue: dispatchQueue!.self,
pageSize: nil,
previousPageToken: nil
) { result, error in
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectation(test: self)
await storage.fetcherService.updateTestBlock(testBlock)
let ref = storage.reference(withPath: "object")
let _ = try await ref.list(maxResults: 123)
}

func testListWithPageSizeAndPageToken() {
Expand Down Expand Up @@ -243,9 +232,7 @@ class StorageListTests: StorageTestHelpers {
waitForExpectation(test: self)
}

func testListWithResponse() throws {
let expectation = self.expectation(description: #function)

func testListWithResponse() async throws {
let jsonString = "{\n" +
" \"prefixes\": [\n" +
" \"object/prefixWithoutSlash\",\n" +
Expand All @@ -263,11 +250,10 @@ class StorageListTests: StorageTestHelpers {
" ],\n" +
" \"nextPageToken\": \"foo\"" +
"}"

let responseData = try XCTUnwrap(jsonString.data(using: .utf8))

fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
statusCode: 200,
httpVersion: "HTTP/1.1",
Expand All @@ -276,35 +262,22 @@ class StorageListTests: StorageTestHelpers {
}

let storage = storage()
await storage.fetcherService.updateTestBlock(testBlock)
let ref = storage.reference(withPath: "object")
StorageListTask.listTask(
reference: ref,
queue: dispatchQueue!.self,
pageSize: nil,
previousPageToken: nil
) { result, error in
XCTAssertNotNil(result)
XCTAssertNil(error)

XCTAssertEqual(result?.items, [ref.child("data1.dat"), ref.child("data2.dat")])
XCTAssertEqual(
result?.prefixes,
[ref.child("prefixWithoutSlash"), ref.child("prefixWithSlash")]
)
XCTAssertEqual(result?.pageToken, "foo")

expectation.fulfill()
}
waitForExpectation(test: self)
let result = try await ref.list(maxResults: 1000)
XCTAssertEqual(result.items, [ref.child("data1.dat"), ref.child("data2.dat")])
XCTAssertEqual(
result.prefixes,
[ref.child("prefixWithoutSlash"), ref.child("prefixWithSlash")]
)
XCTAssertEqual(result.pageToken, "foo")
}

func testListWithErrorResponse() throws {
let expectation = self.expectation(description: #function)

func testListWithErrorResponse() async {
let error = NSError(domain: "com.google.firebase.storage", code: 404)

fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let testBlock = { (fetcher: GTMSessionFetcher,
response: GTMSessionFetcherTestResponse) in
let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
statusCode: 403,
httpVersion: "HTTP/1.1",
Expand All @@ -313,19 +286,13 @@ class StorageListTests: StorageTestHelpers {
}

let storage = storage()
await storage.fetcherService.updateTestBlock(testBlock)
let ref = storage.reference(withPath: "object")
StorageListTask.listTask(
reference: ref,
queue: dispatchQueue!.self,
pageSize: nil,
previousPageToken: nil
) { result, error in
XCTAssertNotNil(error)
XCTAssertNil(result)
XCTAssertEqual((error as? NSError)!.domain, "FIRStorageErrorDomain")
XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
expectation.fulfill()
do {
let _ = try await ref.list(maxResults: 1000)
} catch {
XCTAssertEqual((error as NSError).domain, "FIRStorageErrorDomain")
XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
}
waitForExpectation(test: self)
}
}
Loading

0 comments on commit 272a17e

Please sign in to comment.