-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Starter project for section 10
Showing
14 changed files
with
179 additions
and
27 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
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,50 @@ | ||
import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; | ||
|
||
/// Base class for all all client-side errors that can be generated by the app | ||
sealed class AppException implements Exception { | ||
AppException(this.code, this.message); | ||
final String code; | ||
final String message; | ||
} | ||
|
||
/// Auth | ||
class EmailAlreadyInUseException extends AppException { | ||
EmailAlreadyInUseException() | ||
: super('email-already-in-use', 'Email already in use'.hardcoded); | ||
} | ||
|
||
class WeakPasswordException extends AppException { | ||
WeakPasswordException() | ||
: super('weak-password', 'Password is too weak'.hardcoded); | ||
} | ||
|
||
class WrongPasswordException extends AppException { | ||
WrongPasswordException() | ||
: super('wrong-password', 'Wrong password'.hardcoded); | ||
} | ||
|
||
class UserNotFoundException extends AppException { | ||
UserNotFoundException() : super('user-not-found', 'User not found'.hardcoded); | ||
} | ||
|
||
/// Cart | ||
class CartSyncFailedException extends AppException { | ||
CartSyncFailedException() | ||
: super('cart-sync-failed', | ||
'An error has occurred while updating the shopping cart'.hardcoded); | ||
} | ||
|
||
/// Checkout | ||
class PaymentFailureEmptyCartException extends AppException { | ||
PaymentFailureEmptyCartException() | ||
: super('payment-failure-empty-cart', | ||
'Can\'t place an order if the cart is empty'.hardcoded); | ||
} | ||
|
||
/// Orders | ||
class ParseOrderFailureException extends AppException { | ||
ParseOrderFailureException(this.status) | ||
: super('parse-order-failure', | ||
'Could not parse order status: $status'.hardcoded); | ||
final String status; | ||
} |
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,51 @@ | ||
import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; | ||
|
||
/// An exception type to represent all client-side errors that can be generated | ||
/// by the app | ||
enum AppExceptionEnum { | ||
// Auth | ||
emailAlreadyInUse('email-already-in-use'), | ||
weakPassword('weak-password'), | ||
wrongPassword('wrong-password'), | ||
userNotFound('user-not-found'), | ||
// Cart | ||
cartSyncFailed('cart-sync-failed'), | ||
// Checkout | ||
paymentFailureEmptyCart('payment-failure-empty-cart'), | ||
// Orders | ||
parseOrderFailure('parse-order-failure'); | ||
|
||
const AppExceptionEnum(this.code); | ||
|
||
/// A value that can be sent to the backend when logging the error | ||
final String code; | ||
|
||
/// A user-friendly message that can be shown in the UI. | ||
// * This needs to be a getter variable or a method since the error message | ||
// * can't be declared as const if it's localized | ||
String get message { | ||
switch (this) { | ||
// Auth | ||
case emailAlreadyInUse: | ||
return 'Email already in use'.hardcoded; | ||
case weakPassword: | ||
return 'Password is too weak'.hardcoded; | ||
case wrongPassword: | ||
return 'Wrong password'.hardcoded; | ||
case userNotFound: | ||
return 'User not found'.hardcoded; | ||
// Cart | ||
case cartSyncFailed: | ||
return 'An error has occurred while updating the shopping cart' | ||
.hardcoded; | ||
// Checkout | ||
case paymentFailureEmptyCart: | ||
return 'Can\'t place an order if the cart is empty'.hardcoded; | ||
// Orders | ||
case parseOrderFailure: | ||
return 'Could not parse order status'.hardcoded; | ||
default: | ||
return 'Unknown error'.hardcoded; | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
ecommerce_app/lib/src/features/authentication/domain/fake_app_user.dart
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,11 @@ | ||
import 'package:ecommerce_app/src/features/authentication/domain/app_user.dart'; | ||
|
||
/// Fake user class used to simulate a user account on the backend | ||
class FakeAppUser extends AppUser { | ||
FakeAppUser({ | ||
required super.uid, | ||
required super.email, | ||
required this.password, | ||
}); | ||
final String password; | ||
} |
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
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 |
---|---|---|
|
@@ -86,7 +86,7 @@ class AuthRobot { | |
expect(dialogTitle, findsNothing); | ||
} | ||
|
||
Future<void> signInWithEmailAndPassword() async { | ||
Future<void> enterAndSubmitEmailAndPassword() async { | ||
await enterEmail('[email protected]'); | ||
await tester.pump(); | ||
await enterPassword('test1234'); | ||
|
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import 'package:flutter_test/flutter_test.dart'; | |
|
||
void main() { | ||
const testEmail = '[email protected]'; | ||
const testPassword = '1234'; | ||
const testPassword = 'test1234'; | ||
final testUser = AppUser( | ||
uid: testEmail.split('').reversed.join(), | ||
email: testEmail, | ||
|
@@ -19,15 +19,19 @@ void main() { | |
expect(authRepository.currentUser, null); | ||
expect(authRepository.authStateChanges(), emits(null)); | ||
}); | ||
test('currentUser is not null after sign in', () async { | ||
|
||
test('sign in throws when user not found', () async { | ||
final authRepository = makeAuthRepository(); | ||
addTearDown(authRepository.dispose); | ||
await authRepository.signInWithEmailAndPassword( | ||
testEmail, | ||
testPassword, | ||
await expectLater( | ||
() => authRepository.signInWithEmailAndPassword( | ||
testEmail, | ||
testPassword, | ||
), | ||
throwsA(isA<Exception>()), | ||
); | ||
expect(authRepository.currentUser, testUser); | ||
expect(authRepository.authStateChanges(), emits(testUser)); | ||
expect(authRepository.currentUser, null); | ||
expect(authRepository.authStateChanges(), emits(null)); | ||
}); | ||
|
||
test('currentUser is not null after registration', () async { | ||
|
@@ -44,7 +48,7 @@ void main() { | |
test('currentUser is null after sign out', () async { | ||
final authRepository = makeAuthRepository(); | ||
addTearDown(authRepository.dispose); | ||
await authRepository.signInWithEmailAndPassword( | ||
await authRepository.createUserWithEmailAndPassword( | ||
testEmail, | ||
testPassword, | ||
); | ||
|
@@ -56,11 +60,11 @@ void main() { | |
expect(authRepository.authStateChanges(), emits(null)); | ||
}); | ||
|
||
test('sign in after dispose throws exception', () { | ||
test('create user after dispose throws exception', () { | ||
final authRepository = makeAuthRepository(); | ||
authRepository.dispose(); | ||
expect( | ||
() => authRepository.signInWithEmailAndPassword( | ||
() => authRepository.createUserWithEmailAndPassword( | ||
testEmail, | ||
testPassword, | ||
), | ||
|
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ void main() { | |
setUpAll(() { | ||
registerFallbackValue(const Cart()); | ||
}); | ||
const testUser = AppUser(uid: 'abc'); | ||
const testUser = AppUser(uid: 'abc', email: '[email protected]'); | ||
|
||
late MockAuthRepository authRepository; | ||
late MockRemoteCartRepository remoteCartRepository; | ||
|
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ import 'package:mocktail/mocktail.dart'; | |
import '../../../mocks.dart'; | ||
|
||
void main() { | ||
const testUser = AppUser(uid: 'abc'); | ||
const testUser = AppUser(uid: 'abc', email: '[email protected]'); | ||
setUpAll(() { | ||
// needed for MockOrdersRepository | ||
registerFallbackValue(Order( | ||
|
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