184 lines
5.2 KiB
Dart
184 lines
5.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:pdv/data/repositories/odoo_catalog_repository.dart';
|
|
import 'package:pdv/data/datasources/remote/odoo_session_api_client.dart';
|
|
import 'package:pdv/domain/entities/app_settings.dart';
|
|
import 'package:pdv/domain/entities/app_session.dart';
|
|
import 'package:pdv/domain/entities/pos_device.dart';
|
|
import 'package:pdv/domain/repositories/auth_repository.dart';
|
|
import 'package:pdv/domain/repositories/catalog_repository.dart';
|
|
import 'package:pdv/domain/repositories/settings_repository.dart';
|
|
import 'package:pdv/core/storage/secure_session_storage.dart';
|
|
|
|
void main() {
|
|
const settings = AppSettings(
|
|
serverUrl: 'https://erp.example.com/odoo',
|
|
companyIdentifier: 'EMPRESA01',
|
|
);
|
|
final auth = CatalogTestAuthRepository();
|
|
const settingsRepository = CatalogTestSettingsRepository(settings);
|
|
final storage = CatalogTestSessionStorage()
|
|
..cookie = 'sid=test-session'
|
|
..sessionId = 'session-123';
|
|
|
|
OdooCatalogRepository repositoryWith(http.Client client) =>
|
|
OdooCatalogRepository(
|
|
apiClient: OdooSessionApiClient(
|
|
authRepository: auth,
|
|
settingsRepository: settingsRepository,
|
|
sessionStorage: storage,
|
|
client: client,
|
|
),
|
|
);
|
|
|
|
setUp(() {
|
|
auth.authenticated = true;
|
|
auth.session =
|
|
const AppSession(userId: 5, userName: 'Operador', companyId: 1);
|
|
});
|
|
|
|
test('carrega departamentos usando a sessão Odoo e somente id/nome',
|
|
() async {
|
|
final client = MockClient((request) async {
|
|
expect(request.method, 'GET');
|
|
expect(request.url.path, '/odoo/pdv_pos/departamentos');
|
|
expect(request.url.queryParameters['session_id'], 'session-123');
|
|
expect(request.headers['cookie'], 'sid=test-session');
|
|
return http.Response(
|
|
jsonEncode({
|
|
'status': 'ok',
|
|
'departments': [
|
|
{'id': 3, 'name': 'Acessórios'},
|
|
],
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
final repository = repositoryWith(client);
|
|
|
|
final departments = await repository.fetchDepartments();
|
|
|
|
expect(departments.single.id, 3);
|
|
expect(departments.single.name, 'Acessórios');
|
|
client.close();
|
|
});
|
|
|
|
test('carrega produtos do departamento solicitado', () async {
|
|
final client = MockClient((request) async {
|
|
expect(request.url.path, '/odoo/pdv_pos/produtos_por_departamento');
|
|
expect(request.url.queryParameters['department_id'], '3');
|
|
expect(request.url.queryParameters['session_id'], 'session-123');
|
|
expect(request.headers['cookie'], 'sid=test-session');
|
|
return http.Response(
|
|
jsonEncode({
|
|
'status': 'ok',
|
|
'products': [
|
|
{
|
|
'id': 10,
|
|
'codigo': 'P10',
|
|
'grade': 'UN',
|
|
'descricao': 'Produto teste',
|
|
'departamento_id': 3,
|
|
'preco': 12.34,
|
|
'qtdEmEstoque': 4,
|
|
'write_date': '2026-11-12 12:00:00',
|
|
},
|
|
],
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
final repository = repositoryWith(client);
|
|
|
|
final products = await repository.fetchProductsByDepartment(3);
|
|
|
|
expect(products.single.id, 10);
|
|
expect(products.single.name, 'Produto teste');
|
|
expect(products.single.priceMinorUnits, 1234);
|
|
client.close();
|
|
});
|
|
|
|
test('recusa catálogo quando não há sessão autenticada', () async {
|
|
auth.authenticated = false;
|
|
final client = MockClient((_) async => http.Response('', 500));
|
|
final repository = repositoryWith(client);
|
|
|
|
await expectLater(
|
|
repository.fetchDepartments(),
|
|
throwsA(isA<CatalogException>()),
|
|
);
|
|
client.close();
|
|
});
|
|
}
|
|
|
|
class CatalogTestAuthRepository implements AuthRepository {
|
|
bool authenticated = false;
|
|
AppSession? session;
|
|
|
|
@override
|
|
bool get isAuthenticated => authenticated;
|
|
|
|
@override
|
|
AppSession? get currentSession => session;
|
|
|
|
@override
|
|
Future<AppSession> authenticate({
|
|
required String username,
|
|
required String password,
|
|
required AppSettings settings,
|
|
required PosDevice device,
|
|
required String appVersion,
|
|
}) async =>
|
|
throw UnimplementedError();
|
|
|
|
@override
|
|
Future<void> logout() async {
|
|
authenticated = false;
|
|
session = null;
|
|
}
|
|
}
|
|
|
|
class CatalogTestSettingsRepository implements SettingsRepository {
|
|
const CatalogTestSettingsRepository(this.settings);
|
|
|
|
final AppSettings settings;
|
|
|
|
@override
|
|
Future<AppSettings?> load() async => settings;
|
|
|
|
@override
|
|
Future<void> save(AppSettings settings) async {}
|
|
}
|
|
|
|
class CatalogTestSessionStorage implements SecureSessionStorage {
|
|
String? cookie;
|
|
String? sessionId;
|
|
|
|
@override
|
|
Future<void> clearCookie() async {
|
|
cookie = null;
|
|
sessionId = null;
|
|
}
|
|
|
|
@override
|
|
Future<String?> readCookie() async => cookie;
|
|
|
|
@override
|
|
Future<String?> readServerUrl() async => null;
|
|
|
|
@override
|
|
Future<String?> readSessionId() async => sessionId;
|
|
|
|
@override
|
|
Future<void> writeSession(
|
|
{required String cookie,
|
|
required String serverUrl,
|
|
required String sessionId}) async {
|
|
this.cookie = cookie;
|
|
this.sessionId = sessionId;
|
|
}
|
|
}
|