This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
1f2b82c
commit 18d27f1
Showing
3 changed files
with
37 additions
and
20 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 |
---|---|---|
|
@@ -77,4 +77,4 @@ class UserProvider with ChangeNotifier { | |
_user = null; | ||
notifyListeners(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -31,7 +31,8 @@ void main() { | |
); | ||
} | ||
|
||
testWidgets('LoginPage has email and password fields', (WidgetTester tester) async { | ||
testWidgets('LoginPage has email and password fields', | ||
(WidgetTester tester) async { | ||
await tester.pumpWidget(createLoginPage()); | ||
await tester.pumpAndSettle(); | ||
|
||
|
@@ -48,7 +49,8 @@ void main() { | |
expect(find.byType(ElevatedButton), findsOneWidget); | ||
}); | ||
|
||
testWidgets('Tapping login button with empty fields shows validation errors', (WidgetTester tester) async { | ||
testWidgets('Tapping login button with empty fields shows validation errors', | ||
(WidgetTester tester) async { | ||
await tester.pumpWidget(createLoginPage()); | ||
await tester.pumpAndSettle(); | ||
|
||
|
@@ -59,18 +61,20 @@ void main() { | |
expect(find.text('Please enter your password'), findsOneWidget); | ||
}); | ||
|
||
testWidgets('Shows error dialog on login failure', (WidgetTester tester) async { | ||
testWidgets('Shows error dialog on login failure', | ||
(WidgetTester tester) async { | ||
when(mockUserProvider.login(any, any)).thenAnswer((_) async => false); | ||
|
||
await tester.pumpWidget(createLoginPage()); | ||
await tester.pumpAndSettle(); | ||
|
||
await tester.enterText(find.byType(TextFormField).at(0), '[email protected]'); | ||
await tester.enterText( | ||
find.byType(TextFormField).at(0), '[email protected]'); | ||
await tester.enterText(find.byType(TextFormField).at(1), 'password123'); | ||
|
||
await tester.tap(find.byType(ElevatedButton)); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.text('Login Error'), findsOneWidget); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,27 +16,34 @@ void main() { | |
setUp(() { | ||
mockAuthManager = MockAuthManager(); | ||
mockClient = MockClient(); | ||
userProvider = UserProvider(authManager: mockAuthManager, client: mockClient); | ||
userProvider = | ||
UserProvider(authManager: mockAuthManager, client: mockClient); | ||
}); | ||
|
||
group('UserProvider', () { | ||
test('login success', () async { | ||
when(mockAuthManager.baseUrl).thenReturn('http://example.com'); | ||
when(mockClient.post(Uri.parse('http://example.com/login'), headers: anyNamed('headers'), body: anyNamed('body'))) | ||
.thenAnswer((_) async => http.Response('{"token": "fake_token"}', 200)); | ||
when(mockClient.post(Uri.parse('http://example.com/login'), | ||
headers: anyNamed('headers'), body: anyNamed('body'))) | ||
.thenAnswer( | ||
(_) async => http.Response('{"token": "fake_token"}', 200)); | ||
|
||
final result = await userProvider.login('[email protected]', 'password123'); | ||
final result = | ||
await userProvider.login('[email protected]', 'password123'); | ||
|
||
expect(result, true); | ||
verify(mockAuthManager.login('fake_token')).called(1); | ||
}); | ||
|
||
test('login failure', () async { | ||
when(mockAuthManager.baseUrl).thenReturn('http://example.com'); | ||
when(mockClient.post(Uri.parse('http://example.com/login'), headers: anyNamed('headers'), body: anyNamed('body'))) | ||
.thenAnswer((_) async => http.Response('{"error": "Invalid credentials"}', 401)); | ||
when(mockClient.post(Uri.parse('http://example.com/login'), | ||
headers: anyNamed('headers'), body: anyNamed('body'))) | ||
.thenAnswer((_) async => | ||
http.Response('{"error": "Invalid credentials"}', 401)); | ||
|
||
final result = await userProvider.login('[email protected]', 'wrong_password'); | ||
final result = | ||
await userProvider.login('[email protected]', 'wrong_password'); | ||
|
||
expect(result, false); | ||
verifyNever(mockAuthManager.login(any)); | ||
|
@@ -45,8 +52,10 @@ void main() { | |
test('fetchUser success', () async { | ||
when(mockAuthManager.isLoggedIn).thenReturn(true); | ||
when(mockAuthManager.baseUrl).thenReturn('http://example.com'); | ||
when(mockAuthManager.headers).thenReturn({'Authorization': 'Bearer fake_token'}); | ||
when(mockClient.get(Uri.parse('http://example.com/api/user'), headers: anyNamed('headers'))) | ||
when(mockAuthManager.headers) | ||
.thenReturn({'Authorization': 'Bearer fake_token'}); | ||
when(mockClient.get(Uri.parse('http://example.com/api/user'), | ||
headers: anyNamed('headers'))) | ||
.thenAnswer((_) async => http.Response(''' | ||
{ | ||
"data": { | ||
|
@@ -95,15 +104,19 @@ void main() { | |
expect(userProvider.user!.accountSettings.timezone, 'UTC'); | ||
expect(userProvider.user!.backupTasks.total, 5); | ||
expect(userProvider.user!.relatedEntities.remoteServers, 2); | ||
expect(userProvider.user!.timestamps.accountCreated, DateTime.parse('2023-05-01T12:00:00Z')); | ||
expect(userProvider.user!.timestamps.accountCreated, | ||
DateTime.parse('2023-05-01T12:00:00Z')); | ||
}); | ||
|
||
test('fetchUser failure', () async { | ||
when(mockAuthManager.isLoggedIn).thenReturn(true); | ||
when(mockAuthManager.baseUrl).thenReturn('http://example.com'); | ||
when(mockAuthManager.headers).thenReturn({'Authorization': 'Bearer fake_token'}); | ||
when(mockClient.get(Uri.parse('http://example.com/api/user'), headers: anyNamed('headers'))) | ||
.thenAnswer((_) async => http.Response('{"error": "Unauthorized"}', 401)); | ||
when(mockAuthManager.headers) | ||
.thenReturn({'Authorization': 'Bearer fake_token'}); | ||
when(mockClient.get(Uri.parse('http://example.com/api/user'), | ||
headers: anyNamed('headers'))) | ||
.thenAnswer( | ||
(_) async => http.Response('{"error": "Unauthorized"}', 401)); | ||
|
||
final result = await userProvider.fetchUser(); | ||
|
||
|
@@ -118,4 +131,4 @@ void main() { | |
expect(userProvider.user, isNull); | ||
}); | ||
}); | ||
} | ||
} |