From f59ca1954bc5fdddb14574fed757e80ee5bf67be Mon Sep 17 00:00:00 2001 From: Karl Burtram Date: Wed, 15 Mar 2023 17:52:09 -0700 Subject: [PATCH] Enable user management tests (#1912) * Enable user management tests * Fix update user test --- .../Security/SecurityTestUtils.cs | 54 ++++++++++++++++--- .../Security/UserTests.cs | 19 ++++--- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/SecurityTestUtils.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/SecurityTestUtils.cs index b3d7eb16..5de6d5c0 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/SecurityTestUtils.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/SecurityTestUtils.cs @@ -125,8 +125,9 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security await SecurityTestUtils.DeleteCredential(service, connectionResult, credential); } - internal static async Task CreateLogin(SecurityService service, TestConnectionResult connectionResult, string contextId) + internal static async Task CreateLogin(SecurityService service, TestConnectionResult connectionResult) { + string contextId = System.Guid.NewGuid().ToString(); var initializeLoginViewRequestParams = new InitializeLoginViewRequestParams { ConnectionUri = connectionResult.ConnectionInfo.OwnerUri, @@ -174,9 +175,9 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security internal static async Task CreateUser( UserServiceHandlerImpl service, TestConnectionResult connectionResult, - string contextId, LoginInfo login) { + string contextId = System.Guid.NewGuid().ToString(); var initializeViewRequestParams = new InitializeUserViewParams { ConnectionUri = connectionResult.ConnectionInfo.OwnerUri, @@ -187,7 +188,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security var initializeUserViewContext = new Mock>(); initializeUserViewContext.Setup(x => x.SendResult(It.IsAny())) - .Returns(Task.FromResult(new LoginViewInfo())); + .Returns(Task.FromResult(new UserViewInfo())); await service.HandleInitializeUserViewRequest(initializeViewRequestParams, initializeUserViewContext.Object); @@ -208,17 +209,43 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security createUserContext.Verify(x => x.SendResult(It.Is (p => p.Success && p.User.Name != string.Empty))); + var disposeViewRequestParams = new DisposeUserViewRequestParams + { + ContextId = contextId + }; + + var disposeUserViewContext = new Mock>(); + disposeUserViewContext.Setup(x => x.SendResult(It.IsAny())) + .Returns(Task.FromResult(new object())); + + await service.HandleDisposeUserViewRequest(disposeViewRequestParams, disposeUserViewContext.Object); + return userParams.User; } - internal static async Task UpdateUser( + internal static async Task UpdateUser( UserServiceHandlerImpl service, TestConnectionResult connectionResult, - string contextId, UserInfo user) { + string contextId = System.Guid.NewGuid().ToString(); + var initializeViewRequestParams = new InitializeUserViewParams + { + ConnectionUri = connectionResult.ConnectionInfo.OwnerUri, + ContextId = contextId, + IsNewObject = false, + Database = "master", + Name = user.Name + }; + + var initializeUserViewContext = new Mock>(); + initializeUserViewContext.Setup(x => x.SendResult(It.IsAny())) + .Returns(Task.FromResult(new UserViewInfo())); + + await service.HandleInitializeUserViewRequest(initializeViewRequestParams, initializeUserViewContext.Object); + // update the user - user.OwnedSchemas = new string[] { "dbo" }; + user.DatabaseRoles = new string[] { "db_datareader" }; var updateParams = new UpdateUserParams { ContextId = contextId, @@ -228,7 +255,20 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security // call the create login method await service.HandleUpdateUserRequest(updateParams, updateUserContext.Object); // verify the result - updateUserContext.Verify(x => x.SendResult(It.Is(p => p.Success))); + updateUserContext.Verify(x => x.SendResult(It.Is(p => p.Success))); + + var disposeViewRequestParams = new DisposeUserViewRequestParams + { + ContextId = contextId + }; + + var disposeUserViewContext = new Mock>(); + disposeUserViewContext.Setup(x => x.SendResult(It.IsAny())) + .Returns(Task.FromResult(new object())); + + await service.HandleDisposeUserViewRequest(disposeViewRequestParams, disposeUserViewContext.Object); + + return updateParams.User; } internal static async Task DeleteUser(UserServiceHandlerImpl service, TestConnectionResult connectionResult, UserInfo user) diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/UserTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/UserTests.cs index bc55643c..b6871c87 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/UserTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Security/UserTests.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility; using Microsoft.SqlTools.ServiceLayer.Security; using Microsoft.SqlTools.ServiceLayer.Test.Common; +using NUnit.Framework; namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security { @@ -18,7 +19,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security /// /// Test the basic Create User method handler /// - //[Test] - enable tests in separate change + [Test] public async Task TestHandleCreateUserWithLoginRequest() { using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) @@ -27,11 +28,10 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security SecurityService service = new SecurityService(); UserServiceHandlerImpl userService = new UserServiceHandlerImpl(); var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath); - var contextId = System.Guid.NewGuid().ToString(); + + var login = await SecurityTestUtils.CreateLogin(service, connectionResult); - var login = await SecurityTestUtils.CreateLogin(service, connectionResult, contextId); - - var user = await SecurityTestUtils.CreateUser(userService, connectionResult, contextId, login); + var user = await SecurityTestUtils.CreateUser(userService, connectionResult, login); await SecurityTestUtils.DeleteUser(userService, connectionResult, user); @@ -42,7 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security /// /// Test the basic Update User method handler /// - //[Test] - enable tests in separate change + [Test] public async Task TestHandleUpdateUserWithLoginRequest() { using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) @@ -51,13 +51,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security SecurityService service = new SecurityService(); UserServiceHandlerImpl userService = new UserServiceHandlerImpl(); var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath); - var contextId = System.Guid.NewGuid().ToString(); - var login = await SecurityTestUtils.CreateLogin(service, connectionResult, contextId); + var login = await SecurityTestUtils.CreateLogin(service, connectionResult); - var user = await SecurityTestUtils.CreateUser(userService, connectionResult, contextId, login); + var user = await SecurityTestUtils.CreateUser(userService, connectionResult, login); - await SecurityTestUtils.UpdateUser(userService, connectionResult, contextId, user); + await SecurityTestUtils.UpdateUser(userService, connectionResult, user); await SecurityTestUtils.DeleteUser(userService, connectionResult, user);