Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][locker] Allow partial Things and Secrets (#13119)

aman-pilotf43e4750605c
diff --git a/mobile/apps/locker/changes/keep-fields-optional.md b/mobile/apps/locker/changes/keep-fields-optional.md
new file mode 100644
index 0000000000..bb9febc120
--- /dev/null
+++ b/mobile/apps/locker/changes/keep-fields-optional.md
@@ -0,0 +1 @@
+- Keep fields optional
diff --git a/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart b/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
index 0ff5509ce3..09c744ea4e 100644
--- a/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
@@ -85,9 +85,7 @@ class _AccountCredentialsPageState
@override
bool validateForm() {
- return _nameController.text.trim().isNotEmpty &&
- _usernameController.text.trim().isNotEmpty &&
- _passwordController.text.trim().isNotEmpty;
+ return _nameController.text.trim().isNotEmpty;
}
@override
@@ -128,7 +126,6 @@ class _AccountCredentialsPageState
label: context.strings.username,
hintText: context.strings.usernameHint,
controller: _usernameController,
- isRequired: true,
textInputAction: TextInputAction.next,
onChanged: (_) => onFieldChanged(),
),
@@ -137,7 +134,6 @@ class _AccountCredentialsPageState
label: context.strings.password,
hintText: context.strings.passwordHint,
controller: _passwordController,
- isRequired: true,
isPasswordInput: true,
textInputAction: TextInputAction.next,
onChanged: (_) => onFieldChanged(),
@@ -162,25 +158,34 @@ class _AccountCredentialsPageState
final passwordText = _passwordController.text;
final notesText = _notesController.text;
- final fields = <Widget>[
- buildViewField(label: context.strings.username, value: usernameText),
- const SizedBox(height: 24),
- buildViewField(
- label: context.strings.password,
- value: passwordText,
- isSecret: true,
- ),
- ];
+ final fields = <Widget>[];
+
+ if (usernameText.trim().isNotEmpty) {
+ fields.add(
+ buildViewField(label: context.strings.username, value: usernameText),
+ );
+ }
+
+ if (passwordText.trim().isNotEmpty) {
+ if (fields.isNotEmpty) fields.add(const SizedBox(height: 24));
+ fields.add(
+ buildViewField(
+ label: context.strings.password,
+ value: passwordText,
+ isSecret: true,
+ ),
+ );
+ }
if (notesText.trim().isNotEmpty) {
- fields.addAll([
- const SizedBox(height: 24),
+ if (fields.isNotEmpty) fields.add(const SizedBox(height: 24));
+ fields.add(
buildViewField(
label: context.strings.credentialNotes,
value: notesText,
maxLines: 6,
),
- ]);
+ );
}
return fields;
diff --git a/mobile/apps/locker/lib/ui/pages/physical_records_page.dart b/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
index d8b56537a4..33264e8971 100644
--- a/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
@@ -80,8 +80,7 @@ class _PhysicalRecordsPageState
@override
bool validateForm() {
- return _nameController.text.trim().isNotEmpty &&
- _locationController.text.trim().isNotEmpty;
+ return _nameController.text.trim().isNotEmpty;
}
@override
@@ -120,7 +119,6 @@ class _PhysicalRecordsPageState
label: context.strings.recordLocation,
hintText: context.strings.recordLocationHint,
controller: _locationController,
- isRequired: true,
textCapitalization: TextCapitalization.sentences,
textInputAction: TextInputAction.next,
onChanged: (_) => onFieldChanged(),
@@ -141,22 +139,26 @@ class _PhysicalRecordsPageState
@override
List<Widget> buildViewFields() {
- final fields = <Widget>[
- buildViewField(
- label: context.strings.recordLocation,
- value: _locationController.text,
- ),
- ];
+ final fields = <Widget>[];
+
+ if (_locationController.text.trim().isNotEmpty) {
+ fields.add(
+ buildViewField(
+ label: context.strings.recordLocation,
+ value: _locationController.text,
+ ),
+ );
+ }
if (_notesController.text.trim().isNotEmpty) {
- fields.addAll([
- const SizedBox(height: 24),
+ if (fields.isNotEmpty) fields.add(const SizedBox(height: 24));
+ fields.add(
buildViewField(
label: context.strings.recordNotes,
value: _notesController.text,
maxLines: 6,
),
- ]);
+ );
}
return fields;
diff --git a/mobile/apps/locker/test/services/info_file_service_test.dart b/mobile/apps/locker/test/services/info_file_service_test.dart
index 7babdd887b..bc03603022 100644
--- a/mobile/apps/locker/test/services/info_file_service_test.dart
+++ b/mobile/apps/locker/test/services/info_file_service_test.dart
@@ -7,6 +7,43 @@ import "package:locker/services/info_file_service.dart";
void main() {
group("InfoFileService.extractInfoFromFile", () {
+ test("reads partial records with omitted optional fields", () {
+ final secretFile = EnteFile()
+ ..fileType = FileType.info
+ ..title = "Netflix"
+ ..pubMagicMetadata = PubMagicMetadata(
+ info: {
+ "type": "accountCredential",
+ "data": {"name": "Netflix", "notes": "Ask Sam"},
+ },
+ noThumb: true,
+ );
+ final thingFile = EnteFile()
+ ..fileType = FileType.info
+ ..title = "Passport"
+ ..pubMagicMetadata = PubMagicMetadata(
+ info: {
+ "type": "physicalRecord",
+ "data": {"name": "Passport"},
+ },
+ noThumb: true,
+ );
+
+ final secretItem = InfoFileService.instance.extractInfoFromFile(
+ secretFile,
+ )!;
+ final secret = secretItem.data as AccountCredentialData;
+ final thing =
+ InfoFileService.instance.extractInfoFromFile(thingFile)!.data
+ as PhysicalRecordData;
+
+ expect(secret.username, isEmpty);
+ expect(secret.password, isEmpty);
+ expect(secret.notes, "Ask Sam");
+ expect(thing.location, isEmpty);
+ expect(InfoFileService.instance.getInfoFileTitle(secretItem), "Netflix");
+ });
+
test("parses hyphenated account credential types", () {
final file = EnteFile()
..fileType = FileType.info
diff --git a/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart b/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart
new file mode 100644
index 0000000000..ea3deabe3c
--- /dev/null
+++ b/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart
@@ -0,0 +1,164 @@
+import 'dart:io';
+
+import 'package:ente_components/ente_components.dart';
+import 'package:ente_strings/ente_strings.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/models/file_type.dart';
+import 'package:locker/services/configuration.dart';
+import 'package:locker/services/files/sync/models/file.dart';
+import 'package:locker/services/files/sync/models/file_magic.dart';
+import 'package:locker/ui/pages/account_credentials_page.dart';
+import 'package:locker/ui/pages/base_info_page.dart';
+import 'package:locker/ui/pages/personal_note_page.dart';
+import 'package:locker/ui/pages/physical_records_page.dart';
+
+import '../../test_utils/configuration_test_util.dart';
+
+void main() {
+ late Directory testRoot;
+
+ setUp(() async {
+ testRoot = await setupLockerConfigurationForTest('required_info_fields');
+ await Configuration.instance.setUserID(1);
+ });
+
+ tearDown(() async {
+ clearLockerConfigurationTestHandlers();
+ await testRoot.delete(recursive: true);
+ });
+
+ Future<void> showPage(WidgetTester tester, Widget page) async {
+ await tester.pumpWidget(
+ MaterialApp(
+ localizationsDelegates: StringsLocalizations.localizationsDelegates,
+ supportedLocales: const [Locale('en')],
+ home: page,
+ ),
+ );
+ }
+
+ EnteFile partialFile(String type, Map<String, dynamic> data) => EnteFile()
+ ..fileType = FileType.info
+ ..title = data['name'] as String
+ ..pubMagicMetadata = PubMagicMetadata(
+ info: {'type': type, 'data': data},
+ noThumb: true,
+ );
+
+ testWidgets('Secret requires Account but accepts empty credentials', (
+ tester,
+ ) async {
+ await showPage(tester, const AccountCredentialsPage());
+
+ final fields = tester.widgetList<TextInputComponent>(
+ find.byType(TextInputComponent),
+ );
+ expect(fields.where((field) => field.isRequired).length, 1);
+
+ final state = tester.state(find.byType(AccountCredentialsPage)) as dynamic;
+ await tester.enterText(find.byType(TextField).first, 'Netflix');
+ expect(state.validateForm(), isTrue);
+ expect(state.createInfoData().username, isEmpty);
+ expect(state.createInfoData().password, isEmpty);
+
+ await tester.enterText(find.byType(TextField).first, ' ');
+ expect(state.validateForm(), isFalse);
+ });
+
+ testWidgets('Thing requires Name but accepts an empty Location', (
+ tester,
+ ) async {
+ await showPage(tester, const PhysicalRecordsPage());
+
+ final fields = tester.widgetList<TextInputComponent>(
+ find.byType(TextInputComponent),
+ );
+ expect(fields.where((field) => field.isRequired).length, 1);
+
+ final state = tester.state(find.byType(PhysicalRecordsPage)) as dynamic;
+ await tester.enterText(find.byType(TextField).first, 'Passport');
+ expect(state.validateForm(), isTrue);
+ expect(state.createInfoData().location, isEmpty);
+
+ await tester.enterText(find.byType(TextField).first, ' ');
+ expect(state.validateForm(), isFalse);
+ });
+
+ testWidgets('partial Secret shows Notes without phantom credentials', (
+ tester,
+ ) async {
+ await showPage(
+ tester,
+ AccountCredentialsPage(
+ mode: InfoPageMode.view,
+ existingFile: partialFile('accountCredential', {
+ 'name': 'Netflix',
+ 'notes': 'Ask Sam which email we used',
+ }),
+ ),
+ );
+
+ expect(find.text('Ask Sam which email we used'), findsOneWidget);
+ expect(find.text('Username'), findsNothing);
+ expect(find.text('Password'), findsNothing);
+ expect(find.text('β€’β€’β€’β€’β€’β€’β€’β€’'), findsNothing);
+ });
+
+ testWidgets('partial Thing omits empty Location', (tester) async {
+ await showPage(
+ tester,
+ PhysicalRecordsPage(
+ mode: InfoPageMode.view,
+ existingFile: partialFile('physicalRecord', {
+ 'name': 'Passport',
+ 'notes': 'Replace next year',
+ }),
+ ),
+ );
+
+ expect(find.text('Replace next year'), findsOneWidget);
+ expect(find.text('Location'), findsNothing);
+ });
+
+ testWidgets('clearing saved credentials keeps a Secret valid', (
+ tester,
+ ) async {
+ await showPage(
+ tester,
+ AccountCredentialsPage(
+ existingFile: partialFile('accountCredential', {
+ 'name': 'Netflix',
+ 'username': '[email protected]',
+ 'password': 'old password',
+ }),
+ ),
+ );
+
+ final state = tester.state(find.byType(AccountCredentialsPage)) as dynamic;
+ await tester.enterText(find.byType(TextField).at(1), '');
+ await tester.enterText(find.byType(TextField).at(2), '');
+
+ expect(state.validateForm(), isTrue);
+ expect(state.createInfoData().username, isEmpty);
+ expect(state.createInfoData().password, isEmpty);
+ });
+
+ testWidgets('Note still requires Content and derives an empty Title', (
+ tester,
+ ) async {
+ await showPage(tester, const PersonalNotePage());
+
+ final state = tester.state(find.byType(PersonalNotePage)) as dynamic;
+ await tester.enterText(
+ find.byType(TextField).at(1),
+ 'Remember the spare key',
+ );
+ expect(state.validateForm(), isTrue);
+ expect(state.createInfoData().title, 'Remember the spare key');
+
+ await tester.enterText(find.byType(TextField).at(1), ' ');
+ await tester.enterText(find.byType(TextField).first, 'Manual title');
+ expect(state.validateForm(), isFalse);
+ });
+}