Token-Speicherung von SharedPreferences (Klartext) auf flutter_secure_storage (Android Keystore/iOS Keychain) umgestellt. Optional werden Benutzername/Passwort mitgespeichert (Checkbox auf dem Login-Screen, Default an, abwählbar - löscht dabei auch vorhandene gespeicherte Zugangsdaten), damit die App nach Ablauf des 8h-Tokens automatisch neu einloggt statt den Nutzer zurück auf den Login-Screen zu werfen (ApiClient.versucheAutoRelogin(), greift bei 401 in request()). Tests auf FakeSecureStorage (In-Memory-Testdouble) statt SharedPreferences.setMockInitialValues umgestellt. Kein lokales Flutter-SDK in dieser Umgebung - Änderungen per manuellem Code-Review statt flutter analyze/test verifiziert. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CVgbozhYmuEhiEJHffRXCV
96 lines
3.1 KiB
Dart
96 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:mocktail/mocktail.dart';
|
|
|
|
import 'package:mabea_app/api/api_client.dart';
|
|
|
|
import 'fakes/fake_secure_storage.dart';
|
|
|
|
class _MockClient extends Mock implements http.Client {}
|
|
|
|
void main() {
|
|
late _MockClient httpClient;
|
|
late ApiClient apiClient;
|
|
|
|
setUp(() {
|
|
FakeSecureStorage.zuruecksetzen();
|
|
httpClient = _MockClient();
|
|
apiClient = ApiClient(
|
|
baseUrl: 'http://test.local/api/v1',
|
|
httpClient: httpClient,
|
|
storage: const FakeSecureStorage(),
|
|
);
|
|
});
|
|
|
|
group('login', () {
|
|
test('speichert Token bei Erfolg', () async {
|
|
when(() => httpClient.post(
|
|
Uri.parse('http://test.local/api/v1/auth/login'),
|
|
headers: any(named: 'headers'),
|
|
body: any(named: 'body'),
|
|
)).thenAnswer(
|
|
(_) async => http.Response(jsonEncode({'access_token': 'abc-token'}), 200),
|
|
);
|
|
|
|
final token = await apiClient.login('mitarbeiter1', 'test-passwort-123');
|
|
|
|
expect(token, 'abc-token');
|
|
expect(apiClient.istEingeloggt, isTrue);
|
|
});
|
|
|
|
test('wirft ApiException bei falschem Passwort', () async {
|
|
when(() => httpClient.post(
|
|
Uri.parse('http://test.local/api/v1/auth/login'),
|
|
headers: any(named: 'headers'),
|
|
body: any(named: 'body'),
|
|
)).thenAnswer(
|
|
(_) async => http.Response(jsonEncode({'detail': 'falsch'}), 401),
|
|
);
|
|
|
|
expect(
|
|
() => apiClient.login('mitarbeiter1', 'falsch'),
|
|
throwsA(isA<ApiException>().having((e) => e.status, 'status', 401)),
|
|
);
|
|
});
|
|
});
|
|
|
|
group('request', () {
|
|
test('sendet Bearer-Token, sobald eingeloggt', () async {
|
|
when(() => httpClient.post(
|
|
Uri.parse('http://test.local/api/v1/auth/login'),
|
|
headers: any(named: 'headers'),
|
|
body: any(named: 'body'),
|
|
)).thenAnswer((_) async => http.Response(jsonEncode({'access_token': 'abc-token'}), 200));
|
|
await apiClient.login('mitarbeiter1', 'test-passwort-123');
|
|
|
|
when(() => httpClient.get(
|
|
Uri.parse('http://test.local/api/v1/objekte'),
|
|
headers: any(named: 'headers'),
|
|
)).thenAnswer((_) async => http.Response(jsonEncode([]), 200));
|
|
|
|
await apiClient.request('/objekte');
|
|
|
|
final captured = verify(() => httpClient.get(
|
|
Uri.parse('http://test.local/api/v1/objekte'),
|
|
headers: captureAny(named: 'headers'),
|
|
)).captured;
|
|
final headers = captured.single as Map<String, String>;
|
|
expect(headers['Authorization'], 'Bearer abc-token');
|
|
});
|
|
|
|
test('wirft ApiException bei 4xx-Antwort', () async {
|
|
when(() => httpClient.get(
|
|
Uri.parse('http://test.local/api/v1/objekte/1'),
|
|
headers: any(named: 'headers'),
|
|
)).thenAnswer((_) async => http.Response(jsonEncode({'detail': 'nicht gefunden'}), 404));
|
|
|
|
expect(
|
|
() => apiClient.request('/objekte/1'),
|
|
throwsA(isA<ApiException>().having((e) => e.status, 'status', 404)),
|
|
);
|
|
});
|
|
});
|
|
}
|