From b421cc67f485588d97f67ce1ccc65256b98a85cf Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 3 Jun 2020 18:25:54 +1000 Subject: [PATCH] moves test --- .../BackOfficeCookieManagerTests.cs | 66 +++++++++---------- .../Security/BackOfficeCookieManager.cs | 4 +- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/Umbraco.Tests.Integration/Umbraco.Web.BackOffice.Security/BackOfficeCookieManagerTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Web.BackOffice.Security/BackOfficeCookieManagerTests.cs index f17f5eb83e..8044485cd7 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Web.BackOffice.Security/BackOfficeCookieManagerTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Web.BackOffice.Security/BackOfficeCookieManagerTests.cs @@ -1,7 +1,15 @@  +using Microsoft.AspNetCore.Routing; +using Moq; using NUnit.Framework; +using System; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Hosting; using Umbraco.Tests.Integration.Implementations; +using Umbraco.Web; +using Umbraco.Web.BackOffice.Security; namespace Umbraco.Tests.Security { @@ -13,26 +21,19 @@ namespace Umbraco.Tests.Security { var testHelper = new TestHelper(); - //should force app ctx to show not-configured - ConfigurationManager.AppSettings.Set(Constants.AppSettings.ConfigurationStatus, ""); - var httpContextAccessor = testHelper.GetHttpContextAccessor(); - var globalSettings = testHelper.SettingsForTests.GetDefaultGlobalSettings(); - var umbracoContext = new UmbracoContext( - httpContextAccessor, - Mock.Of(), - Mock.Of(), - globalSettings, - HostingEnvironment, - new TestVariationContextAccessor(), - UriUtility, - new AspNetCookieManager(httpContextAccessor)); - + var globalSettings = testHelper.SettingsForTests.GenerateMockGlobalSettings(); + var runtime = Mock.Of(x => x.Level == RuntimeLevel.Install); var mgr = new BackOfficeCookieManager( - Mock.Of(accessor => accessor.UmbracoContext == umbracoContext), runtime, HostingEnvironment, globalSettings, AppCaches.RequestCache); + Mock.Of(), + runtime, + Mock.Of(), + globalSettings, + Mock.Of(), + Mock.Of()); - var result = mgr.ShouldAuthenticateRequest(Mock.Of(), new Uri("http://localhost/umbraco")); + var result = mgr.ShouldAuthenticateRequest(new Uri("http://localhost/umbraco")); Assert.IsFalse(result); } @@ -40,27 +41,24 @@ namespace Umbraco.Tests.Security [Test] public void ShouldAuthenticateRequest_When_Configured() { - var httpContextAccessor = TestHelper.GetHttpContextAccessor(); - var globalSettings = TestObjects.GetGlobalSettings(); - var umbCtx = new UmbracoContext( - httpContextAccessor, - Mock.Of(), - Mock.Of(), - globalSettings, - HostingEnvironment, - new TestVariationContextAccessor(), - UriUtility, - new AspNetCookieManager(httpContextAccessor)); + var testHelper = new TestHelper(); + + //hostingEnvironment.ToAbsolute(globalSettings.UmbracoPath); + + var httpContextAccessor = testHelper.GetHttpContextAccessor(); + var globalSettings = testHelper.SettingsForTests.GenerateMockGlobalSettings(); + var runtime = Mock.Of(x => x.Level == RuntimeLevel.Run); - var mgr = new BackOfficeCookieManager(Mock.Of(accessor => accessor.UmbracoContext == umbCtx), runtime, HostingEnvironment, globalSettings, AppCaches.RequestCache); + var mgr = new BackOfficeCookieManager( + Mock.Of(), + runtime, + Mock.Of(x => x.ApplicationVirtualPath == "/" && x.ToAbsolute(globalSettings.UmbracoPath) == "/umbraco"), + globalSettings, + Mock.Of(), + Mock.Of()); - var request = new Mock(); - request.Setup(owinRequest => owinRequest.Uri).Returns(new Uri("http://localhost/umbraco")); - - var result = mgr.ShouldAuthenticateRequest( - Mock.Of(context => context.Request == request.Object), - new Uri("http://localhost/umbraco")); + var result = mgr.ShouldAuthenticateRequest(new Uri("http://localhost/umbraco")); Assert.IsTrue(result); } diff --git a/src/Umbraco.Web.BackOffice/Security/BackOfficeCookieManager.cs b/src/Umbraco.Web.BackOffice/Security/BackOfficeCookieManager.cs index 7af272e193..70a57db0e6 100644 --- a/src/Umbraco.Web.BackOffice/Security/BackOfficeCookieManager.cs +++ b/src/Umbraco.Web.BackOffice/Security/BackOfficeCookieManager.cs @@ -23,7 +23,7 @@ namespace Umbraco.Web.BackOffice.Security /// Umbraco's back office cookie needs to be read on two paths: /umbraco and /install and /base therefore we cannot just set the cookie path to be /umbraco, /// instead we'll specify our own cookie manager and return null if the request isn't for an acceptable path. /// - internal class BackOfficeCookieManager : ChunkingCookieManager, ICookieManager + public class BackOfficeCookieManager : ChunkingCookieManager, ICookieManager { private readonly IUmbracoContextAccessor _umbracoContextAccessor; private readonly IRuntimeState _runtime; @@ -74,7 +74,7 @@ namespace Umbraco.Web.BackOffice.Security /// * it is a /base request /// * it is a preview request /// - internal bool ShouldAuthenticateRequest(Uri requestUri, bool checkForceAuthTokens = true) + public bool ShouldAuthenticateRequest(Uri requestUri, bool checkForceAuthTokens = true) { // Do not authenticate the request if we are not running (don't have a db, are not configured) - since we will never need // to know a current user in this scenario - we treat it as a new install. Without this we can have some issues