Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Routing/UmbracoRouteValuesFactoryTests.cs

96 lines
3.7 KiB
C#
Raw Normal View History

2021-02-05 12:19:09 +11:00
using System.Collections.Generic;
using System.Reflection;
Implements Public Access in netcore (#10137) * Getting new netcore PublicAccessChecker in place * Adds full test coverage for PublicAccessChecker * remove PublicAccessComposer * adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller * Implements the required methods on IMemberManager, removes old migrated code * Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops * adds note * adds note * Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling. * Changes name to IUmbracoEndpointBuilder * adds note * Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect * fixing build * Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware. * adds note * removes unused filter, fixes build * fixes WebPath and tests * Looks up entities in one query * remove usings * Fix test, remove stylesheet * Set status code before we write to response to avoid error * Ensures that users and members are validated when logging in. Shares more code between users and members. * Fixes RepositoryCacheKeys to ensure the keys are normalized * oops didn't mean to commit this * Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy * bah, far out this keeps getting recommitted. sorry Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
2021-02-05 12:19:09 +11:00
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Features;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Cms.Web.Website.Routing;
2021-02-05 12:19:09 +11:00
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Website.Routing;
[TestFixture]
public class UmbracoRouteValuesFactoryTests
{
private UmbracoRouteValuesFactory GetFactory(
out Mock<IPublishedRouter> publishedRouter,
out IOptions<UmbracoRenderingDefaultsOptions> renderingDefaults,
out IPublishedRequest request)
{
var builder = new PublishedRequestBuilder(new Uri("https://example.com"), Mock.Of<IFileService>());
builder.SetPublishedContent(Mock.Of<IPublishedContent>());
var builtRequest = request = builder.Build();
publishedRouter = new Mock<IPublishedRouter>();
publishedRouter.Setup(x => x.UpdateRequestAsync(It.IsAny<IPublishedRequest>(), null))
.Returns((IPublishedRequest r, IPublishedContent c) => Task.FromResult(builtRequest))
.Verifiable();
renderingDefaults =
Mock.Of<IOptions<UmbracoRenderingDefaultsOptions>>(x =>
x.Value.DefaultControllerType == typeof(RenderController));
// add the default one
var actionDescriptors = new List<ActionDescriptor>
{
new ControllerActionDescriptor
2021-02-05 12:19:09 +11:00
{
ControllerName = ControllerExtensions.GetControllerName<RenderController>(),
ActionName = nameof(RenderController.Index),
ControllerTypeInfo = typeof(RenderController).GetTypeInfo(),
},
};
var actionSelector = new Mock<IActionSelector>();
actionSelector.Setup(x => x.SelectCandidates(It.IsAny<RouteContext>())).Returns(actionDescriptors);
2021-02-05 12:19:09 +11:00
var factory = new UmbracoRouteValuesFactory(
renderingDefaults,
Mock.Of<IShortStringHelper>(),
new UmbracoFeatures(),
new ControllerActionSearcher(
new NullLogger<ControllerActionSearcher>(),
actionSelector.Object),
publishedRouter.Object);
return factory;
}
[Test]
public async Task Update_Request_To_Not_Found_When_No_Template()
{
var factory = GetFactory(out var publishedRouter, out _, out var request);
await factory.CreateAsync(new DefaultHttpContext(), request);
// The request has content, no template, no hijacked route and no disabled template features so UpdateRequestToNotFound will be called
publishedRouter.Verify(m => m.UpdateRequestAsync(It.IsAny<IPublishedRequest>(), null), Times.Once);
}
[Test]
public async Task Adds_Result_To_Route_Value_Dictionary()
{
var factory = GetFactory(out _, out var renderingDefaults, out var request);
var result = await factory.CreateAsync(new DefaultHttpContext(), request);
Assert.IsNotNull(result);
Assert.AreEqual(renderingDefaults.Value.DefaultControllerType, result.ControllerType);
Assert.AreEqual(UmbracoRouteValues.DefaultActionName, result.ActionName);
Assert.IsNull(result.TemplateName);
}
}