Skip to content

Commit

Permalink
chore(storage): add e2e tests for upload data api (#5622)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikaHsn authored and Nika Hassani committed Nov 20, 2024
1 parent 0cae48f commit 01aac21
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ class StorageCategory extends AmplifyCategory<StoragePluginInterface> {
required StoragePath path,
void Function(StorageTransferProgress)? onProgress,
StorageUploadDataOptions? options,
StorageBucket? bucket,
}) {
return identifyCall(
StorageCategoryMethod.uploadData,
Expand All @@ -153,7 +152,6 @@ class StorageCategory extends AmplifyCategory<StoragePluginInterface> {
data: data,
onProgress: onProgress,
options: options,
bucket: bucket,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ abstract class StoragePluginInterface extends AmplifyPluginInterface {
required StorageDataPayload data,
void Function(StorageTransferProgress)? onProgress,
StorageUploadDataOptions? options,
StorageBucket? bucket,
}) {
throw UnimplementedError('uploadData() has not been implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';

/// {@template amplify_core.storage.upload_data_options}
/// Configurable options for `Amplify.Storage.uploadData`.
Expand All @@ -15,16 +15,20 @@ class StorageUploadDataOptions
const StorageUploadDataOptions({
this.metadata = const {},
this.pluginOptions,
this.bucket,
});

/// The metadata attached to the object to be uploaded.
final Map<String, String> metadata;

/// Optionally specify which bucket to target.
final StorageBucket? bucket;

/// {@macro amplify_core.storage.upload_data_plugin_options}
final StorageUploadDataPluginOptions? pluginOptions;

@override
List<Object?> get props => [metadata, pluginOptions];
List<Object?> get props => [metadata, pluginOptions, bucket];

@override
String get runtimeTypeName => 'StorageUploadDataOptions';
Expand All @@ -33,6 +37,7 @@ class StorageUploadDataOptions
Map<String, Object?> toJson() => {
'metadata': metadata,
'pluginOptions': pluginOptions?.toJson(),
'bucket': bucket,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ void main() {
await Amplify.Storage.uploadData(
path: StoragePath.fromString(publicPath),
data: StorageDataPayload.bytes(bytesData),
bucket: secondaryBucket,
options: StorageUploadDataOptions(
bucket: secondaryBucket,
),
).result;

final downloadResult = await Amplify.Storage.downloadData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ void main() {
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(path),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: mainBucket,
options: StorageUploadDataOptions(
metadata: metadata,
bucket: mainBucket,
),
).result;
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(path),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: secondaryBucket,
options: StorageUploadDataOptions(
metadata: metadata,
bucket: secondaryBucket,
),
).result;
});

Expand Down Expand Up @@ -156,8 +160,10 @@ void main() {
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(expectedResolvedPath),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: secondaryBucket,
options: StorageUploadDataOptions(
metadata: metadata,
bucket: secondaryBucket,
),
).result;
final result = await Amplify.Storage.getProperties(
path: StoragePath.fromIdentityId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ void main() {
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes('data'.codeUnits),
path: storagePath,
bucket: mainBucket,
options: StorageUploadDataOptions(
bucket: mainBucket,
),
).result;
});

Expand All @@ -95,7 +97,9 @@ void main() {
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes('data'.codeUnits),
path: storagePath,
bucket: secondaryBucket,
options: StorageUploadDataOptions(
bucket: secondaryBucket,
),
).result;

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,59 @@ void main() {
});
});

group('multi-bucket', () {
final mainBucket =
StorageBucket.fromOutputs('Storage Integ Test main bucket');
final secondaryBucket = StorageBucket.fromOutputs(
'Storage Integ Test secondary bucket',
);

testWidgets('uploads to multiple buckets', (_) async {
final path = 'public/multi-bucket-upload-data-${uuid()}';
final storagePath = StoragePath.fromString(path);
final data = 'multi bucket upload data byte'.codeUnits;
addTearDownMultiBucket(
storagePath,
[mainBucket, secondaryBucket],
);
// main bucket
final mainResult = await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: storagePath,
options: StorageUploadDataOptions(
bucket: mainBucket,
),
).result;
expect(mainResult.uploadedItem.path, path);

final downloadMainResult = await Amplify.Storage.downloadData(
path: storagePath,
options: StorageDownloadDataOptions(
bucket: mainBucket,
),
).result;
expect(downloadMainResult.bytes, data);

// secondary bucket
final secondaryResult = await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: storagePath,
options: StorageUploadDataOptions(
bucket: secondaryBucket,
),
).result;
expect(secondaryResult.uploadedItem.path, path);

final downloadSecondaryResult = await Amplify.Storage.downloadData(
path: storagePath,
options: StorageDownloadDataOptions(
bucket: secondaryBucket,
),
).result;
expect(downloadSecondaryResult.bytes, data);
});
});

group('upload progress', () {
testWidgets('reports progress for byte data', (_) async {
final path = 'public/upload-data-progress-bytes-${uuid()}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ class AmplifyStorageS3Dart extends StoragePluginInterface
required StoragePath path,
void Function(S3TransferProgress)? onProgress,
StorageUploadDataOptions? options,
StorageBucket? bucket,
}) {
final s3PluginOptions = reifyPluginOptions(
pluginOptions: options?.pluginOptions,
Expand All @@ -285,6 +284,7 @@ class AmplifyStorageS3Dart extends StoragePluginInterface

final s3Options = StorageUploadDataOptions(
metadata: options?.metadata ?? const {},
bucket: options?.bucket,
pluginOptions: s3PluginOptions,
);

Expand All @@ -293,7 +293,6 @@ class AmplifyStorageS3Dart extends StoragePluginInterface
dataPayload: data,
options: s3Options,
onProgress: onProgress,
bucket: bucket,
);

return S3UploadDataOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,8 @@ class StorageS3Service {
void Function(S3TransferProgress)? onProgress,
FutureOr<void> Function()? onDone,
FutureOr<void> Function()? onError,
StorageBucket? bucket,
}) {
final s3ClientInfo = getS3ClientInfo(storageBucket: bucket);
final s3ClientInfo = getS3ClientInfo(storageBucket: options.bucket);
final uploadDataTask = S3UploadTask.fromDataPayload(
dataPayload,
s3Client: s3ClientInfo.client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ void main() {
test('should forward options to StorageS3Service.uploadData API',
() async {
const testOptions = StorageUploadDataOptions(
bucket: StorageBucket.fromBucketInfo(
BucketInfo(
bucketName: 'test-bucket',
region: 'test-region',
),
),
pluginOptions: S3UploadDataPluginOptions(
getProperties: true,
useAccelerateEndpoint: true,
Expand Down Expand Up @@ -646,48 +652,6 @@ void main() {
);
});

test('should forward bucket to StorageS3Service.uploadData API',
() async {
const testBucket = StorageBucket.fromBucketInfo(
BucketInfo(
bucketName: 'test-bucket',
region: 'test-region',
),
);
when(
() => storageS3Service.uploadData(
path: testPath,
dataPayload: any(named: 'dataPayload'),
options: any(named: 'options'),
bucket: testBucket,
),
).thenAnswer((_) => testS3UploadTask);

when(() => testS3UploadTask.result).thenAnswer((_) async => testItem);
uploadDataOperation = storageS3Plugin.uploadData(
data: testData,
path: testPath,
bucket: testBucket,
);
final capturedBucket = verify(
() => storageS3Service.uploadData(
path: testPath,
dataPayload: any(named: 'dataPayload'),
options: any(
named: 'options',
),
bucket: captureAny<StorageBucket>(
named: 'bucket',
),
),
).captured.last;

expect(
capturedBucket,
testBucket,
);
});

test('should forward options.metadata to StorageS3Service.uploadData API',
() async {
const testMetadata = {
Expand Down

0 comments on commit 01aac21

Please sign in to comment.