Skip to content

Commit

Permalink
make user agent parameter optional and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zohreh-deriv committed Nov 24, 2023
1 parent 089a721 commit 4357fc4
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ abstract class BaseTokenService {
Future<GetTokensResponseModel> getUserTokens({
required GetTokensRequestModel request,
required BaseHttpClient client,
required String userAgent,
required String jwtToken,
required AuthConnectionInfo connectionInfo,
String? userAgent,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class DerivTokenService implements BaseTokenService {
Future<GetTokensResponseModel> getUserTokens({
required GetTokensRequestModel request,
required BaseHttpClient client,
required String userAgent,
required String jwtToken,
required AuthConnectionInfo connectionInfo,
String? userAgent,
}) async {
/// Extract login url from connection info.
final String baseUrl = 'https://${connectionInfo.endpoint}/oauth2/api/v1';
Expand All @@ -25,7 +25,7 @@ class DerivTokenService implements BaseTokenService {
request.copyWith(appId: int.parse(connectionInfo.appId)).toJson(),
headers: <String, String>{
'Authorization': 'Bearer $jwtToken',
'User-Agent': userAgent,
'User-Agent': userAgent ?? '',
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
try {
final String userAgent = await getUserAgent();
final AuthorizeEntity authorizeEntity =
await authService.onLoginRequest(request, userAgent);
await authService.onLoginRequest(request, userAgent: userAgent);
final LandingCompanyEntity landingCompanyEntity =
await authService.getLandingCompany(authorizeEntity.country);
_isUserMigrated = _checkUserMigrated(authorizeEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:deriv_auth/core/services/token/models/login_request.dart';
abstract class BaseAuthService {
/// Function before logging user in.
Future<AuthorizeEntity> onLoginRequest(
GetTokensRequestModel request,
String userAgent,
);
GetTokensRequestModel request, {
String? userAgent,
});

/// Log in a user with [token].
Future<AuthorizeEntity> login(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class DerivAuthService extends BaseAuthService {

@override
Future<AuthorizeEntity> onLoginRequest(
GetTokensRequestModel request,
String userAgent, [
GetTokensRequestModel request, {
String? userAgent,
Function? onInvalidJwtToken,
]) async {
}) async {
try {
final String jwtToken = await jwtService.getJwtToken();

Expand Down Expand Up @@ -80,7 +80,7 @@ class DerivAuthService extends BaseAuthService {

jwtService.clearJwtToken();

return onLoginRequest(request, userAgent);
return onLoginRequest(request, userAgent: userAgent);
} else {
throw _mapHttpErrorToDerivAuthError(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void main() {
final GetTokensResponseModel response = await _tokenService.getUserTokens(
request: GetTokensRequestModel(),
client: _client,
userAgent: 'user_agent',
jwtToken: _jwtToken,
connectionInfo: _connectionInfo,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void main() {
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.system));

when(() => service.onLoginRequest(any(), any())).thenAnswer(
when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
Expand All @@ -106,15 +106,15 @@ void main() {

authCubit.systemLogin(email: 'email', password: 'password');

verify(() => service.onLoginRequest(any(), any())).called(1);
verify(() => service.onLoginRequest(any())).called(1);
});

test(
'should emit [AuthLoggedInState] upon a successful social login with one-all.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.social));

when(() => service.onLoginRequest(any(), any())).thenAnswer(
when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
Expand All @@ -130,15 +130,15 @@ void main() {

authCubit.socialLogin(oneAllConnectionToken: 'token');

verify(() => service.onLoginRequest(any(), any())).called(1);
verify(() => service.onLoginRequest(any())).called(1);
});

test(
'should emit [AuthLoggedInState] upon a successful social login with in-house service.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.social));

when(() => service.onLoginRequest(any(), any())).thenAnswer(
when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
Expand All @@ -156,11 +156,11 @@ void main() {
socialAuthDto: mockSocialAuthDto,
);

verify(() => service.onLoginRequest(any(), any())).called(1);
verify(() => service.onLoginRequest(any())).called(1);
});

test('should emit [AuthLoggedInState] upon a successful otp login.', () {
when(() => service.onLoginRequest(any(), any())).thenAnswer(
when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
Expand All @@ -175,15 +175,14 @@ void main() {
);
authCubit.systemLogin(email: 'email', password: 'pass', otp: 'otp');

verify(() => service.onLoginRequest(any(), any())).called(1);
verify(() => service.onLoginRequest(any())).called(1);
});

test('should emit [AuthErrorState] when an exception occurs in service.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.system));

when(() => service.onLoginRequest(any(), any()))
.thenThrow(DerivAuthException(
when(() => service.onLoginRequest(any())).thenThrow(DerivAuthException(
message: 'message',
type: AuthErrorType.expiredAccount,
));
Expand All @@ -201,7 +200,7 @@ void main() {

authCubit.systemLogin(email: 'email', password: 'pass');

verify(() => service.onLoginRequest(any(), any())).called(1);
verify(() => service.onLoginRequest(any())).called(1);
});
test('should emit [AuthLoggedOutState] upon a successful logout.', () {
when(() => service.logout()).thenAnswer((_) => Future<void>.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenAnswer(
Expand All @@ -76,7 +75,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(invalidJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand Down Expand Up @@ -135,7 +133,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
);

expect(response.userId, mockedValidAuthorizeEntity.userId);
Expand All @@ -162,7 +159,6 @@ void main() {
signupProvider: 'signupProvider',
oneAllConnectionToken: 'oneAllConnectionToken',
),
'user_agent',
);

expect(response.userId, mockedValidAuthorizeEntity.userId);
Expand All @@ -189,7 +185,6 @@ void main() {
signupProvider: 'signupProvider',
socialAuthDto: mockSocialAuthDto,
),
'user_agent',
);

expect(response.userId, mockedValidAuthorizeEntity.userId);
Expand Down Expand Up @@ -218,8 +213,7 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
() {
onInvalidJwtToken: () {
when(() => jwtService.getJwtToken()).thenAnswer(
(_) => Future<String>.value(validJwtToken),
);
Expand Down Expand Up @@ -247,7 +241,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -266,7 +259,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand All @@ -286,7 +278,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -305,7 +296,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand All @@ -325,7 +315,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -344,7 +333,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand Down Expand Up @@ -436,7 +424,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -455,7 +442,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand All @@ -475,7 +461,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -494,7 +479,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand All @@ -514,7 +498,6 @@ void main() {
when(() => tokenService.getUserTokens(
request: any(named: 'request'),
client: any(named: 'client'),
userAgent: 'user_agent',
jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)),
connectionInfo: any(named: 'connectionInfo'),
)).thenThrow(
Expand All @@ -533,7 +516,6 @@ void main() {
password: 'pass',
signupProvider: 'signupProvider',
),
'user_agent',
),
throwsA(
isA<DerivAuthException>().having(
Expand Down

0 comments on commit 4357fc4

Please sign in to comment.