-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): SessionExpired Auth Hub event (#2609)
* fix(auth): SessionExpired Auth Hub event * chore: add todo comment
- Loading branch information
1 parent
cf9520c
commit f49134a
Showing
2 changed files
with
78 additions
and
4 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
68 changes: 68 additions & 0 deletions
68
packages/auth/amplify_auth_cognito_test/test/plugin/fetch_auth_session_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,68 @@ | ||
// 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/sdk/cognito_identity_provider.dart'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../common/jwt.dart'; | ||
import '../common/mock_clients.dart'; | ||
import '../common/mock_config.dart'; | ||
import '../common/mock_secure_storage.dart'; | ||
|
||
void main() { | ||
AmplifyLogger().logLevel = LogLevel.verbose; | ||
|
||
late CognitoAuthStateMachine stateMachine; | ||
late AmplifyAuthCognitoDart plugin; | ||
|
||
group('fetchAuthSession', () { | ||
group('when session is expired', () { | ||
setUp(() async { | ||
final expiredIdToken = createJwt( | ||
type: TokenType.id, | ||
expiration: Duration.zero, | ||
); | ||
final secureStorage = MockSecureStorage(); | ||
seedStorage( | ||
secureStorage, | ||
identityPoolKeys: identityPoolKeys, | ||
userPoolKeys: userPoolKeys, | ||
); | ||
secureStorage.write( | ||
key: userPoolKeys[CognitoUserPoolKey.idToken], | ||
value: expiredIdToken.raw, | ||
); | ||
stateMachine = CognitoAuthStateMachine(); | ||
plugin = AmplifyAuthCognitoDart(credentialStorage: secureStorage) | ||
..stateMachine = stateMachine; | ||
await plugin.configure( | ||
config: mockConfig, | ||
authProviderRepo: AmplifyAuthProviderRepository(), | ||
); | ||
|
||
stateMachine.addInstance<CognitoIdentityProviderClient>( | ||
MockCognitoIdentityProviderClient( | ||
initiateAuth: expectAsync0( | ||
() async => throw const AuthNotAuthorizedException( | ||
'Refresh Token has expired.', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('should add a sessionExpired event to Auth Hub', () async { | ||
final authStream = Amplify.Hub.availableStreams[HubChannel.Auth]; | ||
plugin.fetchAuthSession().ignore(); | ||
await expectLater(authStream, emits(AuthHubEvent.sessionExpired())); | ||
}); | ||
}); | ||
|
||
tearDown(() async { | ||
await plugin.close(); | ||
}); | ||
}); | ||
} |