diff --git a/lib/user_profile.dart b/lib/user_profile.dart index 251aa309..507d9f07 100644 --- a/lib/user_profile.dart +++ b/lib/user_profile.dart @@ -62,68 +62,90 @@ class UserProfileDBManager { Future get _db async => InvenTreePreferencesDB.instance.database; + /* + * Check if a profile with the specified name exists in the database + */ Future profileNameExists(String name) async { - final finder = Finder(filter: Filter.equals("name", name)); + final profiles = await getAllProfiles(); - final profiles = await store.find(await _db, finder: finder); + for (var prf in profiles) { + if (name == prf.name) { + return true; + } + } - return profiles.isNotEmpty; + // No match found! + return false; } - Future addProfile(UserProfile profile) async { + /* + * Add a new UserProfile to the profiles database. + */ + Future addProfile(UserProfile profile) async { + + if (profile.name.isEmpty || profile.username.isEmpty || profile.password.isEmpty) { + print("Profile missing required values - not adding to database"); + return false; + } // Check if a profile already exists with the name final bool exists = await profileNameExists(profile.name); if (exists) { print("UserProfile '${profile.name}' already exists"); - return; + return false; } int key = await store.add(await _db, profile.toJson()) as int; - print("Added user profile <${key}> - '${profile.name}'"); - // Record the key profile.key = key; + + return true; } + /* + * Mark the particular profile as selected + */ Future selectProfile(int key) async { - /* - * Mark the particular profile as selected - */ - - final result = await store.record("selected").put(await _db, key); - - return result; + await store.record("selected").put(await _db, key); } - - Future updateProfile(UserProfile profile) async { - - if (profile.key == null) { - await addProfile(profile); - return; + + /* + * Update the selected profile in the database. + * The unique integer is used to determine if the profile already exists. + */ + Future updateProfile(UserProfile profile) async { + + // Prevent invalid profile data from being updated + if (profile.name.isEmpty || profile.username.isEmpty || profile.password.isEmpty) { + print("Profile missing required values - not updating"); + return false; } - final result = await store.record(profile.key).update(await _db, profile.toJson()); + if (profile.key == null) { + bool result = await addProfile(profile); + return result; + } - print("Updated user profile <${profile.key}> - '${profile.name}'"); + await store.record(profile.key).update(await _db, profile.toJson()); - return result; + return true; } + /* + * Remove a user profile from the database + */ Future deleteProfile(UserProfile profile) async { await store.record(profile.key).delete(await _db); - print("Deleted user profile <${profile.key}> - '${profile.name}'"); } + /* + * Return the currently selected profile. + * The key of the UserProfile should match the "selected" property + */ Future getSelectedProfile() async { - /* - * Return the currently selected profile. - * - * key should match the "selected" property - */ final selected = await store.record("selected").get(await _db); @@ -158,11 +180,12 @@ class UserProfileDBManager { if (profiles[idx].key is int) { profileList.add( - UserProfile.fromJson( - profiles[idx].key as int, - profiles[idx].value as Map, - profiles[idx].key == selected, - )); + UserProfile.fromJson( + profiles[idx].key as int, + profiles[idx].value as Map, + profiles[idx].key == selected, + ) + ); } } diff --git a/test/api_test.dart b/test/api_test.dart index 1b5470b4..928c4028 100644 --- a/test/api_test.dart +++ b/test/api_test.dart @@ -10,33 +10,85 @@ void main() { setUp(() async { // Ensure we have a user profile available // This profile will match the dockerized InvenTree setup, running locally - await UserProfileDBManager().addProfile(UserProfile( + + // To start with, there should not be *any* profiles available + var profiles = await UserProfileDBManager().getAllProfiles(); + + for (var prf in profiles) { + UserProfileDBManager().deleteProfile(prf); + } + + // Check that there are *no* profiles in the database + profiles = await UserProfileDBManager().getAllProfiles(); + expect(profiles.length, equals(0)); + + // Now, create one! + bool result = await UserProfileDBManager().addProfile(UserProfile( + name: "Test Profile", username: "testuser", password: "testpassword""", server: "http://localhost:12345", selected: true, )); - final profiles = await UserProfileDBManager().getAllProfiles(); + expect(result, equals(true)); // Ensure we have one profile available + // expect(profiles.length, equals(1)); + profiles = await UserProfileDBManager().getAllProfiles(); + expect(profiles.length, equals(1)); - // Select the profile - await UserProfileDBManager().selectProfile(profiles.first.key ?? 1); + int key = -1; + // Find the first available profile + for (var p in profiles) { + if (p.key != null) { + key = p.key ?? key; + break; + } + } + + // Select the profile + await UserProfileDBManager().selectProfile(key); }); // Run a set of tests for user profile functionality - group("Profile Tests", () async { + group("Profile Tests:", () { - test("Profile Name Check", () async { - bool result = false; + test("Add Invalid Profiles", () async { + // Add a profile with missing data + bool result = await UserProfileDBManager().addProfile( + UserProfile( + username: "what", + password: "why", + ) + ); - result = await UserProfileDBManager().profileNameExists("doesnotexist"); expect(result, equals(false)); - result = await UserProfileDBManager().profileNameExists("testuser"); + // Add a profile with a name that already exists + result = await UserProfileDBManager().addProfile( + UserProfile( + name: "Test Profile", + username: "xyz", + password: "hunter42", + ) + ); + + expect(result, equals(false)); + + // Check that the number of protocols available is still the same + var profiles = await UserProfileDBManager().getAllProfiles(); + + expect(profiles.length, equals(1)); + }); + + test("Profile Name Check", () async { + bool result = await UserProfileDBManager().profileNameExists("doesnotexist"); + expect(result, equals(false)); + + result = await UserProfileDBManager().profileNameExists("Test Profile"); expect(result, equals(true)); }); @@ -46,6 +98,7 @@ void main() { expect(prf, isNot(null)); + expect(prf?.name, equals("Test Profile")); expect(prf?.username, equals("testuser")); expect(prf?.password, equals("testpassword")); expect(prf?.server, equals("http://localhost:12345"));