-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: rememberDevice API Unit Test suite (#5108)
chore: add rememberdevice unit test
- Loading branch information
1 parent
4ec9c63
commit b29eb74
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
packages/auth/amplify_auth_cognito_test/test/plugin/remember_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,86 @@ | ||
// 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_dart/src/state/cognito_state_machine.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'; | ||
|
||
enum DeviceState { untracked, tracked, remembered } | ||
|
||
void main() { | ||
AmplifyLogger().logLevel = LogLevel.verbose; | ||
|
||
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig); | ||
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig); | ||
final testAuthRepo = AmplifyAuthProviderRepository(); | ||
final mockUpdateDeviceStatusResponse = UpdateDeviceStatusResponse(); | ||
|
||
late DeviceMetadataRepository repo; | ||
late AmplifyAuthCognitoDart plugin; | ||
late CognitoAuthStateMachine stateMachine; | ||
late MockSecureStorage secureStorage; | ||
|
||
Future<DeviceState> getDeviceState() async { | ||
final secrets = await repo.get(username); | ||
if (secrets == null) { | ||
return DeviceState.untracked; | ||
} | ||
return secrets.deviceStatus == DeviceRememberedStatusType.remembered | ||
? DeviceState.remembered | ||
: DeviceState.tracked; | ||
} | ||
|
||
group('rememberDevice', () { | ||
setUp(() async { | ||
secureStorage = MockSecureStorage(); | ||
seedStorage( | ||
secureStorage, | ||
userPoolKeys: userPoolKeys, | ||
identityPoolKeys: identityPoolKeys, | ||
deviceKeys: CognitoDeviceKeys(userPoolConfig, username), | ||
); | ||
plugin = AmplifyAuthCognitoDart( | ||
secureStorageFactory: (_) => secureStorage, | ||
); | ||
stateMachine = plugin.stateMachine; | ||
await plugin.configure( | ||
config: mockConfig, | ||
authProviderRepo: testAuthRepo, | ||
); | ||
final mockIdp = MockCognitoIdentityProviderClient( | ||
forgetDevice: () async {}, | ||
updateDeviceStatus: () async => mockUpdateDeviceStatusResponse, | ||
); | ||
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); | ||
repo = stateMachine.getOrCreate<DeviceMetadataRepository>(); | ||
}); | ||
|
||
tearDown(() async { | ||
await plugin.close(); | ||
}); | ||
|
||
test('rememberDevice changes the device state from tracked to remembered', | ||
() async { | ||
expect(await getDeviceState(), DeviceState.tracked); | ||
await plugin.rememberDevice(); | ||
expect(await getDeviceState(), DeviceState.remembered); | ||
}); | ||
|
||
test( | ||
'rememberDevice throws a DeviceNotTrackedException when device is forgotten', | ||
() async { | ||
await plugin.forgetDevice(); | ||
await expectLater( | ||
plugin.rememberDevice, | ||
throwsA(isA<DeviceNotTrackedException>()), | ||
); | ||
}); | ||
}); | ||
} |