Skip to content

Commit 72f999b

Browse files
committed
SEBWIN-978: Created unit tests for new runtime responsibilities and migrated existing tests from runtime controller unit tests.
1 parent e298fab commit 72f999b

19 files changed

Lines changed: 236 additions & 200 deletions

SafeExamBrowser.Core.UnitTests/OperationModel/OperationSequenceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void MustCorrectlyPropagateEventSubscription()
6767
var operations = new Queue<IOperation>();
6868

6969
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
70+
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
7071
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success).Raises(o => o.StatusChanged += null, default(TextKey));
7172

7273
operations.Enqueue(operationA.Object);
@@ -76,14 +77,12 @@ public void MustCorrectlyPropagateEventSubscription()
7677
var sut = new OperationSequence<IOperation>(loggerMock.Object, operations);
7778

7879
sut.StatusChanged += statusChangedHandler;
79-
8080
sut.TryPerform();
8181

8282
Assert.IsTrue(statusChangedCalled);
8383

8484
statusChangedCalled = false;
8585
sut.StatusChanged -= statusChangedHandler;
86-
8786
sut.TryPerform();
8887

8988
Assert.IsFalse(statusChangedCalled);

SafeExamBrowser.Runtime.UnitTests/Operations/ClientOperationTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,16 @@ public class ClientOperationTests
3333
private Action clientReady;
3434
private Action terminated;
3535
private AppConfig appConfig;
36-
private ClientBridge clientBridge;
3736
private Dependencies dependencies;
3837
private Mock<IClientProxy> proxy;
3938
private Mock<ILogger> logger;
40-
private Mock<IMessageBox> messageBox;
4139
private Mock<IProcess> process;
4240
private Mock<IProcessFactory> processFactory;
4341
private Mock<IProxyFactory> proxyFactory;
4442
private RuntimeContext runtimeContext;
4543
private Mock<IRuntimeHost> runtimeHost;
46-
private Mock<IRuntimeWindow> runtimeWindow;
4744
private SessionConfiguration session;
4845
private AppSettings settings;
49-
private Mock<IText> text;
5046
private ClientOperation sut;
5147

5248
[TestInitialize]
@@ -57,29 +53,32 @@ public void Initialize()
5753

5854
appConfig = new AppConfig();
5955
clientReady = new Action(() => runtimeHost.Raise(h => h.ClientReady += null));
60-
clientBridge = new ClientBridge(runtimeHost.Object, runtimeContext);
6156
logger = new Mock<ILogger>();
62-
messageBox = new Mock<IMessageBox>();
6357
process = new Mock<IProcess>();
6458
processFactory = new Mock<IProcessFactory>();
6559
proxy = new Mock<IClientProxy>();
6660
proxyFactory = new Mock<IProxyFactory>();
67-
runtimeWindow = new Mock<IRuntimeWindow>();
6861
session = new SessionConfiguration();
6962
settings = new AppSettings();
70-
text = new Mock<IText>();
7163
terminated = new Action(() =>
7264
{
7365
runtimeHost.Raise(h => h.ClientDisconnected += null);
7466
process.Raise(p => p.Terminated += null, 0);
7567
});
7668

7769
appConfig.ClientLogFilePath = "";
78-
dependencies = new Dependencies(clientBridge, logger.Object, messageBox.Object, runtimeWindow.Object, runtimeContext, text.Object);
7970
session.AppConfig = appConfig;
8071
session.Settings = settings;
8172
runtimeContext.Current = session;
8273
runtimeContext.Next = session;
74+
75+
dependencies = new Dependencies(
76+
new ClientBridge(Mock.Of<IRuntimeHost>(), runtimeContext),
77+
logger.Object,
78+
Mock.Of<IMessageBox>(),
79+
Mock.Of<IRuntimeWindow>(),
80+
runtimeContext,
81+
Mock.Of<IText>());
8382
proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>(), It.IsAny<Interlocutor>())).Returns(proxy.Object);
8483

8584
sut = new ClientOperation(dependencies, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0);

SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,14 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
2727
[TestClass]
2828
public class ClientTerminationOperationTests
2929
{
30-
private Action clientReady;
31-
private Action terminated;
3230
private AppConfig appConfig;
33-
private ClientBridge clientBridge;
3431
private Mock<IClientProxy> proxy;
35-
private Mock<ILogger> logger;
36-
private Mock<IMessageBox> messageBox;
3732
private Mock<IProcess> process;
3833
private Mock<IProcessFactory> processFactory;
3934
private Mock<IProxyFactory> proxyFactory;
4035
private Mock<IRuntimeHost> runtimeHost;
41-
private Mock<IRuntimeWindow> runtimeWindow;
4236
private SessionConfiguration session;
4337
private RuntimeContext runtimeContext;
44-
private Mock<IText> text;
4538
private ClientTerminationOperation sut;
4639

4740
[TestInitialize]
@@ -51,22 +44,11 @@ public void Initialize()
5144
runtimeHost = new Mock<IRuntimeHost>();
5245

5346
appConfig = new AppConfig();
54-
clientReady = new Action(() => runtimeHost.Raise(h => h.ClientReady += null));
55-
clientBridge = new ClientBridge(runtimeHost.Object, runtimeContext);
56-
logger = new Mock<ILogger>();
57-
messageBox = new Mock<IMessageBox>();
5847
process = new Mock<IProcess>();
5948
processFactory = new Mock<IProcessFactory>();
6049
proxy = new Mock<IClientProxy>();
6150
proxyFactory = new Mock<IProxyFactory>();
62-
runtimeWindow = new Mock<IRuntimeWindow>();
6351
session = new SessionConfiguration();
64-
text = new Mock<IText>();
65-
terminated = new Action(() =>
66-
{
67-
runtimeHost.Raise(h => h.ClientDisconnected += null);
68-
process.Raise(p => p.Terminated += null, 0);
69-
});
7052

7153
session.AppConfig = appConfig;
7254
runtimeContext.ClientProcess = process.Object;
@@ -75,7 +57,13 @@ public void Initialize()
7557
runtimeContext.Next = session;
7658
proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>(), It.IsAny<Interlocutor>())).Returns(proxy.Object);
7759

78-
var dependencies = new Dependencies(clientBridge, logger.Object, messageBox.Object, runtimeWindow.Object, runtimeContext, text.Object);
60+
var dependencies = new Dependencies(
61+
new ClientBridge(Mock.Of<IRuntimeHost>(), runtimeContext),
62+
Mock.Of<ILogger>(),
63+
Mock.Of<IMessageBox>(),
64+
Mock.Of<IRuntimeWindow>(),
65+
runtimeContext,
66+
Mock.Of<IText>());
7967

8068
sut = new ClientTerminationOperation(dependencies, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0);
8169
}

SafeExamBrowser.Runtime.UnitTests/Operations/ConfigurationOperationTests.cs

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using System.IO;
1111
using Microsoft.VisualStudio.TestTools.UnitTesting;
1212
using Moq;
13+
using SafeExamBrowser.Communication.Contracts.Data;
1314
using SafeExamBrowser.Communication.Contracts.Hosts;
15+
using SafeExamBrowser.Communication.Contracts.Proxies;
1416
using SafeExamBrowser.Configuration.Contracts;
1517
using SafeExamBrowser.Configuration.Contracts.Cryptography;
1618
using SafeExamBrowser.Core.Contracts.OperationModel;
@@ -19,6 +21,7 @@
1921
using SafeExamBrowser.Runtime.Communication;
2022
using SafeExamBrowser.Runtime.Operations.Session;
2123
using SafeExamBrowser.Settings;
24+
using SafeExamBrowser.Settings.Security;
2225
using SafeExamBrowser.SystemComponents.Contracts;
2326
using SafeExamBrowser.UserInterface.Contracts;
2427
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
@@ -33,20 +36,17 @@ public class ConfigurationOperationTests
3336
private const string FILE_NAME = "SebClientSettings.seb";
3437

3538
private AppConfig appConfig;
39+
private RuntimeContext context;
40+
private Dependencies dependencies;
41+
private SessionConfiguration currentSession;
42+
private SessionConfiguration nextSession;
43+
3644
private Mock<IFileSystem> fileSystem;
3745
private Mock<IHashAlgorithm> hashAlgorithm;
3846
private Mock<ILogger> logger;
3947
private Mock<IConfigurationRepository> repository;
40-
private SessionConfiguration currentSession;
4148
private Mock<IMessageBox> messageBox;
42-
private SessionConfiguration nextSession;
43-
private RuntimeContext runtimeContext;
44-
private Mock<IRuntimeHost> runtimeHost;
45-
private Mock<IRuntimeWindow> runtimeWindow;
46-
private Mock<IText> text;
4749
private Mock<IUserInterfaceFactory> uiFactory;
48-
private ClientBridge clientBridge;
49-
private Dependencies dependencies;
5050

5151
[TestInitialize]
5252
public void Initialize()
@@ -59,20 +59,24 @@ public void Initialize()
5959
currentSession = new SessionConfiguration();
6060
messageBox = new Mock<IMessageBox>();
6161
nextSession = new SessionConfiguration();
62-
runtimeContext = new RuntimeContext();
63-
runtimeHost = new Mock<IRuntimeHost>();
64-
runtimeWindow = new Mock<IRuntimeWindow>();
65-
text = new Mock<IText>();
62+
context = new RuntimeContext();
6663
uiFactory = new Mock<IUserInterfaceFactory>();
6764

65+
dependencies = new Dependencies(
66+
new ClientBridge(Mock.Of<IRuntimeHost>(), context),
67+
logger.Object,
68+
messageBox.Object,
69+
Mock.Of<IRuntimeWindow>(),
70+
context,
71+
Mock.Of<IText>());
72+
6873
appConfig.AppDataFilePath = $@"C:\Not\Really\AppData\File.xml";
6974
appConfig.ProgramDataFilePath = $@"C:\Not\Really\ProgramData\File.xml";
70-
clientBridge = new ClientBridge(runtimeHost.Object, runtimeContext);
7175
currentSession.AppConfig = appConfig;
72-
dependencies = new Dependencies(clientBridge, logger.Object, messageBox.Object, runtimeWindow.Object, runtimeContext, text.Object);
76+
currentSession.Settings = new AppSettings();
7377
nextSession.AppConfig = appConfig;
74-
runtimeContext.Current = currentSession;
75-
runtimeContext.Next = nextSession;
78+
context.Current = currentSession;
79+
context.Next = nextSession;
7680
}
7781

7882
[TestMethod]
@@ -169,13 +173,13 @@ public void Perform_MustAbortIfWishedByUser()
169173
var settings = new AppSettings();
170174
var url = @"http://www.safeexambrowser.org/whatever.seb";
171175

172-
runtimeContext.Current = null;
176+
context.Current = null;
173177
settings.ConfigurationMode = ConfigurationMode.ConfigureClient;
174178
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
175179
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
176180

177181
messageBox
178-
.Setup(m => m.Show(It.IsAny<TextKey>(), It.IsAny<TextKey>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
182+
.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
179183
.Returns(MessageBoxResult.Yes);
180184

181185
var sut = new ConfigurationOperation(new[] { "abc.exe", url }, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -195,7 +199,7 @@ public void Perform_MustNotAbortIfNotWishedByUser()
195199
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
196200

197201
messageBox
198-
.Setup(m => m.Show(It.IsAny<TextKey>(), It.IsAny<TextKey>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
202+
.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
199203
.Returns(MessageBoxResult.No);
200204

201205
var sut = new ConfigurationOperation(new[] { "abc.exe", url }, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -216,7 +220,7 @@ public void Perform_MustInformAboutClientConfigurationError()
216220
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.UnexpectedError);
217221

218222
messageBox
219-
.Setup(m => m.Show(It.IsAny<TextKey>(), It.IsAny<TextKey>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
223+
.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
220224
.Callback(() => informed = true);
221225

222226
var sut = new ConfigurationOperation(new[] { "abc.exe", url }, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -235,7 +239,7 @@ public void Perform_MustNotAllowToAbortIfNotInConfigureClientMode()
235239
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
236240

237241
messageBox
238-
.Setup(m => m.Show(It.IsAny<TextKey>(), It.IsAny<TextKey>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
242+
.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxAction>(), It.IsAny<MessageBoxIcon>(), It.IsAny<IWindow>()))
239243
.Callback(Assert.Fail);
240244

241245
var sut = new ConfigurationOperation(null, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -434,6 +438,7 @@ public void Perform_MustAbortAskingForAdminPasswordIfDecidedByUser()
434438
var url = @"http://www.safeexambrowser.org/whatever.seb";
435439

436440
appConfig.AppDataFilePath = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations), "Testdata", FILE_NAME);
441+
currentSession.Settings = currentSettings;
437442
currentSettings.Security.AdminPasswordHash = "1234";
438443
nextSession.Settings = nextSettings;
439444
nextSettings.Security.AdminPasswordHash = "9876";
@@ -478,7 +483,7 @@ public void Repeat_MustPerformForExamWithCorrectUri()
478483
var settings = new AppSettings { ConfigurationMode = ConfigurationMode.Exam };
479484

480485
currentSession.Settings = currentSettings;
481-
runtimeContext.ReconfigurationFilePath = resource.LocalPath;
486+
context.ReconfigurationFilePath = resource.LocalPath;
482487
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
483488

484489
var sut = new ConfigurationOperation(null, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -500,7 +505,7 @@ public void Repeat_MustPerformForClientConfigurationWithCorrectUri()
500505
var settings = new AppSettings { ConfigurationMode = ConfigurationMode.ConfigureClient };
501506

502507
currentSession.Settings = currentSettings;
503-
runtimeContext.ReconfigurationFilePath = resource.LocalPath;
508+
context.ReconfigurationFilePath = resource.LocalPath;
504509
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
505510
repository.Setup(r => r.ConfigureClientWith(It.Is<Uri>(u => u.Equals(resource)), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
506511

@@ -526,7 +531,7 @@ public void Repeat_MustDeleteTemporaryFileAfterClientConfiguration()
526531
var order = 0;
527532

528533
currentSession.Settings = currentSettings;
529-
runtimeContext.ReconfigurationFilePath = resource.LocalPath;
534+
context.ReconfigurationFilePath = resource.LocalPath;
530535
fileSystem.Setup(f => f.Delete(It.IsAny<string>())).Callback(() => delete = ++order);
531536
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
532537
repository.Setup(r => r.ConfigureClientWith(It.Is<Uri>(u => u.Equals(resource)), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success).Callback(() => configure = ++order);
@@ -549,7 +554,7 @@ public void Repeat_MustFailWithInvalidUri()
549554
var resource = new Uri("file:///C:/does/not/exist.txt");
550555
var settings = default(AppSettings);
551556

552-
runtimeContext.ReconfigurationFilePath = null;
557+
context.ReconfigurationFilePath = null;
553558
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
554559

555560
var sut = new ConfigurationOperation(null, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
@@ -559,7 +564,7 @@ public void Repeat_MustFailWithInvalidUri()
559564
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Never);
560565
Assert.AreEqual(OperationResult.Failed, result);
561566

562-
runtimeContext.ReconfigurationFilePath = resource.LocalPath;
567+
context.ReconfigurationFilePath = resource.LocalPath;
563568
result = sut.Repeat();
564569

565570
fileSystem.Verify(f => f.Delete(It.Is<string>(s => s == resource.LocalPath)), Times.Never);
@@ -577,9 +582,9 @@ public void Repeat_MustAbortForSettingsPasswordIfWishedByUser()
577582
var settings = new AppSettings { ConfigurationMode = ConfigurationMode.ConfigureClient };
578583

579584
currentSession.Settings = currentSettings;
580-
runtimeContext.ReconfigurationFilePath = resource.LocalPath;
585+
context.ReconfigurationFilePath = resource.LocalPath;
581586

582-
dialog.Setup(d => d.Show(It.IsAny<IWindow>())).Returns(new PasswordDialogResult { Success = true });
587+
dialog.Setup(d => d.Show(It.IsAny<IWindow>())).Returns(new PasswordDialogResult { Success = false });
583588
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
584589
uiFactory.Setup(f => f.CreatePasswordDialog(It.IsAny<string>(), It.IsAny<string>())).Returns(dialog.Object);
585590

@@ -590,6 +595,29 @@ public void Repeat_MustAbortForSettingsPasswordIfWishedByUser()
590595
Assert.AreEqual(OperationResult.Aborted, result);
591596
}
592597

598+
[TestMethod]
599+
public void Repeat_MustNotWaitForPasswordViaClientIfCommunicationHasFailed()
600+
{
601+
var clientProxy = new Mock<IClientProxy>();
602+
var currentSettings = new AppSettings();
603+
var location = Path.GetDirectoryName(GetType().Assembly.Location);
604+
var resource = new Uri(Path.Combine(location, nameof(Operations), "Testdata", FILE_NAME));
605+
var settings = new AppSettings { ConfigurationMode = ConfigurationMode.ConfigureClient };
606+
607+
context.ClientProxy = clientProxy.Object;
608+
context.ReconfigurationFilePath = resource.LocalPath;
609+
currentSession.Settings = currentSettings;
610+
currentSettings.Security.KioskMode = KioskMode.CreateNewDesktop;
611+
clientProxy.Setup(c => c.RequestPassword(It.IsAny<PasswordRequestPurpose>(), It.IsAny<Guid>())).Returns(new CommunicationResult(false));
612+
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
613+
614+
var sut = new ConfigurationOperation(null, dependencies, fileSystem.Object, hashAlgorithm.Object, repository.Object, uiFactory.Object);
615+
var result = sut.Repeat();
616+
617+
clientProxy.Verify(c => c.RequestPassword(It.IsAny<PasswordRequestPurpose>(), It.IsAny<Guid>()), Times.Once);
618+
Assert.AreEqual(OperationResult.Aborted, result);
619+
}
620+
593621
[TestMethod]
594622
public void Revert_MustDoNothing()
595623
{

0 commit comments

Comments
 (0)