Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions lib/data/usecases/authentication/remote_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountEntity> 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;
}
}
}
Expand All @@ -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};
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -82,4 +79,10 @@ void main() {

expect(future, throwsA(DomainError.unexpected));
});
}

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));
});
}