Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Aug 6, 2024
1 parent bafce4e commit dbc6ae1
Show file tree
Hide file tree
Showing 41 changed files with 2,032 additions and 1,006 deletions.
3 changes: 1 addition & 2 deletions integration_test/add_tokens_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:privacyidea_authenticator/model/riverpod_states/introduction_sta
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';
import 'package:privacyidea_authenticator/model/tokens/token.dart';
import 'package:privacyidea_authenticator/model/version.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/settings_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_folder_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_notifier.dart';
import 'package:privacyidea_authenticator/utils/customization/application_customization.dart';
Expand Down Expand Up @@ -75,7 +74,7 @@ void main() {
(tester) async {
await tester.pumpWidget(TestsAppWrapper(
overrides: [
settingsProvider.overrideWith((ref) => SettingsNotifier(repository: mockSettingsRepository)),
settingsProvider.overrideWith(() => SettingsNotifier(repoOverride: mockSettingsRepository)),
tokenProvider.overrideWith((ref) => TokenNotifier(repository: mockTokenRepository, ref: ref)),
tokenFolderProvider.overrideWith((ref) => TokenFolderNotifier(repository: mockTokenFolderRepository)),
introductionNotifierProvider.overrideWith(() => IntroductionNotifier(repoOverride: mockIntroductionRepository)),
Expand Down
3 changes: 1 addition & 2 deletions integration_test/copy_to_clipboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:privacyidea_authenticator/model/enums/introduction.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/introduction_state.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';
import 'package:privacyidea_authenticator/model/tokens/hotp_token.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/settings_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_folder_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_notifier.dart';
import 'package:privacyidea_authenticator/utils/customization/application_customization.dart';
Expand Down Expand Up @@ -49,7 +48,7 @@ void main() {
testWidgets('Copy to Clipboard Test', (tester) async {
await tester.pumpWidget(TestsAppWrapper(
overrides: [
settingsProvider.overrideWith((ref) => SettingsNotifier(repository: mockSettingsRepository)),
settingsProvider.overrideWith(() => SettingsNotifier(repoOverride: mockSettingsRepository)),
tokenProvider.overrideWith((ref) => TokenNotifier(repository: mockTokenRepository, ref: ref)),
tokenFolderProvider.overrideWith((ref) => TokenFolderNotifier(repository: mockTokenFolderRepository)),
introductionNotifierProvider.overrideWith(() => IntroductionNotifier(repoOverride: mockIntroductionRepository)),
Expand Down
3 changes: 1 addition & 2 deletions integration_test/rename_and_delete_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:privacyidea_authenticator/model/riverpod_states/introduction_sta
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';
import 'package:privacyidea_authenticator/model/tokens/hotp_token.dart';
import 'package:privacyidea_authenticator/model/tokens/token.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/settings_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_folder_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_notifier.dart';
import 'package:privacyidea_authenticator/utils/customization/application_customization.dart';
Expand Down Expand Up @@ -64,7 +63,7 @@ void main() {
testWidgets('Rename and Delete Token', (tester) async {
await tester.pumpWidget(TestsAppWrapper(
overrides: [
settingsProvider.overrideWith((ref) => SettingsNotifier(repository: mockSettingsRepository)),
settingsProvider.overrideWith(() => SettingsNotifier(repoOverride: mockSettingsRepository)),
tokenProvider.overrideWith((ref) => TokenNotifier(repository: mockTokenRepository, ref: ref)),
tokenFolderProvider.overrideWith((ref) => TokenFolderNotifier(repository: mockTokenFolderRepository)),
introductionNotifierProvider.overrideWith(() => IntroductionNotifier(repoOverride: mockIntroductionRepository)),
Expand Down
3 changes: 1 addition & 2 deletions integration_test/two_step_rollout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:privacyidea_authenticator/mains/main_netknights.dart';
import 'package:privacyidea_authenticator/model/enums/introduction.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/introduction_state.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/settings_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_folder_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_notifier.dart';
import 'package:privacyidea_authenticator/utils/customization/application_customization.dart';
Expand Down Expand Up @@ -53,7 +52,7 @@ void main() {
(tester) async {
await tester.pumpWidget(TestsAppWrapper(
overrides: [
settingsProvider.overrideWith((ref) => SettingsNotifier(repository: mockSettingsRepository)),
settingsProvider.overrideWith(() => SettingsNotifier(repoOverride: mockSettingsRepository)),
tokenProvider.overrideWith((ref) => TokenNotifier(repository: mockTokenRepository, ref: ref)),
tokenFolderProvider.overrideWith((ref) => TokenFolderNotifier(repository: mockTokenFolderRepository)),
introductionNotifierProvider.overrideWith(() => IntroductionNotifier(repoOverride: mockIntroductionRepository)),
Expand Down
44 changes: 35 additions & 9 deletions integration_test/views_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import 'package:pointycastle/asymmetric/api.dart';
import 'package:privacyidea_authenticator/l10n/app_localizations_en.dart';
import 'package:privacyidea_authenticator/mains/main_netknights.dart';
import 'package:privacyidea_authenticator/model/enums/introduction.dart';
import 'package:privacyidea_authenticator/model/push_request.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/introduction_state.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/push_request_state.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';
import 'package:privacyidea_authenticator/model/tokens/token.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/push_request_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/settings_notifier.dart';
import 'package:privacyidea_authenticator/utils/custom_int_buffer.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/generated_providers/push_request_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_folder_notifier.dart';
import 'package:privacyidea_authenticator/utils/riverpod/state_notifiers/token_notifier.dart';
import 'package:privacyidea_authenticator/utils/customization/application_customization.dart';
import 'package:privacyidea_authenticator/utils/globals.dart';
import 'package:privacyidea_authenticator/utils/push_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/generated_providers/introduction_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/state_notifier_providers/push_request_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/state_notifier_providers/settings_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/state_notifier_providers/token_folder_provider.dart';
import 'package:privacyidea_authenticator/utils/riverpod/riverpod_providers/state_notifier_providers/token_provider.dart';
Expand All @@ -38,11 +39,13 @@ void main() {
late final MockFirebaseUtils mockFirebaseUtils;
late final MockPrivacyideaIOClient mockIOClient;
late final MockIntroductionRepository mockIntroductionRepository;
late final MockPushRequestRepository mockPushRequestRepository;
setUp(() {
mockSettingsRepository = MockSettingsRepository();
when(mockSettingsRepository.loadSettings()).thenAnswer((_) async =>
SettingsState(isFirstRun: false, useSystemLocale: false, localePreference: const Locale('en'), latestStartedVersion: Version.parse('999.999.999')));
when(mockSettingsRepository.saveSettings(any)).thenAnswer((_) async => true);

mockTokenRepository = MockTokenRepository();
var tokens = <Token>[];
when(mockTokenRepository.loadTokens()).thenAnswer((_) async => tokens);
Expand All @@ -57,6 +60,7 @@ void main() {
tokens.removeWhere((element) => element.id == (arguments[0] as Token).id);
return true;
});

mockTokenFolderRepository = MockTokenFolderRepository();
when(mockTokenFolderRepository.loadFolders()).thenAnswer((_) async => []);
when(mockTokenFolderRepository.saveReplaceList(any)).thenAnswer((_) async => true);
Expand All @@ -67,6 +71,7 @@ void main() {
mockFirebaseUtils = MockFirebaseUtils();
when(mockFirebaseUtils.getFBToken()).thenAnswer((_) => Future.value('fbToken'));
when(mockRsaUtils.deserializeRSAPublicKeyPKCS1('publicKey')).thenAnswer((_) => RSAPublicKey(BigInt.one, BigInt.one));

mockIOClient = MockPrivacyideaIOClient();
when(mockIOClient.doPost(
url: anyNamed('url'),
Expand All @@ -76,12 +81,33 @@ void main() {
mockIntroductionRepository = MockIntroductionRepository();
when(mockIntroductionRepository.loadCompletedIntroductions())
.thenAnswer((_) async => const IntroductionState(completedIntroductions: {...Introduction.values}));

mockPushRequestRepository = MockPushRequestRepository();
var state = PushRequestState(pushRequests: [], knownPushRequests: CustomIntBuffer(list: []));
when(mockPushRequestRepository.saveState(any)).thenAnswer((invocation) async {
final arguments = invocation.positionalArguments;
state = arguments[0] as PushRequestState;
});
when(mockPushRequestRepository.loadState()).thenAnswer((_) async => state);
when(mockPushRequestRepository.addRequest(any)).thenAnswer((invocation) async {
final arguments = invocation.positionalArguments;
state = state.withRequest(arguments[0] as PushRequest);
return state;
});
when(mockPushRequestRepository.removeRequest(any)).thenAnswer((invocation) async {
final arguments = invocation.positionalArguments;
state = state.withoutRequest(arguments[0] as PushRequest);
return state;
});
when(mockPushRequestRepository.clearState()).thenAnswer((_) async {
state = PushRequestState(pushRequests: [], knownPushRequests: CustomIntBuffer(list: []));
});
});

testWidgets('Views Test', (tester) async {
await tester.pumpWidget(TestsAppWrapper(
overrides: [
settingsProvider.overrideWith((ref) => SettingsNotifier(repository: mockSettingsRepository)),
settingsProvider.overrideWith(() => SettingsNotifier(repoOverride: mockSettingsRepository)),
tokenProvider.overrideWith((ref) => TokenNotifier(
repository: mockTokenRepository,
rsaUtils: mockRsaUtils,
Expand All @@ -90,15 +116,15 @@ void main() {
ref: ref,
)),
pushRequestProvider.overrideWith(
(ref) => PushRequestNotifier(
rsaUtils: mockRsaUtils,
ref: ref,
pushProvider: PushProvider(
() => PushRequestNotifier(
rsaUtilsOverride: mockRsaUtils,
ioClientOverride: mockIOClient,
pushRepoOverride: mockPushRequestRepository,
pushProviderOverride: PushProvider(
rsaUtils: mockRsaUtils,
ioClient: mockIOClient,
firebaseUtils: mockFirebaseUtils,
),
ioClient: mockIOClient,
),
),
tokenFolderProvider.overrideWith((ref) => TokenFolderNotifier(repository: mockTokenFolderRepository)),
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces/repo/push_request_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ abstract class PushRequestRepository {
Future<void> clearState();

/// Add a [PushRequest] to the [PushRequestState]
Future<PushRequestState> add(PushRequest pushRequest, {PushRequestState? state});
Future<PushRequestState> addRequest(PushRequest pushRequest, {PushRequestState? state});

/// Remove a [PushRequest] from the [PushRequestState]
Future<PushRequestState> remove(PushRequest pushRequest, {PushRequestState? state});
Future<PushRequestState> removeRequest(PushRequest pushRequest, {PushRequestState? state});
}
4 changes: 2 additions & 2 deletions lib/mains/main_customizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../l10n/app_localizations.dart';
import '../model/enums/app_feature.dart';
import '../model/riverpod_states/settings_state.dart';
import '../utils/globals.dart';
import '../utils/riverpod/riverpod_providers/state_notifier_providers/settings_provider.dart';
import '../utils/riverpod/riverpod_providers/state_providers/app_constraints_provider.dart';
Expand All @@ -50,7 +51,6 @@ class CustomizationAuthenticator extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
WidgetsFlutterBinding.ensureInitialized();
final locale = ref.watch(settingsProvider).currentLocale;
final applicationCustomizer = ref.watch(applicationCustomizerProvider);
return LayoutBuilder(
builder: (context, constraints) {
Expand All @@ -67,7 +67,7 @@ class CustomizationAuthenticator extends ConsumerWidget {
navigatorKey: globalNavigatorKey,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: locale,
locale: ref.watch(settingsProvider).whenOrNull(data: (data) => data.currentLocale) ?? SettingsState.localeDefault,
title: applicationCustomizer.appName,
theme: applicationCustomizer.generateLightTheme(),
darkTheme: applicationCustomizer.generateDarkTheme(),
Expand Down
4 changes: 2 additions & 2 deletions lib/mains/main_netknights.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:easy_dynamic_theme/easy_dynamic_theme.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';

import '../firebase_options/default_firebase_options.dart';
import '../l10n/app_localizations.dart';
Expand Down Expand Up @@ -77,7 +78,6 @@ class PrivacyIDEAAuthenticator extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
globalRef = ref;
final locale = ref.watch(settingsProvider).currentLocale;
return LayoutBuilder(builder: (context, constraints) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ref.read(appConstraintsProvider.notifier).state = constraints;
Expand All @@ -91,7 +91,7 @@ class PrivacyIDEAAuthenticator extends ConsumerWidget {
navigatorKey: globalNavigatorKey,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: locale,
locale: ref.watch(settingsProvider).whenOrNull(data: (data) => data.currentLocale) ?? SettingsState.localeDefault,
title: _customization.appName,
theme: _customization.generateLightTheme(),
darkTheme: _customization.generateDarkTheme(),
Expand Down
5 changes: 3 additions & 2 deletions lib/model/extensions/enums/introduction_extension.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:privacyidea_authenticator/model/riverpod_states/settings_state.dart';

import '../../../l10n/app_localizations.dart';
import '../../../utils/riverpod/riverpod_providers/state_notifier_providers/settings_provider.dart';
Expand Down Expand Up @@ -28,8 +29,8 @@ extension IntroductionX on Introduction {
state.isUncompleted(Introduction.pollForChallenges) &&
Introduction.dragToken.isConditionFulfilled(ref, state) == false &&
Introduction.addFolder.isConditionFulfilled(ref, state) == false,
Introduction.hidePushTokens =>
ref.watch(settingsProvider).hidePushTokens && state.isCompleted(Introduction.pollForChallenges) && state.isUncompleted(Introduction.hidePushTokens),
Introduction.hidePushTokens => ref.watch(settingsProvider).whenOrNull(data: (data) => data.hidePushTokens) ??
SettingsState.hidePushTokensDefault && state.isCompleted(Introduction.pollForChallenges) && state.isUncompleted(Introduction.hidePushTokens),
Introduction.exportTokens => state.isUncompleted(Introduction.exportTokens),
};

Expand Down
40 changes: 20 additions & 20 deletions lib/model/riverpod_states/settings_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ import '../version.dart';

/// This class contains all device specific settings. E.g., the language used, whether to show the guide on start, etc.
class SettingsState {
static bool get _isFirstRunDefault => true;
static bool get _showGuideOnStartDefault => true;
static bool get _hideOtpsDefault => false;
static bool get _enablePollDefault => false;
static Set<String> get _crashReportRecipientsDefault => {defaultCrashReportRecipient};
static Locale get _localePreferenceDefault => AppLocalizations.supportedLocales
static bool get isFirstRunDefault => true;
static bool get showGuideOnStartDefault => true;
static bool get hideOtpsDefault => false;
static bool get enablePollingDefault => false;
static Set<String> get crashReportRecipientsDefault => {defaultCrashReportRecipient};
static Locale get localeDefault => AppLocalizations.supportedLocales
.firstWhere((locale) => locale.languageCode == (!kIsWeb ? Platform.localeName.substring(0, 2) : 'en'), orElse: () => const Locale('en'));

static bool get _useSystemLocaleDefault => true;
static bool get _enableLoggingDefault => false;
static bool get _hidePushTokensDefault => false;
static Version get _latestStartedVersionDefault => Version.parse('0.0.0');
static bool get useSystemLocaleDefault => true;
static bool get verboseLoggingDefault => false;
static bool get hidePushTokensDefault => false;
static Version get latestStartedVersionDefault => Version.parse('0.0.0');

final bool isFirstRun;
final bool showGuideOnStart;
Expand Down Expand Up @@ -67,16 +67,16 @@ class SettingsState {
bool? verboseLogging,
bool? hidePushTokens,
Version? latestStartedVersion,
}) : isFirstRun = isFirstRun ?? _isFirstRunDefault,
showGuideOnStart = showGuideOnStart ?? _showGuideOnStartDefault,
hideOpts = hideOpts ?? _hideOtpsDefault,
enablePolling = enablePolling ?? _enablePollDefault,
crashReportRecipients = crashReportRecipients ?? _crashReportRecipientsDefault,
localePreference = localePreference ?? _localePreferenceDefault,
useSystemLocale = useSystemLocale ?? _useSystemLocaleDefault,
verboseLogging = verboseLogging ?? _enableLoggingDefault,
hidePushTokens = hidePushTokens ?? _hidePushTokensDefault,
latestStartedVersion = latestStartedVersion ?? _latestStartedVersionDefault;
}) : isFirstRun = isFirstRun ?? isFirstRunDefault,
showGuideOnStart = showGuideOnStart ?? showGuideOnStartDefault,
hideOpts = hideOpts ?? hideOtpsDefault,
enablePolling = enablePolling ?? enablePollingDefault,
crashReportRecipients = crashReportRecipients ?? crashReportRecipientsDefault,
localePreference = localePreference ?? localeDefault,
useSystemLocale = useSystemLocale ?? useSystemLocaleDefault,
verboseLogging = verboseLogging ?? verboseLoggingDefault,
hidePushTokens = hidePushTokens ?? hidePushTokensDefault,
latestStartedVersion = latestStartedVersion ?? latestStartedVersionDefault;

SettingsState copyWith({
bool? isFirstRun,
Expand Down
Loading

0 comments on commit dbc6ae1

Please sign in to comment.