Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/TestHelpers/Objects/TestUmbracoContextFactory.cs
Kenn Jacobsen dc9d4155a3 OpenId Connect authentication for new management API (#13318)
* First attempt at OpenIddict

* Making headway and more TODOs

* Redo current policies for multiple schemas + clean up auth controller

* Fix bad merge

* Clean up some more test code

* Fix spacing

* Include AddAuthentication() in OpenIddict addition

* A little more clean-up

* Move application creation to its own implementation + prepare for middleware to handle valid callback URL

* Enable refresh token flow

* Fix bad merge from v11/dev

* Support auth for Swagger and Postman in non-production environments + use default login screen for back-office logins

* Add workaround to client side login handling so the OAuth return URL is not corrupted before redirection

* Add temporary configuration handling for new backoffice

* Restructure the code somewhat, move singular responsibility from management API project

* Add recurring task for cleaning up old tokens in the DB

* Fix bad merge + make auth controller align with the new management API structure

* Explicitly handle the new management API path as a backoffice path (NOTE: this is potentially behaviorally breaking!)

* Redo handle the new management API requests as backoffice requests, this time in a non-breaking way

* Add/update TODOs

* Revert duplication of current auth policies for OpenIddict (as it breaks everything for V11 without the new management APIs) and introduce a dedicated PoC policy setup for OpenIddict.

* Fix failing unit tests

* Update src/Umbraco.Cms.ManagementApi/Security/BackOfficeApplicationManager.cs

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Update src/Umbraco.Cms.ManagementApi/Security/BackOfficeApplicationManager.cs

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Update src/Umbraco.Cms.ManagementApi/Security/BackOfficeApplicationManager.cs

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Update src/Umbraco.Core/Routing/UmbracoRequestPaths.cs

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
2022-11-01 11:15:31 +01:00

76 lines
2.6 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Moq;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Tests.Common;
using Umbraco.Cms.Web.Common.AspNetCore;
using Umbraco.Cms.Web.Common.UmbracoContext;
namespace Umbraco.Cms.Tests.UnitTests.TestHelpers.Objects;
/// <summary>
/// Simplify creating test UmbracoContext's
/// </summary>
public class TestUmbracoContextFactory
{
public static IUmbracoContextFactory Create(
GlobalSettings globalSettings = null,
IUmbracoContextAccessor umbracoContextAccessor = null,
IHttpContextAccessor httpContextAccessor = null,
IPublishedUrlProvider publishedUrlProvider = null,
UmbracoRequestPathsOptions umbracoRequestPathsOptions = null)
{
if (globalSettings == null)
{
globalSettings = new GlobalSettings();
}
if (umbracoContextAccessor == null)
{
umbracoContextAccessor = new TestUmbracoContextAccessor();
}
if (httpContextAccessor == null)
{
httpContextAccessor = Mock.Of<IHttpContextAccessor>();
}
if (publishedUrlProvider == null)
{
publishedUrlProvider = Mock.Of<IPublishedUrlProvider>();
}
if (umbracoRequestPathsOptions == null)
{
umbracoRequestPathsOptions = new UmbracoRequestPathsOptions();
}
var contentCache = new Mock<IPublishedContentCache>();
var mediaCache = new Mock<IPublishedMediaCache>();
var snapshot = new Mock<IPublishedSnapshot>();
snapshot.Setup(x => x.Content).Returns(contentCache.Object);
snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
var snapshotService = new Mock<IPublishedSnapshotService>();
snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(snapshot.Object);
var hostingEnvironment = TestHelper.GetHostingEnvironment();
var umbracoContextFactory = new UmbracoContextFactory(
umbracoContextAccessor,
snapshotService.Object,
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment, Options.Create(umbracoRequestPathsOptions)),
hostingEnvironment,
new UriUtility(hostingEnvironment),
new AspNetCoreCookieManager(httpContextAccessor),
httpContextAccessor);
return umbracoContextFactory;
}
}