|
| 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/sdk/cognito_identity_provider.dart'; |
| 7 | +import 'package:amplify_core/amplify_core.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import '../common/jwt.dart'; |
| 11 | +import '../common/mock_clients.dart'; |
| 12 | +import '../common/mock_config.dart'; |
| 13 | +import '../common/mock_secure_storage.dart'; |
| 14 | + |
| 15 | +void main() { |
| 16 | + AmplifyLogger().logLevel = LogLevel.verbose; |
| 17 | + |
| 18 | + late CognitoAuthStateMachine stateMachine; |
| 19 | + late AmplifyAuthCognitoDart plugin; |
| 20 | + |
| 21 | + group('fetchAuthSession', () { |
| 22 | + group('when session is expired', () { |
| 23 | + setUp(() async { |
| 24 | + final expiredIdToken = createJwt( |
| 25 | + type: TokenType.id, |
| 26 | + expiration: Duration.zero, |
| 27 | + ); |
| 28 | + final secureStorage = MockSecureStorage(); |
| 29 | + seedStorage( |
| 30 | + secureStorage, |
| 31 | + identityPoolKeys: identityPoolKeys, |
| 32 | + userPoolKeys: userPoolKeys, |
| 33 | + ); |
| 34 | + secureStorage.write( |
| 35 | + key: userPoolKeys[CognitoUserPoolKey.idToken], |
| 36 | + value: expiredIdToken.raw, |
| 37 | + ); |
| 38 | + stateMachine = CognitoAuthStateMachine(); |
| 39 | + plugin = AmplifyAuthCognitoDart(credentialStorage: secureStorage) |
| 40 | + ..stateMachine = stateMachine; |
| 41 | + await plugin.configure( |
| 42 | + config: mockConfig, |
| 43 | + authProviderRepo: AmplifyAuthProviderRepository(), |
| 44 | + ); |
| 45 | + |
| 46 | + stateMachine.addInstance<CognitoIdentityProviderClient>( |
| 47 | + MockCognitoIdentityProviderClient( |
| 48 | + initiateAuth: expectAsync0( |
| 49 | + () async => throw const AuthNotAuthorizedException( |
| 50 | + 'Refresh Token has expired.', |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should add a sessionExpired event to Auth Hub', () async { |
| 58 | + final authStream = Amplify.Hub.availableStreams[HubChannel.Auth]; |
| 59 | + plugin.fetchAuthSession().ignore(); |
| 60 | + await expectLater(authStream, emits(AuthHubEvent.sessionExpired())); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + tearDown(() async { |
| 65 | + await plugin.close(); |
| 66 | + }); |
| 67 | + }); |
| 68 | +} |
0 commit comments