-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
696 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:dart_json_mapper/dart_json_mapper.dart'; | ||
|
||
@JsonSerializable() | ||
class SendOtpRequest { | ||
final String email; | ||
|
||
SendOtpRequest({required this.email}); | ||
|
||
Map<String, dynamic> toJson() => {"email": email}; | ||
|
||
static SendOtpRequest? fromJson(Map<String, dynamic> json) { | ||
try { | ||
if (!json.containsKey('email')) return null; | ||
return JsonMapper.fromMap<SendOtpRequest>(json); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:dart_json_mapper/dart_json_mapper.dart'; | ||
|
||
@JsonSerializable() | ||
class VerifyOtpRequest { | ||
final String email; | ||
final String otp; | ||
|
||
VerifyOtpRequest({required this.email, required this.otp}); | ||
|
||
Map<String, dynamic> toJson() => {"email": email, "otp": otp}; | ||
|
||
static VerifyOtpRequest? fromJson(Map<String, dynamic> json) { | ||
try { | ||
if (!json.containsKey('email') || !json.containsKey('otp')) return null; | ||
return JsonMapper.fromMap<VerifyOtpRequest>(json); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:flutter_bloc_advance/data/models/send_otp_request.dart'; | ||
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('SendOtpRequest', () { | ||
test('should create SendOtpRequest instance with email', () { | ||
// given | ||
const testEmail = '[email protected]'; | ||
|
||
// when | ||
final request = SendOtpRequest(email: testEmail); | ||
|
||
// then | ||
expect(request.email, equals(testEmail)); | ||
}); | ||
|
||
test('should convert SendOtpRequest to JSON correctly', () { | ||
// given | ||
const testEmail = '[email protected]'; | ||
final request = SendOtpRequest(email: testEmail); | ||
|
||
// when | ||
final json = request.toJson(); | ||
|
||
// then | ||
expect(json, { | ||
'email': '[email protected]', | ||
}); | ||
}); | ||
|
||
test('should create SendOtpRequest from JSON correctly', () { | ||
initializeJsonMapper(); | ||
// given | ||
final json = { | ||
'email': '[email protected]', | ||
}; | ||
|
||
// when | ||
final request = SendOtpRequest.fromJson(json); | ||
|
||
// then | ||
expect(request, isNotNull); | ||
expect(request?.email, equals('[email protected]')); | ||
}); | ||
|
||
test('should return null when fromJson is called with invalid JSON', () { | ||
initializeJsonMapper(); | ||
// given | ||
final invalidJson = { | ||
'invalid_key': '[email protected]', | ||
}; | ||
|
||
// when | ||
final request = SendOtpRequest.fromJson(invalidJson); | ||
|
||
// then | ||
expect(request, isNull); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:flutter_bloc_advance/data/models/verify_otp_request.dart'; | ||
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('VerifyOtpRequest', () { | ||
test('should create VerifyOtpRequest instance with email and otp', () { | ||
// given | ||
const testEmail = '[email protected]'; | ||
const testOtp = '123456'; | ||
|
||
// when | ||
final request = VerifyOtpRequest(email: testEmail, otp: testOtp); | ||
|
||
// then | ||
expect(request.email, equals(testEmail)); | ||
expect(request.otp, equals(testOtp)); | ||
}); | ||
|
||
test('should convert VerifyOtpRequest to JSON correctly', () { | ||
// given | ||
const testEmail = '[email protected]'; | ||
const testOtp = '123456'; | ||
final request = VerifyOtpRequest(email: testEmail, otp: testOtp); | ||
|
||
// when | ||
final json = request.toJson(); | ||
|
||
// then | ||
expect(json, { | ||
'email': '[email protected]', | ||
'otp': '123456', | ||
}); | ||
}); | ||
|
||
test('should create VerifyOtpRequest from JSON correctly', () { | ||
initializeJsonMapper(); | ||
// given | ||
final json = { | ||
'email': '[email protected]', | ||
'otp': '123456', | ||
}; | ||
|
||
// when | ||
final request = VerifyOtpRequest.fromJson(json); | ||
|
||
// then | ||
expect(request, isNotNull); | ||
expect(request?.email, equals('[email protected]')); | ||
expect(request?.otp, equals('123456')); | ||
}); | ||
|
||
test('should return null when fromJson is called with invalid JSON', () { | ||
// given | ||
final invalidJson = { | ||
'invalid_key': '[email protected]', | ||
}; | ||
|
||
// when | ||
final request = VerifyOtpRequest.fromJson(invalidJson); | ||
|
||
// then | ||
expect(request, isNull); | ||
}); | ||
|
||
test('should return null when fromJson is called with missing otp', () { | ||
// given | ||
final invalidJson = { | ||
'email': '[email protected]', | ||
}; | ||
|
||
// when | ||
final request = VerifyOtpRequest.fromJson(invalidJson); | ||
|
||
// then | ||
expect(request, isNull); | ||
}); | ||
|
||
test('should return null when fromJson is called with missing email', () { | ||
// given | ||
final invalidJson = { | ||
'otp': '123456', | ||
}; | ||
|
||
// when | ||
final request = VerifyOtpRequest.fromJson(invalidJson); | ||
|
||
// then | ||
expect(request, isNull); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import 'package:flutter_bloc_advance/configuration/local_storage.dart'; | ||
import 'package:flutter_bloc_advance/data/app_api_exception.dart'; | ||
import 'package:flutter_bloc_advance/data/models/jwt_token.dart'; | ||
import 'package:flutter_bloc_advance/data/models/send_otp_request.dart'; | ||
import 'package:flutter_bloc_advance/data/models/user_jwt.dart'; | ||
import 'package:flutter_bloc_advance/data/models/verify_otp_request.dart'; | ||
import 'package:flutter_bloc_advance/data/repository/login_repository.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
|
@@ -50,4 +53,38 @@ void main() { | |
expect(await AppLocalStorage().read(StorageKeys.jwtToken.name), null); | ||
}); | ||
}); | ||
|
||
group("Login Repository sendOtp", () { | ||
test("Given valid email when sendOtp then complete successfully", () async { | ||
TestUtils().setupAuthentication(); | ||
final request = SendOtpRequest(email: "[email protected]"); | ||
|
||
expect(() async => await LoginRepository().sendOtp(request), returnsNormally); | ||
}); | ||
|
||
test("Given invalid email when sendOtp then throws BadRequestException", () async { | ||
TestUtils().setupAuthentication(); | ||
final request = SendOtpRequest(email: ""); | ||
|
||
await expectLater(LoginRepository().sendOtp(request), throwsA(isA<BadRequestException>())); | ||
}); | ||
}); | ||
|
||
group("Login Repository verifyOtp", () { | ||
test("Given valid OTP when verify then return JWTToken successfully", () async { | ||
TestUtils().setupAuthentication(); | ||
final request = VerifyOtpRequest(email: "[email protected]", otp: "123456"); | ||
|
||
final result = await LoginRepository().verifyOtp(request); | ||
expect(result, isA<JWTToken>()); | ||
expect(result?.idToken, "MOCK_TOKEN"); | ||
}); | ||
|
||
test("Given invalid OTP when verify then throws BadRequestException", () async { | ||
TestUtils().setupAuthentication(); | ||
final request = VerifyOtpRequest(email: "[email protected]", otp: "1234567"); | ||
|
||
await expectLater(LoginRepository().verifyOtp(request), throwsA(isA<BadRequestException>())); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.