Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(storage): add e2e tests for uploadData, uploadFile, getUrl, getProperties #4615

Merged
merged 17 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_storage_s3_example/amplifyconfiguration.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'utils/configure.dart';
import 'utils/sign_in_new_user.dart';
import 'utils/tear_down.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('getProperties()', () {
final path = 'public/get-properties-${uuid()}';
final data = 'get properties data'.codeUnits;
const metadata = {'description': 'foo'};
setUpAll(() async {
await configure(amplifyEnvironments['main']!);
addTearDownPath(StoragePath.fromString(path));
await Amplify.Storage.uploadData(
data: HttpPayload.bytes(data),
path: StoragePath.fromString(path),
options: const StorageUploadDataOptions(metadata: metadata),
).result;
});

testWidgets('String StoragePath', (_) async {
final result = await Amplify.Storage.getProperties(
path: StoragePath.fromString(path),
).result;
expect(result.storageItem.path, path);
expect(result.storageItem.metadata, metadata);
expect(result.storageItem.eTag, isNotNull);
expect(result.storageItem.size, data.length);
});

testWidgets('with identity ID', (_) async {
final userIdentityId = await signInNewUser();
final name = 'get-properties-with-identity-id-${uuid()}';
final data = 'with identity ID'.codeUnits;
final expectedResolvedPath = 'private/$userIdentityId/$name';
addTearDownPath(StoragePath.fromString(expectedResolvedPath));
await Amplify.Storage.uploadData(
data: HttpPayload.bytes(data),
path: StoragePath.fromIdentityId(
(identityId) => 'private/$identityId/$name',
Equartey marked this conversation as resolved.
Show resolved Hide resolved
),
options: const StorageUploadDataOptions(metadata: metadata),
).result;
final result = await Amplify.Storage.getProperties(
path: StoragePath.fromIdentityId(
((identityId) => 'private/$identityId/$name'),
),
).result;
expect(result.storageItem.path, expectedResolvedPath);
expect(result.storageItem.metadata, metadata);
expect(result.storageItem.eTag, isNotNull);
expect(result.storageItem.size, data.length);
});

testWidgets('unauthorized path', (_) async {
expect(
() => Amplify.Storage.getProperties(
path: const StoragePath.fromString('unauthorized/path'),
).result,
throwsA(isA<StorageAccessDeniedException>()),
);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_storage_s3/amplify_storage_s3.dart';
import 'package:amplify_storage_s3_example/amplifyconfiguration.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:integration_test/integration_test.dart';

import 'utils/configure.dart';
import 'utils/sign_in_new_user.dart';
import 'utils/tear_down.dart';

Future<List<int>> readData(Uri uri) async {
return (await read(uri)).codeUnits;
}

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('getUrl()', () {
final path = 'public/get-url-${uuid()}';
final data = 'get url data'.codeUnits;
setUpAll(() async {
await configure(amplifyEnvironments['main']!);
addTearDownPath(StoragePath.fromString(path));
await Amplify.Storage.uploadData(
data: HttpPayload.bytes(data),
path: StoragePath.fromString(path),
).result;
});

testWidgets('String StoragePath', (_) async {
final result = await Amplify.Storage.getUrl(
path: StoragePath.fromString(path),
).result;
expect(result.url.path, '/$path');
final actualData = await readData(result.url);
expect(actualData, data);
});

testWidgets('with identity ID', (_) async {
final userIdentityId = await signInNewUser();
final name = 'get-url-with-identity-id-${uuid()}';
final data = 'with identity ID'.codeUnits;
final expectedResolvedPath = 'private/$userIdentityId/$name';
addTearDownPath(StoragePath.fromString(expectedResolvedPath));
await Amplify.Storage.uploadData(
data: HttpPayload.bytes(data),
path: StoragePath.fromIdentityId(
(identityId) => 'private/$identityId/$name',
),
).result;

final result = await Amplify.Storage.getUrl(
path: StoragePath.fromIdentityId(
(identityId) => 'private/$identityId/$name',
),
).result;
expect(result.url.path, '/$expectedResolvedPath');
final actualData = await readData(result.url);
expect(actualData, data);
});

group('unauthorized path', () {
testWidgets('validateObjectExistence true', (_) async {
expect(
() => Amplify.Storage.getUrl(
path: const StoragePath.fromString('unauthorized/path'),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
validateObjectExistence: true,
),
),
).result,
throwsA(isA<StorageAccessDeniedException>()),
);
});

testWidgets('validateObjectExistence false', (_) async {
final result = await Amplify.Storage.getUrl(
path: const StoragePath.fromString('unauthorized/path'),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
validateObjectExistence: false,
),
),
).result;
expect(
() => readData(result.url),
throwsA(isA<ClientException>()),
);
});
});

group('with options', () {
testWidgets('expiresIn', (_) async {
const duration = Duration(seconds: 10);
final result = await Amplify.Storage.getUrl(
path: StoragePath.fromString(path),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
expiresIn: duration,
),
),
).result;
expect(result.url.path, '/$path');
final actualData = await readData(result.url);
expect(actualData, data);
await Future<void>.delayed(duration);
expect(
() => readData(result.url),
throwsA(isA<ClientException>()),
);
});

testWidgets('validateObjectExistence true', (_) async {
expect(
() => Amplify.Storage.getUrl(
path: const StoragePath.fromString('public/non-existent-path'),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
validateObjectExistence: true,
),
),
).result,
throwsA(isA<StorageKeyNotFoundException>()),
);
});

testWidgets('validateObjectExistence false', (_) async {
expect(
Amplify.Storage.getUrl(
path: const StoragePath.fromString('public/non-existent-path'),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
validateObjectExistence: false,
),
),
).result,
completes,
);
});

testWidgets('useAccelerateEndpoint', (_) async {
final result = await Amplify.Storage.getUrl(
path: StoragePath.fromString(path),
options: const StorageGetUrlOptions(
pluginOptions: S3GetUrlPluginOptions(
useAccelerateEndpoint: true,
),
),
).result;
expect(result.url.path, '/$path');
final actualData = await readData(result.url);
expect(actualData, data);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'get_properties_test.dart' as get_properties_test;
import 'get_url_test.dart' as get_url_test;
import 'issues/get_url_special_characters_test.dart'
as get_url_special_characters_tests;
import 'upload_data_test.dart' as upload_data_test;
import 'upload_file_test.dart' as upload_file_test;
import 'use_case_test.dart' as use_case_tests;

void main() {
Expand All @@ -14,5 +18,9 @@ void main() {
group('amplify_storage_s3', () {
get_url_special_characters_tests.main();
use_case_tests.main();
get_url_test.main();
get_properties_test.main();
upload_file_test.main();
upload_data_test.main();
});
}