AgendaTasks/test/features/auth/data/models/token_model_test.dart
m3mo 329ea29966 Add unit tests for auth feature
Tests for UserEntity, UserModel, and TokenModel covering
serialization, parsing, and equality.
2026-02-03 19:45:17 +01:00

125 lines
3.6 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/auth/data/models/token_model.dart';
void main() {
group('TokenModel', () {
group('constructor', () {
test('should create TokenModel with required fields', () {
const token = TokenModel(
accessToken: 'access-token-123',
refreshToken: 'refresh-token-456',
);
expect(token.accessToken, 'access-token-123');
expect(token.refreshToken, 'refresh-token-456');
expect(token.tokenType, 'bearer');
});
test('should create TokenModel with custom tokenType', () {
const token = TokenModel(
accessToken: 'access-token-123',
refreshToken: 'refresh-token-456',
tokenType: 'custom',
);
expect(token.tokenType, 'custom');
});
});
group('fromJson', () {
test('should parse complete JSON correctly', () {
final json = {
'access_token': 'access-token-123',
'refresh_token': 'refresh-token-456',
'token_type': 'bearer',
};
final token = TokenModel.fromJson(json);
expect(token.accessToken, 'access-token-123');
expect(token.refreshToken, 'refresh-token-456');
expect(token.tokenType, 'bearer');
});
test('should default tokenType to bearer when null', () {
final json = {
'access_token': 'access-token-123',
'refresh_token': 'refresh-token-456',
'token_type': null,
};
final token = TokenModel.fromJson(json);
expect(token.tokenType, 'bearer');
});
test('should default tokenType to bearer when missing', () {
final json = {
'access_token': 'access-token-123',
'refresh_token': 'refresh-token-456',
};
final token = TokenModel.fromJson(json);
expect(token.tokenType, 'bearer');
});
test('should parse different token types', () {
final json = {
'access_token': 'access-token-123',
'refresh_token': 'refresh-token-456',
'token_type': 'Bearer',
};
final token = TokenModel.fromJson(json);
expect(token.tokenType, 'Bearer');
});
});
group('toJson', () {
test('should serialize to JSON correctly', () {
const token = TokenModel(
accessToken: 'access-token-123',
refreshToken: 'refresh-token-456',
tokenType: 'bearer',
);
final json = token.toJson();
expect(json['access_token'], 'access-token-123');
expect(json['refresh_token'], 'refresh-token-456');
expect(json['token_type'], 'bearer');
});
test('should include default tokenType in JSON', () {
const token = TokenModel(
accessToken: 'access-token-123',
refreshToken: 'refresh-token-456',
);
final json = token.toJson();
expect(json['token_type'], 'bearer');
});
});
group('round-trip serialization', () {
test('should maintain data after fromJson -> toJson', () {
final originalJson = {
'access_token': 'access-token-123',
'refresh_token': 'refresh-token-456',
'token_type': 'bearer',
};
final token = TokenModel.fromJson(originalJson);
final resultJson = token.toJson();
expect(resultJson['access_token'], originalJson['access_token']);
expect(resultJson['refresh_token'], originalJson['refresh_token']);
expect(resultJson['token_type'], originalJson['token_type']);
});
});
});
}