feat(apps): add core API infrastructure
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/core/api/api_exception.dart';
|
||||
|
||||
void main() {
|
||||
group('ApiException', () {
|
||||
test('creates from DioException with 400 status', () {
|
||||
final dioException = Exception('Bad request');
|
||||
final apiException = ApiException.fromDioError(dioException);
|
||||
|
||||
expect(apiException, isA<ApiException>());
|
||||
expect(apiException.message, contains('Request failed'));
|
||||
});
|
||||
|
||||
test('NetworkException has correct message', () {
|
||||
const exception = NetworkException('No internet');
|
||||
expect(exception.message, 'No internet');
|
||||
});
|
||||
|
||||
test('UnauthorizedException has default message', () {
|
||||
const exception = UnauthorizedException();
|
||||
expect(exception.message, 'Authentication required');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:social_app/core/storage/token_storage.dart';
|
||||
|
||||
class MockTokenStorage extends Mock implements TokenStorage {}
|
||||
|
||||
void main() {
|
||||
late TokenStorage storage;
|
||||
|
||||
setUp(() {
|
||||
storage = MockTokenStorage();
|
||||
});
|
||||
|
||||
group('TokenStorage', () {
|
||||
test('saves and retrieves access token', () async {
|
||||
when(
|
||||
() => storage.getAccessToken(),
|
||||
).thenAnswer((_) async => 'test_access');
|
||||
when(
|
||||
() =>
|
||||
storage.saveTokens(access: 'test_access', refresh: 'test_refresh'),
|
||||
).thenAnswer((_) async {});
|
||||
|
||||
await storage.saveTokens(access: 'test_access', refresh: 'test_refresh');
|
||||
final token = await storage.getAccessToken();
|
||||
|
||||
expect(token, 'test_access');
|
||||
verify(
|
||||
() =>
|
||||
storage.saveTokens(access: 'test_access', refresh: 'test_refresh'),
|
||||
).called(1);
|
||||
});
|
||||
|
||||
test('clear removes all tokens', () async {
|
||||
when(() => storage.clear()).thenAnswer((_) async {});
|
||||
when(() => storage.getAccessToken()).thenAnswer((_) async => null);
|
||||
|
||||
await storage.clear();
|
||||
final token = await storage.getAccessToken();
|
||||
|
||||
expect(token, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user