From d41be3751f9f2ba2916e9e8dde0940b58408eb0f Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Fri, 1 Mar 2024 15:59:05 +0200 Subject: [PATCH] CodeQL fixes --- .../ExceptionUnitTests.cs | 62 +++++++------------ .../Lib/Helper.cs | 15 ++++- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs index 5b63cd0..78535c0 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ExceptionUnitTests.cs @@ -7,20 +7,29 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; [TestClass] public class ExceptionUnitTests : ExecuteQueryTestBase { - [TestMethod] - public async Task TestExecuteQuery_Invalid_Creds_ThrowError() + private Input input = new(); + private Options options = new(); + + [TestInitialize] + public void SetUp() { - var input = new Input() + input = new Input() { - ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord", + ConnectionString = _connString, }; - var options = new Options() + options = new Options() { SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted, CommandTimeoutSeconds = 2, ThrowErrorOnFailure = true }; + } + + [TestMethod] + public async Task TestExecuteQuery_Invalid_Creds_ThrowError() + { + input.ConnectionString = Helper.GetInvalidConnectionString(); var ex = await Assert.ThrowsExceptionAsync(() => MicrosoftSQL.ExecuteQuery(input, options, default)); Assert.IsTrue(ex.Message.Contains("Login failed for user 'SA'.")); @@ -29,17 +38,8 @@ public async Task TestExecuteQuery_Invalid_Creds_ThrowError() [TestMethod] public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage() { - var input = new Input() - { - ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord", - }; - - var options = new Options() - { - SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted, - CommandTimeoutSeconds = 2, - ThrowErrorOnFailure = false - }; + options.ThrowErrorOnFailure = false; + input.ConnectionString = Helper.GetInvalidConnectionString(); var result = await MicrosoftSQL.ExecuteQuery(input, options, default); Assert.IsFalse(result.Success); @@ -50,19 +50,8 @@ public async Task TestExecuteQuery_Invalid_Creds_ReturnErrorMessage() [TestMethod] public void TestExecuteQuery_ExceptionIsThrownWhenQueryFails() { - var input = new Input() - { - Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)", - ExecuteType = ExecuteTypes.NonQuery, - ConnectionString = _connString, - }; - - var options = new Options() - { - SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted, - CommandTimeoutSeconds = 2, - ThrowErrorOnFailure = true - }; + input.Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)"; + input.ExecuteType = ExecuteTypes.NonQuery; var ex = Assert.ThrowsExceptionAsync(async () => await MicrosoftSQL.ExecuteQuery(input, options, default)); Assert.IsTrue(ex.Result.Message.Contains("System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Unit'.")); @@ -71,19 +60,10 @@ public void TestExecuteQuery_ExceptionIsThrownWhenQueryFails() [TestMethod] public async Task TestExecuteQuery_ErrorMessageWhenQueryFails() { - var input = new Input() - { - Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)", - ExecuteType = ExecuteTypes.NonQuery, - ConnectionString = _connString, - }; + input.Query = $"INSERT INTO {_tableName} VALUES (1, Unit, Tests, 456)"; + input.ExecuteType = ExecuteTypes.NonQuery; - var options = new Options() - { - SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted, - CommandTimeoutSeconds = 2, - ThrowErrorOnFailure = false - }; + options.ThrowErrorOnFailure = false; var result = await MicrosoftSQL.ExecuteQuery(input, options, default); Assert.IsFalse(result.Success); diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs index ecf5955..be550aa 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/Helper.cs @@ -4,10 +4,21 @@ namespace Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; internal class Helper { + /// + /// Test credentials for docker server. + /// + private static readonly string _dockerAddress = "127.0.0.1,1433"; + private static readonly string _dockerUsername = "SA"; + private static readonly string _dockerPassword = "Salakala123!"; + internal static string CreateConnectionString() { - var pw = "Salakala123!"; - return $"Server=127.0.0.1,1433;Database=Master;User Id=SA;Password={pw};Encrypt=true;TrustServerCertificate=True;"; + return $"Server={_dockerAddress};Database=Master;User Id={_dockerUsername};Password={_dockerPassword};Encrypt=true;TrustServerCertificate=True;"; + } + + internal static string GetInvalidConnectionString() + { + return $"Server=127.0.0.1,1433;Database=Master;User Id={_dockerUsername};Password={Guid.NewGuid()};Encrypt=true;TrustServerCertificate=True;"; } internal static int GetRowCount(string connString, string table)