From 34a9d403ac760e1f377e884610600f56af61761f Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Tue, 14 Nov 2023 10:48:01 +0100 Subject: [PATCH] use test resource for approved keys --- integration_test/test_res/.gitignore | 3 ++ integration_test/test_res/pubspec.yaml | 6 +++ .../test_res/resources/.gitignore | 1 + integration_test/utils/test_util.dart | 39 +++++++++++++------ pubspec.lock | 7 ++++ pubspec.yaml | 2 + 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 integration_test/test_res/.gitignore create mode 100644 integration_test/test_res/pubspec.yaml create mode 100644 integration_test/test_res/resources/.gitignore diff --git a/integration_test/test_res/.gitignore b/integration_test/test_res/.gitignore new file mode 100644 index 00000000..8fd64062 --- /dev/null +++ b/integration_test/test_res/.gitignore @@ -0,0 +1,3 @@ +/pubspec.lock +/.dart_tool + diff --git a/integration_test/test_res/pubspec.yaml b/integration_test/test_res/pubspec.yaml new file mode 100644 index 00000000..a5fc9916 --- /dev/null +++ b/integration_test/test_res/pubspec.yaml @@ -0,0 +1,6 @@ +name: test_res +environment: + sdk: '>=3.0.0 <4.0.0' +flutter: + assets: + - resources/ diff --git a/integration_test/test_res/resources/.gitignore b/integration_test/test_res/resources/.gitignore new file mode 100644 index 00000000..1dc84162 --- /dev/null +++ b/integration_test/test_res/resources/.gitignore @@ -0,0 +1 @@ +approved_serial_numbers.csv diff --git a/integration_test/utils/test_util.dart b/integration_test/utils/test_util.dart index 449556b2..9641d189 100644 --- a/integration_test/utils/test_util.dart +++ b/integration_test/utils/test_util.dart @@ -15,6 +15,7 @@ */ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart' show rootBundle; import 'package:flutter_test/flutter_test.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:yubico_authenticator/app/views/keys.dart' as app_keys; @@ -113,28 +114,42 @@ extension AppWidgetTester on WidgetTester { expect(find.byKey(screenKey), findsOneWidget); } - Future startUp([Map startUpParams = const {}]) async { - // YA_TEST_APPROVED_KEY_SN should contain comma separated list of - // YubiKey serial numbers which are approved for tests - // To pass the variable to the test use: - // flutter --dart-define=YA_TEST_APPROVED_KEY_SN=SN1,SN2,...,SNn test t - const envVar = String.fromEnvironment('YA_TEST_APPROVED_KEY_SN'); - final approvedSerialNumbers = envVar.split(','); + /// Retrieve a list of test approved serial numbers. + /// + /// To add testing keys add comma separated serial numbers to a file + /// `approved_serial_numbers.csv` in `integration_test/test_res/resources/`. + /// This file is bundled only during test runs and is explicitly ignored from + /// version control. + Future> getApprovedSerialNumbers() async { + const approvedKeysResource = 'approved_serial_numbers.csv'; + String approved = ''; + try { + approved = await rootBundle.loadString( + 'packages/test_res/resources/$approvedKeysResource', + ); + } catch (_) { + testLog(false, 'Failed to read $approvedKeysResource'); + } + + return approved.split(',').map((e) => e.trim()).toList(growable: false); + } + + Future startUp([Map startUpParams = const {}]) async { var result = isAndroid == true ? await android_test_util.startUp(this, startUpParams) : await desktop_test_util.startUp(this, startUpParams); await collectYubiKeyInformation(); - if (!approvedSerialNumbers.contains(yubiKeySerialNumber)) { + final approved = await getApprovedSerialNumbers(); + + if (!approved.contains(yubiKeySerialNumber)) { if (yubiKeySerialNumber == null) { - expect( - approvedSerialNumbers.contains(yubiKeySerialNumber), equals(true), + expect(approved.contains(yubiKeySerialNumber), equals(true), reason: 'No YubiKey connected'); } else { - expect( - approvedSerialNumbers.contains(yubiKeySerialNumber), equals(true), + expect(approved.contains(yubiKeySerialNumber), equals(true), reason: 'YubiKey with S/N $yubiKeySerialNumber is not approved for integration tests.'); } diff --git a/pubspec.lock b/pubspec.lock index aa2e3685..0a6be582 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -792,6 +792,13 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.0" + test_res: + dependency: "direct dev" + description: + path: "integration_test/test_res" + relative: true + source: path + version: "0.0.0" timing: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4aabb6fa..f4d7f0e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -73,6 +73,8 @@ dev_dependencies: sdk: flutter flutter_test: sdk: flutter + test_res: + path: integration_test/test_res # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is