Skip to content

Commit

Permalink
chore(storage): update storage path validation rule (#3579)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode authored Mar 21, 2024
1 parent 8364412 commit b8ec8d8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ extension StoragePath {
}

func validate(_ path: String) throws {
if !path.hasPrefix("/") {
if path.hasPrefix("/") {
let errorDescription = "Invalid StoragePath specified."
let recoverySuggestion = "Please specify a valid StoragePath that contains the prefix / "
let recoverySuggestion = "Please specify a valid StoragePath that does not contain the prefix / "
throw StorageError.validation("path", errorDescription, recoverySuggestion, nil)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid StringStoragePath
/// Then: The operation will fail with a validation error
func testDownloadFileOperationStringStoragePathValidationError() {
let path = StringStoragePath(resolve: { _ in return "my/path" })
let path = StringStoragePath(resolve: { _ in return "/my/path" })
let request = StorageDownloadFileRequest(path: path,
local: testURL,
options: StorageDownloadFileRequest.Options())
Expand Down Expand Up @@ -218,7 +218,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid IdentityIDStoragePath
/// Then: The operation will fail with a validation error
func testDownloadFileOperationIdentityIDStoragePathValidationError() {
let path = IdentityIDStoragePath(resolve: { _ in return "my/path" })
let path = IdentityIDStoragePath(resolve: { _ in return "/my/path" })
let request = StorageDownloadFileRequest(path: path,
local: testURL,
options: StorageDownloadFileRequest.Options())
Expand Down Expand Up @@ -282,7 +282,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an valid StringStoragePath
/// Then: The operation will succeed
func testDownloadFileOperationWithStringStoragePathSucceeds() async throws {
let path = StringStoragePath(resolve: { _ in return "/public/\(self.testKey)" })
let path = StringStoragePath(resolve: { _ in return "public/\(self.testKey)" })
let task = StorageTransferTask(transferType: .download(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceDownloadEvents = [
StorageEvent.initiated(StorageTaskReference(task)),
Expand Down Expand Up @@ -314,15 +314,15 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {

await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
mockStorageService.verifyDownload(serviceKey: "/public/\(self.testKey)", fileURL: url)
mockStorageService.verifyDownload(serviceKey: "public/\(self.testKey)", fileURL: url)
}

/// Given: Storage Download File Operation
/// When: The operation is executed with a request that has an valid IdentityIDStoragePath
/// Then: The operation will succeed
func testDownloadFileOperationWithIdentityIDStoragePathSucceeds() async throws {
mockAuthService.identityId = testIdentityId
let path = IdentityIDStoragePath(resolve: { id in return "/public/\(id)/\(self.testKey)" })
let path = IdentityIDStoragePath(resolve: { id in return "public/\(id)/\(self.testKey)" })
let task = StorageTransferTask(transferType: .download(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceDownloadEvents = [
StorageEvent.initiated(StorageTaskReference(task)),
Expand Down Expand Up @@ -354,7 +354,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {

await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
mockStorageService.verifyDownload(serviceKey: "/public/\(testIdentityId)/\(self.testKey)", fileURL: url)
mockStorageService.verifyDownload(serviceKey: "public/\(testIdentityId)/\(self.testKey)", fileURL: url)
}

// TODO: missing unit tests for pause resume and cancel. do we create a mock of the StorageTaskReference?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid StringStoragePath
/// Then: The operation will fail with a validation error
func testDownloadDataOperationStringStoragePathValidationError() {
let path = StringStoragePath(resolve: { _ in return "my/path" })
let path = StringStoragePath(resolve: { _ in return "/my/path" })
let request = StorageDownloadDataRequest(path: path, options: StorageDownloadDataRequest.Options())
let failedInvoked = expectation(description: "failed was invoked on operation")
let operation = AWSS3StorageDownloadDataOperation(request,
Expand Down Expand Up @@ -208,7 +208,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid IdentityIDStoragePath
/// Then: The operation will fail with a validation error
func testDownloadDataOperationIdentityIdStoragePathValidationError() {
let path = IdentityIDStoragePath(resolve: { _ in return "my/path" })
let path = IdentityIDStoragePath(resolve: { _ in return "/my/path" })
let request = StorageDownloadDataRequest(path: path, options: StorageDownloadDataRequest.Options())
let failedInvoked = expectation(description: "failed was invoked on operation")
let operation = AWSS3StorageDownloadDataOperation(request,
Expand Down Expand Up @@ -271,7 +271,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
StorageEvent.initiated(StorageTaskReference(task)),
StorageEvent.inProcess(Progress()),
StorageEvent.completed(Data())]
let path = StringStoragePath(resolve: { _ in return "/public/\(self.testKey)" })
let path = StringStoragePath(resolve: { _ in return "public/\(self.testKey)" })
let request = StorageDownloadDataRequest(path: path, options: StorageDownloadDataRequest.Options())

let inProcessInvoked = expectation(description: "inProgress was invoked on operation")
Expand All @@ -296,7 +296,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {

await fulfillment(of: [inProcessInvoked, completeInvoked], timeout: 1)
XCTAssertTrue(operation.isFinished)
mockStorageService.verifyDownload(serviceKey: "/public/\(self.testKey)", fileURL: nil)
mockStorageService.verifyDownload(serviceKey: "public/\(self.testKey)", fileURL: nil)
}

/// Given: Storage Download Data Operation
Expand All @@ -309,7 +309,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
StorageEvent.initiated(StorageTaskReference(task)),
StorageEvent.inProcess(Progress()),
StorageEvent.completed(Data())]
let path = IdentityIDStoragePath(resolve: { id in return "/public/\(id)/\(self.testKey)" })
let path = IdentityIDStoragePath(resolve: { id in return "public/\(id)/\(self.testKey)" })
let request = StorageDownloadDataRequest(path: path, options: StorageDownloadDataRequest.Options())

let inProcessInvoked = expectation(description: "inProgress was invoked on operation")
Expand All @@ -334,7 +334,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {

await fulfillment(of: [inProcessInvoked, completeInvoked], timeout: 1)
XCTAssertTrue(operation.isFinished)
mockStorageService.verifyDownload(serviceKey: "/public/\(testIdentityId)/\(self.testKey)", fileURL: nil)
mockStorageService.verifyDownload(serviceKey: "public/\(testIdentityId)/\(self.testKey)", fileURL: nil)
}

// TODO: missing unit tets for pause resume and cancel. do we create a mock of the StorageTaskReference?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid StringStoragePath
/// Then: The operation will fail with a validation error
func testUploadDataOperationStringStoragePathValidationError() {
let path = StringStoragePath(resolve: { _ in return "my/path" })
let path = StringStoragePath(resolve: { _ in return "/my/path" })
let failedInvoked = expectation(description: "failed was invoked on operation")
let options = StorageUploadDataRequest.Options(accessLevel: .protected)
let request = StorageUploadDataRequest(path: path, data: testData, options: options)
Expand Down Expand Up @@ -254,7 +254,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid IdentityIDStoragePath
/// Then: The operation will fail with a validation error
func testUploadDataOperationIdentityIDStoragePathValidationError() {
let path = IdentityIDStoragePath(resolve: { _ in return "my/path" })
let path = IdentityIDStoragePath(resolve: { _ in return "/my/path" })
let failedInvoked = expectation(description: "failed was invoked on operation")
let options = StorageUploadDataRequest.Options(accessLevel: .protected)
let request = StorageUploadDataRequest(path: path, data: testData, options: options)
Expand Down Expand Up @@ -316,7 +316,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an valid StringStoragePath
/// Then: The operation will succeed
func testUploadDataOperationWithStringStoragePathSucceeds() async throws {
let path = StringStoragePath(resolve: { _ in return "/public/\(self.testKey)" })
let path = StringStoragePath(resolve: { _ in return "public/\(self.testKey)" })
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
StorageEvent.initiated(StorageTaskReference(task)),
Expand Down Expand Up @@ -354,7 +354,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
XCTAssertEqual(mockStorageService.uploadCalled, 1)
mockStorageService.verifyUpload(serviceKey: "/public/\(self.testKey)",
mockStorageService.verifyUpload(serviceKey: "public/\(self.testKey)",
key: testKey,
uploadSource: expectedUploadSource,
contentType: testContentType,
Expand All @@ -366,7 +366,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
/// Then: The operation will succeed
func testUploadDataOperationWithIdentityIDStoragePathSucceeds() async throws {
mockAuthService.identityId = testIdentityId
let path = IdentityIDStoragePath(resolve: { id in return "/public/\(id)/\(self.testKey)" })
let path = IdentityIDStoragePath(resolve: { id in return "public/\(id)/\(self.testKey)" })
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
StorageEvent.initiated(StorageTaskReference(task)),
Expand Down Expand Up @@ -403,7 +403,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
XCTAssertEqual(mockStorageService.uploadCalled, 1)
mockStorageService.verifyUpload(serviceKey: "/public/\(testIdentityId)/\(testKey)",
mockStorageService.verifyUpload(serviceKey: "public/\(testIdentityId)/\(testKey)",
key: testKey,
uploadSource: expectedUploadSource,
contentType: testContentType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid StringStoragePath
/// Then: The operation will fail with a validation error
func testUploadFileOperationStringStoragePathValidationError() {
let path = StringStoragePath(resolve: { _ in return "my/path" })
let path = StringStoragePath(resolve: { _ in return "/my/path" })
mockAuthService.identityId = testIdentityId
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
Expand Down Expand Up @@ -305,7 +305,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an invalid IdentityIDStoragePath
/// Then: The operation will fail with a validation error
func testUploadFileOperationIdentityIDStoragePathValidationError() {
let path = IdentityIDStoragePath(resolve: { _ in return "my/path" })
let path = IdentityIDStoragePath(resolve: { _ in return "/my/path" })
mockAuthService.identityId = testIdentityId
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
Expand Down Expand Up @@ -397,7 +397,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an valid StringStoragePath
/// Then: The operation will succeed
func testUploadFileOperationWithStringStoragePathSucceeds() async throws {
let path = StringStoragePath(resolve: { _ in return "/public/\(self.testKey)" })
let path = StringStoragePath(resolve: { _ in return "public/\(self.testKey)" })
mockAuthService.identityId = testIdentityId
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
Expand Down Expand Up @@ -439,7 +439,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
XCTAssertEqual(mockStorageService.uploadCalled, 1)
mockStorageService.verifyUpload(serviceKey: "/public/\(testKey)",
mockStorageService.verifyUpload(serviceKey: "public/\(testKey)",
key: testKey,
uploadSource: expectedUploadSource,
contentType: testContentType,
Expand All @@ -450,7 +450,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
/// When: The operation is executed with a request that has an valid IdentityIDStoragePath
/// Then: The operation will succeed
func testUploadFileOperationWithIdentityIDStoragePathSucceeds() async throws {
let path = IdentityIDStoragePath(resolve: { id in return "/public/\(id)/\(self.testKey)" })
let path = IdentityIDStoragePath(resolve: { id in return "public/\(id)/\(self.testKey)" })
mockAuthService.identityId = testIdentityId
let task = StorageTransferTask(transferType: .upload(onEvent: { _ in }), bucket: "bucket", key: "key")
mockStorageService.storageServiceUploadEvents = [
Expand Down Expand Up @@ -491,7 +491,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
await waitForExpectations(timeout: 1)
XCTAssertTrue(operation.isFinished)
XCTAssertEqual(mockStorageService.uploadCalled, 1)
mockStorageService.verifyUpload(serviceKey: "/public/\(testIdentityId)/\(testKey)",
mockStorageService.verifyUpload(serviceKey: "public/\(testIdentityId)/\(testKey)",
key: testKey,
uploadSource: expectedUploadSource,
contentType: testContentType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AWSS3StorageGetURLTaskTests: XCTestCase {
/// - Then: A URL should be returned.
func testGetURLTaskSuccess() async throws {

let somePath = "/path"
let somePath = "path"
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory())

let serviceMock = MockAWSS3StorageService()
Expand All @@ -43,7 +43,7 @@ class AWSS3StorageGetURLTaskTests: XCTestCase {
/// - When: AWSS3StorageGetURLTask value is invoked
/// - Then: A storage service error should be returned, with an underlying service error
func testGetURLTaskNoBucket() async throws {
let somePath = "/path"
let somePath = "path"

let serviceMock = MockAWSS3StorageService()
serviceMock.getPreSignedURLHandler = { _, _, _ in
Expand Down Expand Up @@ -74,7 +74,7 @@ class AWSS3StorageGetURLTaskTests: XCTestCase {
/// - When: AWSS3StorageGetURLTask value is invoked
/// - Then: A storage validation error should be returned
func testGetURLTaskWithInvalidPath() async throws {
let somePath = "path"
let somePath = "/path"
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory())

let serviceMock = MockAWSS3StorageService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AWSS3StorageListObjectsTaskTests: XCTestCase {
}

let request = StorageListRequest(
path: StringStoragePath.fromString("/path"), options: .init())
path: StringStoragePath.fromString("path"), options: .init())
let task = AWSS3StorageListObjectsTask(
request,
storageConfiguration: AWSS3StoragePluginConfiguration(),
Expand All @@ -56,7 +56,7 @@ class AWSS3StorageListObjectsTaskTests: XCTestCase {
}

let request = StorageListRequest(
path: StringStoragePath.fromString("/path"), options: .init())
path: StringStoragePath.fromString("path"), options: .init())
let task = AWSS3StorageListObjectsTask(
request,
storageConfiguration: AWSS3StoragePluginConfiguration(),
Expand All @@ -83,7 +83,7 @@ class AWSS3StorageListObjectsTaskTests: XCTestCase {
let serviceMock = MockAWSS3StorageService()

let request = StorageListRequest(
path: StringStoragePath.fromString("path"), options: .init())
path: StringStoragePath.fromString("/path"), options: .init())
let task = AWSS3StorageListObjectsTask(
request,
storageConfiguration: AWSS3StoragePluginConfiguration(),
Expand Down
Loading

0 comments on commit b8ec8d8

Please sign in to comment.