From 875bb57c95376c8f31b46675f230b8bbf9d03a58 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 12 Feb 2019 15:44:07 +0100 Subject: [PATCH] SEBWIN-296: Fixed unit tests for password negotiation via client. --- .../RuntimeControllerTests.cs | 214 +++++++++++------- 1 file changed, 133 insertions(+), 81 deletions(-) diff --git a/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs b/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs index 146dbb9a..71d80f19 100644 --- a/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs @@ -6,111 +6,163 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.Communication.Data; +using SafeExamBrowser.Contracts.Communication.Events; +using SafeExamBrowser.Contracts.Communication.Hosts; +using SafeExamBrowser.Contracts.Communication.Proxies; +using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; +using SafeExamBrowser.Contracts.Core.OperationModel; +using SafeExamBrowser.Contracts.I18n; +using SafeExamBrowser.Contracts.Logging; +using SafeExamBrowser.Contracts.UserInterface; +using SafeExamBrowser.Contracts.UserInterface.MessageBox; +using SafeExamBrowser.Contracts.UserInterface.Windows; +using SafeExamBrowser.Runtime.Operations.Events; namespace SafeExamBrowser.Runtime.UnitTests { [TestClass] public class RuntimeControllerTests { - [TestMethod] - public void TODO() + private AppConfig appConfig; + private Mock bootstrapSequence; + private Mock clientProxy; + private Mock currentSession; + private Settings currentSettings; + private Mock logger; + private Mock messageBox; + private Mock nextSession; + private Settings nextSettings; + private Mock shutdown; + private Mock text; + private Mock uiFactory; + private RuntimeController sut; + private Mock runtimeHost; + private Mock service; + private SessionContext sessionContext; + private Mock sessionSequence; + + [TestInitialize] + public void Initialize() { - Assert.Fail(); + appConfig = new AppConfig(); + bootstrapSequence = new Mock(); + clientProxy = new Mock(); + currentSession = new Mock(); + currentSettings = new Settings(); + logger = new Mock(); + messageBox = new Mock(); + nextSession = new Mock(); + nextSettings = new Settings(); + runtimeHost = new Mock(); + service = new Mock(); + sessionContext = new SessionContext(); + sessionSequence = new Mock(); + shutdown = new Mock(); + text = new Mock(); + uiFactory = new Mock(); + + currentSession.SetupGet(s => s.Settings).Returns(currentSettings); + nextSession.SetupGet(s => s.Settings).Returns(nextSettings); + + sessionContext.ClientProxy = clientProxy.Object; + sessionContext.Current = currentSession.Object; + sessionContext.Next = nextSession.Object; + + uiFactory.Setup(u => u.CreateRuntimeWindow(It.IsAny())).Returns(new Mock().Object); + uiFactory.Setup(u => u.CreateSplashScreen(It.IsAny())).Returns(new Mock().Object); + + sut = new RuntimeController( + appConfig, + logger.Object, + messageBox.Object, + bootstrapSequence.Object, + sessionSequence.Object, + runtimeHost.Object, + service.Object, + sessionContext, + shutdown.Object, + text.Object, + uiFactory.Object); } - //[TestMethod] - //public void MustRequestPasswordViaDialogOnDefaultDesktop() - //{ - // var clientProxy = new Mock(); - // var session = new Mock(); - // var url = @"http://www.safeexambrowser.org/whatever.seb"; + [TestMethod] + public void MustRequestPasswordViaDialogOnDefaultDesktop() + { + var args = new PasswordRequiredEventArgs(); + var password = "test1234"; + var passwordDialog = new Mock(); + var result = new Mock(); - // passwordDialog.Setup(d => d.Show(null)).Returns(new PasswordDialogResultStub { Success = true }); - // repository.SetupGet(r => r.CurrentSession).Returns(session.Object); - // repository.Setup(r => r.LoadSettings(It.IsAny(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); - // session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object); - // settings.KioskMode = KioskMode.DisableExplorerShell; + currentSettings.KioskMode = KioskMode.DisableExplorerShell; + passwordDialog.Setup(p => p.Show(It.IsAny())).Returns(result.Object); + result.SetupGet(r => r.Password).Returns(password); + result.SetupGet(r => r.Success).Returns(true); + uiFactory.Setup(u => u.CreatePasswordDialog(It.IsAny(), It.IsAny())).Returns(passwordDialog.Object); - // sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); - // sut.Perform(); + sut.TryStart(); + sessionSequence.Raise(s => s.ActionRequired += null, args); - // clientProxy.Verify(c => c.RequestPassword(It.IsAny(), It.IsAny()), Times.Never); - // passwordDialog.Verify(p => p.Show(null), Times.AtLeastOnce); - // session.VerifyGet(s => s.ClientProxy, Times.Never); - //} + clientProxy.VerifyNoOtherCalls(); + passwordDialog.Verify(p => p.Show(It.IsAny()), Times.Once); + uiFactory.Verify(u => u.CreatePasswordDialog(It.IsAny(), It.IsAny()), Times.Once); - //[TestMethod] - //public void MustRequestPasswordViaClientDuringReconfigurationOnNewDesktop() - //{ - // var clientProxy = new Mock(); - // var communication = new CommunicationResult(true); - // var passwordReceived = new Action((p, id) => - // { - // runtimeHost.Raise(r => r.PasswordReceived += null, new PasswordReplyEventArgs { RequestId = id, Success = true }); - // }); - // var session = new Mock(); - // var url = @"http://www.safeexambrowser.org/whatever.seb"; + Assert.AreEqual(true, args.Success); + Assert.AreEqual(password, args.Password); + } - // clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(communication).Callback(passwordReceived); - // passwordDialog.Setup(d => d.Show(null)).Returns(new PasswordDialogResultStub { Success = true }); - // repository.SetupGet(r => r.CurrentSession).Returns(session.Object); - // repository.Setup(r => r.LoadSettings(It.IsAny(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); - // session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object); - // settings.KioskMode = KioskMode.CreateNewDesktop; + [TestMethod] + public void MustRequestPasswordViaClientOnNewDesktop() + { + var args = new PasswordRequiredEventArgs(); + var passwordReceived = new Action((p, id) => + { + runtimeHost.Raise(r => r.PasswordReceived += null, new PasswordReplyEventArgs { RequestId = id, Success = true }); + }); - // sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); - // sut.Perform(); + currentSettings.KioskMode = KioskMode.CreateNewDesktop; + clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(new CommunicationResult(true)).Callback(passwordReceived); - // clientProxy.Verify(c => c.RequestPassword(It.IsAny(), It.IsAny()), Times.AtLeastOnce); - // passwordDialog.Verify(p => p.Show(null), Times.Never); - // session.VerifyGet(s => s.ClientProxy, Times.AtLeastOnce); - //} + sut.TryStart(); + sessionSequence.Raise(s => s.ActionRequired += null, args); - //[TestMethod] - //public void MustAbortAskingForPasswordViaClientIfDecidedByUser() - //{ - // var clientProxy = new Mock(); - // var communication = new CommunicationResult(true); - // var passwordReceived = new Action((p, id) => - // { - // runtimeHost.Raise(r => r.PasswordReceived += null, new PasswordReplyEventArgs { RequestId = id, Success = false }); - // }); - // var session = new Mock(); - // var url = @"http://www.safeexambrowser.org/whatever.seb"; + clientProxy.Verify(c => c.RequestPassword(It.IsAny(), It.IsAny()), Times.Once); + uiFactory.Verify(u => u.CreatePasswordDialog(It.IsAny(), It.IsAny()), Times.Never); + } - // clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(communication).Callback(passwordReceived); - // repository.SetupGet(r => r.CurrentSession).Returns(session.Object); - // repository.Setup(r => r.LoadSettings(It.IsAny(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); - // session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object); - // settings.KioskMode = KioskMode.CreateNewDesktop; + [TestMethod] + public void MustAbortAskingForPasswordViaClientIfDecidedByUser() + { + var args = new PasswordRequiredEventArgs(); + var passwordReceived = new Action((p, id) => + { + runtimeHost.Raise(r => r.PasswordReceived += null, new PasswordReplyEventArgs { RequestId = id, Success = false }); + }); - // sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); + currentSettings.KioskMode = KioskMode.CreateNewDesktop; + clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(new CommunicationResult(true)).Callback(passwordReceived); - // var result = sut.Perform(); + sut.TryStart(); + sessionSequence.Raise(s => s.ActionRequired += null, args); - // Assert.AreEqual(OperationResult.Aborted, result); - //} + clientProxy.Verify(c => c.RequestPassword(It.IsAny(), It.IsAny()), Times.Once); + uiFactory.Verify(u => u.CreatePasswordDialog(It.IsAny(), It.IsAny()), Times.Never); + } - //[TestMethod] - //public void MustNotWaitForPasswordViaClientIfCommunicationHasFailed() - //{ - // var clientProxy = new Mock(); - // var communication = new CommunicationResult(false); - // var session = new Mock(); - // var url = @"http://www.safeexambrowser.org/whatever.seb"; + [TestMethod] + public void MustNotWaitForPasswordViaClientIfCommunicationHasFailed() + { + var args = new PasswordRequiredEventArgs(); - // clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(communication); - // repository.SetupGet(r => r.CurrentSession).Returns(session.Object); - // repository.Setup(r => r.TryLoadSettings(It.IsAny(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); - // session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object); - // settings.KioskMode = KioskMode.CreateNewDesktop; + currentSettings.KioskMode = KioskMode.CreateNewDesktop; + clientProxy.Setup(c => c.RequestPassword(It.IsAny(), It.IsAny())).Returns(new CommunicationResult(false)); - // sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext); - - // var result = sut.Perform(); - - // Assert.AreEqual(OperationResult.Aborted, result); - //} + sut.TryStart(); + sessionSequence.Raise(s => s.ActionRequired += null, args); + } } }