78 lines
2.6 KiB
Dart
78 lines
2.6 KiB
Dart
|
|
// 功能描述:购物车公开标识、整数金额及登录失效契约回归;版本:1.0.0。
|
|||
|
|
import 'dart:convert';
|
|||
|
|
import 'package:flutter_test/flutter_test.dart';
|
|||
|
|
import 'package:http/http.dart' as http;
|
|||
|
|
import 'package:http/testing.dart';
|
|||
|
|
import 'package:user_app/data/repositories/client_repository.dart';
|
|||
|
|
import 'package:user_app/data/services/api_client.dart';
|
|||
|
|
import 'package:user_app/domain/models/cart_item.dart';
|
|||
|
|
|
|||
|
|
void main() {
|
|||
|
|
test('购物车迟到响应不跨账号继续加购或更新界面', () async {
|
|||
|
|
var token = 'old';
|
|||
|
|
final repo = ClientRepository(
|
|||
|
|
ApiClient(
|
|||
|
|
() => token,
|
|||
|
|
client: MockClient((_) async {
|
|||
|
|
token = 'new';
|
|||
|
|
return http.Response('{"code":0,"details":{}}', 200);
|
|||
|
|
}),
|
|||
|
|
),
|
|||
|
|
);
|
|||
|
|
await expectLater(repo.cartItem('p1'), throwsA(isA<SessionExpiredException>()));
|
|||
|
|
});
|
|||
|
|
test('条件数量更新携带原版本,图片仅接受可信协议', () async {
|
|||
|
|
final value = <String, Object?>{
|
|||
|
|
'product_identity': 'p1',
|
|||
|
|
'name': '商品',
|
|||
|
|
'price_amount': 1999,
|
|||
|
|
'stock_quantity': 4,
|
|||
|
|
'quantity': 2,
|
|||
|
|
'selected': false,
|
|||
|
|
'available': true,
|
|||
|
|
'revision': 'revision',
|
|||
|
|
'image_url': 'javascript:alert(1)',
|
|||
|
|
};
|
|||
|
|
final repo = ClientRepository(
|
|||
|
|
ApiClient(
|
|||
|
|
() => 'test-token',
|
|||
|
|
client: MockClient((request) async {
|
|||
|
|
expect(request.headers['authorization'], 'test-token');
|
|||
|
|
if (request.method == 'PUT') {
|
|||
|
|
expect(jsonDecode(request.body), {
|
|||
|
|
'quantity': 2,
|
|||
|
|
'selected': false,
|
|||
|
|
'revision': 'revision',
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
return http.Response(
|
|||
|
|
jsonEncode({'code': 0, 'details': value}),
|
|||
|
|
200,
|
|||
|
|
headers: {'content-type': 'application/json; charset=utf-8'},
|
|||
|
|
);
|
|||
|
|
}),
|
|||
|
|
),
|
|||
|
|
);
|
|||
|
|
final item = await repo.cartItem('p1');
|
|||
|
|
expect(item.product.imageUrl, '');
|
|||
|
|
expect(item.amount, 3998);
|
|||
|
|
expect((await repo.setCartItem(item, quantity: 2, selected: false)).selected, isFalse);
|
|||
|
|
expect(
|
|||
|
|
() => CartItem.fromJson({...value, 'price_amount': 1.5}, (s) => s),
|
|||
|
|
throwsFormatException,
|
|||
|
|
);
|
|||
|
|
});
|
|||
|
|
test('购物车接口401清理会话', () async {
|
|||
|
|
var expired = false;
|
|||
|
|
final repo = ClientRepository(
|
|||
|
|
ApiClient(
|
|||
|
|
() => 'test',
|
|||
|
|
onUnauthorized: (_) => expired = true,
|
|||
|
|
client: MockClient((_) async => http.Response('{"code":1301}', 401)),
|
|||
|
|
),
|
|||
|
|
);
|
|||
|
|
await expectLater(repo.cart(), throwsA(isA<SessionExpiredException>()));
|
|||
|
|
expect(expired, isTrue);
|
|||
|
|
});
|
|||
|
|
}
|