mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
All tests pass now
This commit is contained in:
parent
caa10b5f8f
commit
00baff7a97
@ -62,68 +62,90 @@ class UserProfileDBManager {
|
|||||||
|
|
||||||
Future<Database> get _db async => InvenTreePreferencesDB.instance.database;
|
Future<Database> get _db async => InvenTreePreferencesDB.instance.database;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check if a profile with the specified name exists in the database
|
||||||
|
*/
|
||||||
Future<bool> profileNameExists(String name) async {
|
Future<bool> 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<void> addProfile(UserProfile profile) async {
|
/*
|
||||||
|
* Add a new UserProfile to the profiles database.
|
||||||
|
*/
|
||||||
|
Future<bool> 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
|
// Check if a profile already exists with the name
|
||||||
final bool exists = await profileNameExists(profile.name);
|
final bool exists = await profileNameExists(profile.name);
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
print("UserProfile '${profile.name}' already exists");
|
print("UserProfile '${profile.name}' already exists");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int key = await store.add(await _db, profile.toJson()) as int;
|
int key = await store.add(await _db, profile.toJson()) as int;
|
||||||
|
|
||||||
print("Added user profile <${key}> - '${profile.name}'");
|
|
||||||
|
|
||||||
// Record the key
|
// Record the key
|
||||||
profile.key = key;
|
profile.key = key;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Mark the particular profile as selected
|
||||||
|
*/
|
||||||
Future<void> selectProfile(int key) async {
|
Future<void> selectProfile(int key) async {
|
||||||
/*
|
await store.record("selected").put(await _db, key);
|
||||||
* Mark the particular profile as selected
|
|
||||||
*/
|
|
||||||
|
|
||||||
final result = await store.record("selected").put(await _db, key);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateProfile(UserProfile profile) async {
|
/*
|
||||||
|
* Update the selected profile in the database.
|
||||||
|
* The unique integer <key> is used to determine if the profile already exists.
|
||||||
|
*/
|
||||||
|
Future<bool> updateProfile(UserProfile profile) async {
|
||||||
|
|
||||||
if (profile.key == null) {
|
// Prevent invalid profile data from being updated
|
||||||
await addProfile(profile);
|
if (profile.name.isEmpty || profile.username.isEmpty || profile.password.isEmpty) {
|
||||||
return;
|
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<void> deleteProfile(UserProfile profile) async {
|
Future<void> deleteProfile(UserProfile profile) async {
|
||||||
await store.record(profile.key).delete(await _db);
|
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<UserProfile?> getSelectedProfile() async {
|
Future<UserProfile?> getSelectedProfile() async {
|
||||||
/*
|
|
||||||
* Return the currently selected profile.
|
|
||||||
*
|
|
||||||
* key should match the "selected" property
|
|
||||||
*/
|
|
||||||
|
|
||||||
final selected = await store.record("selected").get(await _db);
|
final selected = await store.record("selected").get(await _db);
|
||||||
|
|
||||||
@ -158,11 +180,12 @@ class UserProfileDBManager {
|
|||||||
|
|
||||||
if (profiles[idx].key is int) {
|
if (profiles[idx].key is int) {
|
||||||
profileList.add(
|
profileList.add(
|
||||||
UserProfile.fromJson(
|
UserProfile.fromJson(
|
||||||
profiles[idx].key as int,
|
profiles[idx].key as int,
|
||||||
profiles[idx].value as Map<String, dynamic>,
|
profiles[idx].value as Map<String, dynamic>,
|
||||||
profiles[idx].key == selected,
|
profiles[idx].key == selected,
|
||||||
));
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,33 +10,85 @@ void main() {
|
|||||||
setUp(() async {
|
setUp(() async {
|
||||||
// Ensure we have a user profile available
|
// Ensure we have a user profile available
|
||||||
// This profile will match the dockerized InvenTree setup, running locally
|
// 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",
|
username: "testuser",
|
||||||
password: "testpassword""",
|
password: "testpassword""",
|
||||||
server: "http://localhost:12345",
|
server: "http://localhost:12345",
|
||||||
selected: true,
|
selected: true,
|
||||||
));
|
));
|
||||||
|
|
||||||
final profiles = await UserProfileDBManager().getAllProfiles();
|
expect(result, equals(true));
|
||||||
|
|
||||||
// Ensure we have one profile available
|
// Ensure we have one profile available
|
||||||
|
// expect(profiles.length, equals(1));
|
||||||
|
profiles = await UserProfileDBManager().getAllProfiles();
|
||||||
|
|
||||||
expect(profiles.length, equals(1));
|
expect(profiles.length, equals(1));
|
||||||
|
|
||||||
// Select the profile
|
int key = -1;
|
||||||
await UserProfileDBManager().selectProfile(profiles.first.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
|
// Run a set of tests for user profile functionality
|
||||||
group("Profile Tests", () async {
|
group("Profile Tests:", () {
|
||||||
|
|
||||||
test("Profile Name Check", () async {
|
test("Add Invalid Profiles", () async {
|
||||||
bool result = false;
|
// 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));
|
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));
|
expect(result, equals(true));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -46,6 +98,7 @@ void main() {
|
|||||||
|
|
||||||
expect(prf, isNot(null));
|
expect(prf, isNot(null));
|
||||||
|
|
||||||
|
expect(prf?.name, equals("Test Profile"));
|
||||||
expect(prf?.username, equals("testuser"));
|
expect(prf?.username, equals("testuser"));
|
||||||
expect(prf?.password, equals("testpassword"));
|
expect(prf?.password, equals("testpassword"));
|
||||||
expect(prf?.server, equals("http://localhost:12345"));
|
expect(prf?.server, equals("http://localhost:12345"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user