Skip to content

Commit

Permalink
test(storage): updating mockito suites for delim changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnandrew committed Jul 9, 2024
1 parent 7a13ca0 commit d0ce057
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ class AmplifyStorageS3Dart extends StoragePluginInterface
defaultPluginOptions: const SubpathStrategy(),
);
final s3Options = StorageListOptions(
pluginOptions: s3PluginOptions,
subpaths: s3PluginOptions,
nextToken: options?.nextToken,
pageSize: options?.pageSize ?? 1000,
);

return S3ListOperation(
request: StorageListRequest(
path: path,
Expand All @@ -167,17 +166,16 @@ class AmplifyStorageS3Dart extends StoragePluginInterface
nextToken: options?.nextToken,
pageSize: options?.pageSize ?? 1000,
);
final result = storageS3Service.list(
path: path,
options: s3Options,
);

return S3ListOperation(
request: StorageListRequest(
path: path,
options: options,
),
result: result,
result: storageS3Service.list(
path: path,
options: s3Options,
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,75 +126,6 @@ class StorageS3Service {
Future<S3ListResult> list({
required StoragePath path,
required StorageListOptions options,
}) async {
final s3PluginOptions = options.pluginOptions as S3ListPluginOptions? ??
const S3ListPluginOptions();

final resolvedPath = await _pathResolver.resolvePath(path: path);

if (!s3PluginOptions.listAll) {
final request = s3.ListObjectsV2Request.build((builder) {
builder
..bucket = _storageOutputs.bucketName
..prefix = resolvedPath
..maxKeys = options.pageSize
..continuationToken = options.nextToken
..delimiter = s3PluginOptions.excludeSubPaths
? s3PluginOptions.delimiter
: null;
});

try {
return S3ListResult.fromPaginatedResult(
await _defaultS3Client.listObjectsV2(request).result,
);
} on smithy.UnknownSmithyHttpException catch (error) {
// S3Client.headObject may return 403 error
throw error.toStorageException();
} on AWSHttpException catch (error) {
throw error.toNetworkException();
}
}

smithy.PaginatedResult<s3.ListObjectsV2Output, int, String> listResult;
S3ListResult recursiveResult;

try {
final request = s3.ListObjectsV2Request.build((builder) {
builder
..bucket = _storageOutputs.bucketName
..prefix = resolvedPath
..delimiter = s3PluginOptions.excludeSubPaths
? s3PluginOptions.delimiter
: null;
});

listResult = await _defaultS3Client.listObjectsV2(request).result;
recursiveResult = S3ListResult.fromPaginatedResult(
listResult,
);

while (listResult.hasNext) {
listResult = await listResult.next().result;
recursiveResult = recursiveResult.merge(
S3ListResult.fromPaginatedResult(
listResult,
),
);
}

return recursiveResult;
} on smithy.UnknownSmithyHttpException catch (error) {
// S3Client.headObject may return 403 error
throw error.toStorageException();
} on AWSHttpException catch (error) {
throw error.toNetworkException();
}
}

Future<S3ListResult> list2({
required StoragePath path,
required StorageListOptions options,
}) async {
final s3PluginOptions =
options.subpaths as SubpathStrategy? ?? const SubpathStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ void main() {

test('should forward default options to StorageS3Service.list() API',
() async {
const defaultOptions =
// StorageListOptions(pluginOptions: S3ListPluginOptions());
StorageListOptions(subpaths: SubpathStrategy());
// StorageListOptions(pluginOptions: SubpathStrategy());
const defaultOptions = StorageListOptions(subpaths: SubpathStrategy());

when(
() => storageS3Service.list(
Expand All @@ -115,35 +112,32 @@ void main() {
(_) async => testResult,
);

// final listOperation = storageS3Plugin.list(path: testPath);
// final listOperation = storageS3Plugin.list(path: testPath);

// final capturedOptions = verify(
// () => storageS3Service.list(
// path: testPath,
// options: captureAny<StorageListOptions>(
// named: 'options',
// ),
// ),
// ).captured.last;
//
// expect(
// capturedOptions,
// defaultOptions,
// );
//
// final result = await listOperation.result;
// expect(
// result,
// testResult,
// );
final listOperation = storageS3Plugin.list(path: testPath);

final capturedOptions = verify(
() => storageS3Service.list(
path: testPath,
options: captureAny<StorageListOptions>(
named: 'options',
),
),
).captured.last;

expect(
capturedOptions,
defaultOptions,
);

final result = await listOperation.result;
expect(
result,
testResult,
);
});

test('should forward options to StorageS3Service.list() API', () async {
const testOptions = StorageListOptions(
// pluginOptions: S3ListPluginOptions(),
subpaths: SubpathStrategy(),
// pluginOptions: SubpathStrategy.exclude(),
nextToken: 'next-token-123',
pageSize: 2,
);
Expand All @@ -155,9 +149,9 @@ void main() {
),
).thenAnswer(
(_) async => testResult,
); // returns null if not match
);

final listOperation = storageS3Plugin.list2(
final listOperation = storageS3Plugin.list(
path: testPath,
options: testOptions,
);
Expand Down
169 changes: 169 additions & 0 deletions packages/storage/amplify_storage_s3_dart/test/amplify_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// 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_core/src/types/storage/storage_path_from_identity_id.dart';
import 'package:amplify_storage_s3_dart/amplify_storage_s3_dart.dart';
import 'package:amplify_storage_s3_dart/src/model/s3_subpath_strategy.dart';
import 'package:amplify_storage_s3_dart/src/storage_s3_service/storage_s3_service.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

import 'test_utils/mocks.dart';
import 'test_utils/test_path_resolver.dart';
import 'test_utils/test_token_provider.dart';

const testPath = StoragePath.fromString('some/path.txt');

void main() {
final testConfig = const AmplifyConfig(
storage: StorageConfig(
plugins: {
S3PluginConfig.pluginKey: S3PluginConfig(
bucket: '123',
region: 'west-2',
),
},
),
// ignore: invalid_use_of_internal_member
).toAmplifyOutputs();

final testAuthProviderRepo = AmplifyAuthProviderRepository()
..registerAuthProvider(
APIAuthorizationType.userPools.authProviderToken,
TestTokenIdentityProvider(),
)
..registerAuthProvider(
APIAuthorizationType.iam.authProviderToken,
TestIamAuthProvider(),
);

group('AmplifyStorageS3Dart API', () {
late DependencyManager dependencyManager;
late AmplifyStorageS3Dart storageS3Plugin;
late StorageS3Service storageS3Service;

setUp(() async {
dependencyManager = AmplifyDependencyManager();
storageS3Service = MockStorageS3Service();
storageS3Plugin = AmplifyStorageS3Dart(
dependencyManagerOverride: dependencyManager,
);

await storageS3Plugin.configure(
config: testConfig,
authProviderRepo: testAuthProviderRepo,
);

dependencyManager.addInstance<StorageS3Service>(storageS3Service);

when(
() => storageS3Service.abortIncompleteMultipartUploads(),
).thenAnswer((_) async {});
});

tearDown(() {
dependencyManager.close();
});

group('list()', () {
const testPath = StoragePath.fromString('some/path');
final testResult = S3ListResult(
<String>[],
<S3Item>[],
hasNextPage: false,
metadata: S3ListMetadata.fromDelimiter(),
);

setUpAll(() {
registerFallbackValue(
const StorageListOptions(),
);
});

test('should forward default options to StorageS3Service.list() API',
() async {
const defaultOptions =
// StorageListOptions(pluginOptions: S3ListPluginOptions());
StorageListOptions(subpaths: SubpathStrategy());
// StorageListOptions(pluginOptions: SubpathStrategy());

when(
() => storageS3Service.list(
path: testPath,
options: defaultOptions,
),
).thenAnswer(
(_) async => testResult,
);

final listOperation = storageS3Plugin.list(path: testPath);
// final listOperation2 = storageS3Plugin.list(path: testPath);
//
final capturedOptions = verify(
() => storageS3Service.list(
path: testPath,
options: captureAny<StorageListOptions>(
named: 'options',
),
),
).captured.last;

expect(
capturedOptions,
defaultOptions,
);

final result = await listOperation.result;
expect(
result,
testResult,
);
});

// test('should forward options to StorageS3Service.list() API', () async {
// const testOptions = StorageListOptions(
// // pluginOptions: S3ListPluginOptions(),
// subpaths: SubpathStrategy(),
// // pluginOptions: SubpathStrategy.exclude(),
// nextToken: 'next-token-123',
// pageSize: 2,
// );
//
// when(
// () => storageS3Service.list(
// path: testPath,
// options: testOptions,
// ),
// ).thenAnswer(
// (_) async => testResult,
// ); // returns null if not match
//
// final listOperation = storageS3Plugin.list2(
// path: testPath,
// options: testOptions,
// );
//
// final capturedOptions = verify(
// () => storageS3Service.list(
// path: testPath,
// options: captureAny<StorageListOptions>(
// named: 'options',
// ),
// ),
// ).captured.last;
//
// expect(
// capturedOptions,
// testOptions,
// );
//
// final result = await listOperation.result;
// expect(
// result,
// testResult,
// );
// });
});
});
}

0 comments on commit d0ce057

Please sign in to comment.