-
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.
Browse files
Browse the repository at this point in the history
[#15] Implement backend for logout functionality
- Loading branch information
Showing
7 changed files
with
159 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'logout_request.g.dart'; | ||
|
||
@JsonSerializable() | ||
class LogoutRequest { | ||
final String token; | ||
final String clientId; | ||
final String clientSecret; | ||
|
||
LogoutRequest({ | ||
required this.token, | ||
required this.clientId, | ||
required this.clientSecret, | ||
}); | ||
|
||
factory LogoutRequest.fromJson(Map<String, dynamic> json) => | ||
_$LogoutRequestFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$LogoutRequestToJson(this); | ||
} |
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
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,37 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:injectable/injectable.dart'; | ||
import 'package:survey/api/exception/network_exceptions.dart'; | ||
import 'package:survey/api/repository/auth_repository.dart'; | ||
import 'package:survey/database/hive_utils.dart'; | ||
import 'package:survey/database/shared_preferences_utils.dart'; | ||
import 'package:survey/usecase/base/base_use_case.dart'; | ||
|
||
@Injectable() | ||
class LogoutUseCase extends NoInputUseCase<void> { | ||
final AuthRepository _repository; | ||
final SharedPreferencesUtils _sharedPreferencesUtils; | ||
final HiveUtils _hiveUtils; | ||
|
||
const LogoutUseCase( | ||
this._repository, | ||
this._sharedPreferencesUtils, | ||
this._hiveUtils, | ||
); | ||
|
||
@override | ||
Future<Result<void>> call() async { | ||
final token = await _sharedPreferencesUtils.accessToken; | ||
return _repository | ||
.logout(token: token) | ||
.then((value) => _clearTokensAndCache()) | ||
.onError<NetworkExceptions>( | ||
(exception, stackTrace) => Failed(UseCaseException(exception))); | ||
} | ||
|
||
Result<void> _clearTokensAndCache() { | ||
_sharedPreferencesUtils.clear(); | ||
_hiveUtils.clearSurveys(); | ||
return Success(null); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:survey/api/exception/network_exceptions.dart'; | ||
import 'package:survey/usecase/base/base_use_case.dart'; | ||
import 'package:survey/usecase/logout_use_case.dart'; | ||
|
||
import '../../test/mock/mock_dependencies.mocks.dart'; | ||
|
||
void main() { | ||
group('LogoutUseCaseTest', () { | ||
late MockAuthRepository mockRepository; | ||
late MockSharedPreferencesUtils mockSharedPreferencesUtils; | ||
late MockHiveUtils mockHiveUtils; | ||
late LogoutUseCase useCase; | ||
|
||
setUp(() { | ||
mockRepository = MockAuthRepository(); | ||
mockSharedPreferencesUtils = MockSharedPreferencesUtils(); | ||
mockHiveUtils = MockHiveUtils(); | ||
useCase = LogoutUseCase( | ||
mockRepository, | ||
mockSharedPreferencesUtils, | ||
mockHiveUtils, | ||
); | ||
}); | ||
|
||
test( | ||
'When executing use case and repository returns success, it returns Success result', | ||
() async { | ||
when(mockRepository.logout(token: anyNamed('token'))) | ||
.thenAnswer((_) async => null); | ||
|
||
final result = await useCase.call(); | ||
|
||
expect(result, isA<Success>()); | ||
verify(mockSharedPreferencesUtils.clear()).called(1); | ||
verify(mockHiveUtils.clearSurveys()).called(1); | ||
}); | ||
|
||
test( | ||
'When executing use case and repository returns error, it returns Failed result', | ||
() async { | ||
final exception = NetworkExceptions.badRequest(); | ||
when(mockRepository.logout(token: anyNamed('token'))) | ||
.thenAnswer((_) => Future.error(exception)); | ||
|
||
final result = await useCase.call(); | ||
|
||
expect(result, isA<Failed>()); | ||
expect((result as Failed).exception.actualException, exception); | ||
}); | ||
}); | ||
} |