/* * Copyright (c) 2022 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/. */ using System; using CefSharp; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Browser.Contracts.Filters; using SafeExamBrowser.Browser.Handlers; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Configuration.Contracts.Cryptography; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings.Browser; using SafeExamBrowser.Settings.Browser.Filter; using SafeExamBrowser.Settings.Browser.Proxy; using BrowserSettings = SafeExamBrowser.Settings.Browser.BrowserSettings; using Request = SafeExamBrowser.Browser.Contracts.Filters.Request; using ResourceHandler = SafeExamBrowser.Browser.Handlers.ResourceHandler; namespace SafeExamBrowser.Browser.UnitTests.Handlers { [TestClass] public class RequestHandlerTests { private AppConfig appConfig; private Mock filter; private Mock keyGenerator; private Mock logger; private BrowserSettings settings; private WindowSettings windowSettings; private ResourceHandler resourceHandler; private Mock text; private TestableRequestHandler sut; [TestInitialize] public void Initialize() { appConfig = new AppConfig(); filter = new Mock(); keyGenerator = new Mock(); logger = new Mock(); settings = new BrowserSettings(); windowSettings = new WindowSettings(); text = new Mock(); resourceHandler = new ResourceHandler(appConfig, filter.Object, keyGenerator.Object, logger.Object, settings, windowSettings, text.Object); sut = new TestableRequestHandler(appConfig, filter.Object, logger.Object, resourceHandler, settings, windowSettings, text.Object); } [TestMethod] public void MustBlockSpecialWindowDispositions() { Assert.IsTrue(sut.OnOpenUrlFromTab(default(IWebBrowser), default(IBrowser), default(IFrame), default(string), WindowOpenDisposition.NewBackgroundTab, default(bool))); Assert.IsTrue(sut.OnOpenUrlFromTab(default(IWebBrowser), default(IBrowser), default(IFrame), default(string), WindowOpenDisposition.NewPopup, default(bool))); Assert.IsTrue(sut.OnOpenUrlFromTab(default(IWebBrowser), default(IBrowser), default(IFrame), default(string), WindowOpenDisposition.NewWindow, default(bool))); Assert.IsTrue(sut.OnOpenUrlFromTab(default(IWebBrowser), default(IBrowser), default(IFrame), default(string), WindowOpenDisposition.SaveToDisk, default(bool))); } [TestMethod] public void MustDetectQuitUrl() { var eventFired = false; var quitUrl = "http://www.byebye.com"; var request = new Mock(); request.SetupGet(r => r.Url).Returns(quitUrl); settings.QuitUrl = quitUrl; sut.QuitUrlVisited += (url) => eventFired = true; var blocked = sut.OnBeforeBrowse(Mock.Of(), Mock.Of(), Mock.Of