|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart'; |
| 5 | +import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart'; |
| 6 | +import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart'; |
| 7 | +import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'; |
| 8 | +import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart'; |
| 9 | +import 'package:amplify_auth_cognito_test/common/mock_clients.dart'; |
| 10 | +import 'package:amplify_auth_cognito_test/common/mock_config.dart'; |
| 11 | +import 'package:amplify_auth_cognito_test/common/mock_secure_storage.dart'; |
| 12 | +import 'package:amplify_core/amplify_core.dart'; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +void main() { |
| 16 | + AmplifyLogger().logLevel = LogLevel.verbose; |
| 17 | + |
| 18 | + final userPoolKeys = CognitoUserPoolKeys(userPoolConfig); |
| 19 | + final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig); |
| 20 | + final testAuthRepo = AmplifyAuthProviderRepository(); |
| 21 | + |
| 22 | + late DeviceMetadataRepository repo; |
| 23 | + late AmplifyAuthCognitoDart plugin; |
| 24 | + late CognitoAuthStateMachine stateMachine; |
| 25 | + late MockSecureStorage secureStorage; |
| 26 | + |
| 27 | + group('forgetDevice', () { |
| 28 | + setUp(() async { |
| 29 | + secureStorage = MockSecureStorage(); |
| 30 | + seedStorage( |
| 31 | + secureStorage, |
| 32 | + userPoolKeys: userPoolKeys, |
| 33 | + identityPoolKeys: identityPoolKeys, |
| 34 | + deviceKeys: CognitoDeviceKeys(userPoolConfig, username), |
| 35 | + ); |
| 36 | + plugin = AmplifyAuthCognitoDart( |
| 37 | + secureStorageFactory: (_) => secureStorage, |
| 38 | + ); |
| 39 | + stateMachine = plugin.stateMachine; |
| 40 | + await plugin.configure( |
| 41 | + config: mockConfig, |
| 42 | + authProviderRepo: testAuthRepo, |
| 43 | + ); |
| 44 | + final mockIdp = MockCognitoIdentityProviderClient( |
| 45 | + forgetDevice: () async {}, |
| 46 | + ); |
| 47 | + stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); |
| 48 | + repo = stateMachine.getOrCreate<DeviceMetadataRepository>(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should remove the local device when called with no device ID', |
| 52 | + () async { |
| 53 | + expect(await repo.get(username), isNotNull); |
| 54 | + await plugin.forgetDevice(); |
| 55 | + expect(await repo.get(username), isNull); |
| 56 | + }); |
| 57 | + |
| 58 | + test( |
| 59 | + 'should remove the local device when the device ID matches the local device ID', |
| 60 | + () async { |
| 61 | + expect(await repo.get(username), isNotNull); |
| 62 | + await plugin.forgetDevice(const CognitoDevice(id: deviceKey)); |
| 63 | + expect(await repo.get(username), isNull); |
| 64 | + }); |
| 65 | + |
| 66 | + test( |
| 67 | + 'should not remove the local device when the device ID does not match the local device ID', |
| 68 | + () async { |
| 69 | + expect(await repo.get(username), isNotNull); |
| 70 | + await plugin.forgetDevice(const CognitoDevice(id: 'other-device-id')); |
| 71 | + expect(await repo.get(username), isNotNull); |
| 72 | + }); |
| 73 | + |
| 74 | + tearDown(() async { |
| 75 | + await plugin.close(); |
| 76 | + }); |
| 77 | + }); |
| 78 | +} |
0 commit comments