yubioath-flutter/integration_test/test_util.dart

193 lines
5.5 KiB
Dart
Raw Normal View History

2022-05-12 16:35:14 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:yubico_authenticator/app/views/device_button.dart';
import 'package:yubico_authenticator/app/views/keys.dart' as app_keys;
2022-10-03 16:34:06 +03:00
import 'package:yubico_authenticator/app/views/keys.dart';
2022-05-12 16:35:14 +03:00
import 'package:yubico_authenticator/core/state.dart';
2022-10-03 16:34:06 +03:00
import 'package:yubico_authenticator/management/views/keys.dart';
2022-05-12 16:35:14 +03:00
2022-09-13 13:32:13 +03:00
import 'android/util.dart' as android_test_util;
import 'approved_yubikeys.dart';
2022-09-13 13:32:13 +03:00
import 'desktop/util.dart' as desktop_test_util;
2022-09-12 07:34:49 +03:00
const shortestWaitMs = 10;
const shortWaitMs = 50;
const longWaitMs = 200;
const veryLongWaitS = 10; // seconds
/// information about YubiKey as seen by the app
String? yubiKeyName;
String? yubiKeyFirmware;
String? yubiKeySerialNumber;
bool collectedYubiKeyInformation = false;
2022-09-12 07:34:49 +03:00
extension AppWidgetTester on WidgetTester {
2022-09-12 07:34:49 +03:00
Future<void> shortestWait() async {
await pump(const Duration(milliseconds: shortestWaitMs));
}
Future<void> shortWait() async {
await pump(const Duration(milliseconds: shortWaitMs));
}
Future<void> longWait() async {
await pump(const Duration(milliseconds: longWaitMs));
}
Future<void> veryLongWait() async {
await pump(const Duration(seconds: veryLongWaitS));
}
/// waits up to [timeOutSec] seconds evaluating whether [Finder] f is
/// visible
Future<Finder> waitForFinder(Finder f, [int timeOutSec = 20]) async {
int delayMs = 500;
int elapsedTime = 0;
var evaluated = f.evaluate();
while (evaluated.isEmpty && elapsedTime < timeOutSec * 1000) {
await pump(Duration(milliseconds: delayMs));
elapsedTime += delayMs;
evaluated = f.evaluate();
}
if (evaluated.isEmpty) {
testLog(false, 'Failed to find ${f.description} in $timeOutSec seconds.');
}
return f;
}
Finder findDeviceButton() {
return find.byType(DeviceButton).hitTestable();
}
2022-09-12 07:34:49 +03:00
/// Taps the device button
Future<void> tapDeviceButton() async {
await tap(findDeviceButton());
await pump(const Duration(milliseconds: 500));
}
2022-10-03 16:34:06 +03:00
/// Drawer helpers
bool hasDrawer() => scaffoldGlobalKey.currentState!.hasDrawer;
/// Open drawer
Future<void> openDrawer() async {
if (hasDrawer()) {
scaffoldGlobalKey.currentState!.openDrawer();
await pump(const Duration(milliseconds: 500));
}
}
/// Close drawer
Future<void> closeDrawer() async {
if (hasDrawer()) {
scaffoldGlobalKey.currentState!.closeDrawer();
await pump(const Duration(milliseconds: 500));
}
}
/// Is drawer opened?
/// If there is no drawer say it is open (all items are available)
bool isDrawerOpened() =>
hasDrawer() == false || scaffoldGlobalKey.currentState!.isDrawerOpen;
/// Management screen
Future<void> openManagementScreen() async {
if (!isDrawerOpened()) {
await openDrawer();
}
await tap(find.byKey(managementAppDrawer));
await pump(const Duration(milliseconds: 500));
expect(find.byKey(screenKey), findsOneWidget);
}
2022-09-13 13:32:13 +03:00
Future<void> startUp([Map<dynamic, dynamic> startUpParams = const {}]) async {
var result = isAndroid == true
2022-09-13 13:32:13 +03:00
? await android_test_util.startUp(this, startUpParams)
: await desktop_test_util.startUp(this, startUpParams);
2022-09-13 13:32:13 +03:00
await collectYubiKeyInformation();
if (!approvedYubiKeys.contains(yubiKeySerialNumber)) {
testLog(false,
'The connected key is refused by the tests: $yubiKeySerialNumber');
expect(approvedYubiKeys.contains(yubiKeySerialNumber), equals(true));
}
return result;
}
2022-09-12 07:34:49 +03:00
void testLog(bool quiet, String message) {
if (!quiet) {
printToConsole(message);
}
}
/// get key information
2022-09-13 13:32:13 +03:00
Future<void> collectYubiKeyInformation() async {
if (collectedYubiKeyInformation) {
return;
}
await tapDeviceButton();
var deviceInfo = find.byKey(app_keys.deviceInfoListTile);
if (deviceInfo.evaluate().isNotEmpty) {
ListTile lt = deviceInfo.evaluate().single.widget as ListTile;
yubiKeyName = (lt.title as Text).data;
var subtitle = (lt.subtitle as Text?)?.data;
if (subtitle != null) {
RegExpMatch? match = RegExp(r'S/N: (?<SN>\d.*) F/W: (?<FW>\d\.\d\.\d)')
.firstMatch(subtitle);
if (match != null) {
yubiKeySerialNumber = match.namedGroup('SN');
yubiKeyFirmware = match.namedGroup('FW');
} else {
match = RegExp(r'F/W: (?<FW>\d\.\d\.\d)').firstMatch(subtitle);
if (match != null) {
yubiKeyFirmware = match.namedGroup('FW');
}
}
}
}
// close the opened menu
await tapAt(const Offset(0, 0));
await longWait();
testLog(false,
'Connected YubiKey: $yubiKeySerialNumber/$yubiKeyFirmware - $yubiKeyName');
if (!approvedYubiKeys.contains(yubiKeySerialNumber)) {
2022-09-13 13:32:13 +03:00
if (yubiKeySerialNumber == null) {
expect(approvedYubiKeys.contains(yubiKeySerialNumber), equals(true),
reason: 'No YubiKey connected');
} else {
expect(approvedYubiKeys.contains(yubiKeySerialNumber), equals(true),
reason:
'YubiKey with S/N $yubiKeySerialNumber is not approved for integration tests.');
}
}
collectedYubiKeyInformation = true;
}
}
2022-09-13 13:32:13 +03:00
@isTest
2022-09-13 13:32:13 +03:00
void appTest(
String description,
WidgetTesterCallback callback, {
bool? skip,
Map startUpParams = const {},
}) {
testWidgets(description, (WidgetTester tester) async {
await tester.startUp(startUpParams);
await callback(tester);
});
}