public AzureDatabaseLifecycleManagementService(
     AzureDatabaseManagementService azureDatabaseManagementService,
     SqlServerCloudOdsDatabaseSecurityConfigurator cloudOdsSqlConfigurator,
     ICloudOdsDatabaseSqlServerSecurityConfiguration cloudOdsDatabaseSqlServerSecurityConfiguration,
     IRawSqlConnectionService rawSqlConnectionService,
     ICloudOdsDatabaseNameProvider cloudOdsDatabaseNameProvider)
 {
     _azureDatabaseManagementService = azureDatabaseManagementService;
     _cloudOdsSqlConfigurator        = cloudOdsSqlConfigurator;
     _cloudOdsDatabaseSqlServerSecurityConfiguration = cloudOdsDatabaseSqlServerSecurityConfiguration;
     _rawSqlConnectionService      = rawSqlConnectionService;
     _cloudOdsDatabaseNameProvider = cloudOdsDatabaseNameProvider;
 }
        public void ShouldNotErrorOnSecondRun()
        {
            var mockRawSqlConnectionService      = new Mock <IRawSqlConnectionService>();
            var mockCloudOdsDatabaseNameProvider = new Mock <ICloudOdsDatabaseNameProvider>();
            var sqlConfigurator = new SqlServerCloudOdsDatabaseSecurityConfigurator(mockRawSqlConnectionService.Object, mockCloudOdsDatabaseNameProvider.Object);


            var config = GetOdsSqlConfiguration();

            sqlConfigurator.CreateServerLogins(config);

            var runtimeConfiguration = GetRuntimeConfiguration(config);

            sqlConfigurator.ApplyConfiguration(config, runtimeConfiguration);
        }
        public void ShouldNotLeaveAdminAppUserInDbOwnerRoleAfterRemovalOfSetupConfiguration()
        {
            var mockRawSqlConnectionService      = new Mock <IRawSqlConnectionService>();
            var mockCloudOdsDatabaseNameProvider = new Mock <ICloudOdsDatabaseNameProvider>();
            var sqlConfigurator = new SqlServerCloudOdsDatabaseSecurityConfigurator(mockRawSqlConnectionService.Object, mockCloudOdsDatabaseNameProvider.Object);

            var sqlConfig = GetOdsSqlConfiguration();
            var setupSecurityConfiguration = GetSetupConfiguration(sqlConfig);

            sqlConfigurator.RemoveConfiguration(sqlConfig, setupSecurityConfiguration);

            foreach (var expectedDatabaseSetup in _expectedSqlServerSetup.ExpectedDatabaseSetups)
            {
                using (var connection = new SqlConnection(GetConnectionStringForDatabase(expectedDatabaseSetup.DatabaseName)))
                {
                    connection.Open();

                    var sql = @"SELECT us.name AS Username, dp.name AS Role
FROM sys.sysusers us right
JOIN  sys.database_role_members rm ON us.uid = rm.member_principal_id
JOIN sys.database_principals dp ON rm.role_principal_id = dp.principal_id";

                    using (var command = new SqlCommand(sql, connection))
                    {
                        var userRolesActuallyInDatabase = new Dictionary <string, List <string> >();

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var userName = reader["Username"].ToString();
                                var role     = reader["Role"].ToString();

                                if (!userRolesActuallyInDatabase.ContainsKey(userName))
                                {
                                    userRolesActuallyInDatabase[userName] = new List <string>();
                                }

                                userRolesActuallyInDatabase[userName].Add(role);
                            }
                        }

                        userRolesActuallyInDatabase.Any(r => r.Key == sqlConfig.AdminAppCredentials.UserName && r.Value.Contains(DbOwner)).ShouldBeFalse();
                    }
                }
            }
        }
        public void Setup()
        {
            RemoveExistingLogins();

            var mockRawSqlConnectionService      = new Mock <IRawSqlConnectionService>();
            var mockCloudOdsDatabaseNameProvider = new Mock <ICloudOdsDatabaseNameProvider>();
            var _sqlConfigurator = new SqlServerCloudOdsDatabaseSecurityConfigurator(mockRawSqlConnectionService.Object, mockCloudOdsDatabaseNameProvider.Object);

            var config = GetOdsSqlConfiguration();

            _sqlConfigurator.CreateServerLogins(config);

            var setupConfiguration   = GetSetupConfiguration(config);
            var runtimeConfiguration = GetRuntimeConfiguration(config);

            _sqlConfigurator.ApplyConfiguration(config, setupConfiguration);
            _sqlConfigurator.ApplyConfiguration(config, runtimeConfiguration);
        }
        public async Task ShouldResetToMinimal()
        {
            var mockRawSqlConnectionService      = new Mock <IRawSqlConnectionService>();
            var mockCloudOdsDatabaseNameProvider = new Mock <ICloudOdsDatabaseNameProvider>();
            var mockCloudOdsDatabaseSqlServerSecurityConfiguration = new Mock <ICloudOdsDatabaseSqlServerSecurityConfiguration>();
            var dbManagementService = new AzureDatabaseManagementService(mockRawSqlConnectionService.Object);
            var dbConfigurator      = new SqlServerCloudOdsDatabaseSecurityConfigurator(mockRawSqlConnectionService.Object, mockCloudOdsDatabaseNameProvider.Object);

            mockCloudOdsDatabaseSqlServerSecurityConfiguration
            .Setup(a => a.GetSetupConfiguration(It.IsAny <OdsSqlConfiguration>()))
            .Returns(GetSetupConfiguration(GetSqlConfiguration()));

            var dbLifecycleManagementService = new AzureDatabaseLifecycleManagementService(dbManagementService, dbConfigurator, mockCloudOdsDatabaseSqlServerSecurityConfiguration.Object, mockRawSqlConnectionService.Object, mockCloudOdsDatabaseNameProvider.Object);
            var sqlConfiguration             = GetSqlConfiguration();

            var productionLifecycleManagementService = new AzureProductionLifecycleManagementService(dbLifecycleManagementService);
            var result = await productionLifecycleManagementService.ResetToMinimal(sqlConfiguration, CancellationToken.None);

            result.Error.ShouldBe(false);
        }