Added lots of commands

Simplified KeePass client
This commit is contained in:
Geoffroy BONNEVILLE
2020-03-26 12:25:22 +01:00
parent 4b1210f414
commit a17d6b05ae
46 changed files with 1057 additions and 246 deletions

View File

@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using AutoMapper;
using ModernKeePass.Domain.Entities;
using ModernKeePass.Infrastructure.KeePass;
using ModernKeePassLib;
using ModernKeePassLib.Security;
using NUnit.Framework;
namespace ModernKeePass.KeePassDatabaseTests
{
[TestFixture]
public class AutomapperProfilesTest
{
private IMapper _mapper;
[SetUp]
public void SetUp()
{
_mapper = new Mapper(new MapperConfiguration(conf => conf.AddProfile(new EntryMappingProfile())));
}
[Test]
public void Assert_Mapping_Configuration_Is_Valid()
{
_mapper.ConfigurationProvider.AssertConfigurationIsValid();
}
[Test]
public void FromDtoToModel_Should_Map_PwEntry_To_Entry()
{
var pwEntry = new PwEntry(true, true)
{
ExpiryTime = DateTime.Now,
BackgroundColor = Color.White,
ForegroundColor = Color.Black
};
pwEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(true, "Test"));
pwEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString(true, "toto"));
pwEntry.Strings.Set(PwDefs.PasswordField, new ProtectedString(true, "password"));
pwEntry.Strings.Set(PwDefs.UrlField, new ProtectedString(true, "http://google.com"));
pwEntry.Strings.Set(PwDefs.NotesField, new ProtectedString(true, "blabla"));
pwEntry.Strings.Set("additional", new ProtectedString(true, "custom"));
var entry = _mapper.Map<PwEntry, EntryEntity>(pwEntry);
Assert.That(entry.ExpirationDate, Is.Not.EqualTo(default(DateTimeOffset)));
Assert.That(entry.BackgroundColor, Is.EqualTo(Color.White));
Assert.That(entry.ForegroundColor, Is.EqualTo(Color.Black));
Assert.That(entry.Name, Is.EqualTo("Test"));
Assert.That(entry.UserName, Is.EqualTo("toto"));
Assert.That(entry.Password, Is.EqualTo("password"));
Assert.That(entry.Url, Is.EqualTo(new Uri("http://google.com")));
Assert.That(entry.Notes, Is.EqualTo("blabla"));
Assert.That(entry.AdditionalFields, Is.Not.Empty);
Assert.That(entry.AdditionalFields["additional"], Is.EqualTo("custom"));
}
[Test]
public void FromModelToDto_Should_Map_Entry_To_PwEntry()
{
var entry = new EntryEntity
{
Id = "VGhlIHF1aWNrIGJyb3duIA==",
Name = "Test",
UserName = "toto",
Password = "password",
Url = new Uri("http://google.com"),
Notes = "blabla",
ExpirationDate = DateTimeOffset.Now,
BackgroundColor = Color.White,
ForegroundColor = Color.Black,
AdditionalFields = new Dictionary<string, string> {
{
"additional", "custom"
}
}
};
var pwEntry = new PwEntry(false, false);
_mapper.Map(entry, pwEntry);
Assert.That(pwEntry.ExpiryTime, Is.Not.EqualTo(default(DateTime)));
Assert.That(pwEntry.BackgroundColor, Is.EqualTo(Color.White));
Assert.That(pwEntry.ForegroundColor, Is.EqualTo(Color.Black));
Assert.That(pwEntry.Strings.GetSafe(PwDefs.TitleField).ReadString(), Is.EqualTo("Test"));
Assert.That(pwEntry.Strings.GetSafe(PwDefs.UserNameField).ReadString(), Is.EqualTo("toto"));
Assert.That(pwEntry.Strings.GetSafe(PwDefs.PasswordField).ReadString(), Is.EqualTo("password"));
Assert.That(pwEntry.Strings.GetSafe(PwDefs.UrlField).ReadString(), Is.EqualTo(new Uri("http://google.com")));
Assert.That(pwEntry.Strings.GetSafe(PwDefs.NotesField).ReadString(), Is.EqualTo("blabla"));
Assert.That(pwEntry.Strings.GetSafe("additional").ReadString(), Is.EqualTo("custom"));
}
}
}

View File

@@ -0,0 +1,155 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Services;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.Domain.Entities;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Infrastructure.KeePass;
using NSubstitute;
using NUnit.Framework;
using FileInfo = ModernKeePass.Domain.Dtos.FileInfo;
namespace ModernKeePass.KeePassDatabaseTests
{
[TestFixture]
public class DatabaseTests
{
private IDatabaseProxy _database;
private FileInfo _fileInfo;
private readonly Credentials _credentials = new Credentials
{
Password = "test"
};
[SetUp]
public void SetUp()
{
var settingsService = Substitute.For<ISettingsService>();
var fileProxy = Substitute.For<IFileProxy>();
fileProxy.OpenBinaryFile(Arg.Any<string>()).Returns(async parameters =>
{
await using var stream = File.Open((string) parameters[0], FileMode.OpenOrCreate);
var contents = new byte[stream.Length];
await stream.ReadAsync(contents, 0, (int) stream.Length);
return contents;
});
fileProxy.WriteBinaryContentsToFile(Arg.Any<string>(), Arg.Any<byte[]>()).Returns(async parameters =>
{
await using var stream = File.Open((string)parameters[0], FileMode.OpenOrCreate);
var contents = (byte[]) parameters[1];
await stream.WriteAsync(contents, 0, contents.Length);
});
var fileService = new FileService(fileProxy);
var mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile(typeof(EntryMappingProfile)); }));
_database = new KeePassDatabaseClient(settingsService, fileService, mapper);
}
[TearDown]
public void TearDown()
{
_database.CloseDatabase();
if (!string.IsNullOrEmpty(_fileInfo?.Path)) File.Delete(_fileInfo.Path);
}
[Test]
public async Task TestOpen()
{
var FileInfo = new FileInfo
{
Path = Path.Combine(Directory.GetCurrentDirectory(), "Data", "TestDatabase.kdbx")
};
var rootGroup = await _database.Open(FileInfo, _credentials);
Assert.That(rootGroup.Name, Is.EqualTo("TestDatabase"));
Assert.That(rootGroup.Entries.Count(), Is.EqualTo(2));
}
[Test]
public async Task TestCreateAndSave()
{
_fileInfo = new FileInfo
{
Path = Path.Combine(Path.GetTempPath(), "NewDatabase.kdbx")
};
await _database.Create(_fileInfo, _credentials);
await _database.SaveDatabase();
_database.CloseDatabase();
Assert.DoesNotThrowAsync(async () => { await _database.Open(_fileInfo, _credentials); });
}
[Test]
public async Task TestSaveAs()
{
var originalFileInfo = new FileInfo
{
Path = Path.Combine(Directory.GetCurrentDirectory(), "Data", "TestDatabase.kdbx")
};
_fileInfo = new FileInfo
{
Path = Path.Combine(Path.GetTempPath(), "SavedDatabase.kdbx")
};
await _database.Open(originalFileInfo, _credentials);
await _database.SaveDatabase(_fileInfo);
_database.CloseDatabase();
Assert.DoesNotThrowAsync(async () => { await _database.Open(_fileInfo, _credentials); });
}
[Test]
public async Task TestAddGroup()
{
var originalFileInfo = new FileInfo
{
Path = Path.Combine(Directory.GetCurrentDirectory(), "Data", "TestDatabase.kdbx")
};
_fileInfo = new FileInfo
{
Path = Path.Combine(Path.GetTempPath(), "SavedDatabase.kdbx")
};
var newGroup = new GroupEntity {Name = "New Group Test"};
var rootGroup = await _database.Open(originalFileInfo, _credentials);
await _database.AddEntity(rootGroup, newGroup);
await _database.SaveDatabase(_fileInfo);
_database.CloseDatabase();
rootGroup = await _database.Open(_fileInfo, _credentials);
Assert.That(newGroup.Id, Is.Not.Empty);
Assert.That(rootGroup.SubGroups.Count, Is.EqualTo(7));
Assert.That(rootGroup.SubGroups.Last().Name, Is.EqualTo("New Group Test"));
}
[Test]
public async Task TestAddEntry()
{
var originalFileInfo = new FileInfo
{
Path = Path.Combine(Directory.GetCurrentDirectory(), "Data", "TestDatabase.kdbx")
};
_fileInfo = new FileInfo
{
Path = Path.Combine(Path.GetTempPath(), "SavedDatabase.kdbx")
};
var newEntry = new EntryEntity
{
Name = "New Entry Test"
};
var rootGroup = await _database.Open(originalFileInfo, _credentials);
await _database.AddEntity(rootGroup, newEntry);
await _database.SaveDatabase(_fileInfo);
_database.CloseDatabase();
rootGroup = await _database.Open(_fileInfo, _credentials);
Assert.That(newEntry.Id, Is.Not.Empty);
Assert.That(rootGroup.Entries.Count, Is.EqualTo(3));
Assert.That(rootGroup.Entries.Last().Name, Is.EqualTo("New Entry Test"));
}
}
}

View File

@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="Data\TestDatabase.kdbx" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\TestDatabase.kdbx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="ModernKeePassLib" Version="2.44.1" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Splat" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ModernKeePass.Application\Application.csproj" />
<ProjectReference Include="..\ModernKeePass.Domain\Domain.csproj" />
<ProjectReference Include="..\ModernKeePass.Infrastructure\Infrastructure.csproj" />
</ItemGroup>
</Project>