public void TestInstanceCreationGoodConnectionString() { const string connStr = "Server=(localdb)\\MSSQLLocalDB;Database=moonglade-dev;Trusted_Connection=True;"; var helper = new SetupRunner(connStr); Assert.IsTrue(helper.DatabaseConnectionString == connStr); }
private static void TryInitFirstRun(IDbConnection dbConnection, ILogger logger) { var setupHelper = new SetupRunner(dbConnection); if (!setupHelper.TestDatabaseConnection(ex => { Trace.WriteLine(ex); Console.WriteLine(ex); })) { return; } if (!setupHelper.IsFirstRun()) { return; } try { logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); logger.LogInformation("Database setup successfully."); } catch (Exception e) { logger.LogCritical(e, e.Message); } }
public void IsFirstRun_No() { _mockDbConnection.SetupDapper(c => c.ExecuteScalar <int>(It.IsAny <string>(), null, null, null, null)).Returns(1); var setupHelper = new SetupRunner(_mockDbConnection.Object); var result = setupHelper.IsFirstRun(); Assert.IsFalse(result); }
public void TestDatabaseConnection_OK() { _mockDbConnection.SetupDapper(c => c.ExecuteScalar <int>(It.IsAny <string>(), null, null, null, null)).Returns(1); var setupHelper = new SetupRunner(_mockDbConnection.Object); var result = setupHelper.TestDatabaseConnection(); Assert.AreEqual(true, result); }
public void InitFirstRun_OK() { _mockDbConnection.SetupDapper(c => c.Execute(It.IsAny <string>(), null, null, null, null)).Returns(251); var setupHelper = new SetupRunner(_mockDbConnection.Object); Assert.DoesNotThrow(() => { setupHelper.InitFirstRun(); }); }
public void SetupDatabase_OK() { _mockDbConnection.SetupDapper(c => c.Execute(It.IsAny <string>(), null, null, null, null)).Returns(996); var setupHelper = new SetupRunner(_mockDbConnection.Object); Assert.DoesNotThrow(() => { setupHelper.SetupDatabase(); }); }
public void TestDatabaseConnection_Exception_NoLogger() { _mockDbConnection.SetupDapper(c => c.ExecuteScalar <int>(It.IsAny <string>(), null, null, null, null)) .Throws(new DataException("996")); var setupHelper = new SetupRunner(_mockDbConnection.Object); var result = setupHelper.TestDatabaseConnection(); Assert.AreEqual(false, result); }
public void ResetDefaultConfiguration_OK() { mockDbConnection.SetupDapper(c => c.Execute(It.IsAny <string>(), null, null, null, null)).Returns(251); var setupHelper = new SetupRunner(mockDbConnection.Object); Assert.DoesNotThrow(() => { setupHelper.ResetDefaultConfiguration(); }); }
public void ClearData_OK() { mockDbConnection.SetupDapper(c => c.Execute(It.IsAny <string>(), null, null, null, null)).Returns(251); var setupHelper = new SetupRunner(mockDbConnection.Object); Assert.DoesNotThrow(() => { setupHelper.ClearData(); }); }
public async Task <IActionResult> Reset([FromServices] IDbConnection dbConnection, [FromServices] IHostApplicationLifetime applicationLifetime) { _logger.LogWarning($"System reset is requested by '{User.Identity?.Name}', IP: {HttpContext.Connection.RemoteIpAddress}."); var setupHelper = new SetupRunner(dbConnection); setupHelper.ClearData(); applicationLifetime.StopApplication(); return(Accepted()); }
public async Task Invoke(HttpContext httpContext, IConfiguration configuration, IHostApplicationLifetime appLifetime, ILogger <FirstRunMiddleware> logger) { var initFlag = AppDomain.CurrentDomain.GetData(Token); if (null != initFlag) { // Don't need to check bool true or false, exists means everything await _next(httpContext); return; } var conn = configuration.GetConnectionString(Constants.DbConnectionName); var setupHelper = new SetupRunner(conn); if (!setupHelper.TestDatabaseConnection(exception => { logger.LogCritical(exception, $"Error {nameof(SetupRunner.TestDatabaseConnection)}, connection string: {conn}"); })) { httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; await httpContext.Response.WriteAsync("Database connection failed. Please see error log, fix it and RESTART this application."); appLifetime.StopApplication(); } else { if (setupHelper.IsFirstRun()) { try { logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); logger.LogInformation("Database setup successfully."); } catch (Exception e) { logger.LogCritical(e, e.Message); httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; await httpContext.Response.WriteAsync("Error initializing first run, please check error log."); appLifetime.StopApplication(); } } AppDomain.CurrentDomain.SetData(Token, true); await _next(httpContext); } }
public async Task <IActionResult> Reset([FromServices] IDbConnection dbConnection, [FromServices] IHostApplicationLifetime applicationLifetime) { _logger.LogWarning($"System reset is requested by '{User.Identity.Name}', IP: {HttpContext.Connection.RemoteIpAddress}."); var setupHelper = new SetupRunner(dbConnection); setupHelper.ClearData(); await _blogAudit.AddAuditEntry(EventType.Settings, AuditEventId.SettingsSavedAdvanced, "System reset."); applicationLifetime.StopApplication(); return(Accepted()); }
public static async Task InitStartUp(this WebApplication app) { using var scope = app.Services.CreateScope(); var services = scope.ServiceProvider; var env = services.GetRequiredService <IWebHostEnvironment>(); var dbConnection = services.GetRequiredService <IDbConnection>(); var setupHelper = new SetupRunner(dbConnection); try { if (!setupHelper.TestDatabaseConnection()) { return; } } catch (Exception e) { app.Logger.LogCritical(e, e.Message); return; } if (setupHelper.IsFirstRun()) { try { app.Logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); app.Logger.LogInformation("Database setup successfully."); } catch (Exception e) { app.Logger.LogCritical(e, e.Message); } } try { var mediator = services.GetRequiredService <IMediator>(); var iconData = await mediator.Send(new GetAssetDataQuery(AssetId.SiteIconBase64)); MemoryStreamIconGenerator.GenerateIcons(iconData, env.WebRootPath, app.Logger); } catch (Exception e) { // Non critical error, just log, do not block application start app.Logger.LogError(e, e.Message); } }
public void TestInstanceCreationInvalidConnectionString() { Assert.Throws(typeof(ArgumentNullException), () => { var helper = new SetupRunner(null); }); Assert.Throws(typeof(ArgumentNullException), () => { var helper = new SetupRunner(string.Empty); }); Assert.Throws(typeof(ArgumentNullException), () => { var helper = new SetupRunner(" "); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); //Should fix Coors problem. services.Configure <IISOptions>(options => { options.ForwardClientCertificate = false; }); //Adds services for the application. SetupRunner.Run(new IdentitySetup(services, Configuration)); SetupRunner.Run(new ApplicationSetup(services, Configuration)); SetupRunner.Run(new AuthenticationSetup(services, Configuration)); SetupRunner.Run(new MVCSetup(services, Configuration)); SetupRunner.Run(new MediatRSetup(services, Configuration)); SetupRunner.Run(new SwaggerSetup(services, Configuration)); SetupRunner.Run(new ServiceWorkersSetup(services, Configuration)); }