public void Version1ConfigWillBeUpdated() { XDocument xml = XDocument.Parse(Version1Settings); Mock <IXmlFileService> fileService = new Mock <IXmlFileService>(); fileService. Setup(m => m.TryLoad(It.IsAny <string>(), out xml)). Returns(true); Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>(); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.ASCII.GetBytes("abcdefgh"))))). Returns("protected_abcdefgh_v2"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v2"))). Returns(SecureStringExtensions.SecureStringToBytes(SecureStringExtensions.StringToSecureString("abcdefgh_v2"), Encoding.Unicode)); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("abcdefgh_v2"))))). Returns("protected_abcdefgh_v3"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v3"))). Returns(Encoding.UTF8.GetBytes("abcdefgh_v3")); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("martinstoeckli"))))). Returns("protected_martinstoeckli_v3"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_martinstoeckli_v3"))). Returns(Encoding.UTF8.GetBytes("martinstoeckli_v3")); SettingsServiceBase service = new TestableService(fileService.Object, dataProtectionService.Object); SettingsModel settings = service.LoadSettingsOrDefault(); Assert.AreEqual("twofish_gcm", settings.SelectedEncryptionAlgorithm); Assert.AreEqual(true, settings.AdoptCloudEncryptionAlgorithm); Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCode); Assert.AreEqual(2, settings.TransferCodeHistory.Count); Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCodeHistory[0]); Assert.AreEqual("b2bgiqeghfvn2ufx", settings.TransferCodeHistory[1]); Assert.AreEqual(SettingsModel.NewestSupportedRevision, settings.Revision); Assert.IsNotNull(settings.Credentials); Assert.AreEqual(CloudStorageClientFactory.CloudStorageIdWebdav, settings.Credentials.CloudStorageId); Assert.AreEqual("https://webdav.hidrive.strato.com/users/martinstoeckli/", settings.Credentials.Url); Assert.AreEqual("martinstoeckli_v3", settings.Credentials.Username); Assert.AreEqual("abcdefgh_v3", settings.Credentials.UnprotectedPassword); // Updated settings where saved fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Once); }
public void LoadSettingsCreatesDefaultIfNoFileFound() { XDocument xml = null; Mock <IXmlFileService> fileService = new Mock <IXmlFileService>(); fileService. Setup(m => m.TryLoad(It.IsAny <string>(), out xml)). Returns(false); Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>(); SettingsServiceBase service = new TestableService(fileService.Object, dataProtectionService.Object, CommonMocksAndStubs.EnvironmentService()); SettingsModel settings = service.LoadSettingsOrDefault(); // Created new settings and stored it Assert.IsNotNull(settings); fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Once); }
public void LoadsSettingsReturnsStoredSettings() { SettingsModel storedSettings = new SettingsModel { TransferCode = "abcdefgh" }; XDocument xml = XmlUtils.SerializeToXmlDocument(storedSettings); Mock <IXmlFileService> fileService = new Mock <IXmlFileService>(); fileService. Setup(m => m.TryLoad(It.IsAny <string>(), out xml)). Returns(true); Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>(); SettingsServiceBase service = new TestableService(fileService.Object, dataProtectionService.Object, CommonMocksAndStubs.EnvironmentService()); SettingsModel settings = service.LoadSettingsOrDefault(); // Loaded existing settings and did not store it Assert.IsNotNull(settings); Assert.AreEqual("abcdefgh", settings.TransferCode); fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Never); }