From 21b215d3710a59bbe9c1dbcda48ae1b9646d4caa Mon Sep 17 00:00:00 2001 From: Lucas Fidelis Date: Fri, 10 Jun 2022 07:51:01 -0300 Subject: [PATCH 1/2] feat: ensure RemoteAuthentication throw UnexpectedError if RemoteAccountModel throws when HttpClient returns 204 --- .../authentication/remote_authentication.dart | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/data/usecases/authentication/remote_authentication.dart b/lib/data/usecases/authentication/remote_authentication.dart index fe1edba7..d3e564ec 100644 --- a/lib/data/usecases/authentication/remote_authentication.dart +++ b/lib/data/usecases/authentication/remote_authentication.dart @@ -8,20 +8,17 @@ class RemoteAuthentication implements Authentication { final HttpClient httpClient; final String url; - RemoteAuthentication({ - required this.httpClient, - required this.url - }); + RemoteAuthentication({required this.httpClient, required this.url}); Future auth(AuthenticationParams params) async { final body = RemoteAuthenticationParams.fromDomain(params).toJson(); try { final httpResponse = await httpClient.request(url: url, method: 'post', body: body); return RemoteAccountModel.fromJson(httpResponse).toEntity(); - } on HttpError catch(error) { - throw error == HttpError.unauthorized - ? DomainError.invalidCredentials - : DomainError.unexpected; + } on HttpError catch (error) { + throw error == HttpError.unauthorized ? DomainError.invalidCredentials : DomainError.unexpected; + } catch (error) { + throw DomainError.unexpected; } } } @@ -30,13 +27,9 @@ class RemoteAuthenticationParams { final String email; final String password; - RemoteAuthenticationParams({ - required this.email, - required this.password - }); + RemoteAuthenticationParams({required this.email, required this.password}); - factory RemoteAuthenticationParams.fromDomain(AuthenticationParams params) => - RemoteAuthenticationParams(email: params.email, password: params.secret); + factory RemoteAuthenticationParams.fromDomain(AuthenticationParams params) => RemoteAuthenticationParams(email: params.email, password: params.secret); Map toJson() => {'email': email, 'password': password}; -} \ No newline at end of file +} From 7575fbe3668df60bcd5de78ba1c25dae6b4f0006 Mon Sep 17 00:00:00 2001 From: Lucas Fidelis Date: Fri, 10 Jun 2022 07:51:09 -0300 Subject: [PATCH 2/2] test: ensure RemoteAuthentication throw UnexpectedError if RemoteAccountModel throws when HttpClient returns 204 --- .../remote_authentication_test.dart | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/data/usecases/authentication/remote_authentication_test.dart b/test/data/usecases/authentication/remote_authentication_test.dart index 9dc40538..e3fd1ba3 100644 --- a/test/data/usecases/authentication/remote_authentication_test.dart +++ b/test/data/usecases/authentication/remote_authentication_test.dart @@ -1,3 +1,4 @@ +import 'package:flutter_test/flutter_test.dart'; import 'package:fordev/domain/helpers/helpers.dart'; import 'package:fordev/domain/usecases/usecases.dart'; import 'package:fordev/data/http/http.dart'; @@ -30,11 +31,7 @@ void main() { test('Should call HttpClient with correct values', () async { await sut.auth(params); - verify(() => httpClient.request( - url: url, - method: 'post', - body: {'email': params.email, 'password': params.secret} - )); + verify(() => httpClient.request(url: url, method: 'post', body: {'email': params.email, 'password': params.secret})); }); test('Should throw UnexpectedError if HttpClient returns 400', () async { @@ -82,4 +79,10 @@ void main() { expect(future, throwsA(DomainError.unexpected)); }); -} \ No newline at end of file + + test('Should throw UnexpectedError if RemoteAccountModel throws when HttpClient returns 204', () async { + httpClient.mockRequest(null); + final future = sut.auth(params); + expect(future, throwsA(DomainError.unexpected)); + }); +}