Skip to content

Fix Undefined baseUrl and origin in API Service Test #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ class Tasks {
};
}
}

String origin = 'http://localhost:8080';

Future<List<Tasks>> fetchTasks(String uuid, String encryptionSecret) async {
var baseUrl = await CredentialsStorage.getApiUrl();
Future<List<Tasks>> 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';
Expand Down
4 changes: 3 additions & 1 deletion lib/app/modules/home/controllers/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class HomeController extends GetxController {
Future<void> refreshTasks(String clientId, String encryptionSecret) async {
TaskDatabase taskDatabase = TaskDatabase();
await taskDatabase.open();
List<Tasks> tasksFromServer = await fetchTasks(clientId, encryptionSecret);
var mockCredentialsStorage;
List<Tasks> tasksFromServer = await fetchTasks(clientId, encryptionSecret,
credentialsStorage: mockCredentialsStorage);
await updateTasksInDatabase(tasksFromServer);
List<Tasks> fetchedTasks = await taskDatabase.fetchTasksFromDatabase();
tasks.value = fetchedTasks;
Expand Down
7 changes: 4 additions & 3 deletions lib/app/utils/taskchampion/credentials_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?> getEncryptionSecret() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(_encryptionSecretKey);
Expand All @@ -16,7 +18,6 @@ class CredentialsStorage {

static Future<String?> getApiUrl() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(_apiUrlKey);
return prefs.getString('apiUrl') ?? 'https://default-api-url.com';
}

}
46 changes: 38 additions & 8 deletions test/api_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Tasks>>());
});

group('Tasks model', () {
Expand Down Expand Up @@ -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<List<Tasks>>());
});
Expand All @@ -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);
});
});

Expand Down