Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 13, 2024
1 parent 1f2b82c commit 18d27f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/user_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ class UserProvider with ChangeNotifier {
_user = null;
notifyListeners();
}
}
}
14 changes: 9 additions & 5 deletions test/login_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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);
});
}
}
41 changes: 27 additions & 14 deletions test/user_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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": {
Expand Down Expand Up @@ -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();

Expand All @@ -118,4 +131,4 @@ void main() {
expect(userProvider.user, isNull);
});
});
}
}

0 comments on commit 18d27f1

Please sign in to comment.