Adding tests for App

WIP make VMs app agnostic
This commit is contained in:
BONNEVILLE Geoffroy
2017-11-22 18:54:03 +01:00
parent f2d97b4e7e
commit 5120c8177b
19 changed files with 467 additions and 40 deletions

View File

@@ -1,6 +1,7 @@
using System;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Interfaces;
using ModernKeePass.ViewModels;
using ModernKeePassLib;
using ModernKeePassLib.Cryptography.KeyDerivation;
@@ -10,7 +11,7 @@ using ModernKeePassLib.Serialization;
namespace ModernKeePass.Common
{
public class DatabaseHelper
public class DatabaseHelper: IDatabase
{
public enum DatabaseStatus
{
@@ -37,7 +38,7 @@ namespace ModernKeePass.Common
}
}
public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed;
public int Status { get; set; } = (int)DatabaseStatus.Closed;
public string Name => DatabaseFile?.Name;
public bool RecycleBinEnabled
@@ -52,7 +53,7 @@ namespace ModernKeePass.Common
set
{
_databaseFile = value;
Status = DatabaseStatus.Opening;
Status = (int)DatabaseStatus.Opening;
}
}
@@ -84,24 +85,26 @@ namespace ModernKeePass.Common
{
try
{
if (key == null) Status = DatabaseStatus.NoCompositeKey;
if (key == null)
{
Status = (int)DatabaseStatus.NoCompositeKey;
return;
}
var ioConnection = IOConnectionInfo.FromFile(DatabaseFile);
if (createNew) _pwDatabase.New(ioConnection, key);
else _pwDatabase.Open(ioConnection, key, new NullStatusLogger());
if (_pwDatabase.IsOpen)
{
Status = DatabaseStatus.Opened;
RootGroup = new GroupVm(_pwDatabase.RootGroup, null, RecycleBinEnabled ? _pwDatabase.RecycleBinUuid : null);
}
if (!_pwDatabase.IsOpen) return;
Status = (int)DatabaseStatus.Opened;
RootGroup = new GroupVm(_pwDatabase.RootGroup, null, RecycleBinEnabled ? _pwDatabase.RecycleBinUuid : null);
}
catch (InvalidCompositeKeyException)
{
Status = DatabaseStatus.CompositeKeyError;
Status = (int)DatabaseStatus.CompositeKeyError;
}
catch (Exception ex)
{
Status = DatabaseStatus.Error;
Status = (int)DatabaseStatus.Error;
}
}
@@ -109,11 +112,19 @@ namespace ModernKeePass.Common
/// Save the current database to another file and open it
/// </summary>
/// <param name="file">The new database file</param>
internal void Save(StorageFile file)
public bool Save(StorageFile file)
{
DatabaseFile = file;
_pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger());
Status = DatabaseStatus.Opened;
try
{
_pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger());
Status = (int)DatabaseStatus.Opened;
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
@@ -129,9 +140,10 @@ namespace ModernKeePass.Common
}
catch (Exception ex)
{
return false;
// TODO: put this at a better place (e.g. in views)
MessageDialogHelper.ShowErrorDialog(ex);
}
return false;
}
/// <summary>
@@ -140,7 +152,7 @@ namespace ModernKeePass.Common
public void Close()
{
_pwDatabase?.Close();
Status = DatabaseStatus.Closed;
Status = (int)DatabaseStatus.Closed;
}
public void AddDeletedItem(PwUuid id)
@@ -150,7 +162,7 @@ namespace ModernKeePass.Common
public void CreateRecycleBin()
{
RecycleBin = RootGroup.AddNewGroup("Recycle bin");
RecycleBin = ((GroupVm)RootGroup).AddNewGroup("Recycle bin");
RecycleBin.IsSelected = true;
RecycleBin.IconSymbol = Symbol.Delete;
}

View File

@@ -0,0 +1,20 @@
using ModernKeePass.ViewModels;
using ModernKeePassLib;
using ModernKeePassLib.Keys;
namespace ModernKeePass.Interfaces
{
public interface IDatabase
{
bool RecycleBinEnabled { get; set; }
int Status { get; set; }
GroupVm RootGroup { get; set; }
GroupVm RecycleBin { get; set; }
void Open(CompositeKey key, bool createNew);
void UpdateCompositeKey(CompositeKey key);
bool Save();
void CreateRecycleBin();
void AddDeletedItem(PwUuid id);
}
}

View File

@@ -123,6 +123,7 @@
<Compile Include="Common\ToastNotificationHelper.cs" />
<Compile Include="Converters\DiscreteIntToSolidColorBrushConverter.cs" />
<Compile Include="Converters\NullToBooleanConverter.cs" />
<Compile Include="Interfaces\IDatabase.cs" />
<Compile Include="Interfaces\IHasSelectableObject.cs" />
<Compile Include="Interfaces\ISelectableModel.cs" />
<Compile Include="Pages\BasePages\LayoutAwarePageBase.cs" />

View File

@@ -2,6 +2,7 @@
using Windows.Storage;
using Windows.UI.Xaml;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
@@ -18,7 +19,7 @@ namespace ModernKeePass.ViewModels
Success = 5
}
private readonly App _app = Application.Current as App;
//private readonly App _app = Application.Current as App;
private bool _hasPassword;
private bool _hasKeyFile;
private string _password = string.Empty;
@@ -27,6 +28,8 @@ namespace ModernKeePass.ViewModels
private StorageFile _keyFile;
private string _keyFileText = "Select key file from disk...";
public IDatabase Database { get; set; }
public bool HasPassword
{
get { return _hasPassword; }
@@ -90,15 +93,23 @@ namespace ModernKeePass.ViewModels
}
public GroupVm RootGroup { get; set; }
public double PasswordComplexityIndicator => QualityEstimation.EstimatePasswordBits(Password?.ToCharArray());
public CompositeKeyVm() : this((Application.Current as App)?.Database) { }
public CompositeKeyVm(IDatabase database)
{
Database = database;
}
public bool OpenDatabase(bool createNew)
{
_app.Database.Open(CreateCompositeKey(), createNew);
switch (_app.Database.Status)
Database.Open(CreateCompositeKey(), createNew);
switch ((DatabaseHelper.DatabaseStatus)Database.Status)
{
case DatabaseHelper.DatabaseStatus.Opened:
RootGroup = _app.Database.RootGroup;
RootGroup = Database.RootGroup;
return true;
case DatabaseHelper.DatabaseStatus.CompositeKeyError:
var errorMessage = new StringBuilder("Error: wrong ");
@@ -113,7 +124,7 @@ namespace ModernKeePass.ViewModels
public void UpdateKey()
{
_app.Database.UpdateCompositeKey(CreateCompositeKey());
Database.UpdateCompositeKey(CreateCompositeKey());
UpdateStatus("Database composite key updated.", StatusTypes.Success);
}

View File

@@ -14,6 +14,7 @@ namespace ModernKeePass.ViewModels
{
public class EntryVm : INotifyPropertyChanged, IPwEntity
{
public IDatabase Database { get; set; }
public GroupVm ParentGroup { get; private set; }
public GroupVm PreviousGroup { get; private set; }
@@ -150,7 +151,6 @@ namespace ModernKeePass.ViewModels
public event PropertyChangedEventHandler PropertyChanged;
private readonly PwEntry _pwEntry;
private readonly App _app = Application.Current as App;
private bool _isEditMode;
private bool _isRevealPassword;
private double _passwordLength = 25;
@@ -160,16 +160,20 @@ namespace ModernKeePass.ViewModels
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public EntryVm() { }
public EntryVm(PwEntry entry, GroupVm parent)
public EntryVm() : this(null, null) { }
internal EntryVm(PwEntry entry, GroupVm parent) : this(entry, parent, (Application.Current as App)?.Database) { }
public EntryVm(PwEntry entry, GroupVm parent, IDatabase database)
{
Database = database;
_pwEntry = entry;
ParentGroup = parent;
}
public void GeneratePassword()
{
var pwProfile = new PwProfile()
var pwProfile = new PwProfile
{
GeneratorType = PasswordGeneratorType.CharSet,
Length = (uint)PasswordLength,
@@ -208,9 +212,9 @@ namespace ModernKeePass.ViewModels
public void MarkForDelete()
{
if (_app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.IdUuid == null)
_app.Database.CreateRecycleBin();
Move(_app.Database.RecycleBinEnabled && !ParentGroup.IsSelected ? _app.Database.RecycleBin : null);
if (Database.RecycleBinEnabled && Database.RecycleBin?.IdUuid == null)
Database.CreateRecycleBin();
Move(Database.RecycleBinEnabled && !ParentGroup.IsSelected ? Database.RecycleBin : null);
}
public void UndoDelete()
@@ -225,7 +229,7 @@ namespace ModernKeePass.ViewModels
PreviousGroup.RemovePwEntry(_pwEntry);
if (destination == null)
{
_app.Database.AddDeletedItem(IdUuid);
Database.AddDeletedItem(IdUuid);
return;
}
ParentGroup = destination;
@@ -236,13 +240,13 @@ namespace ModernKeePass.ViewModels
public void CommitDelete()
{
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
if (_app.Database.RecycleBinEnabled && !PreviousGroup.IsSelected) _app.Database.RecycleBin.AddPwEntry(_pwEntry);
else _app.Database.AddDeletedItem(IdUuid);
if (Database.RecycleBinEnabled && !PreviousGroup.IsSelected) Database.RecycleBin.AddPwEntry(_pwEntry);
else Database.AddDeletedItem(IdUuid);
}
public void Save()
{
_app.Database.Save();
Database.Save();
}
}
}

View File

@@ -49,14 +49,14 @@ namespace ModernKeePass.ViewModels
{
var app = (App)Application.Current;
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var isDatabaseOpen = app.Database != null && app.Database.Status == DatabaseHelper.DatabaseStatus.Opened;
var isDatabaseOpen = app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened;
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
{
new MainMenuItemVm
{
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
IsSelected = app.Database.Status == DatabaseHelper.DatabaseStatus.Opening
IsSelected = app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening
},
new MainMenuItemVm
{
@@ -69,7 +69,7 @@ namespace ModernKeePass.ViewModels
},
new MainMenuItemVm {
Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Copy,
IsSelected = (app.Database == null || app.Database.Status == DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
IsSelected = (app.Database == null || app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
},
new MainMenuItemVm
{
@@ -78,7 +78,7 @@ namespace ModernKeePass.ViewModels
};
// Auto-select the Recent Items menu item if the conditions are met
SelectedItem = mainMenuItems.FirstOrDefault(m => m.IsSelected);
if (app.Database != null && app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
if (app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened)
mainMenuItems.Add(new MainMenuItemVm
{
Title = app.Database.Name,

View File

@@ -10,7 +10,7 @@ namespace ModernKeePass.ViewModels
{
public bool ShowPasswordBox
{
get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; }
get { return ((App)Application.Current).Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening; }
}
public string Name
@@ -21,7 +21,7 @@ namespace ModernKeePass.ViewModels
public OpenVm()
{
var app = Application.Current as App;
if (app?.Database == null || app.Database.Status != DatabaseHelper.DatabaseStatus.Opening) return;
if (app?.Database == null || app.Database.Status != (int) DatabaseHelper.DatabaseStatus.Opening) return;
OpenFile(app.Database.DatabaseFile);
}