|
| 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 | + final mockDevice = DeviceType(deviceKey: deviceKey); |
| 22 | + final mockDeviceResponse = GetDeviceResponse(device: mockDevice); |
| 23 | + |
| 24 | + late DeviceMetadataRepository repo; |
| 25 | + late AmplifyAuthCognitoDart plugin; |
| 26 | + late CognitoAuthStateMachine stateMachine; |
| 27 | + late MockSecureStorage secureStorage; |
| 28 | + |
| 29 | + Future<String?> getDeviceKey() async { |
| 30 | + final secrets = await repo.get(username); |
| 31 | + return secrets?.deviceKey; |
| 32 | + } |
| 33 | + |
| 34 | + group( |
| 35 | + 'fetchCurrentDevice expected return value and DeviceNotTrackedException', |
| 36 | + () { |
| 37 | + setUp(() async { |
| 38 | + secureStorage = MockSecureStorage(); |
| 39 | + seedStorage( |
| 40 | + secureStorage, |
| 41 | + userPoolKeys: userPoolKeys, |
| 42 | + identityPoolKeys: identityPoolKeys, |
| 43 | + deviceKeys: CognitoDeviceKeys(userPoolConfig, username), |
| 44 | + ); |
| 45 | + plugin = AmplifyAuthCognitoDart( |
| 46 | + secureStorageFactory: (_) => secureStorage, |
| 47 | + ); |
| 48 | + stateMachine = plugin.stateMachine; |
| 49 | + await plugin.configure( |
| 50 | + config: mockConfig, |
| 51 | + authProviderRepo: testAuthRepo, |
| 52 | + ); |
| 53 | + final mockIdp = MockCognitoIdentityProviderClient( |
| 54 | + getDevice: () async => mockDeviceResponse, |
| 55 | + forgetDevice: () async {}, |
| 56 | + ); |
| 57 | + stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); |
| 58 | + repo = stateMachine.getOrCreate<DeviceMetadataRepository>(); |
| 59 | + }); |
| 60 | + |
| 61 | + test( |
| 62 | + 'should get the current device. current device id should be equal to the local device id', |
| 63 | + () async { |
| 64 | + final currentDeviceKey = await getDeviceKey(); |
| 65 | + expect(currentDeviceKey, isNotNull); |
| 66 | + final currentDevice = await plugin.fetchCurrentDevice(); |
| 67 | + expect(currentDeviceKey, currentDevice.id); |
| 68 | + }); |
| 69 | + |
| 70 | + test( |
| 71 | + 'should should throw a DeviceNotTrackedException when current device key is null', |
| 72 | + () async { |
| 73 | + await plugin.forgetDevice(); |
| 74 | + await expectLater( |
| 75 | + plugin.fetchCurrentDevice, |
| 76 | + throwsA(isA<DeviceNotTrackedException>()), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + tearDown(() async { |
| 81 | + await plugin.close(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + group('fetchCurrentDevice AWSHttpException', () { |
| 86 | + setUp(() async { |
| 87 | + secureStorage = MockSecureStorage(); |
| 88 | + seedStorage( |
| 89 | + secureStorage, |
| 90 | + userPoolKeys: userPoolKeys, |
| 91 | + identityPoolKeys: identityPoolKeys, |
| 92 | + deviceKeys: CognitoDeviceKeys(userPoolConfig, username), |
| 93 | + ); |
| 94 | + plugin = AmplifyAuthCognitoDart( |
| 95 | + secureStorageFactory: (_) => secureStorage, |
| 96 | + ); |
| 97 | + stateMachine = plugin.stateMachine; |
| 98 | + await plugin.configure( |
| 99 | + config: mockConfig, |
| 100 | + authProviderRepo: testAuthRepo, |
| 101 | + ); |
| 102 | + final mockIdp = MockCognitoIdentityProviderClient( |
| 103 | + getDevice: () async => throw AWSHttpException( |
| 104 | + AWSHttpRequest.get(Uri.parse('https://www.amazon.com')), |
| 105 | + ), |
| 106 | + ); |
| 107 | + stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp); |
| 108 | + repo = stateMachine.getOrCreate<DeviceMetadataRepository>(); |
| 109 | + }); |
| 110 | + |
| 111 | + test('should throw a NetworkException', () async { |
| 112 | + await expectLater( |
| 113 | + plugin.fetchCurrentDevice, |
| 114 | + throwsA(isA<NetworkException>()), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + tearDown(() async { |
| 119 | + await plugin.close(); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
0 commit comments