-
Notifications
You must be signed in to change notification settings - Fork 260
chore(auth): Unit tests for fetchCurrentDevice API #5074
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
Merged
hahnandrew
merged 10 commits into
feat/fetchCurrentDevice
from
feat/fetchCurrentDevice-unit-test
Jul 16, 2024
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ffd7ad8
feat: added unit tests
hahnandrew 97e6278
chore: append test to file name
hahnandrew 685fa37
feat: adding implementation for unit test ci
hahnandrew 1cbc9fa
test: restructure fetchcurrentdevice test
hahnandrew aa89197
test: improve integration test wording
hahnandrew 6f57370
Update packages/auth/amplify_auth_cognito_test/test/plugin/fetch_curr…
hahnandrew 550879a
Update packages/auth/amplify_auth_cognito_test/test/plugin/fetch_curr…
hahnandrew 93e841f
Update packages/auth/amplify_auth_cognito_test/test/plugin/fetch_curr…
hahnandrew 4bc1f1e
Update packages/auth/amplify_auth_cognito_test/test/plugin/fetch_curr…
hahnandrew 10e5a0f
test: improve wording for clarity
hahnandrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/auth/amplify_auth_cognito_test/test/plugin/fetch_current_device_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart'; | ||
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart'; | ||
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart'; | ||
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'; | ||
import 'package:amplify_auth_cognito_test/common/mock_clients.dart'; | ||
import 'package:amplify_auth_cognito_test/common/mock_config.dart'; | ||
import 'package:amplify_auth_cognito_test/common/mock_secure_storage.dart'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
AmplifyLogger().logLevel = LogLevel.verbose; | ||
|
||
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig); | ||
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig); | ||
final testAuthRepo = AmplifyAuthProviderRepository(); | ||
final mockDevice = DeviceType(deviceKey: deviceKey); | ||
final mockDeviceResponse = GetDeviceResponse(device: mockDevice); | ||
|
||
late DeviceMetadataRepository repo; | ||
late AmplifyAuthCognitoDart plugin; | ||
|
||
group('fetchCurrentDevice', () { | ||
setUp(() async { | ||
final secureStorage = MockSecureStorage(); | ||
seedStorage( | ||
secureStorage, | ||
userPoolKeys: userPoolKeys, | ||
identityPoolKeys: identityPoolKeys, | ||
deviceKeys: CognitoDeviceKeys(userPoolConfig, username), | ||
); | ||
plugin = AmplifyAuthCognitoDart( | ||
secureStorageFactory: (_) => secureStorage, | ||
); | ||
await plugin.configure( | ||
config: mockConfig, | ||
authProviderRepo: testAuthRepo, | ||
); | ||
repo = plugin.stateMachine.getOrCreate<DeviceMetadataRepository>(); | ||
}); | ||
|
||
group('Cognito GetDevice returns successfully.', () { | ||
setUp(() async { | ||
final mockIdp = MockCognitoIdentityProviderClient( | ||
getDevice: () async => mockDeviceResponse, | ||
forgetDevice: () async {}, | ||
); | ||
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); | ||
}); | ||
|
||
test( | ||
'This test should get the current device, where the current device id is equal to the local device id', | ||
hahnandrew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
() async { | ||
final secrets = await repo.get(username); | ||
final currentDeviceKey = secrets?.deviceKey; | ||
expect(currentDeviceKey, isNotNull); | ||
final currentDevice = await plugin.fetchCurrentDevice(); | ||
expect(currentDeviceKey, currentDevice.id); | ||
}); | ||
|
||
test( | ||
'This test should throw a DeviceNotTrackedException when current device key is null', | ||
hahnandrew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
() async { | ||
await plugin.forgetDevice(); | ||
await expectLater( | ||
plugin.fetchCurrentDevice, | ||
throwsA(isA<DeviceNotTrackedException>()), | ||
); | ||
}); | ||
}); | ||
|
||
group('This test should have Cognito GetDevice throw a AWSHttpException', | ||
hahnandrew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
() { | ||
setUp(() async { | ||
final mockIdp = MockCognitoIdentityProviderClient( | ||
getDevice: () async => throw AWSHttpException( | ||
AWSHttpRequest.get(Uri.parse('https://aws.amazon.com/cognito/')), | ||
), | ||
); | ||
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); | ||
}); | ||
|
||
test('This test should have Cognito GetDevice throw a NetworkException', | ||
() async { | ||
await expectLater( | ||
plugin.fetchCurrentDevice, | ||
throwsA(isA<NetworkException>()), | ||
); | ||
}); | ||
}); | ||
|
||
tearDown(() async { | ||
await plugin.close(); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.