From e2cb01b47468d6820ce388b4946fdb892ddfc749 Mon Sep 17 00:00:00 2001 From: shwet-07 <144674061+Shweta-281@users.noreply.github.com> Date: Tue, 8 Apr 2025 13:00:28 +0530 Subject: [PATCH 1/2] Fix Undefined baseUrl and origin in API Service Test --- lib/api_service.dart | 9 +++- .../taskchampion/credentials_storage.dart | 7 +-- test/api_service_test.dart | 46 +++++++++++++++---- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 384a5726..d2396898 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -66,10 +66,15 @@ class Tasks { }; } } + String origin = 'http://localhost:8080'; -Future> fetchTasks(String uuid, String encryptionSecret) async { - var baseUrl = await CredentialsStorage.getApiUrl(); +Future> fetchTasks( + String uuid, + String encryptionSecret, { + required CredentialsStorage credentialsStorage, // Make it required +}) async { + final baseUrl = await CredentialsStorage.getApiUrl(); try { String url = '$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret'; diff --git a/lib/app/utils/taskchampion/credentials_storage.dart b/lib/app/utils/taskchampion/credentials_storage.dart index bc0b0e83..e58d8c86 100644 --- a/lib/app/utils/taskchampion/credentials_storage.dart +++ b/lib/app/utils/taskchampion/credentials_storage.dart @@ -3,7 +3,9 @@ import 'package:shared_preferences/shared_preferences.dart'; class CredentialsStorage { static const String _encryptionSecretKey = 'encryptionSecret'; static const String _clientIdKey = 'clientId'; - static const String _apiUrlKey = 'ccsyncBackendUrl'; + final SharedPreferences prefs; + const CredentialsStorage(this.prefs); + static Future getEncryptionSecret() async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.getString(_encryptionSecretKey); @@ -16,7 +18,6 @@ class CredentialsStorage { static Future getApiUrl() async { SharedPreferences prefs = await SharedPreferences.getInstance(); - return prefs.getString(_apiUrlKey); + return prefs.getString('apiUrl') ?? 'https://default-api-url.com'; } - } diff --git a/test/api_service_test.dart b/test/api_service_test.dart index ba252fac..a7901a54 100644 --- a/test/api_service_test.dart +++ b/test/api_service_test.dart @@ -2,28 +2,54 @@ import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as mockClient; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:http/http.dart' as http; -import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:taskwarrior/api_service.dart'; import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart'; import 'api_service_test.mocks.dart'; -class MockCredentialsStorage extends Mock implements CredentialsStorage {} +// Define the missing constants +const String baseUrl = + 'https://your-api-base-url.com'; // Replace with your actual base URL +const String origin = + 'http://localhost:8080'; // Replace with your actual origin + +class MockCredentialsStorage extends Mock implements CredentialsStorage { + getApiUrl() {} +} class MockMethodChannel extends Mock implements MethodChannel {} -@GenerateMocks([MockMethodChannel, http.Client]) +@GenerateMocks([http.Client, CredentialsStorage]) void main() { TestWidgetsFlutterBinding.ensureInitialized(); - databaseFactory = databaseFactoryFfi; - MockClient mockClient = MockClient(); + // Mock SharedPreferences + SharedPreferences.setMockInitialValues({}); + + var mockCredentialsStorage = MockCredentialsStorage(); setUpAll(() { - sqfliteFfiInit(); + when(mockCredentialsStorage.getApiUrl()) + .thenAnswer((_) async => 'https://test-api.com'); + }); + + test('Fetch data successfully', () async { + final client = MockClient(); + when(client.get(any)) + .thenAnswer((_) async => http.Response('[{"id":1}]', 200)); + + final tasks = await fetchTasks( + 'test-uuid', + 'test-secret', + credentialsStorage: mockCredentialsStorage, // Inject mock + ); + + expect(tasks, isA>()); }); group('Tasks model', () { @@ -94,7 +120,8 @@ void main() { "Content-Type": "application/json", })).thenAnswer((_) async => http.Response(responseJson, 200)); - final result = await fetchTasks('123', 'secret'); + final result = await fetchTasks('123', 'secret', + credentialsStorage: mockCredentialsStorage); expect(result, isA>()); }); @@ -103,7 +130,10 @@ void main() { const uuid = '123'; const encryptionSecret = 'secret'; - expect(await fetchTasks(uuid, encryptionSecret), isEmpty); + expect( + await fetchTasks(uuid, encryptionSecret, + credentialsStorage: mockCredentialsStorage), + isEmpty); }); }); From 13f92b14f4c9813d306beafefb8e62f900e0f37f Mon Sep 17 00:00:00 2001 From: shwet-07 <144674061+Shweta-281@users.noreply.github.com> Date: Tue, 8 Apr 2025 13:13:51 +0530 Subject: [PATCH 2/2] fix --- lib/app/modules/home/controllers/home_controller.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/app/modules/home/controllers/home_controller.dart b/lib/app/modules/home/controllers/home_controller.dart index 32df5896..a760ad86 100644 --- a/lib/app/modules/home/controllers/home_controller.dart +++ b/lib/app/modules/home/controllers/home_controller.dart @@ -112,7 +112,9 @@ class HomeController extends GetxController { Future refreshTasks(String clientId, String encryptionSecret) async { TaskDatabase taskDatabase = TaskDatabase(); await taskDatabase.open(); - List tasksFromServer = await fetchTasks(clientId, encryptionSecret); + var mockCredentialsStorage; + List tasksFromServer = await fetchTasks(clientId, encryptionSecret, + credentialsStorage: mockCredentialsStorage); await updateTasksInDatabase(tasksFromServer); List fetchedTasks = await taskDatabase.fetchTasksFromDatabase(); tasks.value = fetchedTasks;