yubioath-flutter/integration_test/utils/oath_test_util.dart

418 lines
13 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
2023-12-04 17:28:07 +03:00
* Copyright (C) 2023 Yubico.
2022-10-04 13:12:54 +03:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-09-12 07:34:49 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.dart';
2023-07-03 12:27:25 +03:00
import 'package:yubico_authenticator/app/views/keys.dart' as app_keys;
import 'package:yubico_authenticator/app/views/keys.dart';
2023-11-24 14:18:30 +03:00
import 'package:yubico_authenticator/core/state.dart';
2022-09-12 13:58:17 +03:00
import 'package:yubico_authenticator/oath/keys.dart' as keys;
import 'package:yubico_authenticator/oath/models.dart';
2022-09-12 07:34:49 +03:00
import 'package:yubico_authenticator/oath/views/account_list.dart';
import 'package:yubico_authenticator/oath/views/account_view.dart';
2023-06-30 16:32:10 +03:00
import '../utils/test_util.dart';
2023-11-24 14:18:30 +03:00
import 'android/util.dart';
2022-09-12 07:34:49 +03:00
class Account {
final String? issuer;
final String name;
final String secret;
final bool? touch;
final OathType? oathType;
final HashAlgorithm? hashAlgorithm;
2023-11-29 19:43:52 +03:00
// final PeriodValues? periodValues;
// final bool? digits;
2022-09-12 07:34:49 +03:00
const Account({
this.issuer,
this.name = '',
this.secret = 'abba',
this.touch,
this.oathType,
this.hashAlgorithm,
// this.periodValues,
// this.digits
2022-09-12 07:34:49 +03:00
});
@override
String toString() => '$issuer/$name';
}
extension OathFunctions on WidgetTester {
/// Opens the device menu and taps the "Add account" menu item
Future<void> tapAddAccount() async {
2023-01-11 12:04:15 +03:00
await tapActionIconButton();
await longWait();
2022-09-12 13:58:17 +03:00
await tap(find.byKey(keys.addAccountAction).hitTestable());
2022-09-12 07:34:49 +03:00
await longWait();
2023-11-24 14:18:30 +03:00
if (isDesktop) {
await tap(find.byKey(keys.addAccountManuallyButton).hitTestable());
await longWait();
}
2022-09-12 07:34:49 +03:00
}
Future<void> addAccount(Account a, {bool quiet = true}) async {
var accountView = await findAccount(a);
if (accountView != null) {
testLog(quiet, 'Account already exists: $a');
return;
}
await tapAddAccount();
if (isAndroid) {
await grantCameraPermissions(this);
2022-09-12 07:34:49 +03:00
}
2022-09-12 13:58:17 +03:00
var issuerText = find.byKey(keys.issuerField).hitTestable();
2022-09-12 07:34:49 +03:00
await tap(issuerText);
await enterText(issuerText, a.issuer ?? '');
await shortWait();
2022-09-12 13:58:17 +03:00
var nameText = find.byKey(keys.nameField).hitTestable();
2022-09-12 07:34:49 +03:00
await tap(nameText);
await enterText(nameText, a.name);
await shortWait();
2022-09-12 13:58:17 +03:00
var secretText = find.byKey(keys.secretField).hitTestable();
2022-09-12 07:34:49 +03:00
await tap(secretText);
await enterText(secretText, a.secret);
await shortWait();
2023-11-29 20:31:04 +03:00
if (isAndroid) {
FocusManager.instance.primaryFocus?.unfocus();
await shortWait();
}
if (a.touch != null && a.touch == true) {
var requireTouchFilterChip =
find.byKey(keys.requireTouchFilterChip).hitTestable();
await tap(requireTouchFilterChip);
}
await shortWait();
if (a.oathType != null) {
var oathTypeFilterChip =
find.byKey(keys.oathTypeFilterChip).hitTestable();
await tap(oathTypeFilterChip);
await shortWait();
if (a.oathType == OathType.hotp) {
var hotp = find.byKey(keys.oathTypeHotpFilterValue).hitTestable();
await tap(hotp);
} else {
var totp = find.byKey(keys.oathTypeTotpFilterValue).hitTestable();
await tap(totp);
}
}
await shortWait();
if (a.hashAlgorithm != null) {
var algoTypeFilterChip =
find.byKey(keys.hashAlgorithmFilterChip).hitTestable();
await tap(algoTypeFilterChip);
await shortWait();
if (a.hashAlgorithm == HashAlgorithm.sha1) {
var sha1 = find.byKey(keys.hashAlgorithmSha1FilterValue).hitTestable();
await tap(sha1);
} else if (a.hashAlgorithm == HashAlgorithm.sha256) {
var sha256 =
find.byKey(keys.hashAlgorithmSha256FilterValue).hitTestable();
await tap(sha256);
} else {
var sha512 =
find.byKey(keys.hashAlgorithmSha512FilterValue).hitTestable();
await tap(sha512);
}
}
await shortWait();
2022-09-12 13:58:17 +03:00
await tap(find.byKey(keys.saveButton));
2022-09-12 07:34:49 +03:00
/// the following pump is because of NEO keys
await pump(const Duration(seconds: 1));
2022-09-12 07:34:49 +03:00
accountView = await findAccount(a);
if (accountView != null) {
testLog(quiet, 'Added account $a');
}
}
Finder findAccountList() {
2022-09-12 08:01:10 +03:00
var accountList =
find.byType(AccountList).hitTestable(at: Alignment.topCenter);
2024-09-10 12:11:35 +03:00
// expect(accountList, findsOneWidget);
2022-09-12 07:34:49 +03:00
return accountList;
}
Future<AccountList?> getAccountList() async {
return findAccountList().evaluate().single.widget as AccountList;
}
Future<AccountView?> findAccount(Account a, {bool quiet = true}) async {
2022-09-12 13:58:17 +03:00
if (find.byKey(keys.noAccountsView).hitTestable().evaluate().isNotEmpty) {
/// if there is no OATH account on the YubiKey, the app shows
/// No accounts [MessagePage]
return null;
}
2023-01-02 20:02:32 +03:00
await shortWait();
2023-02-27 13:14:25 +03:00
2022-09-12 08:01:10 +03:00
/// find an AccountView with issuer/name in the account list
2022-09-12 07:34:49 +03:00
var matchingAccounts = find.descendant(
of: findAccountList(),
matching: find.byWidgetPredicate(
(widget) =>
widget is AccountView &&
widget.credential.name == a.name &&
widget.credential.issuer == a.issuer,
skipOffstage: false));
matchingAccounts.evaluate().forEach((element) {
var widget = element.widget;
if (widget is AccountView) {
testLog(quiet,
'Found ${widget.credential.issuer}/${widget.credential.name} matching account');
} else {
printToConsole('Unexpected widget type found: $widget');
}
});
2022-09-12 08:01:10 +03:00
/// return the AccountView if there is only one found
2022-09-12 07:34:49 +03:00
var evaluated = matchingAccounts.evaluate();
return evaluated.isEmpty
? null
: evaluated.length != 1
? null
: evaluated.single.widget as AccountView;
}
Future<void> openAccountDialog(Account a) async {
var accountView = await findAccount(a);
expect(accountView, isNotNull);
if (accountView != null) {
2023-02-27 13:14:25 +03:00
final accountFinder = find.byWidget(accountView);
await ensureVisible(accountFinder);
final codeButtonFinder = find.descendant(
of: accountFinder, matching: find.bySubtype<FilledButton>());
await tap(codeButtonFinder);
2022-09-12 07:34:49 +03:00
await shortWait();
}
}
Future<void> deleteAccount(Account a, {bool quiet = true}) async {
2022-09-12 08:01:10 +03:00
/// only delete account if it exists
2022-09-12 07:34:49 +03:00
var accountView = await findAccount(a);
if (accountView == null) {
testLog(quiet, 'Account to delete does not exist: $a');
return;
}
await openAccountDialog(a);
/// click the delete IconButton in the account dialog
2024-03-08 16:05:06 +03:00
var deleteIconButton = find.byIcon(Symbols.delete).hitTestable();
2022-09-12 07:34:49 +03:00
expect(deleteIconButton, findsOneWidget);
await tap(deleteIconButton);
await longWait();
/// click the delete Button in the delete dialog
2022-09-12 13:58:17 +03:00
var deleteButton = find.byKey(keys.deleteButton).hitTestable();
2022-09-12 07:34:49 +03:00
expect(deleteButton, findsOneWidget);
await tap(deleteButton);
await longWait();
2023-01-02 20:02:32 +03:00
await longWait();
2022-09-12 07:34:49 +03:00
2022-09-12 08:01:10 +03:00
/// try to find account
2022-09-12 07:34:49 +03:00
var deletedAccountView = await findAccount(a);
expect(deletedAccountView, isNull);
if (deletedAccountView == null) {
testLog(quiet, 'Deleted account $a');
}
}
Future<void> renameAccount(
Account a,
String? newIssuer,
String newName, {
bool quiet = true,
}) async {
var accountView = await findAccount(a);
if (accountView == null) {
testLog(quiet, 'Account to rename does not exist: $a');
return;
}
await openAccountDialog(a);
2024-03-08 11:30:47 +03:00
var renameIconButton = find.byIcon(Symbols.edit).hitTestable();
/// only newer FW supports renaming
if (renameIconButton.evaluate().isEmpty) {
/// close the dialog and return
2023-01-02 20:02:32 +03:00
testLog(false, 'This YubiKey does not support account renaming');
await tapAt(const Offset(10, 10));
await shortWait();
return;
}
2022-09-12 07:34:49 +03:00
expect(renameIconButton, findsOneWidget);
await tap(renameIconButton);
await longWait();
2022-09-12 08:01:10 +03:00
/// fill new info
2022-09-12 13:58:17 +03:00
var issuerTextField = find.byKey(keys.issuerField).hitTestable();
2022-09-12 07:34:49 +03:00
await tap(issuerTextField);
await enterText(issuerTextField, newIssuer ?? '');
2022-09-12 13:58:17 +03:00
var nameTextField = find.byKey(keys.nameField).hitTestable();
2022-09-12 07:34:49 +03:00
await tap(nameTextField);
await enterText(nameTextField, newName);
await longWait();
2022-09-12 07:34:49 +03:00
2022-09-12 13:58:17 +03:00
var saveButton = find.byKey(keys.saveButton).hitTestable();
2022-09-12 07:34:49 +03:00
expect(saveButton, findsOneWidget);
await tap(saveButton);
await longWait();
2023-07-03 12:27:25 +03:00
/// close the account dialog by tapping the close button
var closeButton = find.byKey(app_keys.closeButton).hitTestable();
// Wait for toast to clear
await waitForFinder(closeButton);
await tap(closeButton);
2022-09-12 07:34:49 +03:00
await longWait();
2022-09-12 08:01:10 +03:00
/// verify accounts in the list
2022-09-12 07:34:49 +03:00
var renamedAccount = Account(issuer: newIssuer, name: newName);
var renamedAccountView = await findAccount(renamedAccount);
await longWait();
2022-09-12 07:34:49 +03:00
var originalAccountView = await findAccount(a);
expect(renamedAccountView, isNotNull);
expect(originalAccountView, isNull);
if (renamedAccountView != null && originalAccountView == null) {
testLog(quiet, 'Renamed account from $a to $renamedAccount');
}
}
/// Factory reset OATH application
Future<void> resetOATH() async {
final targetKey = approvedKeys[0]; // only reset approved keys!
/// 1. make sure we are using approved key
await switchToKey(targetKey);
await shortWait();
2024-09-10 12:11:35 +03:00
/// 2. open the home view
await tap(find.byKey(homeDrawer).hitTestable());
await shortWait();
/// 3. open menu
await tap(find.byKey(actionsIconButtonKey).hitTestable());
await shortWait();
await tap(find.byKey(yubikeyFactoryResetMenuButton));
await shortWait();
2024-09-10 12:11:35 +03:00
/// 4. then toggle 'Piv' in the 'Factory reset' reset_dialog.dart
await tap(find.byKey(factoryResetPickResetOath));
await longWait();
2024-09-10 12:11:35 +03:00
/// 5. Click reset TextButton: done
await tap(find.byKey(factoryResetReset));
2024-09-10 12:11:35 +03:00
await longWait();
}
/// Opens the device menu and taps the "Set/Manage password" menu item
Future<void> tapSetOrManagePassword() async {
await tapActionIconButton();
await tap(find.byKey(keys.setOrManagePasswordAction));
await longWait();
}
2022-09-12 07:34:49 +03:00
Future<void> setOathPassword(String newPassword) async {
await tapSetOrManagePassword();
await longWait();
2022-09-12 13:58:17 +03:00
var newPasswordEntry = find.byKey(keys.newPasswordField);
2022-09-12 07:34:49 +03:00
await tap(newPasswordEntry);
await enterText(newPasswordEntry, newPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
var confirmPasswordEntry = find.byKey(keys.confirmPasswordField);
2022-09-12 07:34:49 +03:00
await tap(confirmPasswordEntry);
await enterText(confirmPasswordEntry, newPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
await tap(find.byKey(keys.savePasswordButton));
/// the following pause is because of NEO keys
await pump(const Duration(seconds: 1));
2022-09-12 07:34:49 +03:00
2022-09-12 08:01:10 +03:00
/// after tapping Save, the dialog is closed and the save button does not exist
2022-09-12 13:58:17 +03:00
expect(find.byKey(keys.savePasswordButton).hitTestable(), findsNothing);
2022-09-12 08:01:10 +03:00
}
Future<void> replaceOathPassword(
String currentPassword, String newPassword) async {
await tapSetOrManagePassword();
await shortWait();
2022-09-12 13:58:17 +03:00
var currentPasswordEntry = find.byKey(keys.currentPasswordField);
2022-09-12 08:01:10 +03:00
await tap(currentPasswordEntry);
await enterText(currentPasswordEntry, currentPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
var newPasswordEntry = find.byKey(keys.newPasswordField);
2022-09-12 08:01:10 +03:00
await tap(newPasswordEntry);
await enterText(newPasswordEntry, newPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
var confirmPasswordEntry = find.byKey(keys.confirmPasswordField);
2022-09-12 08:01:10 +03:00
await tap(confirmPasswordEntry);
await enterText(confirmPasswordEntry, newPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
await tap(find.byKey(keys.savePasswordButton));
2022-09-12 08:01:10 +03:00
await longWait();
2022-09-12 13:58:17 +03:00
expect(find.byKey(keys.savePasswordButton).hitTestable(), findsNothing);
2022-09-12 07:34:49 +03:00
}
Future<void> unlockOathSession(String newPassword) async {
2022-09-12 13:58:17 +03:00
var validatePasswordEntry = find.byKey(keys.passwordField);
2022-09-12 07:34:49 +03:00
await tap(validatePasswordEntry);
await enterText(validatePasswordEntry, newPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
var unlockButton = find.byKey(keys.unlockButton);
2022-09-12 07:34:49 +03:00
await tap(unlockButton);
2023-01-02 20:02:32 +03:00
/// the following pump is because of NEO keys
await pump(const Duration(seconds: 1));
2022-09-12 07:34:49 +03:00
2022-09-12 13:58:17 +03:00
expect(find.byKey(keys.unlockButton).hitTestable(), findsNothing);
2022-09-12 07:34:49 +03:00
}
Future<void> removeOathPassword(String currentPassword) async {
await tapSetOrManagePassword();
await longWait();
2022-09-12 13:58:17 +03:00
var currentPasswordEntry = find.byKey(keys.currentPasswordField);
2022-09-12 07:34:49 +03:00
await tap(currentPasswordEntry);
await enterText(currentPasswordEntry, currentPassword);
await shortWait();
2022-09-12 13:58:17 +03:00
await tap(find.byKey(keys.removePasswordButton));
2023-01-02 20:02:32 +03:00
/// the following pump is because of NEO keys
await pump(const Duration(seconds: 1));
2022-09-12 07:34:49 +03:00
2022-09-12 13:58:17 +03:00
expect(find.byKey(keys.removePasswordButton).hitTestable(), findsNothing);
2022-09-12 07:34:49 +03:00
}
}