diff --git a/lib/api/authentication_api_service.dart b/lib/api/authentication_api_service.dart index 2aafc8d..5c5683c 100644 --- a/lib/api/authentication_api_service.dart +++ b/lib/api/authentication_api_service.dart @@ -7,7 +7,8 @@ part 'authentication_api_service.g.dart'; @RestApi() abstract class AuthenticationApiService { - factory AuthenticationApiService(Dio dio, {String baseUrl}) = _AuthenticationApiService; + factory AuthenticationApiService(Dio dio, {String baseUrl}) = + _AuthenticationApiService; @POST('/oauth/token') Future login( diff --git a/lib/env.dart b/lib/env.dart index 0adb9de..aa6753e 100644 --- a/lib/env.dart +++ b/lib/env.dart @@ -4,9 +4,11 @@ class Env { static String get restApiEndpoint { return FlutterConfig.get('REST_API_ENDPOINT'); } + static String get clientId { return FlutterConfig.get('CLIENT_ID'); } + static String get clientSecret { return FlutterConfig.get('CLIENT_SECRET'); } diff --git a/lib/repository/authentication_repository.dart b/lib/repositories/authentication_repository.dart similarity index 78% rename from lib/repository/authentication_repository.dart rename to lib/repositories/authentication_repository.dart index a8f26ea..7bceca6 100644 --- a/lib/repository/authentication_repository.dart +++ b/lib/repositories/authentication_repository.dart @@ -24,18 +24,16 @@ class AuthenticationRepositoryImpl extends AuthenticationRepository { required String password, }) async { try { - final response = await _authenticationApiService.login( - LoginRequest( - email: email, - password: password, - clientId: Env.clientId, - clientSecret: Env.clientSecret, - grantType: grantType, - ), - ); + final response = await _authenticationApiService.login(LoginRequest( + email: email, + password: password, + clientId: Env.clientId, + clientSecret: Env.clientSecret, + grantType: grantType, + )); return response.toLoginModel(); } catch (exception) { throw NetworkExceptions.fromDioException(exception); } } -} \ No newline at end of file +} diff --git a/lib/usecases/login_use_case.dart b/lib/usecases/login_use_case.dart index ba8b792..a069383 100644 --- a/lib/usecases/login_use_case.dart +++ b/lib/usecases/login_use_case.dart @@ -1,6 +1,6 @@ import 'dart:async'; import 'package:survey_flutter/model/login_model.dart'; -import 'package:survey_flutter/repository/authentication_repository.dart'; +import 'package:survey_flutter/repositories/authentication_repository.dart'; import 'package:survey_flutter/usecases/base/base_use_case.dart'; import 'package:injectable/injectable.dart'; @@ -32,4 +32,4 @@ class LoginUseCase extends UseCase { return Failed(UseCaseException(exception)); } } -} \ No newline at end of file +} diff --git a/test/api/repositories/authentication_repository_test.dart b/test/api/repositories/authentication_repository_test.dart new file mode 100644 index 0000000..ed2fa7a --- /dev/null +++ b/test/api/repositories/authentication_repository_test.dart @@ -0,0 +1,57 @@ +import 'package:flutter_config/flutter_config.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:survey_flutter/repositories/authentication_repository.dart'; +import 'package:survey_flutter/api/exception/network_exceptions.dart'; +import 'package:survey_flutter/model/response/login_response.dart'; + +import '../../mocks/generate_mocks.mocks.dart'; + +void main() { + group('AuthenticationRepositoryTest', () { + late MockAuthenticationApiService mockAuthApiService; + late AuthenticationRepositoryImpl authRepository; + + const email = "email"; + const password = "password"; + + setUpAll(() { + FlutterConfig.loadValueForTesting({ + 'CLIENT_ID': 'CLIENT_ID', + 'CLIENT_SECRET': 'CLIENT_SECRET', + }); + }); + + setUp(() { + mockAuthApiService = MockAuthenticationApiService(); + authRepository = AuthenticationRepositoryImpl(mockAuthApiService); + }); + + test('When login successfully, it returns correct model', () async { + final loginResponse = LoginResponse( + id: "", + accessToken: "", + tokenType: "", + expiresIn: 0, + refreshToken: "", + createdAt: 0, + ); + + when(mockAuthApiService.login(any)) + .thenAnswer((_) async => loginResponse); + + final result = + await authRepository.login(email: email, password: password); + + expect(result, loginResponse.toLoginModel()); + }); + + test('When login fail, it returns failed exception', () async { + when(mockAuthApiService.login(any)).thenThrow(Exception()); + + final result = authRepository.login(email: email, password: password); + + expect(result, throwsA(isA())); + }); + }); +} diff --git a/test/api/usecases/login_use_case_test.dart b/test/api/usecases/login_use_case_test.dart new file mode 100644 index 0000000..cc933f2 --- /dev/null +++ b/test/api/usecases/login_use_case_test.dart @@ -0,0 +1,64 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:survey_flutter/model/login_model.dart'; +import 'package:survey_flutter/usecases/base/base_use_case.dart'; +import 'package:survey_flutter/usecases/login_use_case.dart'; +import '../../mocks/generate_mocks.mocks.dart'; + +void main() { + group('LoginUseCaseTest', () { + late MockAuthenticationRepository mockAuthRepository; + late LoginUseCase useCase; + + const email = "email"; + const password = "password"; + + setUp(() { + mockAuthRepository = MockAuthenticationRepository(); + useCase = LoginUseCase(mockAuthRepository); + }); + + test('When login success, it returns success model', () async { + const loginModel = LoginModel( + id: "", + accessToken: "", + expiresIn: 0, + refreshToken: "", + ); + when(mockAuthRepository.login( + email: email, + password: password, + )).thenAnswer((_) async => loginModel); + + final result = await useCase.call(LoginParams( + email: email, + password: password, + )); + + verify(mockAuthRepository.login( + email: email, + password: password, + )); + expect(result, isA>()); + }); + + test('When login fail, it returns failure with exception', () async { + when(mockAuthRepository.login( + email: email, + password: password, + )).thenAnswer((_) => Future.error(Exception)); + + final result = await useCase.call(LoginParams( + email: email, + password: password, + )); + + verify(mockAuthRepository.login( + email: email, + password: password, + )); + + expect(result, isA>()); + }); + }); +} diff --git a/test/mocks/generate_mocks.dart b/test/mocks/generate_mocks.dart index 56d02d3..9cf6f15 100644 --- a/test/mocks/generate_mocks.dart +++ b/test/mocks/generate_mocks.dart @@ -1,11 +1,9 @@ import 'package:dio/dio.dart'; import 'package:survey_flutter/api/authentication_api_service.dart'; import 'package:mockito/annotations.dart'; +import 'package:survey_flutter/repositories/authentication_repository.dart'; -@GenerateMocks([ - AuthenticationApiService, - DioError, -]) +@GenerateMocks([AuthenticationApiService, AuthenticationRepository, DioError]) main() { // empty class to generate mock repository classes }