Skip to content

Commit

Permalink
Test updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaulekhi committed Dec 25, 2024
1 parent fe03e05 commit 4d54905
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 103 deletions.
37 changes: 24 additions & 13 deletions lib/view_model/pre_auth_view_models/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,7 @@ class LoginViewModel extends BaseModel {
arguments: MainScreenArgs(mainScreenIndex: 0, fromSignUp: false),
);
}
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");
}
await storingCredentialsInSecureStorage();
},
onActionException: (e) async {
print('here');
Expand All @@ -195,6 +183,29 @@ class LoginViewModel extends BaseModel {
}
}

/// Storing credentials in secure storage.
///
/// **params**:
/// None
///
/// **returns**:
/// None
Future<void> storingCredentialsInSecureStorage() async {
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");
}
}

/// Fetch the previous user credentials.
///
/// **params**:
Expand Down
39 changes: 25 additions & 14 deletions lib/view_model/pre_auth_view_models/signup_details_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SignupDetailsViewModel extends BaseModel {
late OrgInfo selectedOrganization;

/// Secure local storage instance.
final secureStorage = const FlutterSecureStorage();
FlutterSecureStorage secureStorage = const FlutterSecureStorage();

/// TextEditingController for handling confirmation password input field.
TextEditingController confirmPassword = TextEditingController();
Expand Down Expand Up @@ -202,19 +202,7 @@ class SignupDetailsViewModel extends BaseModel {
Routes.splashScreen,
);
}
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");
}
await storingCredentialsInSecureStorage();
}
}
},
Expand All @@ -228,4 +216,27 @@ class SignupDetailsViewModel extends BaseModel {
);
}
}

/// Storing credentials in secure storage.
///
/// **params**:
/// None
///
/// **returns**:
/// None
Future<void> storingCredentialsInSecureStorage() async {
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");
}
}
}
7 changes: 0 additions & 7 deletions lib/widgets/custom_alert_dialog_with_checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CustomAlertDialogWithCheckbox extends StatefulWidget {
required this.dialogSubTitle,
required this.checkboxLabel,
this.initialCheckboxValue = false,
this.onCheckboxChanged,
});

/// Indicates whether the order of action buttons should be reversed.
Expand Down Expand Up @@ -52,9 +51,6 @@ class CustomAlertDialogWithCheckbox extends StatefulWidget {
/// Initial value for the checkbox.
final bool initialCheckboxValue;

/// Callback for checkbox value change.
final ValueChanged<bool>? onCheckboxChanged;

@override
_CustomAlertDialogWithCheckboxState createState() =>
_CustomAlertDialogWithCheckboxState();
Expand Down Expand Up @@ -128,9 +124,6 @@ class _CustomAlertDialogWithCheckboxState
setState(() {
_checkboxValue = val!;
});
if (widget.onCheckboxChanged != null) {
widget.onCheckboxChanged?.call(_checkboxValue);
}
},
),
],
Expand Down
19 changes: 14 additions & 5 deletions lib/widgets/directly_login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ class DirectlyLogin extends StatelessWidget {
color: const Color(0xFF4285F4),
),
recognizer: TapGestureRecognizer()
..onTap = () {
model.email.text = model.prevUserEmail ?? '';
model.password.text = model.prevUserPassword ?? '';
model.login();
},
..onTap = loginUsingPrevCredentials,
),
],
),
Expand All @@ -59,4 +55,17 @@ class DirectlyLogin extends StatelessWidget {
},
);
}

/// This method is used to login useing saved detials.
///
/// **params**:
/// None
///
/// **returns**:
/// None
Future<void> loginUsingPrevCredentials() async {
model.email.text = model.prevUserEmail ?? '';
model.password.text = model.prevUserPassword ?? '';
await model.login();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ import '../../helpers/test_locator.dart';

// import '../../helpers/test_helpers.dart';

/// Mock Class for Flutter Secure Storage for error detection.
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {
@override
Future<void> write({
required String key,
required String? value,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Storing error");
}
return Future.value(null);
}

@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Unable to read");
}
return Future.value(null);
}
}

final data = {
'login': {
'user': {
Expand Down Expand Up @@ -214,6 +250,36 @@ Future<void> main() async {
contains("Unable to read"),
);
});
test('Should handle exception while storing data', () async {
final model = LoginViewModel();
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
final mockSecureStorage = MockFlutterSecureStorage();
model.secureStorage = mockSecureStorage;

String log = "";

await runZonedGuarded(
() async {
await model.storingCredentialsInSecureStorage();
},
(error, stack) {
expect(error, isA<Exception>());
expect(error.toString(), contains("Storing error"));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);
expect(
log,
contains("Storing error"),
);
});
});
}

Expand All @@ -235,22 +301,3 @@ class MockUserConfig extends Mock implements UserConfig {
@override
Future<bool> updateUser(User user) async => true;
}

/// Mock Class for Flutter Secure Storage.
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {
@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Unable to read");
}
return Future.value(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// ignore_for_file: talawa_api_doc
// ignore_for_file: talawa_good_doc_comments

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -554,6 +556,36 @@ void main() {
),
);
});
test('Should handle exception while storing data', () async {
final model = SignupDetailsViewModel();
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
final mockSecureStorage = MockFlutterSecureStorage();
model.secureStorage = mockSecureStorage;

String log = "";

await runZonedGuarded(
() async {
await model.storingCredentialsInSecureStorage();
},
(error, stack) {
expect(error, isA<Exception>());
expect(error.toString(), contains("Storing error"));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);
expect(
log,
contains("Storing error"),
);
});
});
}

Expand All @@ -579,3 +611,39 @@ class MockUserConfig extends Mock implements UserConfig {
@override
Future<dynamic> updateUserMemberRequestOrg(List<OrgInfo> orgs) async => null;
}

/// Mock Class for Flutter Secure Storage for error detection.
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {
@override
Future<void> write({
required String key,
required String? value,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Storing error");
}
return Future.value(null);
}

@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Unable to read");
}
return Future.value(null);
}
}
Loading

0 comments on commit 4d54905

Please sign in to comment.