2018-01-23 15:33:54 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-24 12:34:32 +01:00
|
|
|
|
using System;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
using Moq;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
|
|
|
|
using SafeExamBrowser.Runtime.Behaviour.Operations;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.UnitTests.Behaviour.Operations
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
2018-02-12 12:21:55 +01:00
|
|
|
|
public class ServiceConnectionOperationTests
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-01-24 12:34:32 +01:00
|
|
|
|
private Mock<ILogger> logger;
|
|
|
|
|
private Mock<IServiceProxy> service;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private Mock<IConfigurationRepository> configuration;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
private Mock<IProgressIndicator> progressIndicator;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
private Mock<IText> text;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
private ServiceConnectionOperation sut;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
logger = new Mock<ILogger>();
|
|
|
|
|
service = new Mock<IServiceProxy>();
|
2018-02-06 15:12:11 +01:00
|
|
|
|
configuration = new Mock<IConfigurationRepository>();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
progressIndicator = new Mock<IProgressIndicator>();
|
2018-01-24 12:34:32 +01:00
|
|
|
|
text = new Mock<IText>();
|
|
|
|
|
|
2018-02-12 12:21:55 +01:00
|
|
|
|
sut = new ServiceConnectionOperation(configuration.Object, logger.Object, service.Object, text.Object);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustConnectToService()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(true);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(true);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Verify(s => s.Connect(null), Times.Exactly(2));
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailIfServiceNotAvailable()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Throws<Exception>();
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Throws<Exception>();
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAbortIfServiceMandatoryAndNotAvailable()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
Assert.IsTrue(sut.Abort);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotAbortIfServiceOptionalAndNotAvailable()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
|
2018-01-25 09:50:46 +01:00
|
|
|
|
service.VerifySet(s => s.Ignore = true);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
Assert.IsFalse(sut.Abort);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustDisconnectWhenReverting()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(true);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(true);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
|
|
|
|
service.Verify(s => s.Disconnect(), Times.Exactly(2));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 09:50:46 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailWhenDisconnecting()
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(true);
|
2018-01-25 09:50:46 +01:00
|
|
|
|
service.Setup(s => s.Disconnect()).Throws<Exception>();
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-25 09:50:46 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
|
|
|
|
service.Verify(s => s.Disconnect(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
[TestMethod]
|
2018-01-24 12:34:32 +01:00
|
|
|
|
public void MustNotDisconnnectIfNotAvailable()
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Returns(false);
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Throws<Exception>();
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.Setup(s => s.Connect(null)).Throws<Exception>();
|
2018-02-16 13:42:27 +01:00
|
|
|
|
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional });
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
|
|
|
|
sut.Perform();
|
|
|
|
|
sut.Revert();
|
|
|
|
|
|
|
|
|
|
service.Verify(s => s.Disconnect(), Times.Never);
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|