-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
982c099
commit bc024b4
Showing
5 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
// ignore_for_file: talawa_good_doc_comments | ||
|
||
// import 'package:flutter/material.dart'; | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
@@ -170,6 +172,48 @@ Future<void> main() async { | |
expect(model.validate, AutovalidateMode.disabled); | ||
verify(databaseFunctions.gqlNonAuthMutation(queries.loginUser('', ''))); | ||
}); | ||
|
||
test("Check if prev user is fetched correctly with success", () async { | ||
final model = LoginViewModel(); | ||
FlutterSecureStorage.setMockInitialValues( | ||
{"userEmail": "[email protected]", "userPassword": "password123"}, | ||
); | ||
await model.fetchPrevUser(); | ||
|
||
expect(model.prevUserEmail, "[email protected]"); | ||
expect(model.prevUserPassword, "password123"); | ||
}); | ||
|
||
test("Check if fetching previous user result in error", () async { | ||
final model = LoginViewModel(); | ||
FlutterSecureStorage.setMockInitialValues( | ||
{"userEmail": "[email protected]", "userPassword": "password123"}, | ||
); | ||
final mockSecureStorage = MockFlutterSecureStorage(); | ||
model.secureStorage = mockSecureStorage; | ||
|
||
String log = ""; | ||
|
||
await runZonedGuarded( | ||
() async { | ||
await model.fetchPrevUser(); | ||
}, | ||
(error, stack) { | ||
expect(error, isA<Exception>()); | ||
expect(error.toString(), contains("Unable to read")); | ||
expect(stack, isNotNull); | ||
}, | ||
zoneSpecification: ZoneSpecification( | ||
print: (self, parent, zone, line) { | ||
log = line; | ||
}, | ||
), | ||
); | ||
expect( | ||
log, | ||
contains("Unable to read"), | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
|
@@ -191,3 +235,22 @@ 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_localizations/flutter_localizations.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
@@ -23,6 +26,25 @@ import '../../../helpers/test_locator.dart'; | |
/// MockBuildContext class helps to mock the BuildContext class. | ||
class MockBuildContext extends Mock implements BuildContext {} | ||
|
||
/// Mock Class for Flutter Secure Storage. | ||
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage { | ||
@override | ||
Future<void> delete({ | ||
required String key, | ||
IOSOptions? iOptions, | ||
AndroidOptions? aOptions, | ||
LinuxOptions? lOptions, | ||
WebOptions? webOptions, | ||
MacOsOptions? mOptions, | ||
WindowsOptions? wOptions, | ||
}) async { | ||
if (key == "userEmail" || key == "userPassword") { | ||
throw Exception("Deletion error"); | ||
} | ||
return Future.value(null); | ||
} | ||
} | ||
|
||
/// MockCallbackFunction class helps to mock the callback function. | ||
class MockCallbackFunction extends Mock { | ||
/// call function helps to mock the call function. | ||
|
@@ -307,5 +329,60 @@ Future<void> main() async { | |
|
||
verify(model.logout()); | ||
}); | ||
test('Should delete stored values if checkBoxVal is false', () async { | ||
FlutterSecureStorage.setMockInitialValues( | ||
{"userEmail": "[email protected]", "userPassword": "password123"}, | ||
); | ||
const secureStorage = FlutterSecureStorage(); | ||
const bool checkBoxVal = false; | ||
if (checkBoxVal == false) { | ||
try { | ||
await secureStorage.delete(key: "userEmail"); | ||
await secureStorage.delete(key: "userPassword"); | ||
} catch (e) { | ||
print("Unable to delete stored value : $e"); | ||
} | ||
} | ||
final userEmail = await secureStorage.read(key: "userEmail"); | ||
final userPassword = await secureStorage.read(key: "userPassword"); | ||
expect(userEmail, isNull); | ||
expect(userPassword, isNull); | ||
}); | ||
test('Should handle exception during deletion', () async { | ||
FlutterSecureStorage.setMockInitialValues( | ||
{"userEmail": "[email protected]", "userPassword": "password123"}, | ||
); | ||
final mockSecureStorage = MockFlutterSecureStorage(); | ||
const checkBoxVal = false; | ||
|
||
String log = ""; | ||
|
||
await runZonedGuarded( | ||
() async { | ||
if (checkBoxVal == false) { | ||
try { | ||
await mockSecureStorage.delete(key: "userEmail"); | ||
await mockSecureStorage.delete(key: "userPassword"); | ||
} catch (e) { | ||
print("Unable to delete stored value: $e"); | ||
} | ||
} | ||
}, | ||
(error, stack) { | ||
expect(error, isA<Exception>()); | ||
expect(error.toString(), contains("Deletion error")); | ||
expect(stack, isNotNull); | ||
}, | ||
zoneSpecification: ZoneSpecification( | ||
print: (self, parent, zone, line) { | ||
log = line; | ||
}, | ||
), | ||
); | ||
expect( | ||
log, | ||
contains("Deletion error"), | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters