-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Auth): Add fetchCurrentDevice API (#5251)
feat(Auth): Add fetchCurrentDevice API (#5251)
- Loading branch information
1 parent
ee26d74
commit c6a06ef
Showing
9 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
96 changes: 96 additions & 0 deletions
96
packages/auth/amplify_auth_cognito_test/test/plugin/fetch_current_device_test.dart
This file contains 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,96 @@ | ||
// 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.appClientId); | ||
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig.poolId); | ||
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.appClientId, username), | ||
); | ||
plugin = AmplifyAuthCognitoDart( | ||
secureStorageFactory: (_) => secureStorage, | ||
); | ||
await plugin.configure( | ||
config: mockConfig, | ||
authProviderRepo: testAuthRepo, | ||
); | ||
repo = plugin.stateMachine.getOrCreate<DeviceMetadataRepository>(); | ||
}); | ||
|
||
group('should successfully', () { | ||
setUp(() async { | ||
final mockIdp = MockCognitoIdentityProviderClient( | ||
getDevice: () async => mockDeviceResponse, | ||
forgetDevice: () async {}, | ||
); | ||
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); | ||
}); | ||
|
||
test( | ||
'return the current device where the current device id is equal to the local device id', | ||
() async { | ||
final secrets = await repo.get(username); | ||
final currentDeviceKey = secrets?.deviceKey; | ||
expect(currentDeviceKey, isNotNull); | ||
final currentDevice = await plugin.fetchCurrentDevice(); | ||
expect(currentDeviceKey, currentDevice.id); | ||
}); | ||
|
||
test('throw a DeviceNotTrackedException when current device key is null', | ||
() async { | ||
await plugin.forgetDevice(); | ||
await expectLater( | ||
plugin.fetchCurrentDevice, | ||
throwsA(isA<DeviceNotTrackedException>()), | ||
); | ||
}); | ||
}); | ||
|
||
group('should throw', () { | ||
setUp(() async { | ||
final mockIdp = MockCognitoIdentityProviderClient( | ||
getDevice: () async => throw AWSHttpException( | ||
AWSHttpRequest.get(Uri.parse('https://aws.amazon.com/cognito/')), | ||
), | ||
); | ||
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); | ||
}); | ||
|
||
test('a NetworkException', () async { | ||
await expectLater( | ||
plugin.fetchCurrentDevice, | ||
throwsA(isA<NetworkException>()), | ||
); | ||
}); | ||
}); | ||
|
||
tearDown(() async { | ||
await plugin.close(); | ||
}); | ||
}); | ||
} |
This file contains 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