2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2021-01-08 17:21:35 +11:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-06-03 18:25:54 +10:00
|
|
|
using Moq;
|
2016-02-16 14:51:15 +01:00
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
|
|
|
|
using Umbraco.Cms.Tests.UnitTests.TestHelpers;
|
|
|
|
|
using Umbraco.Cms.Web.BackOffice.Controllers;
|
|
|
|
|
using Umbraco.Cms.Web.BackOffice.Security;
|
2020-06-09 12:35:31 +10:00
|
|
|
using Umbraco.Extensions;
|
2016-02-16 14:51:15 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.BackOffice.Security;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class BackOfficeCookieManagerTests
|
2016-02-16 14:51:15 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void ShouldAuthenticateRequest_When_Not_Configured()
|
|
|
|
|
{
|
|
|
|
|
var globalSettings = new GlobalSettings();
|
2022-11-01 11:15:31 +01:00
|
|
|
var umbracoRequestPathsOptions = new UmbracoRequestPathsOptions();
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
var runtime = Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Install);
|
|
|
|
|
var mgr = new BackOfficeCookieManager(
|
|
|
|
|
Mock.Of<IUmbracoContextAccessor>(),
|
|
|
|
|
runtime,
|
2022-11-01 11:15:31 +01:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), TestHelper.GetHostingEnvironment(), Options.Create(umbracoRequestPathsOptions)),
|
2022-06-21 08:09:38 +02:00
|
|
|
Mock.Of<IBasicAuthService>());
|
|
|
|
|
|
|
|
|
|
var result = mgr.ShouldAuthenticateRequest("/umbraco");
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldAuthenticateRequest_When_Configured()
|
|
|
|
|
{
|
|
|
|
|
var globalSettings = new GlobalSettings();
|
2022-11-01 11:15:31 +01:00
|
|
|
var umbracoRequestPathsOptions = new UmbracoRequestPathsOptions();
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
var runtime = Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Run);
|
|
|
|
|
var mgr = new BackOfficeCookieManager(
|
|
|
|
|
Mock.Of<IUmbracoContextAccessor>(),
|
|
|
|
|
runtime,
|
|
|
|
|
new UmbracoRequestPaths(
|
|
|
|
|
Options.Create(globalSettings),
|
|
|
|
|
Mock.Of<IHostingEnvironment>(x =>
|
2022-11-01 11:15:31 +01:00
|
|
|
x.ApplicationVirtualPath == "/" && x.ToAbsolute(globalSettings.UmbracoPath) == "/umbraco"),
|
|
|
|
|
Options.Create(umbracoRequestPathsOptions)),
|
2022-06-21 08:09:38 +02:00
|
|
|
Mock.Of<IBasicAuthService>());
|
|
|
|
|
|
|
|
|
|
var result = mgr.ShouldAuthenticateRequest("/umbraco");
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldAuthenticateRequest_Is_Back_Office()
|
2016-02-16 14:51:15 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var globalSettings = new GlobalSettings();
|
2022-11-01 11:15:31 +01:00
|
|
|
var umbracoRequestPathsOptions = new UmbracoRequestPathsOptions();
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
var runtime = Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Run);
|
|
|
|
|
|
|
|
|
|
GenerateAuthPaths(out var remainingTimeoutSecondsPath, out var isAuthPath);
|
|
|
|
|
|
|
|
|
|
var mgr = new BackOfficeCookieManager(
|
|
|
|
|
Mock.Of<IUmbracoContextAccessor>(),
|
|
|
|
|
runtime,
|
|
|
|
|
new UmbracoRequestPaths(
|
|
|
|
|
Options.Create(globalSettings),
|
|
|
|
|
Mock.Of<IHostingEnvironment>(x =>
|
|
|
|
|
x.ApplicationVirtualPath == "/" && x.ToAbsolute(globalSettings.UmbracoPath) == "/umbraco" &&
|
2022-11-01 11:15:31 +01:00
|
|
|
x.ToAbsolute(Constants.SystemDirectories.Install) == "/install"),
|
|
|
|
|
Options.Create(umbracoRequestPathsOptions)),
|
2022-06-21 08:09:38 +02:00
|
|
|
Mock.Of<IBasicAuthService>());
|
|
|
|
|
|
|
|
|
|
var result = mgr.ShouldAuthenticateRequest(remainingTimeoutSecondsPath);
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
|
|
|
|
|
result = mgr.ShouldAuthenticateRequest(isAuthPath);
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldAuthenticateRequest_Not_Back_Office()
|
|
|
|
|
{
|
|
|
|
|
var globalSettings = new GlobalSettings();
|
2022-11-01 11:15:31 +01:00
|
|
|
var umbracoRequestPathsOptions = new UmbracoRequestPathsOptions();
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
var runtime = Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Run);
|
|
|
|
|
|
|
|
|
|
var mgr = new BackOfficeCookieManager(
|
|
|
|
|
Mock.Of<IUmbracoContextAccessor>(),
|
|
|
|
|
runtime,
|
|
|
|
|
new UmbracoRequestPaths(
|
|
|
|
|
Options.Create(globalSettings),
|
|
|
|
|
Mock.Of<IHostingEnvironment>(x =>
|
|
|
|
|
x.ApplicationVirtualPath == "/" && x.ToAbsolute(globalSettings.UmbracoPath) == "/umbraco" &&
|
2022-11-01 11:15:31 +01:00
|
|
|
x.ToAbsolute(Constants.SystemDirectories.Install) == "/install"),
|
|
|
|
|
Options.Create(umbracoRequestPathsOptions)),
|
2022-06-21 08:09:38 +02:00
|
|
|
Mock.Of<IBasicAuthService>());
|
|
|
|
|
|
|
|
|
|
var result = mgr.ShouldAuthenticateRequest("/notbackoffice");
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
result = mgr.ShouldAuthenticateRequest("/umbraco/api/notbackoffice");
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
result = mgr.ShouldAuthenticateRequest("/umbraco/surface/notbackoffice");
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateAuthPaths(out string remainingTimeoutSecondsPath, out string isAuthPath)
|
|
|
|
|
{
|
|
|
|
|
var controllerName = ControllerExtensions.GetControllerName<AuthenticationController>();
|
|
|
|
|
|
|
|
|
|
// this path is not a back office request even though it's in the same controller - it's a 'special' endpoint
|
|
|
|
|
var rPath = remainingTimeoutSecondsPath =
|
|
|
|
|
$"/umbraco/{Constants.Web.Mvc.BackOfficePathSegment}/{Constants.Web.Mvc.BackOfficeApiArea}/{controllerName}/{nameof(AuthenticationController.GetRemainingTimeoutSeconds)}"
|
|
|
|
|
.ToLower();
|
|
|
|
|
|
|
|
|
|
// this is on the same controller but is considered a back office request
|
|
|
|
|
var aPath = isAuthPath =
|
|
|
|
|
$"/umbraco/{Constants.Web.Mvc.BackOfficePathSegment}/{Constants.Web.Mvc.BackOfficeApiArea}/{controllerName}/{nameof(AuthenticationController.IsAuthenticated)}"
|
|
|
|
|
.ToLower();
|
2016-02-16 14:51:15 +01:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|