-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #930 from SocialGouv/feat/401-handling
feat(interceptors): add UnauthorizedInterceptor to handle 401 errors
- Loading branch information
Showing
7 changed files
with
182 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
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
42 changes: 42 additions & 0 deletions
42
lib/network/interceptors/logout_after_too_many_401_interceptor.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,42 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:pass_emploi_app/features/login/login_actions.dart'; | ||
import 'package:pass_emploi_app/network/interceptors/pass_emploi_base_interceptor.dart'; | ||
import 'package:pass_emploi_app/redux/app_state.dart'; | ||
import 'package:pass_emploi_app/repositories/remote_config_repository.dart'; | ||
import 'package:redux/redux.dart'; | ||
|
||
class LogoutAfterTooMany401Interceptor extends PassEmploiBaseInterceptor { | ||
final RemoteConfigRepository _remoteConfigRepository; | ||
|
||
LogoutAfterTooMany401Interceptor(RemoteConfigRepository remoteConfigRepository) | ||
: _remoteConfigRepository = remoteConfigRepository; | ||
|
||
late final Store<AppState> _store; | ||
int unauthorizedCount = 0; | ||
|
||
@override | ||
void onPassEmploiError(DioException err, ErrorInterceptorHandler handler) { | ||
final maxUnauthorizedErrorsBeforeLogout = _remoteConfigRepository.maxUnauthorizedErrorsBeforeLogout(); | ||
|
||
if (maxUnauthorizedErrorsBeforeLogout == null) { | ||
handler.next(err); | ||
return; | ||
} | ||
|
||
if (err.response?.statusCode == 401) { | ||
unauthorizedCount++; | ||
if (unauthorizedCount >= maxUnauthorizedErrorsBeforeLogout) { | ||
_onUnauthorizedErrorCountExceeded(); | ||
} | ||
} else { | ||
unauthorizedCount = 0; | ||
} | ||
handler.next(err); | ||
} | ||
|
||
void _onUnauthorizedErrorCountExceeded() { | ||
_store.dispatch(RequestLogoutAction(LogoutReason.tooMany401)); | ||
} | ||
|
||
void setStore(Store<AppState> store) => _store = store; | ||
} |
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
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
104 changes: 104 additions & 0 deletions
104
test/network/interceptors/logout_after_too_many_401_interceptor_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,104 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:pass_emploi_app/features/login/login_actions.dart'; | ||
import 'package:pass_emploi_app/network/interceptors/logout_after_too_many_401_interceptor.dart'; | ||
import 'package:pass_emploi_app/redux/app_state.dart'; | ||
import 'package:pass_emploi_app/repositories/remote_config_repository.dart'; | ||
import 'package:redux/redux.dart'; | ||
|
||
import '../../doubles/spies.dart'; | ||
|
||
class MockStore extends Mock implements Store<AppState> {} | ||
|
||
class MockInterceptorHandler extends Mock implements ErrorInterceptorHandler {} | ||
|
||
class MockRemoteConfigRepository extends Mock implements RemoteConfigRepository { | ||
void withUnhautorizedLimitAt(int? limit) { | ||
when(() => maxUnauthorizedErrorsBeforeLogout()).thenReturn(limit); | ||
} | ||
} | ||
|
||
void main() { | ||
late LogoutAfterTooMany401Interceptor interceptor; | ||
late MockInterceptorHandler interceptorHandler; | ||
late MockRemoteConfigRepository remoteConfigRepository; | ||
late MockStore mockStore; | ||
|
||
setUp(() { | ||
remoteConfigRepository = MockRemoteConfigRepository(); | ||
interceptor = LogoutAfterTooMany401Interceptor(remoteConfigRepository); | ||
interceptorHandler = MockInterceptorHandler(); | ||
mockStore = MockStore(); | ||
}); | ||
|
||
test('should do nothing when unauthorized limit is null', () { | ||
// Given | ||
remoteConfigRepository.withUnhautorizedLimitAt(null); | ||
final dioError = DioException( | ||
requestOptions: RequestOptions(path: '/test'), | ||
response: Response(statusCode: 401, requestOptions: RequestOptions(path: '/test')), | ||
); | ||
interceptor.setStore(mockStore); | ||
|
||
// When | ||
interceptor.onPassEmploiError(dioError, interceptorHandler); | ||
|
||
// Then | ||
expect(interceptor.unauthorizedCount, 0); | ||
verifyNever(() => mockStore.dispatch(any)); | ||
}); | ||
|
||
test('increments unauthorizedCount on 401 error', () { | ||
// Given | ||
remoteConfigRepository.withUnhautorizedLimitAt(10); | ||
final dioError = DioException( | ||
requestOptions: RequestOptions(path: '/test'), | ||
response: Response(statusCode: 401, requestOptions: RequestOptions(path: '/test')), | ||
); | ||
interceptor.setStore(mockStore); | ||
|
||
// When | ||
interceptor.onPassEmploiError(dioError, interceptorHandler); | ||
|
||
// Then | ||
expect(interceptor.unauthorizedCount, 1); | ||
verifyNever(() => mockStore.dispatch(any)); | ||
}); | ||
|
||
test('dispatches RequestLogoutAction when limit is exceeded', () { | ||
// Given | ||
remoteConfigRepository.withUnhautorizedLimitAt(1); | ||
final store = StoreSpy(); | ||
final dioError = DioException( | ||
requestOptions: RequestOptions(path: '/test'), | ||
response: Response(statusCode: 401, requestOptions: RequestOptions(path: '/test')), | ||
); | ||
interceptor.setStore(store); | ||
|
||
// When | ||
interceptor.onPassEmploiError(dioError, interceptorHandler); | ||
|
||
// Then | ||
expect(interceptor.unauthorizedCount, 1); | ||
expect(store.dispatchedAction, isA<RequestLogoutAction>()); | ||
}); | ||
|
||
test('reset unauthorizedCount for non-401 errors', () { | ||
// Given | ||
remoteConfigRepository.withUnhautorizedLimitAt(10); | ||
interceptor.unauthorizedCount = 5; | ||
final dioError = DioException( | ||
requestOptions: RequestOptions(path: '/test'), | ||
response: Response(statusCode: 500, requestOptions: RequestOptions(path: '/test')), | ||
); | ||
interceptor.setStore(mockStore); | ||
|
||
// When | ||
interceptor.onPassEmploiError(dioError, interceptorHandler); | ||
|
||
// Then | ||
expect(interceptor.unauthorizedCount, 0); | ||
verifyNever(() => mockStore.dispatch(any)); | ||
}); | ||
} |
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