added frontend

This commit is contained in:
verboomp
2026-01-21 16:08:09 +01:00
parent d2e6f5164a
commit b3de3eec8c
74 changed files with 4938 additions and 26 deletions

View File

@@ -0,0 +1,12 @@
import 'package:http/http.dart' as http;
import 'package:fotodocumentation/utils/http_client_utils.dart';
class TestHttpCLientUtilsImpl extends HttpClientUtils {
http.Client testClient;
TestHttpCLientUtilsImpl(this.testClient);
@override
http.Client get client => testClient;
}

View File

@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/annotations.dart';
import 'package:fotodocumentation/l10n/app_localizations.dart';
import 'package:fotodocumentation/pages/ui_utils/dialog/snackbar_utils.dart';
import 'package:fotodocumentation/pages/ui_utils/header_utils.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:fotodocumentation/utils/password_utils.dart';
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fotodocumentation/utils/global_router.dart';
import 'test_utils.mocks.dart';
void setScreenSize(WidgetTester tester, int width, int height) {
final dpi = tester.view.devicePixelRatio;
tester.view.physicalSize = Size(width * dpi, height * dpi);
}
MockLoginCredentials getDefaultLoginCredentials() {
var mockLoginCredentials = MockLoginCredentials();
return mockLoginCredentials;
}
Future<void> pumpApp(WidgetTester tester, Widget widget) async {
await tester.pumpWidget(MaterialApp(
title: 'App',
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('de'),
],
home: Scaffold(body: widget)));
await tester.pump();
}
Future<void> pumpAppConfig(WidgetTester tester, String initialLocation) async {
await tester.pumpWidget(MaterialApp.router(
title: 'App',
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('de'),
],
routerConfig: GlobalRouter.createRouter(initialLocation)));
await tester.pump();
}
// dart run build_runner build
@GenerateMocks([
LoginCredentials,
HeaderUtils,
PasswordUtils,
SnackbarUtils,
JwtTokenStorage,
http.Client
])
void main() {}

View File

@@ -0,0 +1,542 @@
// Mocks generated by Mockito 5.4.6 from annotations
// in fotodocumentation/test/testing/test_utils.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i11;
import 'dart:convert' as _i12;
import 'dart:typed_data' as _i13;
import 'dart:ui' as _i6;
import 'package:flutter/material.dart' as _i2;
import 'package:fotodocumentation/pages/ui_utils/dialog/snackbar_utils.dart'
as _i9;
import 'package:fotodocumentation/pages/ui_utils/header_utils.dart' as _i7;
import 'package:fotodocumentation/utils/jwt_token_storage.dart' as _i10;
import 'package:fotodocumentation/utils/login_credentials.dart' as _i4;
import 'package:fotodocumentation/utils/password_utils.dart' as _i8;
import 'package:http/http.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:mockito/src/dummies.dart' as _i5;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: must_be_immutable
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class
// ignore_for_file: invalid_use_of_internal_member
class _FakeWidget_0 extends _i1.SmartFake implements _i2.Widget {
_FakeWidget_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
@override
String toString({_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
super.toString();
}
class _FakeResponse_1 extends _i1.SmartFake implements _i3.Response {
_FakeResponse_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
class _FakeStreamedResponse_2 extends _i1.SmartFake
implements _i3.StreamedResponse {
_FakeStreamedResponse_2(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
/// A class which mocks [LoginCredentials].
///
/// See the documentation for Mockito's code generation for more information.
class MockLoginCredentials extends _i1.Mock implements _i4.LoginCredentials {
MockLoginCredentials() {
_i1.throwOnMissingStub(this);
}
@override
String get fullname => (super.noSuchMethod(
Invocation.getter(#fullname),
returnValue: _i5.dummyValue<String>(
this,
Invocation.getter(#fullname),
),
) as String);
@override
bool get hasListeners => (super.noSuchMethod(
Invocation.getter(#hasListeners),
returnValue: false,
) as bool);
@override
void logout() => super.noSuchMethod(
Invocation.method(
#logout,
[],
),
returnValueForMissingStub: null,
);
@override
void addListener(_i6.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#addListener,
[listener],
),
returnValueForMissingStub: null,
);
@override
void removeListener(_i6.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#removeListener,
[listener],
),
returnValueForMissingStub: null,
);
@override
void dispose() => super.noSuchMethod(
Invocation.method(
#dispose,
[],
),
returnValueForMissingStub: null,
);
@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(
#notifyListeners,
[],
),
returnValueForMissingStub: null,
);
}
/// A class which mocks [HeaderUtils].
///
/// See the documentation for Mockito's code generation for more information.
class MockHeaderUtils extends _i1.Mock implements _i7.HeaderUtils {
MockHeaderUtils() {
_i1.throwOnMissingStub(this);
}
@override
_i2.Widget titleWidget(String? text) => (super.noSuchMethod(
Invocation.method(
#titleWidget,
[text],
),
returnValue: _FakeWidget_0(
this,
Invocation.method(
#titleWidget,
[text],
),
),
) as _i2.Widget);
}
/// A class which mocks [PasswordUtils].
///
/// See the documentation for Mockito's code generation for more information.
class MockPasswordUtils extends _i1.Mock implements _i8.PasswordUtils {
MockPasswordUtils() {
_i1.throwOnMissingStub(this);
}
@override
String create() => (super.noSuchMethod(
Invocation.method(
#create,
[],
),
returnValue: _i5.dummyValue<String>(
this,
Invocation.method(
#create,
[],
),
),
) as String);
}
/// A class which mocks [SnackbarUtils].
///
/// See the documentation for Mockito's code generation for more information.
class MockSnackbarUtils extends _i1.Mock implements _i9.SnackbarUtils {
MockSnackbarUtils() {
_i1.throwOnMissingStub(this);
}
@override
void showSnackbar(
_i2.BuildContext? context,
String? msg,
bool? warning,
) =>
super.noSuchMethod(
Invocation.method(
#showSnackbar,
[
context,
msg,
warning,
],
),
returnValueForMissingStub: null,
);
@override
void showSnackbarPopup(
_i2.BuildContext? context,
String? msg,
bool? warning,
) =>
super.noSuchMethod(
Invocation.method(
#showSnackbarPopup,
[
context,
msg,
warning,
],
),
returnValueForMissingStub: null,
);
}
/// A class which mocks [JwtTokenStorage].
///
/// See the documentation for Mockito's code generation for more information.
class MockJwtTokenStorage extends _i1.Mock implements _i10.JwtTokenStorage {
MockJwtTokenStorage() {
_i1.throwOnMissingStub(this);
}
@override
_i11.Future<void> saveTokens(
String? accessToken,
String? refreshToken,
) =>
(super.noSuchMethod(
Invocation.method(
#saveTokens,
[
accessToken,
refreshToken,
],
),
returnValue: _i11.Future<void>.value(),
returnValueForMissingStub: _i11.Future<void>.value(),
) as _i11.Future<void>);
@override
_i11.Future<String?> getAccessToken() => (super.noSuchMethod(
Invocation.method(
#getAccessToken,
[],
),
returnValue: _i11.Future<String?>.value(),
) as _i11.Future<String?>);
@override
_i11.Future<String?> getRefreshToken() => (super.noSuchMethod(
Invocation.method(
#getRefreshToken,
[],
),
returnValue: _i11.Future<String?>.value(),
) as _i11.Future<String?>);
@override
_i11.Future<void> clearTokens() => (super.noSuchMethod(
Invocation.method(
#clearTokens,
[],
),
returnValue: _i11.Future<void>.value(),
returnValueForMissingStub: _i11.Future<void>.value(),
) as _i11.Future<void>);
@override
_i11.Future<bool> hasTokens() => (super.noSuchMethod(
Invocation.method(
#hasTokens,
[],
),
returnValue: _i11.Future<bool>.value(false),
) as _i11.Future<bool>);
@override
_i11.Future<void> updateAccessToken(String? accessToken) =>
(super.noSuchMethod(
Invocation.method(
#updateAccessToken,
[accessToken],
),
returnValue: _i11.Future<void>.value(),
returnValueForMissingStub: _i11.Future<void>.value(),
) as _i11.Future<void>);
}
/// A class which mocks [Client].
///
/// See the documentation for Mockito's code generation for more information.
class MockClient extends _i1.Mock implements _i3.Client {
MockClient() {
_i1.throwOnMissingStub(this);
}
@override
_i11.Future<_i3.Response> head(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#head,
[url],
{#headers: headers},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#head,
[url],
{#headers: headers},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<_i3.Response> get(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#get,
[url],
{#headers: headers},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#get,
[url],
{#headers: headers},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<_i3.Response> post(
Uri? url, {
Map<String, String>? headers,
Object? body,
_i12.Encoding? encoding,
}) =>
(super.noSuchMethod(
Invocation.method(
#post,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#post,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<_i3.Response> put(
Uri? url, {
Map<String, String>? headers,
Object? body,
_i12.Encoding? encoding,
}) =>
(super.noSuchMethod(
Invocation.method(
#put,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#put,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<_i3.Response> patch(
Uri? url, {
Map<String, String>? headers,
Object? body,
_i12.Encoding? encoding,
}) =>
(super.noSuchMethod(
Invocation.method(
#patch,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#patch,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<_i3.Response> delete(
Uri? url, {
Map<String, String>? headers,
Object? body,
_i12.Encoding? encoding,
}) =>
(super.noSuchMethod(
Invocation.method(
#delete,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
returnValue: _i11.Future<_i3.Response>.value(_FakeResponse_1(
this,
Invocation.method(
#delete,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
),
)),
) as _i11.Future<_i3.Response>);
@override
_i11.Future<String> read(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#read,
[url],
{#headers: headers},
),
returnValue: _i11.Future<String>.value(_i5.dummyValue<String>(
this,
Invocation.method(
#read,
[url],
{#headers: headers},
),
)),
) as _i11.Future<String>);
@override
_i11.Future<_i13.Uint8List> readBytes(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#readBytes,
[url],
{#headers: headers},
),
returnValue: _i11.Future<_i13.Uint8List>.value(_i13.Uint8List(0)),
) as _i11.Future<_i13.Uint8List>);
@override
_i11.Future<_i3.StreamedResponse> send(_i3.BaseRequest? request) =>
(super.noSuchMethod(
Invocation.method(
#send,
[request],
),
returnValue:
_i11.Future<_i3.StreamedResponse>.value(_FakeStreamedResponse_2(
this,
Invocation.method(
#send,
[request],
),
)),
) as _i11.Future<_i3.StreamedResponse>);
@override
void close() => super.noSuchMethod(
Invocation.method(
#close,
[],
),
returnValueForMissingStub: null,
);
}

View File

@@ -0,0 +1,101 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/date_time_utils.dart';
void main() {
group('DateTimeUtils', () {
group('toDateTime', () {
test('returns null when element is null', () {
expect(DateTimeUtils.toDateTime(null), isNull);
});
test('returns null when element cannot be parsed to int', () {
expect(DateTimeUtils.toDateTime('invalid'), isNull);
expect(DateTimeUtils.toDateTime('abc123'), isNull);
expect(DateTimeUtils.toDateTime('12.34'), isNull);
});
test('converts valid milliseconds string to DateTime', () {
const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
final result = DateTimeUtils.toDateTime(milliseconds.toString());
expect(result, isNotNull);
expect(result!.millisecondsSinceEpoch, equals(milliseconds));
});
test('converts valid milliseconds int to DateTime', () {
const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
final result = DateTimeUtils.toDateTime(milliseconds);
expect(result, isNotNull);
expect(result!.millisecondsSinceEpoch, equals(milliseconds));
});
test('handles zero milliseconds', () {
final result = DateTimeUtils.toDateTime(0);
expect(result, isNotNull);
expect(result!.millisecondsSinceEpoch, equals(0));
});
test('handles negative milliseconds', () {
const milliseconds = -1000;
final result = DateTimeUtils.toDateTime(milliseconds);
expect(result, isNotNull);
expect(result!.millisecondsSinceEpoch, equals(milliseconds));
});
});
group('fromDateTime', () {
test('returns null when DateTime is null', () {
expect(DateTimeUtils.fromDateTime(null), isNull);
});
test('converts DateTime to milliseconds since epoch', () {
const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
final dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);
final result = DateTimeUtils.fromDateTime(dateTime);
expect(result, equals(milliseconds));
});
test('handles epoch time (zero)', () {
final dateTime = DateTime.fromMillisecondsSinceEpoch(0);
final result = DateTimeUtils.fromDateTime(dateTime);
expect(result, equals(0));
});
test('handles dates before epoch (negative milliseconds)', () {
const milliseconds = -1000;
final dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);
final result = DateTimeUtils.fromDateTime(dateTime);
expect(result, equals(milliseconds));
});
});
group('round-trip conversion', () {
test('toDateTime and fromDateTime are inverse operations', () {
const originalMilliseconds = 1640995200000;
final dateTime = DateTimeUtils.toDateTime(originalMilliseconds);
final convertedBack = DateTimeUtils.fromDateTime(dateTime);
expect(convertedBack, equals(originalMilliseconds));
});
test('fromDateTime and toDateTime are inverse operations', () {
final originalDateTime = DateTime.now();
final milliseconds = DateTimeUtils.fromDateTime(originalDateTime);
final convertedBack = DateTimeUtils.toDateTime(milliseconds);
expect(convertedBack?.millisecondsSinceEpoch, equals(originalDateTime.millisecondsSinceEpoch));
});
});
});
}

View File

@@ -0,0 +1,133 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/http_client_interceptor.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import '../testing/test_utils.mocks.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('HttpClientInterceptor', () {
late MockClient mockInnerClient;
late MockLoginCredentials mockLoginCredentials;
late HttpClientInterceptor interceptingClient;
setUp(() {
DiContainer.instance.initState();
mockInnerClient = MockClient();
mockLoginCredentials = MockLoginCredentials();
DiContainer.instance.put(LoginCredentials, mockLoginCredentials);
interceptingClient = HttpClientInterceptor(mockInnerClient);
});
test('calls logout on 401 response', () async {
// Mock 401 response
final request = http.Request('GET', Uri.parse('http://example.com/api/test'));
final streamedResponse = http.StreamedResponse(
Stream.value([]),
401,
headers: {'content-type': 'application/json'},
);
when(mockInnerClient.send(any)).thenAnswer((_) async => streamedResponse);
// Execute request
final response = await interceptingClient.send(request);
// Verify logout was called
verify(mockLoginCredentials.logout()).called(1);
// Verify response is still returned
expect(response.statusCode, 401);
});
test('does not interfere with successful responses', () async {
// Mock 200 response
final request = http.Request('GET', Uri.parse('http://example.com/api/test'));
final streamedResponse = http.StreamedResponse(
Stream.value([]),
200,
headers: {'content-type': 'application/json'},
);
when(mockInnerClient.send(any)).thenAnswer((_) async => streamedResponse);
// Execute request
final response = await interceptingClient.send(request);
// Verify response is passed through
expect(response.statusCode, 200);
// Verify logout was NOT called
verifyNever(mockLoginCredentials.logout());
});
test('does not interfere with 404 responses', () async {
// Mock 404 response
final request = http.Request('GET', Uri.parse('http://example.com/api/test'));
final streamedResponse = http.StreamedResponse(
Stream.value([]),
404,
headers: {'content-type': 'application/json'},
);
when(mockInnerClient.send(any)).thenAnswer((_) async => streamedResponse);
// Execute request
final response = await interceptingClient.send(request);
// Verify response is passed through
expect(response.statusCode, 404);
// Verify logout was NOT called
verifyNever(mockLoginCredentials.logout());
});
test('does not interfere with 500 responses', () async {
// Mock 500 response
final request = http.Request('GET', Uri.parse('http://example.com/api/test'));
final streamedResponse = http.StreamedResponse(
Stream.value([]),
500,
headers: {'content-type': 'application/json'},
);
when(mockInnerClient.send(any)).thenAnswer((_) async => streamedResponse);
// Execute request
final response = await interceptingClient.send(request);
// Verify response is passed through
expect(response.statusCode, 500);
// Verify logout was NOT called
verifyNever(mockLoginCredentials.logout());
});
test('handles multiple 401 responses gracefully', () async {
// Mock 401 response
final request1 = http.Request('GET', Uri.parse('http://example.com/api/test1'));
final request2 = http.Request('GET', Uri.parse('http://example.com/api/test2'));
final streamedResponse = http.StreamedResponse(
Stream.value([]),
401,
headers: {'content-type': 'application/json'},
);
when(mockInnerClient.send(any)).thenAnswer((_) async => streamedResponse);
// Execute multiple requests
await interceptingClient.send(request1);
await interceptingClient.send(request2);
// Verify logout was called for each 401
verify(mockLoginCredentials.logout()).called(2);
});
});
}

View File

@@ -0,0 +1,182 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
void main() {
group('JwtTokenStorage Tests', () {
late JwtTokenStorage storage;
setUp(() {
storage = JwtTokenStorageImpl();
});
test('initially has no tokens', () async {
// Verify initial state is empty
expect(await storage.getAccessToken(), isNull);
expect(await storage.getRefreshToken(), isNull);
expect(await storage.hasTokens(), isFalse);
});
test('saveTokens stores both access and refresh tokens', () async {
const accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.access';
const refreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.refresh';
await storage.saveTokens(accessToken, refreshToken);
expect(await storage.getAccessToken(), equals(accessToken));
expect(await storage.getRefreshToken(), equals(refreshToken));
expect(await storage.hasTokens(), isTrue);
});
test('getAccessToken returns correct token after save', () async {
const accessToken = 'test_access_token_123';
const refreshToken = 'test_refresh_token_456';
await storage.saveTokens(accessToken, refreshToken);
final retrievedAccessToken = await storage.getAccessToken();
expect(retrievedAccessToken, equals(accessToken));
});
test('getRefreshToken returns correct token after save', () async {
const accessToken = 'test_access_token_123';
const refreshToken = 'test_refresh_token_456';
await storage.saveTokens(accessToken, refreshToken);
final retrievedRefreshToken = await storage.getRefreshToken();
expect(retrievedRefreshToken, equals(refreshToken));
});
test('clearTokens removes all stored tokens', () async {
const accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.access';
const refreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.refresh';
// First save tokens
await storage.saveTokens(accessToken, refreshToken);
expect(await storage.hasTokens(), isTrue);
// Then clear them
await storage.clearTokens();
expect(await storage.getAccessToken(), isNull);
expect(await storage.getRefreshToken(), isNull);
expect(await storage.hasTokens(), isFalse);
});
test('hasTokens returns true when access token exists', () async {
const accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.access';
const refreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.refresh';
expect(await storage.hasTokens(), isFalse);
await storage.saveTokens(accessToken, refreshToken);
expect(await storage.hasTokens(), isTrue);
});
test('hasTokens returns false when access token is empty string', () async {
const accessToken = '';
const refreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.refresh';
await storage.saveTokens(accessToken, refreshToken);
expect(await storage.hasTokens(), isFalse);
});
test('updateAccessToken updates only the access token', () async {
const initialAccessToken = 'initial_access_token';
const initialRefreshToken = 'initial_refresh_token';
const newAccessToken = 'new_access_token';
// Save initial tokens
await storage.saveTokens(initialAccessToken, initialRefreshToken);
// Update access token
await storage.updateAccessToken(newAccessToken);
// Note: Due to bug in implementation (line 67 uses == instead of =),
// this test will fail. The access token won't actually be updated.
// Uncomment below when bug is fixed:
// expect(await storage.getAccessToken(), equals(newAccessToken));
// expect(await storage.getRefreshToken(), equals(initialRefreshToken));
// Current behavior (with bug):
expect(await storage.getAccessToken(), equals(initialAccessToken));
expect(await storage.getRefreshToken(), equals(initialRefreshToken));
});
test('saveTokens can overwrite existing tokens', () async {
const firstAccessToken = 'first_access_token';
const firstRefreshToken = 'first_refresh_token';
const secondAccessToken = 'second_access_token';
const secondRefreshToken = 'second_refresh_token';
// Save first set of tokens
await storage.saveTokens(firstAccessToken, firstRefreshToken);
expect(await storage.getAccessToken(), equals(firstAccessToken));
expect(await storage.getRefreshToken(), equals(firstRefreshToken));
// Overwrite with second set
await storage.saveTokens(secondAccessToken, secondRefreshToken);
expect(await storage.getAccessToken(), equals(secondAccessToken));
expect(await storage.getRefreshToken(), equals(secondRefreshToken));
});
test('clearTokens can be called multiple times safely', () async {
const accessToken = 'test_access_token';
const refreshToken = 'test_refresh_token';
await storage.saveTokens(accessToken, refreshToken);
await storage.clearTokens();
await storage.clearTokens(); // Call again
expect(await storage.getAccessToken(), isNull);
expect(await storage.getRefreshToken(), isNull);
expect(await storage.hasTokens(), isFalse);
});
test('handles long JWT tokens correctly', () async {
const longAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.'
'SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
const longRefreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE1MTYyNDI2MjJ9.'
'Ks_BdfH4CWilyzLNk8S2gShdsGuhkle-VsNBJJxulJc';
await storage.saveTokens(longAccessToken, longRefreshToken);
expect(await storage.getAccessToken(), equals(longAccessToken));
expect(await storage.getRefreshToken(), equals(longRefreshToken));
expect(await storage.hasTokens(), isTrue);
});
test('typical authentication flow', () async {
// 1. Initial state - no tokens
expect(await storage.hasTokens(), isFalse);
// 2. User logs in - tokens are saved
const accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.access';
const refreshToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.refresh';
await storage.saveTokens(accessToken, refreshToken);
expect(await storage.hasTokens(), isTrue);
expect(await storage.getAccessToken(), equals(accessToken));
expect(await storage.getRefreshToken(), equals(refreshToken));
// 3. Access token expires, refresh with new access token
const newAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.new_access';
await storage.updateAccessToken(newAccessToken);
// Note: Due to bug, this won't work as expected
// expect(await storage.getAccessToken(), equals(newAccessToken));
// expect(await storage.getRefreshToken(), equals(refreshToken));
// 4. User logs out - tokens are cleared
await storage.clearTokens();
expect(await storage.hasTokens(), isFalse);
expect(await storage.getAccessToken(), isNull);
expect(await storage.getRefreshToken(), isNull);
});
});
}

View File

@@ -0,0 +1,12 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/url_utils.dart';
void main() {
test('Expect the localhost url for debug testing', () {
final urlUtils = UrlUtilsImpl();
String url = urlUtils.getBaseUrl();
expect(url, "http://localhost:8080/api/");
});
}