public void SaveSettings_PreexistsAndWeSave_MovesCredentials([Values(true, false)] bool isResumable) { ServerSettingsModel.PasswordForSession = Settings.Default.LanguageForgePass = Settings.Default.LanguageForgeUser = null; using (var folder = new TemporaryFolder("ServerSettingsModel")) { const string user = "******"; const string pass = "******"; var subdomain = isResumable ? "resumable" : "hg-public"; var oldHost = $"{subdomain}.languagedepot.org/tpi"; var oldUrl = $"https://{user}:{pass}@{oldHost}"; const string newDomainAndProj = ".languageforge.org/tpi"; var newUrl = $"https://{subdomain}{newDomainAndProj}"; var newUrlWithCredentials = $"https://{user}:{pass}@{subdomain}{newDomainAndProj}"; // Precondition is some url that is not our default from the ServerSettingsModel var original = HgRepository.CreateOrUseExisting(folder.Path, new NullProgress()); original.SetKnownRepositoryAddresses(new[] { new HttpRepositoryPath("languageForge.org [legacy sync]", oldUrl, false) }); var m = new ServerSettingsModel(); m.InitFromProjectPath(folder.Path); m.SaveSettings(); Assert.AreEqual(user, Settings.Default.LanguageForgeUser); Assert.AreEqual(pass, ServerSettingsModel.DecryptPassword(Settings.Default.LanguageForgePass)); Assert.That(folder.Combine(".hg"), Does.Exist); Assert.That(folder.Combine(".hg", "hgrc"), Does.Exist); var repo = HgRepository.CreateOrUseExisting(folder.Path, new NullProgress()); var address = repo.GetDefaultNetworkAddress <HttpRepositoryPath>(); Assert.AreEqual(newUrl, address.URI); Assert.AreEqual(isResumable ? newUrl : newUrlWithCredentials, address.GetPotentialRepoUri(null, null, null), "The new 'potential' URI should contain credentials only when non-resumable"); } }
public void EncryptPassword_RoundTrips() { const string password = "******"; var encrypted = ServerSettingsModel.EncryptPassword(password); var decrypted = ServerSettingsModel.DecryptPassword(encrypted); Assert.AreEqual(password, decrypted); Assert.AreNotEqual(password, encrypted); }
public void RemoveCredentialsFromIniIfNecessary_PreservesOtherData(string savedUser, string savedPass, string newSavedPass) { const string iniUsername = "******"; const string urlEnd = "hg-private.languageforge.org/auc-flex"; const string oldUrl = "https://" + "dinah:shibboleth@" + urlEnd; const string newUrl = "https://" + urlEnd; const string networkDirName = "Old Network Share"; const string networkDirRepo = "//chorus-box/projects/Wao"; Chorus.Properties.Settings.Default.LanguageForgeUser = savedUser; Chorus.Properties.Settings.Default.LanguageForgePass = ServerSettingsModel.EncryptPassword(savedPass); using (var tempDir = new TemporaryFolder("HgRepoURI")) { var repo = new HgRepository(tempDir.Path, _progress); var iniPath = repo.GetPathToHgrc(); Directory.CreateDirectory(Path.GetDirectoryName(iniPath)); var ini = repo.GetMercurialConfigForRepository(); ini.Sections.GetOrCreate("ui").Set("username", iniUsername); var paths = ini.Sections.GetOrCreate("paths"); paths.Set(networkDirName, networkDirRepo); paths.Set("LanguageForge", oldUrl); ini.Save(); // SUT repo.RemoveCredentialsFromIniIfNecessary(); ini = repo.GetMercurialConfigForRepository(); Assert.AreEqual(iniUsername, ini.Sections["ui"].GetValue("username"), "Other data should be preserved"); var newPaths = ini.Sections["paths"]; Assert.AreEqual(2, newPaths.ItemCount); Assert.AreEqual(networkDirRepo, newPaths.GetValue(networkDirName)); var actualUrl = newPaths.GetValue(newPaths.GetKeys().First(k => !k.Equals(networkDirName))); Assert.AreEqual(newUrl, actualUrl); Assert.AreEqual("dinah", Chorus.Properties.Settings.Default.LanguageForgeUser, "username is always saved"); Assert.AreEqual("shibboleth", ServerSettingsModel.PasswordForSession, "should have read password from file"); if (string.IsNullOrEmpty(newSavedPass)) { Assert.That(Chorus.Properties.Settings.Default.LanguageForgePass, Is.Null.Or.Empty); } else { Assert.That(ServerSettingsModel.DecryptPassword(Chorus.Properties.Settings.Default.LanguageForgePass), Is.EqualTo(newSavedPass), "should have saved the password to user settings"); } } }
public void DecryptPassword_NullAndEmptyDoNotCrash() { Assert.DoesNotThrow(() => ServerSettingsModel.DecryptPassword(null)); Assert.DoesNotThrow(() => ServerSettingsModel.DecryptPassword(string.Empty)); }