From 71dfb81d43de00e6ba1605c7d28d61cf736edee7 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Fri, 5 Oct 2018 10:58:52 -0400 Subject: [PATCH] Finish setup wizard tests, fix a few bugs --- src/Tgstation.Server.Host/Core/SetupWizard.cs | 25 +++--- .../Core/TestSetupWizard.cs | 80 +++++++++++++------ 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/src/Tgstation.Server.Host/Core/SetupWizard.cs b/src/Tgstation.Server.Host/Core/SetupWizard.cs index 02ebbb7b55..6c54f7ebbd 100644 --- a/src/Tgstation.Server.Host/Core/SetupWizard.cs +++ b/src/Tgstation.Server.Host/Core/SetupWizard.cs @@ -155,21 +155,30 @@ namespace Tgstation.Server.Host.Core await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); await console.WriteAsync("Enter the database name (Can be from previous installation. Otherwise, should not exist): ", false, cancellationToken).ConfigureAwait(false); - var databaseName = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); + string databaseName; + + do + { + databaseName = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); + if (!String.IsNullOrWhiteSpace(databaseName)) + break; + await console.WriteAsync("Invalid database name!", true, cancellationToken).ConfigureAwait(false); + } + while (true); var dbExists = await PromptYesNo("Does this database already exist? (y/n): ", cancellationToken).ConfigureAwait(false); - bool? useWinAuth; + bool useWinAuth; if (databaseConfiguration.DatabaseType == DatabaseType.SqlServer && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) useWinAuth = await PromptYesNo("Use Windows Authentication? (y/n): ", cancellationToken).ConfigureAwait(false); else - useWinAuth = null; + useWinAuth = false; await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); string username = null; string password = null; - if (useWinAuth != true) + if (!useWinAuth) { await console.WriteAsync("Enter username: ", false, cancellationToken).ConfigureAwait(false); username = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); @@ -192,7 +201,7 @@ namespace Tgstation.Server.Host.Core ApplicationName = application.VersionPrefix, DataSource = serverAddress ?? "(local)" }; - if (useWinAuth.Value) + if (useWinAuth) csb.IntegratedSecurity = true; else { @@ -421,14 +430,12 @@ namespace Tgstation.Server.Host.Core } var userConfigFileName = String.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", hostingEnvironment.EnvironmentName); - var existenceTask = ioManager.FileExists(userConfigFileName, default); - var exists = existenceTask.GetAwaiter().GetResult(); + var exists = await ioManager.FileExists(userConfigFileName, cancellationToken).ConfigureAwait(false); bool shouldRunBasedOnAutodetect; if (exists) { - var readTask = ioManager.ReadAllBytes(userConfigFileName, default); - var bytes = readTask.GetAwaiter().GetResult(); + var bytes = await ioManager.ReadAllBytes(userConfigFileName, cancellationToken).ConfigureAwait(false); var contents = Encoding.UTF8.GetString(bytes); var existingConfigIsEmpty = String.IsNullOrWhiteSpace(contents); logger.LogTrace("Configuration json detected. Empty: {0}", existingConfigIsEmpty); diff --git a/tests/Tgstation.Server.Host.Tests/Core/TestSetupWizard.cs b/tests/Tgstation.Server.Host.Tests/Core/TestSetupWizard.cs index 2db14803d4..61d9f29a53 100644 --- a/tests/Tgstation.Server.Host.Tests/Core/TestSetupWizard.cs +++ b/tests/Tgstation.Server.Host.Tests/Core/TestSetupWizard.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using Moq.Protected; using System; using System.Collections.Generic; using System.Data.Common; @@ -35,13 +36,10 @@ namespace Tgstation.Server.Host.Core.Tests var mockLogger = new Mock>(); Assert.ThrowsException(() => new SetupWizard(mockIOManager.Object, mockConsole.Object, mockHostingEnvironment.Object, mockApplication.Object, mockDBConnectionFactory.Object, mockLogger.Object, null)); } - - //TODO + [TestMethod] - public async Task WIPTestWithUserStupiditiy() + public async Task TestWithUserStupiditiy() { - Assert.Inconclusive(); - var mockIOManager = new Mock(); var mockConsole = new Mock(); var mockHostingEnvironment = new Mock(); @@ -64,38 +62,39 @@ namespace Tgstation.Server.Host.Core.Tests await Assert.ThrowsExceptionAsync(() => wizard.CheckRunWizard(default)).ConfigureAwait(false); testGeneralConfig.SetupWizardMode = SetupWizardMode.Only; - mockConsole.SetupGet(x => x.Available).Returns(true).Verifiable(); - Assert.IsFalse(await wizard.CheckRunWizard(default).ConfigureAwait(false)); + await Assert.ThrowsExceptionAsync(() => wizard.CheckRunWizard(default)).ConfigureAwait(false); + mockConsole.SetupGet(x => x.Available).Returns(true).Verifiable(); mockIOManager.Setup(x => x.FileExists(It.IsNotNull(), It.IsAny())).Returns(Task.FromResult(true)).Verifiable(); mockIOManager.Setup(x => x.ReadAllBytes(It.IsNotNull(), It.IsAny())).Returns(Task.FromResult(Encoding.UTF8.GetBytes("cucked"))).Verifiable(); mockIOManager.Setup(x => x.WriteAllBytes(It.IsNotNull(), It.IsNotNull(), It.IsAny())).Returns(Task.CompletedTask).Verifiable(); - var mockGoodDbConnection = new Mock(); - mockGoodDbConnection.Setup(x => x.OpenAsync(It.IsAny())).Returns(Task.CompletedTask).Verifiable(); - - var mockBadDbConnection = new Mock(); - mockGoodDbConnection.Setup(x => x.OpenAsync(It.IsAny())).Throws(new Exception()).Verifiable(); - - void AddVersionReturn(Mock mock) => mock.Setup(x => x.ExecuteScalarAsync(It.IsAny())).Returns(Task.FromResult("1.2.3")).Verifiable(); var mockSuccessCommand = new Mock(); mockSuccessCommand.Setup(x => x.ExecuteNonQueryAsync(It.IsAny())).Returns(Task.FromResult(0)).Verifiable(); - AddVersionReturn(mockSuccessCommand); + mockSuccessCommand.Setup(x => x.ExecuteScalarAsync(It.IsAny())).Returns(Task.FromResult("1.2.3")).Verifiable(); var mockFailCommand = new Mock(); mockFailCommand.Setup(x => x.ExecuteNonQueryAsync(It.IsAny())).Throws(new Exception()).Verifiable(); - AddVersionReturn(mockFailCommand); - var secondTime = false; + + void SetDbCommandCreator(Mock mock, Func creator) => mock.Protected().Setup("CreateDbCommand").Returns(creator).Verifiable(); + + var mockGoodDbConnection = new Mock(); + mockGoodDbConnection.Setup(x => x.OpenAsync(It.IsAny())).Returns(Task.CompletedTask).Verifiable(); + SetDbCommandCreator(mockGoodDbConnection, () => mockSuccessCommand.Object); + + var mockBadDbConnection = new Mock(); + mockBadDbConnection.Setup(x => x.OpenAsync(It.IsAny())).Throws(new Exception()).Verifiable(); + var invokeTimes = 0; var mockUglyDbConnection = new Mock(); - mockUglyDbConnection.Setup(x => x.CreateCommand()).Returns(() => + SetDbCommandCreator(mockUglyDbConnection, () => { - if (!secondTime) + if (invokeTimes < 2) { - secondTime = true; + ++invokeTimes; return mockSuccessCommand.Object; } else return mockFailCommand.Object; - }).Verifiable(); + }); mockDBConnectionFactory.Setup(x => x.CreateConnection(It.IsAny(), DatabaseType.SqlServer)).Returns(mockBadDbConnection.Object).Verifiable(); mockDBConnectionFactory.Setup(x => x.CreateConnection(It.IsAny(), DatabaseType.MariaDB)).Returns(mockGoodDbConnection.Object).Verifiable(); @@ -132,6 +131,7 @@ namespace Tgstation.Server.Host.Core.Tests nameof(DatabaseType.MariaDB), "bleh", "blah", + "NO", "user", "pass", //general config @@ -144,21 +144,47 @@ namespace Tgstation.Server.Host.Core.Tests "fake token", //saved, now for second run //this time use defaults amap - - //TODO + String.Empty, + //test MySQL errors + nameof(DatabaseType.MySql), + String.Empty, + String.Empty, + "DbName", + "n", + "user", + "pass", + //general config + String.Empty, + String.Empty, + String.Empty, + //third run, we already hit all the code coverage so just get through it + String.Empty, + nameof(DatabaseType.MariaDB), + String.Empty, + "dbname", + "y", + "user", + "pass", + String.Empty, + String.Empty, + String.Empty }); var inputPos = 0; + mockApplication.SetupGet(x => x.VersionPrefix).Returns("sumfuk").Verifiable(); + mockConsole.Setup(x => x.PressAnyKeyAsync(It.IsAny())).Returns(Task.CompletedTask).Verifiable(); mockConsole.Setup(x => x.ReadLineAsync(It.IsAny(), It.IsAny())).Returns(() => { if (inputPos == finalInputSequence.Count) Assert.Fail("Exhausted input sequence!"); - return Task.FromResult(finalInputSequence[inputPos++]); + var res = finalInputSequence[inputPos++]; + return Task.FromResult(res); }).Verifiable(); mockConsole.Setup(x => x.WriteAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.CompletedTask).Verifiable(); + Assert.IsFalse(await wizard.CheckRunWizard(default).ConfigureAwait(false)); //first real run Assert.IsTrue(await wizard.CheckRunWizard(default).ConfigureAwait(false)); @@ -169,15 +195,19 @@ namespace Tgstation.Server.Host.Core.Tests //third run testGeneralConfig.SetupWizardMode = SetupWizardMode.Autodetect; mockIOManager.Setup(x => x.WriteAllBytes(It.IsNotNull(), It.IsNotNull(), It.IsAny())).Throws(new Exception()).Verifiable(); - await Assert.ThrowsExceptionAsync(() => wizard.CheckRunWizard(default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => wizard.CheckRunWizard(default)).ConfigureAwait(false); + Assert.AreEqual(finalInputSequence.Count, inputPos); mockFailCommand.VerifyAll(); mockSuccessCommand.VerifyAll(); mockIOManager.VerifyAll(); mockGeneralConfigurationOptions.VerifyAll(); mockConsole.VerifyAll(); mockGoodDbConnection.VerifyAll(); + mockBadDbConnection.VerifyAll(); + mockUglyDbConnection.VerifyAll(); mockDBConnectionFactory.VerifyAll(); + mockApplication.VerifyAll(); } } }