Skip to content

Commit

Permalink
Abstract settings into own class (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvanpoppel committed Apr 27, 2020
1 parent 18ce864 commit 0478b70
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 168 deletions.
2 changes: 1 addition & 1 deletion KeeTrayTOTP.Tests/Menu/LegacyTrayMenuTests.cs
Expand Up @@ -27,7 +27,7 @@ public void LegacyTrayMenuItemProvider_ShouldReturnNull()
public void LegacyTrayMenuItemProvider_ShouldAddItemsDirectlyToMainWindowsTrayContextMenu()
{
var plugin = CreatePluginHostMock(out var host);
host.Object.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_LegacyTrayMenuProvider_Enable, true);
host.Object.CustomConfig.SetBool("traymenulegacymenuprovider_enable", true);
var oldItemCount = host.Object.MainWindow.TrayContextMenu.Items.Count;

plugin.Initialize(host.Object);
Expand Down
2 changes: 1 addition & 1 deletion KeeTrayTOTP.Tests/Menu/MenuItemProviderTests.cs
Expand Up @@ -35,8 +35,8 @@ public void MenuItemProvider_ShouldDefaultReturnTheTrayMenuItemProvider()
[TestMethod]
public void MenuItemProvider_ShouldReturnTheLegacyTrayMenuItemProvider_IfSetInConfig()
{
_host.Object.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_LegacyTrayMenuProvider_Enable, true);
_plugin.Initialize(_host.Object);
_plugin.Settings.LegacyTrayMenuProviderEnable = true;

var sut = new MenuItemProvider(_plugin, _host.Object);

Expand Down
168 changes: 168 additions & 0 deletions KeeTrayTOTP.Tests/SettingsTests.cs
@@ -0,0 +1,168 @@
using FluentAssertions;
using KeePass.App.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace KeeTrayTOTP.Tests
{
[TestClass]
public class SettingsTests
{
private readonly Settings sut = new Settings(new AceCustomConfig());

[TestMethod]
public void AutoTypeEnable_GetAndSetChangeCorrectField()
{
sut.AutoTypeEnable.Should().BeTrue("because that is the default value");
sut.AutoTypeEnable = !sut.AutoTypeEnable;
sut.AutoTypeEnable.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.AutoTypeEnable.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void AutoTypeFieldName_GetAndSetChangeCorrectField()
{
sut.AutoTypeFieldName.Should().Be("TOTP", "because that is the default value");
sut.AutoTypeFieldName = "xxxx";
sut.AutoTypeFieldName.Should().Be("xxxx", "because the setting was changed to a non-default value");
sut.Reset();
sut.AutoTypeFieldName.Should().Be("TOTP", "because settings are reset");
}

[TestMethod]
public void EntryContextCopyVisible_GetAndSetChangeCorrectField()
{
sut.EntryContextCopyVisible.Should().BeTrue("because that is the default value");
sut.EntryContextCopyVisible = !sut.EntryContextCopyVisible;
sut.EntryContextCopyVisible.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.EntryContextCopyVisible.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void EntryContextSetupVisible_GetAndSetChangeCorrectField()
{
sut.EntryContextSetupVisible.Should().BeTrue("because that is the default value");
sut.EntryContextSetupVisible = !sut.EntryContextSetupVisible;
sut.EntryContextSetupVisible.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.EntryContextSetupVisible.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void EntryListRefreshRate_GetReturnsDefault()
{
sut.EntryListRefreshRate.Should().Be(300, "because that is the default value");
}

[TestMethod]
public void FirstInstallShown_GetAndSetChangeCorrectField()
{
sut.FirstInstallShown.Should().BeFalse("because that is the default value");
sut.FirstInstallShown = !sut.FirstInstallShown;
sut.FirstInstallShown.Should().BeTrue("because the setting was changed to a non-default value");
}

[TestMethod]
public void LegacyTrayMenuProviderEnable_GetAndSetChangeCorrectField()
{
sut.LegacyTrayMenuProviderEnable.Should().BeFalse("because that is the default value");
sut.LegacyTrayMenuProviderEnable = !sut.FirstInstallShown;
sut.LegacyTrayMenuProviderEnable.Should().BeTrue("because the setting was changed to a non-default value");
sut.Reset();
sut.LegacyTrayMenuProviderEnable.Should().BeFalse("because settings are reset");
}

[TestMethod]
public void NotifyContextVisible_GetAndSetChangeCorrectField()
{
sut.NotifyContextVisible.Should().BeTrue("because that is the default value");
sut.NotifyContextVisible = !sut.NotifyContextVisible;
sut.NotifyContextVisible.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.NotifyContextVisible.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void TimeCorrectionEnable_GetAndSetChangeCorrectField()
{
sut.TimeCorrectionEnable.Should().BeFalse("because that is the default value");
sut.TimeCorrectionEnable = !sut.TimeCorrectionEnable;
sut.TimeCorrectionEnable.Should().BeTrue("because the setting was changed to a non-default value");
sut.Reset();
sut.TimeCorrectionEnable.Should().BeFalse("because settings are reset");
}

[TestMethod]
public void TimeCorrectionList_GetAndSetChangeCorrectField()
{
sut.TimeCorrectionList.Should().HaveCount(0, "because that is the default value");
sut.TimeCorrectionList = new [] { "Url1", "Url2", "Url3"};
sut.TimeCorrectionList.Should().BeEquivalentTo(new[] { "Url1", "Url2", "Url3" });
}

[TestMethod]
public void TimeCorrectionRefreshTime_GetAndSetChangeCorrectField()
{
sut.TimeCorrectionRefreshTime.Should().Be(60, "because that is the default value");
sut.TimeCorrectionRefreshTime += 100;
sut.TimeCorrectionRefreshTime.Should().Be(160, "because the setting was changed to a non-default value");
sut.Reset();
sut.TimeCorrectionRefreshTime.Should().Be(60, "because settings are reset");
}

[TestMethod]
public void TOTPColumnCopyEnable_GetAndSetChangeCorrectField()
{
sut.TOTPColumnCopyEnable.Should().BeTrue("because that is the default value");
sut.TOTPColumnCopyEnable = !sut.TOTPColumnCopyEnable;
sut.TOTPColumnCopyEnable.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.TOTPColumnCopyEnable.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void TOTPColumnTimerVisible_GetAndSetChangeCorrectField()
{
sut.TOTPColumnTimerVisible.Should().BeTrue("because that is the default value");
sut.TOTPColumnTimerVisible = !sut.TOTPColumnTimerVisible;
sut.TOTPColumnTimerVisible.Should().BeFalse("because the setting was changed to a non-default value");
sut.Reset();
sut.TOTPColumnTimerVisible.Should().BeTrue("because settings are reset");
}

[TestMethod]
public void TOTPSeedStringName_GetAndSetChangeCorrectField()
{
sut.TOTPSeedStringName.Should().Be("TOTP Seed", "because that is the default value");
sut.TOTPSeedStringName = "SeedSeedSeed";
sut.TOTPSeedStringName.Should().Be("SeedSeedSeed", "because the setting was changed to a non-default value");
sut.Reset();
sut.TOTPSeedStringName.Should().Be("TOTP Seed", "because settings are reset");
}

[TestMethod]
public void TOTPSettingsStringName_GetAndSetChangeCorrectField()
{
sut.TOTPSettingsStringName.Should().Be("TOTP Settings", "because that is the default value");
sut.TOTPSettingsStringName = "SettingsSettingsSettings";
sut.TOTPSettingsStringName.Should().Be("SettingsSettingsSettings", "because the setting was changed to a non-default value");
sut.Reset();
sut.TOTPSettingsStringName.Should().Be("TOTP Settings", "because settings are reset");
}

[TestMethod]
public void TrimTextLength_GetReturnsDefault()
{
sut.TrimTextLength.Should().Be(25, "because that is the default value");
}

[TestMethod]
public void TrimTrayText_GetAndSetChangeCorrectField()
{
sut.TrimTrayText.Should().BeFalse("because that is the default value");
sut.TrimTrayText = !sut.TrimTrayText;
sut.TrimTrayText.Should().BeTrue("because the setting was changed to a non-default value");
}
}
}
31 changes: 12 additions & 19 deletions KeeTrayTOTP.Tests/TrayTOTP_ColumnproviderTests.cs
Expand Up @@ -14,15 +14,14 @@ namespace KeeTrayTOTP.Tests
public class TrayTOTP_ColumnproviderTests : IDisposable
{
private readonly KeeTrayTOTPExt _plugin;
private readonly IPluginHost _pluginHost;

const string InvalidSeed = "C5CYMIHWQUUZMKUGZHGEOSJSQDE4L===!";
const string ValidSeed = "JBSWY3DPEHPK3PXP";
const string ValidSettings = "30;6";

public TrayTOTP_ColumnproviderTests()
{
(_plugin, _pluginHost) = CreateInitializedPlugin();
(_plugin, _) = CreateInitializedPlugin();
}

[DataRow(ValidSeed, ValidSettings, "TOTP Enabled")]
Expand All @@ -39,13 +38,11 @@ public void GetCellDataStatus_ShouldReturnExpectedValues(string seed, string set
var pwEntry = new KeePassLib.PwEntry(true, true);
if (seed != null)
{
var seedKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName, Localization.Strings.TOTPSeed);
pwEntry.Strings.Set(seedKey, new ProtectedString(false, seed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, seed));
}
if (settings != null)
{
var settingsKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName, Localization.Strings.TOTPSettings);
pwEntry.Strings.Set(settingsKey, new ProtectedString(false, settings));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, settings));
}

var actual = column.GetCellData("TOTP Status", pwEntry);
Expand All @@ -66,13 +63,12 @@ public void GetCellDataCode_ShouldReturnExpectedValues(string seed, string setti
var pwEntry = new KeePassLib.PwEntry(true, true);
if (seed != null)
{
var seedKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName, Localization.Strings.TOTPSeed);
pwEntry.Strings.Set(seedKey, new ProtectedString(false, seed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, seed));
}
if (settings != null)
{
var settingsKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName, Localization.Strings.TOTPSettings);
pwEntry.Strings.Set(settingsKey, new ProtectedString(false, settings));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, settings));

}

var actual = column.GetCellData("TOTP", pwEntry);
Expand All @@ -85,14 +81,12 @@ public void GetCellDataCode_ShouldReturnExpectedValues(string seed, string setti
[DataTestMethod]
public void GetCellDataCode_WithValidSeedAndSettings_ShouldReturnA6DigitCodeWithDuration(bool showTimer, string regex)
{
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_TOTPColumnTimer_Visible, showTimer);
_plugin.Settings.TOTPColumnTimerVisible = showTimer;

var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
var seedKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName, Localization.Strings.TOTPSeed);
pwEntry.Strings.Set(seedKey, new ProtectedString(false, ValidSeed));
var settingsKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName, Localization.Strings.TOTPSettings);
pwEntry.Strings.Set(settingsKey, new ProtectedString(false, ValidSettings));
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, ValidSeed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, ValidSettings));

var actual = column.GetCellData("TOTP", pwEntry);

Expand All @@ -104,10 +98,9 @@ public void GetCellData_WithAnInvalidColumn_ShouldReturnEmptyString()
{
var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
var seedKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName, Localization.Strings.TOTPSeed);
pwEntry.Strings.Set(seedKey, new ProtectedString(false, ValidSeed));
var settingsKey = _pluginHost.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName, Localization.Strings.TOTPSettings);
pwEntry.Strings.Set(settingsKey, new ProtectedString(false, ValidSettings));

pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, ValidSeed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, ValidSettings));

var actual = column.GetCellData("InvalidColumnName", pwEntry);

Expand Down

0 comments on commit 0478b70

Please sign in to comment.