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;
|
2018-03-06 11:49:51 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
|
2018-03-15 14:32:07 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Proxies;
|
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-03-21 10:23:15 +01:00
|
|
|
|
private ServiceOperation 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-03-21 10:23:15 +01:00
|
|
|
|
sut = new ServiceOperation(configuration.Object, logger.Object, service.Object, text.Object);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustConnectToService()
|
|
|
|
|
{
|
2018-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Verify(s => s.Connect(null, true), Times.Exactly(2));
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailIfServiceNotAvailable()
|
|
|
|
|
{
|
2018-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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]
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public void MustFailIfServiceMandatoryAndNotAvailable()
|
2018-01-24 12:34:32 +01:00
|
|
|
|
{
|
2018-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public void MustNotFailIfServiceOptionalAndNotAvailable()
|
2018-01-24 12:34:32 +01:00
|
|
|
|
{
|
2018-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-01-24 12:34:32 +01:00
|
|
|
|
|
2018-01-25 09:50:46 +01:00
|
|
|
|
service.VerifySet(s => s.Ignore = true);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustDisconnectWhenReverting()
|
|
|
|
|
{
|
2018-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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-03-15 09:55:04 +01:00
|
|
|
|
service.Setup(s => s.Connect(null, true)).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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|