public void DbConfigConnectionString_WithDefaultDbTypeSetToOracle_ShouldBeCorrectConnectionString(SupportedDatabaseTypes?configDbType, string expectedConnectionString) { lock (TestCollectionFixtures.LockObj) { try { var configuration = GetConfiguration(configDbType); DefaultDbConfigValues.WithOracleDefaultDbType(); DefaultDbConfigValues.WithEmptyAdminUserAndPassword(); var dbConfig = configuration.CreateDbConfig(); dbConfig.ConnectionString.Should().Be(expectedConnectionString); var useridStr = configDbType == SupportedDatabaseTypes.Postgres ? "username" : "user id"; dbConfig.AdminUser.Should().BeNull(); dbConfig.AdminPassword.Should().BeNull(); dbConfig.AdminConnectionString.ContainsIgnoreCase($"{useridStr}=;").Should().BeTrue(); dbConfig.AdminConnectionString.ContainsIgnoreCase("password=;").Should().BeTrue(); } finally { DefaultDbConfigValues.WithLibraryDefaultDbType(); DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); } } }
public void DbConfigConnectionString_WithLibraryDefaultDbType_ShouldBeCorrectConnectionString(SupportedDatabaseTypes?configDbType, string expectedConnectionString) { lock (TestCollectionFixtures.LockObj) { try { var configuration = GetConfiguration(configDbType); configuration.CreateDbConfig().ConnectionString.Should().Be(expectedConnectionString); DefaultDbConfigValues.WithLibraryDefaultDbType(); DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); } finally { DefaultDbConfigValues.WithLibraryDefaultDbType(); DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); } } }
public void ValidateDatabaseAdminValues_NoConfiguration_ShouldValidateFailOnBothAdminUserAndAdminPassword(bool throwIfFail) { lock (TestCollectionFixtures.LockObj) { try { var provider = new ServiceCollection() .AddSingleton(GetConfiguration()) .AddDefaultDbConfig() .BuildServiceProvider(); DefaultDbConfigValues.WithEmptyAdminUserAndPassword(); var dbConfig = provider.GetRequiredService <IConfiguration>().CreateDbConfig(); var adminConnectionString = dbConfig.AdminConnectionString; if (throwIfFail) { Action validateWithThrow = () => dbConfig.ValidateAdminValues(); var exception = validateWithThrow.Should().Throw <AggregateException>().Subject.FirstOrDefault(); exception.InnerExceptions.Should().HaveCount(2); exception.InnerExceptions.All(x => x is ArgumentNullException).Should().BeTrue(); exception.IsInvalidDatabaseAdminException().Should().BeTrue(); return; } var invalidAdminValues = dbConfig.ValidateDatabaseAdminValues(throwIfFail: false); DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); invalidAdminValues.Should().HaveCount(2); var adminUser = invalidAdminValues.FirstOrDefault(x => x.IsAdminUser()); var adminPassword = invalidAdminValues.FirstOrDefault(x => x.IsAdminPassword()); adminUser.Should().NotBeNull(); adminPassword.Should().NotBeNull(); adminUser.ConfigurationKeys.Should() .Contain("'Database.Upgrade.User'") .And .Contain("'Database.AdminUser'"); adminPassword.ConfigurationKeys.Should() .Contain("'Database.Upgrade.Password'") .And .Contain("'Database.AdminPassword'"); adminUser.Value.Should().BeNullOrEmpty(); adminPassword.Value.Should().BeNullOrEmpty(); adminUser.GeneratedArgumentException.GetType().Should().Be <ArgumentNullException>(); adminPassword.GeneratedArgumentException.GetType().Should().Be <ArgumentNullException>(); adminUser.GeneratedArgumentException.ParamName.Should().Be("Database.Upgrade.User"); adminPassword.GeneratedArgumentException.ParamName.Should().Be("Database.Upgrade.Password"); var useridStr = dbConfig.DbType == SupportedDatabaseTypes.Postgres ? "username" : "user id"; adminConnectionString.ContainsIgnoreCase($"{useridStr}=;").Should().BeTrue(); adminConnectionString.ContainsIgnoreCase("password=;").Should().BeTrue(); adminUser.GeneratedArgumentException.IsInvalidDatabaseAdminException().Should().BeTrue(); adminPassword.GeneratedArgumentException.IsInvalidDatabaseAdminException().Should().BeTrue(); } finally { DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); } } }
public void ValidateDatabaseAdminValues_NullOrInvalidAdminPasswordButAdminUserSpecified_ShouldValidateFailOnAdminPasswordOnly(string adminPasswordValue, string adminPasswordKey = null, bool throwIfFail = false) { lock (TestCollectionFixtures.LockObj) { try { DefaultDbConfigValues.WithEmptyAdminUserAndPassword(); adminPasswordKey ??= "database:adminPassword"; var provider = new ServiceCollection() .AddSingleton(GetConfiguration(new Dictionary <string, string> { { "database:adminUser", "xxx" }, { adminPasswordKey, adminPasswordValue } })) .AddDefaultDbConfig() .BuildServiceProvider(); Func <InvalidAdminValue[]> validate = () => provider.GetRequiredService <IConfiguration>().CreateDbConfig().ValidateDatabaseAdminValues(throwIfFail: throwIfFail); if (throwIfFail) { ArgumentException exception; if (string.IsNullOrEmpty(adminPasswordValue)) { exception = validate.Should().Throw <ArgumentNullException>().Subject.FirstOrDefault(); exception.ParamName.Should().Be("Database.Upgrade.Password"); exception.Message.Should().Be($"All required configuration parameters ['{exception.ParamName}', 'Database.AdminPassword'] is not set. (Parameter '{exception.ParamName}')"); } else { exception = validate.Should().Throw <ArgumentException>().Subject.FirstOrDefault(); exception.Message.Should().Be($"Required configuration parameter {adminPasswordKey.ConvertConfigKeyToParamNameStyle()} has an invalid value '{adminPasswordValue}'."); } exception.IsInvalidDatabaseAdminException().Should().BeTrue(); return; } var invalidAdminValues = provider.ValidateDatabaseAdminValues(throwIfFail: false); invalidAdminValues.Should().HaveCount(1); var adminUser = invalidAdminValues.FirstOrDefault(x => x.IsAdminUser()); var adminPassword = invalidAdminValues.FirstOrDefault(x => x.IsAdminPassword()); adminUser.Should().BeNull(); adminPassword.Should().NotBeNull(); var argException = adminPassword?.GeneratedArgumentException; var confKeys = adminPassword.ConfigurationKeys; var value = adminPassword.Value; var configuredValue = adminPasswordValue; var adminKey = adminPasswordKey.ConvertConfigKeyToParamNameStyle(); if (string.IsNullOrEmpty(adminPasswordValue)) { adminPassword.ConfigurationKeys.Should() .Contain("'Database.Upgrade.Password'") .And .Contain("'Database.AdminPassword'"); value.Should().BeNullOrEmpty(); argException.GetType().Should().Be <ArgumentNullException>(); argException.ParamName.Should().Be(confKeys.FirstOrDefault().Replace("'", "")); argException.Message.Should().Be($"All required configuration parameters ['{argException.ParamName}', {confKeys.LastOrDefault()}] is not set. (Parameter '{argException.ParamName}')"); } else { confKeys.Should().HaveCount(1); confKeys.Should().Contain(adminKey); value.Should().Be(configuredValue); argException.GetType().Should().Be <ArgumentException>(); argException.Message.Should().Be($"Required configuration parameter {adminKey} has an invalid value '{configuredValue}'."); } argException.IsInvalidDatabaseAdminException().Should().BeTrue(); } finally { DefaultDbConfigValues.WithLibraryDefaultAdminUserAndPassword(); } } }