From 54c1d0418ad61a8ee03ecf4b8da7a1cc96b9580e Mon Sep 17 00:00:00 2001 From: Alex Ma Date: Tue, 30 Aug 2022 14:24:44 -0700 Subject: [PATCH] Added error message to validate connect params. (#1660) * Added error message to validate connect params. * removed connectionParam == null as ConnnectionCompleteParams cannot be nulled. * Added ValidateConnectParams to other connection services * removed message from error * reverted change to connectionServiceCore * added updated message * restore null and added errormessage to GetConnectionCompleteParams * fix for sr.xlf * fix for tab * another fix to tabs * added message to connection complete error * small fix --- .../Connection/ConnectionService.cs | 3 +- .../Connection/ConnectionService.cs | 8 ++--- .../Connection/ConnectionServiceTests.cs | 29 ++++++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs index f24a7c19..6fee2c02 100644 --- a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs @@ -139,7 +139,7 @@ namespace Microsoft.Kusto.ServiceLayer.Connection return new ConnectionCompleteParams { OwnerUri = connectionParams.OwnerUri, - Messages = paramValidationErrorMessage + ErrorMessage = paramValidationErrorMessage, }; } @@ -371,6 +371,7 @@ namespace Microsoft.Kusto.ServiceLayer.Connection catch (Exception ex) { response.Messages = ex.ToString(); + response.ErrorMessage = ex.Message; } return response; diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index c03994c3..dd0897cb 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -319,20 +319,19 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection /// null upon validation success public ConnectionCompleteParams ValidateConnectParams(ConnectParams connectionParams) { - string paramValidationErrorMessage; if (connectionParams == null) { return new ConnectionCompleteParams { - Messages = SR.ConnectionServiceConnectErrorNullParams + ErrorMessage = SR.ConnectionServiceConnectErrorNullParams }; } - if (!connectionParams.IsValid(out paramValidationErrorMessage)) + if (!connectionParams.IsValid(out string paramValidationErrorMessage)) { return new ConnectionCompleteParams { OwnerUri = connectionParams.OwnerUri, - Messages = paramValidationErrorMessage + ErrorMessage = paramValidationErrorMessage }; } @@ -569,6 +568,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection catch (Exception ex) { response.Messages = ex.ToString(); + response.ErrorMessage = ex.Message; } return response; diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionServiceTests.cs index 6a31bf2f..b3bc8026 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionServiceTests.cs @@ -247,6 +247,27 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection Assert.False(disconnectResult); } + /// + /// Verify that we cannot connect to the default database when no username + /// is provided and the authentication type is basic auth. + /// + [Test] + public async Task CantConnectWithoutUsername() + { + // Connect + var connectionDetails = TestObjects.GetTestConnectionDetails(); + connectionDetails.UserName = ""; + var connectionResult = await + TestObjects.GetTestConnectionService() + .Connect(new ConnectParams() + { + OwnerUri = "file:///my/test/file.sql", + Connection = connectionDetails + }); + + Assert.That(connectionResult.ErrorMessage, Is.EqualTo(SR.ConnectionParamsValidateNullSqlAuth("UserName"))); + } + /// /// Verify that we can connect to the default database when no database name is /// provided as a parameter. @@ -402,7 +423,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection Connection = invalidConnectionDetails }); - Assert.That(connectionResult.Messages, Is.Not.Null.Or.Empty, "check that an error was caught"); + Assert.That(connectionResult.ErrorMessage, Is.Not.Null.Or.Empty, "check that an error was caught"); } static readonly object[] invalidParameters = @@ -419,7 +440,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection new object[] {"Integrated", "file://my/sample/file.sql", null, "test", "sa", "123456"}, new object[] {"Integrated", "", "my-server", "test", "sa", "123456"}, new object[] {"Integrated", "file://my/sample/file.sql", "", "test", "sa", "123456"} - }; + }; /// /// Verify that when connecting with invalid parameters, an error is thrown. /// @@ -442,7 +463,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection } }); - Assert.That(connectionResult.Messages, Is.Not.Null.Or.Empty, "check that an error was caught"); + Assert.That(connectionResult.ErrorMessage, Is.Not.Null.Or.Empty, "check that an error was caught"); } static readonly object[] noUserNameOrPassword = @@ -492,7 +513,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection TestObjects.GetTestConnectionService() .Connect(null); - Assert.That(connectionResult.Messages, Is.Not.Null.Or.Empty, "check that an error was caught"); + Assert.That(connectionResult.ErrorMessage, Is.Not.Null.Or.Empty, "check that an error was caught"); }