mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Changed VMs references to database singleton
Added some unit tests (WIP)
This commit is contained in:
@@ -1,20 +1,29 @@
|
|||||||
using ModernKeePass.ViewModels;
|
using Windows.Storage;
|
||||||
|
using ModernKeePass.ViewModels;
|
||||||
using ModernKeePassLib;
|
using ModernKeePassLib;
|
||||||
|
using ModernKeePassLib.Cryptography.KeyDerivation;
|
||||||
using ModernKeePassLib.Keys;
|
using ModernKeePassLib.Keys;
|
||||||
|
|
||||||
namespace ModernKeePass.Interfaces
|
namespace ModernKeePass.Interfaces
|
||||||
{
|
{
|
||||||
public interface IDatabase
|
public interface IDatabase
|
||||||
{
|
{
|
||||||
|
string Name { get; }
|
||||||
bool RecycleBinEnabled { get; set; }
|
bool RecycleBinEnabled { get; set; }
|
||||||
int Status { get; set; }
|
int Status { get; set; }
|
||||||
GroupVm RootGroup { get; set; }
|
GroupVm RootGroup { get; set; }
|
||||||
GroupVm RecycleBin { get; set; }
|
GroupVm RecycleBin { get; set; }
|
||||||
|
StorageFile DatabaseFile { get; set; }
|
||||||
|
PwUuid DataCipher { get; set; }
|
||||||
|
PwCompressionAlgorithm CompressionAlgorithm { get; set; }
|
||||||
|
KdfParameters KeyDerivation { get; set; }
|
||||||
|
|
||||||
void Open(CompositeKey key, bool createNew);
|
void Open(CompositeKey key, bool createNew);
|
||||||
void UpdateCompositeKey(CompositeKey key);
|
void UpdateCompositeKey(CompositeKey key);
|
||||||
bool Save();
|
bool Save();
|
||||||
|
bool Save(StorageFile file);
|
||||||
void CreateRecycleBin();
|
void CreateRecycleBin();
|
||||||
void AddDeletedItem(PwUuid id);
|
void AddDeletedItem(PwUuid id);
|
||||||
|
void Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -4,17 +4,24 @@ namespace ModernKeePass.ViewModels
|
|||||||
{
|
{
|
||||||
public class AboutVm
|
public class AboutVm
|
||||||
{
|
{
|
||||||
public string Name { get; } = Package.Current.DisplayName;
|
private readonly Package _package;
|
||||||
|
|
||||||
|
public string Name => _package.DisplayName;
|
||||||
|
|
||||||
public string Version
|
public string Version
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var package = Package.Current;
|
var version = _package.Id.Version;
|
||||||
var version = package.Id.Version;
|
|
||||||
|
|
||||||
return $"{version.Major}.{version.Minor}";
|
return $"{version.Major}.{version.Minor}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AboutVm() : this(Package.Current) { }
|
||||||
|
|
||||||
|
public AboutVm(Package package)
|
||||||
|
{
|
||||||
|
_package = package;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,6 @@ namespace ModernKeePass.ViewModels
|
|||||||
{
|
{
|
||||||
public class EntryVm : INotifyPropertyChanged, IPwEntity
|
public class EntryVm : INotifyPropertyChanged, IPwEntity
|
||||||
{
|
{
|
||||||
public IDatabase Database { get; set; }
|
|
||||||
public GroupVm ParentGroup { get; private set; }
|
public GroupVm ParentGroup { get; private set; }
|
||||||
|
|
||||||
public GroupVm PreviousGroup { get; private set; }
|
public GroupVm PreviousGroup { get; private set; }
|
||||||
@@ -151,6 +150,7 @@ namespace ModernKeePass.ViewModels
|
|||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
private readonly PwEntry _pwEntry;
|
private readonly PwEntry _pwEntry;
|
||||||
|
private readonly IDatabase _database;
|
||||||
private bool _isEditMode;
|
private bool _isEditMode;
|
||||||
private bool _isRevealPassword;
|
private bool _isRevealPassword;
|
||||||
private double _passwordLength = 25;
|
private double _passwordLength = 25;
|
||||||
@@ -160,13 +160,13 @@ namespace ModernKeePass.ViewModels
|
|||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntryVm() : this(null, null) { }
|
public EntryVm() { }
|
||||||
|
|
||||||
internal EntryVm(PwEntry entry, GroupVm parent) : this(entry, parent, (Application.Current as App)?.Database) { }
|
internal EntryVm(PwEntry entry, GroupVm parent) : this(entry, parent, (Application.Current as App)?.Database) { }
|
||||||
|
|
||||||
public EntryVm(PwEntry entry, GroupVm parent, IDatabase database)
|
public EntryVm(PwEntry entry, GroupVm parent, IDatabase database)
|
||||||
{
|
{
|
||||||
Database = database;
|
_database = database;
|
||||||
_pwEntry = entry;
|
_pwEntry = entry;
|
||||||
ParentGroup = parent;
|
ParentGroup = parent;
|
||||||
}
|
}
|
||||||
@@ -212,9 +212,9 @@ namespace ModernKeePass.ViewModels
|
|||||||
|
|
||||||
public void MarkForDelete()
|
public void MarkForDelete()
|
||||||
{
|
{
|
||||||
if (Database.RecycleBinEnabled && Database.RecycleBin?.IdUuid == null)
|
if (_database.RecycleBinEnabled && _database.RecycleBin?.IdUuid == null)
|
||||||
Database.CreateRecycleBin();
|
_database.CreateRecycleBin();
|
||||||
Move(Database.RecycleBinEnabled && !ParentGroup.IsSelected ? Database.RecycleBin : null);
|
Move(_database.RecycleBinEnabled && !ParentGroup.IsSelected ? _database.RecycleBin : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UndoDelete()
|
public void UndoDelete()
|
||||||
@@ -229,7 +229,7 @@ namespace ModernKeePass.ViewModels
|
|||||||
PreviousGroup.RemovePwEntry(_pwEntry);
|
PreviousGroup.RemovePwEntry(_pwEntry);
|
||||||
if (destination == null)
|
if (destination == null)
|
||||||
{
|
{
|
||||||
Database.AddDeletedItem(IdUuid);
|
_database.AddDeletedItem(IdUuid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ParentGroup = destination;
|
ParentGroup = destination;
|
||||||
@@ -240,13 +240,13 @@ namespace ModernKeePass.ViewModels
|
|||||||
public void CommitDelete()
|
public void CommitDelete()
|
||||||
{
|
{
|
||||||
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
|
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
|
||||||
if (Database.RecycleBinEnabled && !PreviousGroup.IsSelected) Database.RecycleBin.AddPwEntry(_pwEntry);
|
if (_database.RecycleBinEnabled && !PreviousGroup.IsSelected) _database.RecycleBin.AddPwEntry(_pwEntry);
|
||||||
else Database.AddDeletedItem(IdUuid);
|
else _database.AddDeletedItem(IdUuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Save()
|
public void Save()
|
||||||
{
|
{
|
||||||
Database.Save();
|
_database.Save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -32,16 +32,10 @@ namespace ModernKeePass.ViewModels
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsSelected
|
public bool IsSelected
|
||||||
{
|
{
|
||||||
get { return _app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.Id == Id; }
|
get { return _database.RecycleBinEnabled && _database.RecycleBin?.Id == Id; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value && _pwGroup != null) _app.Database.RecycleBin = this;
|
if (value && _pwGroup != null) _database.RecycleBin = this;
|
||||||
/*else if (value && _pwGroup == null)
|
|
||||||
{
|
|
||||||
var recycleBin = _app.Database.RootGroup.AddNewGroup("Recycle bin");
|
|
||||||
recycleBin.IsSelected = true;
|
|
||||||
recycleBin.IconSymbol = Symbol.Delete;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,17 +80,22 @@ namespace ModernKeePass.ViewModels
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly PwGroup _pwGroup;
|
private readonly PwGroup _pwGroup;
|
||||||
private readonly App _app = Application.Current as App;
|
private readonly IDatabase _database;
|
||||||
private bool _isEditMode;
|
private bool _isEditMode;
|
||||||
|
|
||||||
public GroupVm() {}
|
public GroupVm() {}
|
||||||
|
|
||||||
public GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null)
|
internal GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null) : this(pwGroup, parent,
|
||||||
|
(Application.Current as App)?.Database, recycleBinId)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public GroupVm(PwGroup pwGroup, GroupVm parent, IDatabase database, PwUuid recycleBinId = null)
|
||||||
{
|
{
|
||||||
_pwGroup = pwGroup;
|
_pwGroup = pwGroup;
|
||||||
|
_database = database;
|
||||||
ParentGroup = parent;
|
ParentGroup = parent;
|
||||||
|
|
||||||
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _app.Database.RecycleBin = this;
|
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _database.RecycleBin = this;
|
||||||
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)).OrderBy(e => e.Name));
|
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)).OrderBy(e => e.Name));
|
||||||
Entries.Insert(0, new EntryVm ());
|
Entries.Insert(0, new EntryVm ());
|
||||||
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)).OrderBy(g => g.Name));
|
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)).OrderBy(g => g.Name));
|
||||||
@@ -133,9 +132,9 @@ namespace ModernKeePass.ViewModels
|
|||||||
|
|
||||||
public void MarkForDelete()
|
public void MarkForDelete()
|
||||||
{
|
{
|
||||||
if (_app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.IdUuid == null)
|
if (_database.RecycleBinEnabled && _database.RecycleBin?.IdUuid == null)
|
||||||
_app.Database.CreateRecycleBin();
|
_database.CreateRecycleBin();
|
||||||
Move(_app.Database.RecycleBinEnabled && !IsSelected ? _app.Database.RecycleBin : null);
|
Move(_database.RecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -151,7 +150,7 @@ namespace ModernKeePass.ViewModels
|
|||||||
PreviousGroup._pwGroup.Groups.Remove(_pwGroup);
|
PreviousGroup._pwGroup.Groups.Remove(_pwGroup);
|
||||||
if (destination == null)
|
if (destination == null)
|
||||||
{
|
{
|
||||||
_app.Database.AddDeletedItem(IdUuid);
|
_database.AddDeletedItem(IdUuid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ParentGroup = destination;
|
ParentGroup = destination;
|
||||||
@@ -162,13 +161,13 @@ namespace ModernKeePass.ViewModels
|
|||||||
public void CommitDelete()
|
public void CommitDelete()
|
||||||
{
|
{
|
||||||
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
||||||
if (_app.Database.RecycleBinEnabled && !PreviousGroup.IsSelected) _app.Database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
if (_database.RecycleBinEnabled && !PreviousGroup.IsSelected) _database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
||||||
else _app.Database.AddDeletedItem(IdUuid);
|
else _database.AddDeletedItem(IdUuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Save()
|
public void Save()
|
||||||
{
|
{
|
||||||
_app.Database.Save();
|
_database.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
@@ -14,16 +14,16 @@ namespace ModernKeePass.ViewModels
|
|||||||
{
|
{
|
||||||
public class SettingsDatabaseVm: NotifyPropertyChangedBase, IHasSelectableObject
|
public class SettingsDatabaseVm: NotifyPropertyChangedBase, IHasSelectableObject
|
||||||
{
|
{
|
||||||
private readonly App _app = Application.Current as App;
|
private readonly IDatabase _database;
|
||||||
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
|
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
|
||||||
private GroupVm _selectedItem;
|
private GroupVm _selectedItem;
|
||||||
|
|
||||||
public bool HasRecycleBin
|
public bool HasRecycleBin
|
||||||
{
|
{
|
||||||
get { return _app.Database.RecycleBinEnabled; }
|
get { return _database.RecycleBinEnabled; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
_app.Database.RecycleBinEnabled = value;
|
_database.RecycleBinEnabled = value;
|
||||||
OnPropertyChanged();
|
OnPropertyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,26 +47,26 @@ namespace ModernKeePass.ViewModels
|
|||||||
{
|
{
|
||||||
for (var inx = 0; inx < CipherPool.GlobalPool.EngineCount; ++inx)
|
for (var inx = 0; inx < CipherPool.GlobalPool.EngineCount; ++inx)
|
||||||
{
|
{
|
||||||
if (CipherPool.GlobalPool[inx].CipherUuid.Equals(_app.Database.DataCipher)) return inx;
|
if (CipherPool.GlobalPool[inx].CipherUuid.Equals(_database.DataCipher)) return inx;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
set { _app.Database.DataCipher = CipherPool.GlobalPool[value].CipherUuid; }
|
set { _database.DataCipher = CipherPool.GlobalPool[value].CipherUuid; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<string> Compressions => Enum.GetNames(typeof(PwCompressionAlgorithm)).Take((int)PwCompressionAlgorithm.Count);
|
public IEnumerable<string> Compressions => Enum.GetNames(typeof(PwCompressionAlgorithm)).Take((int)PwCompressionAlgorithm.Count);
|
||||||
|
|
||||||
public string CompressionName
|
public string CompressionName
|
||||||
{
|
{
|
||||||
get { return Enum.GetName(typeof(PwCompressionAlgorithm), _app.Database.CompressionAlgorithm); }
|
get { return Enum.GetName(typeof(PwCompressionAlgorithm), _database.CompressionAlgorithm); }
|
||||||
set { _app.Database.CompressionAlgorithm = (PwCompressionAlgorithm)Enum.Parse(typeof(PwCompressionAlgorithm), value); }
|
set { _database.CompressionAlgorithm = (PwCompressionAlgorithm)Enum.Parse(typeof(PwCompressionAlgorithm), value); }
|
||||||
}
|
}
|
||||||
public IEnumerable<string> KeyDerivations => KdfPool.Engines.Select(e => e.Name);
|
public IEnumerable<string> KeyDerivations => KdfPool.Engines.Select(e => e.Name);
|
||||||
|
|
||||||
public string KeyDerivationName
|
public string KeyDerivationName
|
||||||
{
|
{
|
||||||
get { return KdfPool.Get(_app.Database.KeyDerivation.KdfUuid).Name; }
|
get { return KdfPool.Get(_database.KeyDerivation.KdfUuid).Name; }
|
||||||
set { _app.Database.KeyDerivation = KdfPool.Engines.FirstOrDefault(e => e.Name == value)?.GetDefaultParameters(); }
|
set { _database.KeyDerivation = KdfPool.Engines.FirstOrDefault(e => e.Name == value)?.GetDefaultParameters(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISelectableModel SelectedItem
|
public ISelectableModel SelectedItem
|
||||||
@@ -89,9 +89,12 @@ namespace ModernKeePass.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SettingsDatabaseVm()
|
public SettingsDatabaseVm() : this((Application.Current as App)?.Database) { }
|
||||||
|
|
||||||
|
public SettingsDatabaseVm(IDatabase database)
|
||||||
{
|
{
|
||||||
Groups = _app?.Database.RootGroup.Groups;
|
_database = database;
|
||||||
|
Groups = _database.RootGroup.Groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move to another setting class (or a static class)
|
// TODO: Move to another setting class (or a static class)
|
||||||
|
@@ -45,18 +45,19 @@ namespace ModernKeePass.ViewModels
|
|||||||
|
|
||||||
public MainVm() {}
|
public MainVm() {}
|
||||||
|
|
||||||
public MainVm(Frame referenceFrame, Frame destinationFrame)
|
internal MainVm(Frame referenceFrame, Frame destinationFrame) : this(referenceFrame, destinationFrame, (Application.Current as App)?.Database) { }
|
||||||
|
|
||||||
|
public MainVm(Frame referenceFrame, Frame destinationFrame, IDatabase database)
|
||||||
{
|
{
|
||||||
var app = (App)Application.Current;
|
|
||||||
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
|
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
|
||||||
var isDatabaseOpen = app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened;
|
var isDatabaseOpen = database != null && database.Status == (int) DatabaseHelper.DatabaseStatus.Opened;
|
||||||
|
|
||||||
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
|
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
|
||||||
{
|
{
|
||||||
new MainMenuItemVm
|
new MainMenuItemVm
|
||||||
{
|
{
|
||||||
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
|
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
|
||||||
IsSelected = app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening
|
IsSelected = database != null && database.Status == (int) DatabaseHelper.DatabaseStatus.Opening
|
||||||
},
|
},
|
||||||
new MainMenuItemVm
|
new MainMenuItemVm
|
||||||
{
|
{
|
||||||
@@ -69,7 +70,7 @@ namespace ModernKeePass.ViewModels
|
|||||||
},
|
},
|
||||||
new MainMenuItemVm {
|
new MainMenuItemVm {
|
||||||
Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Copy,
|
Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Copy,
|
||||||
IsSelected = (app.Database == null || app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
|
IsSelected = (database == null || database.Status == (int) DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
|
||||||
},
|
},
|
||||||
new MainMenuItemVm
|
new MainMenuItemVm
|
||||||
{
|
{
|
||||||
@@ -78,13 +79,13 @@ namespace ModernKeePass.ViewModels
|
|||||||
};
|
};
|
||||||
// Auto-select the Recent Items menu item if the conditions are met
|
// Auto-select the Recent Items menu item if the conditions are met
|
||||||
SelectedItem = mainMenuItems.FirstOrDefault(m => m.IsSelected);
|
SelectedItem = mainMenuItems.FirstOrDefault(m => m.IsSelected);
|
||||||
if (app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened)
|
if (database != null && database.Status == (int) DatabaseHelper.DatabaseStatus.Opened)
|
||||||
mainMenuItems.Add(new MainMenuItemVm
|
mainMenuItems.Add(new MainMenuItemVm
|
||||||
{
|
{
|
||||||
Title = app.Database.Name,
|
Title = database.Name,
|
||||||
PageType = typeof(GroupDetailPage),
|
PageType = typeof(GroupDetailPage),
|
||||||
Destination = referenceFrame,
|
Destination = referenceFrame,
|
||||||
Parameter = app.Database.RootGroup,
|
Parameter = database.RootGroup,
|
||||||
Group = 1,
|
Group = 1,
|
||||||
SymbolIcon = Symbol.ProtectedDocument
|
SymbolIcon = Symbol.ProtectedDocument
|
||||||
});
|
});
|
||||||
|
@@ -3,26 +3,25 @@ using Windows.Storage;
|
|||||||
using Windows.Storage.AccessCache;
|
using Windows.Storage.AccessCache;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
using ModernKeePass.Common;
|
using ModernKeePass.Common;
|
||||||
|
using ModernKeePass.Interfaces;
|
||||||
|
|
||||||
namespace ModernKeePass.ViewModels
|
namespace ModernKeePass.ViewModels
|
||||||
{
|
{
|
||||||
public class OpenVm: INotifyPropertyChanged
|
public class OpenVm: INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
public bool ShowPasswordBox
|
public bool ShowPasswordBox => _database?.Status == (int) DatabaseHelper.DatabaseStatus.Opening;
|
||||||
{
|
|
||||||
get { return ((App)Application.Current).Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name
|
public string Name => _database?.Name;
|
||||||
{
|
|
||||||
get { return ((App)Application.Current).Database.Name; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public OpenVm()
|
private readonly IDatabase _database;
|
||||||
|
|
||||||
|
public OpenVm() : this((Application.Current as App)?.Database) { }
|
||||||
|
|
||||||
|
public OpenVm(IDatabase database)
|
||||||
{
|
{
|
||||||
var app = Application.Current as App;
|
_database = database;
|
||||||
if (app?.Database == null || app.Database.Status != (int) DatabaseHelper.DatabaseStatus.Opening) return;
|
if (database == null || database.Status != (int) DatabaseHelper.DatabaseStatus.Opening) return;
|
||||||
OpenFile(app.Database.DatabaseFile);
|
OpenFile(database.DatabaseFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
@@ -34,8 +33,7 @@ namespace ModernKeePass.ViewModels
|
|||||||
|
|
||||||
public void OpenFile(StorageFile file)
|
public void OpenFile(StorageFile file)
|
||||||
{
|
{
|
||||||
var database = ((App)Application.Current).Database;
|
_database.DatabaseFile = file;
|
||||||
database.DatabaseFile = file;
|
|
||||||
NotifyPropertyChanged("Name");
|
NotifyPropertyChanged("Name");
|
||||||
NotifyPropertyChanged("ShowPasswordBox");
|
NotifyPropertyChanged("ShowPasswordBox");
|
||||||
AddToRecentList(file);
|
AddToRecentList(file);
|
||||||
|
@@ -1,20 +1,27 @@
|
|||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
|
using ModernKeePass.Interfaces;
|
||||||
|
|
||||||
namespace ModernKeePass.ViewModels
|
namespace ModernKeePass.ViewModels
|
||||||
{
|
{
|
||||||
public class SaveVm
|
public class SaveVm
|
||||||
{
|
{
|
||||||
|
private readonly IDatabase _database;
|
||||||
|
public SaveVm() : this((Application.Current as App)?.Database) { }
|
||||||
|
|
||||||
|
public SaveVm(IDatabase database)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
}
|
||||||
|
|
||||||
public void Save(bool close = true)
|
public void Save(bool close = true)
|
||||||
{
|
{
|
||||||
var app = (App)Application.Current;
|
if (close && _database.Save()) _database.Close();
|
||||||
if (close && app.Database.Save()) app.Database.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Save(StorageFile file)
|
internal void Save(StorageFile file)
|
||||||
{
|
{
|
||||||
var app = (App)Application.Current;
|
_database.Save(file);
|
||||||
app.Database.Save(file);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -4,7 +4,6 @@ using Windows.Storage;
|
|||||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||||
using ModernKeePass.Common;
|
using ModernKeePass.Common;
|
||||||
using ModernKeePass.ViewModels;
|
using ModernKeePass.ViewModels;
|
||||||
using ModernKeePassLib.Keys;
|
|
||||||
|
|
||||||
namespace ModernKeePassApp.Test
|
namespace ModernKeePassApp.Test
|
||||||
{
|
{
|
||||||
@@ -16,20 +15,20 @@ namespace ModernKeePassApp.Test
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCreate()
|
public void TestCreate()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(_database.Status, (int) DatabaseHelper.DatabaseStatus.Closed);
|
Assert.AreEqual((int) DatabaseHelper.DatabaseStatus.Closed, _database.Status);
|
||||||
_database.DatabaseFile = ApplicationData.Current.TemporaryFolder.CreateFileAsync("NewDatabase.kdbx").GetAwaiter().GetResult();
|
_database.DatabaseFile = ApplicationData.Current.TemporaryFolder.CreateFileAsync("NewDatabase.kdbx").GetAwaiter().GetResult();
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Opening);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Opening, _database.Status);
|
||||||
OpenOrCreateDatabase(true);
|
OpenOrCreateDatabase(true);
|
||||||
_database.Close();
|
_database.Close();
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Closed);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Closed, _database.Status);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestOpen()
|
public void TestOpen()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Closed);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Closed, _database.Status);
|
||||||
_database.DatabaseFile = Package.Current.InstalledLocation.GetFileAsync(@"Databases\TestDatabase.kdbx").GetAwaiter().GetResult();
|
_database.DatabaseFile = Package.Current.InstalledLocation.GetFileAsync(@"Databases\TestDatabase.kdbx").GetAwaiter().GetResult();
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Opening);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Opening, _database.Status);
|
||||||
OpenOrCreateDatabase(false);
|
OpenOrCreateDatabase(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,26 +38,21 @@ namespace ModernKeePassApp.Test
|
|||||||
TestOpen();
|
TestOpen();
|
||||||
Assert.IsTrue(_database.Save(ApplicationData.Current.TemporaryFolder.CreateFileAsync("SaveDatabase.kdbx").GetAwaiter().GetResult()));
|
Assert.IsTrue(_database.Save(ApplicationData.Current.TemporaryFolder.CreateFileAsync("SaveDatabase.kdbx").GetAwaiter().GetResult()));
|
||||||
_database.Close();
|
_database.Close();
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Closed);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Closed, _database.Status);
|
||||||
TestOpen();
|
TestOpen();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OpenOrCreateDatabase(bool createNew)
|
private void OpenOrCreateDatabase(bool createNew)
|
||||||
{
|
{
|
||||||
_database.Open(null, createNew);
|
_database.Open(null, createNew);
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.NoCompositeKey);
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.NoCompositeKey, _database.Status);
|
||||||
var compositeKey = new CompositeKey();
|
var compositeKey = new CompositeKeyVm(_database)
|
||||||
if (!createNew)
|
|
||||||
{
|
{
|
||||||
_database.Open(compositeKey);
|
HasPassword = true,
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.CompositeKeyError);
|
Password = "test"
|
||||||
}
|
};
|
||||||
compositeKey.AddUserKey(new KcpPassword("test"));
|
compositeKey.OpenDatabase(createNew);
|
||||||
|
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Opened, _database.Status);
|
||||||
_database.Open(compositeKey, createNew);
|
|
||||||
/*var compositeKey = new CompositeKeyVm(_database);
|
|
||||||
compositeKey.OpenDatabase(createNew);*/
|
|
||||||
Assert.AreEqual(_database.Status, (int)DatabaseHelper.DatabaseStatus.Opened);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
66
ModernKeePassApp.Test/Mock/DatabaseHelperMock.cs
Normal file
66
ModernKeePassApp.Test/Mock/DatabaseHelperMock.cs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
using System;
|
||||||
|
using ModernKeePass.Interfaces;
|
||||||
|
using ModernKeePass.ViewModels;
|
||||||
|
using ModernKeePassLib;
|
||||||
|
using ModernKeePassLib.Cryptography.KeyDerivation;
|
||||||
|
using ModernKeePassLib.Keys;
|
||||||
|
using Windows.Storage;
|
||||||
|
|
||||||
|
namespace ModernKeePassApp.Test.Mock
|
||||||
|
{
|
||||||
|
public class DatabaseHelperMock : IDatabase
|
||||||
|
{
|
||||||
|
public PwCompressionAlgorithm CompressionAlgorithm { get; set; }
|
||||||
|
|
||||||
|
public StorageFile DatabaseFile { get; set; }
|
||||||
|
|
||||||
|
public PwUuid DataCipher { get; set; }
|
||||||
|
|
||||||
|
public KdfParameters KeyDerivation { get; set; }
|
||||||
|
|
||||||
|
public string Name => "MockDatabase";
|
||||||
|
|
||||||
|
public GroupVm RecycleBin { get; set; }
|
||||||
|
|
||||||
|
public bool RecycleBinEnabled { get; set; }
|
||||||
|
|
||||||
|
public GroupVm RootGroup { get; set; }
|
||||||
|
|
||||||
|
public int Status { get; set; }
|
||||||
|
|
||||||
|
public void AddDeletedItem(PwUuid id)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateRecycleBin()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Open(CompositeKey key, bool createNew)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Save()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Save(StorageFile file)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateCompositeKey(CompositeKey key)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -118,7 +118,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="DatabaseTests.cs" />
|
<Compile Include="DatabaseTests.cs" />
|
||||||
|
<Compile Include="Mock\DatabaseHelperMock.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="ViewModelsTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<AppxManifest Include="Package.appxmanifest">
|
<AppxManifest Include="Package.appxmanifest">
|
||||||
|
40
ModernKeePassApp.Test/ViewModelsTests.cs
Normal file
40
ModernKeePassApp.Test/ViewModelsTests.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using Windows.ApplicationModel;
|
||||||
|
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||||
|
using ModernKeePass.ViewModels;
|
||||||
|
using ModernKeePassApp.Test.Mock;
|
||||||
|
|
||||||
|
namespace ModernKeePassApp.Test
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ViewModelsTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void TestAboutVm()
|
||||||
|
{
|
||||||
|
var aboutVm = new AboutVm(Package.Current);
|
||||||
|
Assert.AreEqual("1.0", aboutVm.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestMainVm()
|
||||||
|
{
|
||||||
|
var database = new DatabaseHelperMock();
|
||||||
|
var mainVm = new MainVm(null, null, database);
|
||||||
|
Assert.AreEqual(1, mainVm.MainMenuItems.Count());
|
||||||
|
var firstGroup = mainVm.MainMenuItems.FirstOrDefault();
|
||||||
|
Assert.AreEqual(5, firstGroup.Count());
|
||||||
|
|
||||||
|
database.Status = 1;
|
||||||
|
mainVm = new MainVm(null, null, database);
|
||||||
|
Assert.IsNotNull(mainVm.SelectedItem);
|
||||||
|
Assert.AreEqual("Open", ((MainMenuItemVm)mainVm.SelectedItem).Title);
|
||||||
|
|
||||||
|
database.Status = 2;
|
||||||
|
mainVm = new MainVm(null, null, database);
|
||||||
|
Assert.IsNotNull(mainVm.SelectedItem);
|
||||||
|
Assert.AreEqual(2, mainVm.MainMenuItems.Count());
|
||||||
|
Assert.AreEqual("Save", ((MainMenuItemVm)mainVm.SelectedItem).Title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user