forked from eatvio/eatvio_app
feat(tests): increased tests coverage
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/prefs.dart';
|
||||
import 'package:eatvio_app/core/token_storage.dart';
|
||||
import 'package:eatvio_app/features/auth/models/oauth_verification_confirm_response.dart';
|
||||
import 'package:eatvio_app/features/auth/models/oauth_verification_request_response.dart';
|
||||
import 'package:eatvio_app/features/auth/models/token_model.dart';
|
||||
import 'package:eatvio_app/features/auth/presentation/bloc/oauth_cubit/oauth_cubit.dart';
|
||||
import 'package:eatvio_app/features/auth/presentation/bloc/oauth_cubit/oauth_state.dart';
|
||||
@@ -185,5 +188,220 @@ void main() {
|
||||
final loadedState = oauthCubit!.state as OAuthStateLoaded;
|
||||
expect(loadedState.token, tokenFixture.token);
|
||||
});
|
||||
|
||||
test('signInViaSocialMedia - обработка ошибки валидации email/phone', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
final dioException = DioException(
|
||||
requestOptions: RequestOptions(path: '/api/auth/oauth/signin'),
|
||||
response: Response(
|
||||
statusCode: 422,
|
||||
data: {
|
||||
'errors': {
|
||||
'email': ['Некорректный формат email'],
|
||||
},
|
||||
},
|
||||
requestOptions: RequestOptions(path: '/api/auth/oauth/signin'),
|
||||
),
|
||||
);
|
||||
|
||||
when(
|
||||
mockAuthRepository.signInViaSocialMedia(
|
||||
provider: anyNamed('provider'),
|
||||
accessToken: anyNamed('accessToken'),
|
||||
refreshToken: anyNamed('refreshToken'),
|
||||
clientId: anyNamed('clientId'),
|
||||
),
|
||||
).thenThrow(dioException);
|
||||
|
||||
await oauthCubit!.signInViaSocialMedia(
|
||||
provider: signInRequestFixture['provider'] as String,
|
||||
accessToken: signInRequestFixture['access_token'] as String,
|
||||
refreshToken: signInRequestFixture['refresh_token'] as String,
|
||||
clientId: signInRequestFixture['client_id'] as String,
|
||||
);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(oauthCubit!.state, isA<OAuthStateError>());
|
||||
final errorState = oauthCubit!.state as OAuthStateError;
|
||||
expect(errorState.errorMessage, 'validation_error');
|
||||
});
|
||||
|
||||
test('signUpViaSocialMedia - обработка ошибки валидации email', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
final dioException = DioException(
|
||||
requestOptions: RequestOptions(path: '/api/auth/oauth/signup'),
|
||||
response: Response(
|
||||
statusCode: 422,
|
||||
data: {
|
||||
'errors': {
|
||||
'email': ['Некорректный формат email'],
|
||||
},
|
||||
},
|
||||
requestOptions: RequestOptions(path: '/api/auth/oauth/signup'),
|
||||
),
|
||||
);
|
||||
|
||||
when(
|
||||
mockAuthRepository.signUpViaSocialMedia(
|
||||
provider: anyNamed('provider'),
|
||||
accessToken: anyNamed('accessToken'),
|
||||
id: anyNamed('id'),
|
||||
refreshToken: anyNamed('refreshToken'),
|
||||
firstName: anyNamed('firstName'),
|
||||
lastName: anyNamed('lastName'),
|
||||
email: anyNamed('email'),
|
||||
phone: anyNamed('phone'),
|
||||
dateOfBirth: anyNamed('dateOfBirth'),
|
||||
gender: anyNamed('gender'),
|
||||
weight: anyNamed('weight'),
|
||||
height: anyNamed('height'),
|
||||
clientId: anyNamed('clientId'),
|
||||
),
|
||||
).thenThrow(dioException);
|
||||
|
||||
final result = await oauthCubit!.signUpViaSocialMedia(
|
||||
provider: signUpRequestFixture['provider'] as String,
|
||||
accessToken: signUpRequestFixture['access_token'] as String,
|
||||
id: signUpRequestFixture['id'] as String,
|
||||
refreshToken: signUpRequestFixture['refresh_token'] as String,
|
||||
firstName: signUpRequestFixture['first_name'] as String,
|
||||
lastName: signUpRequestFixture['last_name'] as String,
|
||||
email: signUpRequestFixture['email'] as String,
|
||||
phone: signUpRequestFixture['phone'] as String,
|
||||
dateOfBirth: signUpRequestFixture['date_of_birth'] as String,
|
||||
gender: signUpRequestFixture['gender'] as String,
|
||||
weight: signUpRequestFixture['weight'] as int,
|
||||
height: signUpRequestFixture['height'] as int,
|
||||
clientId: signUpRequestFixture['client_id'] as String,
|
||||
);
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.length, 1);
|
||||
expect(result[0], RegistrationValidationErrors.incorrectEmailFormat);
|
||||
});
|
||||
|
||||
test('requestOAuthVerification - успешный запрос кода верификации', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
const verificationResponse = OAuthVerificationRequestResponse(email: 't***@example.com', status: 'sent');
|
||||
|
||||
when(mockAuthRepository.requestOAuthVerification(email: 'test@example.com', phone: null)).thenAnswer((_) async => verificationResponse);
|
||||
|
||||
final result = await oauthCubit!.requestOAuthVerification(email: 'test@example.com');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.email, 't***@example.com');
|
||||
expect(oauthCubit!.state, isA<OAuthState>());
|
||||
});
|
||||
|
||||
test('requestOAuthVerification - обработка ошибки', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
when(mockAuthRepository.requestOAuthVerification(email: anyNamed('email'), phone: anyNamed('phone'))).thenThrow(Exception('Ошибка запроса'));
|
||||
|
||||
final result = await oauthCubit!.requestOAuthVerification(email: 'test@example.com');
|
||||
|
||||
expect(result, isNull);
|
||||
expect(oauthCubit!.state, isA<OAuthStateError>());
|
||||
});
|
||||
|
||||
test('confirmOAuthVerification - успешное подтверждение верификации', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
final verificationResponse = OAuthVerificationConfirmResponse(success: true, message: 'Success', token: tokenFixture.token, user: {}, isNewUser: false);
|
||||
|
||||
when(
|
||||
mockAuthRepository.confirmOAuthVerification(
|
||||
provider: anyNamed('provider'),
|
||||
accessToken: anyNamed('accessToken'),
|
||||
refreshToken: anyNamed('refreshToken'),
|
||||
clientId: anyNamed('clientId'),
|
||||
id: anyNamed('id'),
|
||||
email: anyNamed('email'),
|
||||
phone: anyNamed('phone'),
|
||||
code: anyNamed('code'),
|
||||
verificationType: anyNamed('verificationType'),
|
||||
firstName: anyNamed('firstName'),
|
||||
lastName: anyNamed('lastName'),
|
||||
dateOfBirth: anyNamed('dateOfBirth'),
|
||||
gender: anyNamed('gender'),
|
||||
weight: anyNamed('weight'),
|
||||
height: anyNamed('height'),
|
||||
),
|
||||
).thenAnswer((_) async => verificationResponse);
|
||||
|
||||
await oauthCubit!.confirmOAuthVerification(
|
||||
provider: 'google',
|
||||
accessToken: 'access_token',
|
||||
refreshToken: 'refresh_token',
|
||||
clientId: 'client_id',
|
||||
id: 'id',
|
||||
email: 'test@example.com',
|
||||
phone: null,
|
||||
code: '123456',
|
||||
verificationType: 'email',
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
dateOfBirth: '2000-01-01',
|
||||
gender: 'male',
|
||||
weight: 70,
|
||||
height: 175,
|
||||
);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(oauthCubit!.state, isA<OAuthStateLoaded>());
|
||||
final loadedState = oauthCubit!.state as OAuthStateLoaded;
|
||||
expect(loadedState.token, tokenFixture.token);
|
||||
});
|
||||
|
||||
test('confirmOAuthVerification - обработка ошибки', () async {
|
||||
oauthCubit = OAuthCubit(authRepository: mockAuthRepository, prefs: mockPrefs);
|
||||
|
||||
when(
|
||||
mockAuthRepository.confirmOAuthVerification(
|
||||
provider: anyNamed('provider'),
|
||||
accessToken: anyNamed('accessToken'),
|
||||
refreshToken: anyNamed('refreshToken'),
|
||||
clientId: anyNamed('clientId'),
|
||||
id: anyNamed('id'),
|
||||
email: anyNamed('email'),
|
||||
phone: anyNamed('phone'),
|
||||
code: anyNamed('code'),
|
||||
verificationType: anyNamed('verificationType'),
|
||||
firstName: anyNamed('firstName'),
|
||||
lastName: anyNamed('lastName'),
|
||||
dateOfBirth: anyNamed('dateOfBirth'),
|
||||
gender: anyNamed('gender'),
|
||||
weight: anyNamed('weight'),
|
||||
height: anyNamed('height'),
|
||||
),
|
||||
).thenThrow(Exception('Ошибка подтверждения'));
|
||||
|
||||
expect(
|
||||
() => oauthCubit!.confirmOAuthVerification(
|
||||
provider: 'google',
|
||||
accessToken: 'access_token',
|
||||
refreshToken: 'refresh_token',
|
||||
clientId: 'client_id',
|
||||
id: 'id',
|
||||
email: 'test@example.com',
|
||||
phone: null,
|
||||
code: '123456',
|
||||
verificationType: 'email',
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
dateOfBirth: '2000-01-01',
|
||||
gender: 'male',
|
||||
weight: 70,
|
||||
height: 175,
|
||||
),
|
||||
throwsException,
|
||||
);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(oauthCubit!.state, isA<OAuthStateError>());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user