Adds tests for all routes and fixes up some of the issues

This commit is contained in:
Shannon
2020-05-14 20:59:29 +10:00
parent 18a1a72349
commit ac6f691208
12 changed files with 238 additions and 50 deletions

View File

@@ -54,7 +54,7 @@
{
public const string InstallArea = "UmbracoInstall";
public const string BackOfficeArea = "UmbracoBackOffice";
public const string BackOfficeArea = "UmbracoApi";
}
}
}

View File

@@ -41,6 +41,7 @@ namespace Umbraco.Web
// adds the virtual directory if any
// see also VirtualPathUtility.ToAbsolute
// TODO: Does this do anything differently than IHostingEnvironment.ToAbsolute? Seems it does less, maybe should be removed?
public string ToAbsolute(string url)
{
//return ResolveUrl(url);

View File

@@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\Umbraco.Tests.Common\Umbraco.Tests.Common.csproj" />
<ProjectReference Include="..\Umbraco.Web.BackOffice\Umbraco.Web.BackOffice.csproj" />
<ProjectReference Include="..\Umbraco.Web.Common\Umbraco.Web.Common.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,108 @@
using Microsoft.AspNetCore.Routing;
using Moq;
using NUnit.Framework;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Hosting;
using Umbraco.Extensions;
using Umbraco.Web.BackOffice.Controllers;
using Umbraco.Web.BackOffice.Routing;
using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Common.Controllers;
using Umbraco.Web.WebApi;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Routing
{
[TestFixture]
public class BackOfficeAreaRoutesTests
{
[TestCase(RuntimeLevel.Install)]
[TestCase(RuntimeLevel.BootFailed)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Boot)]
public void RuntimeState_No_Routes(RuntimeLevel level)
{
var routes = GetBackOfficeAreaRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(0, endpoints.DataSources.Count);
}
[Test]
public void RuntimeState_Upgrade()
{
var routes = GetBackOfficeAreaRoutes(RuntimeLevel.Upgrade);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(1, endpoints.DataSources.Count);
var route = endpoints.DataSources.First();
Assert.AreEqual(2, route.Endpoints.Count);
AssertMinimalBackOfficeRoutes(route);
}
[Test]
public void RuntimeState_Run()
{
var routes = GetBackOfficeAreaRoutes(RuntimeLevel.Run);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(1, endpoints.DataSources.Count);
var route = endpoints.DataSources.First();
Assert.AreEqual(4, route.Endpoints.Count);
AssertMinimalBackOfficeRoutes(route);
var endpoint3 = (RouteEndpoint)route.Endpoints[2];
var previewControllerName = ControllerExtensions.GetControllerName<PreviewController>();
Assert.AreEqual($"umbraco/{previewControllerName.ToLowerInvariant()}/{{action}}/{{id?}}", endpoint3.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.BackOfficeArea, endpoint3.RoutePattern.Defaults["area"]);
Assert.AreEqual("Index", endpoint3.RoutePattern.Defaults["action"]);
Assert.AreEqual(previewControllerName, endpoint3.RoutePattern.Defaults["controller"]);
var endpoint4 = (RouteEndpoint)route.Endpoints[3];
var apiControllerName = ControllerExtensions.GetControllerName<Testing1Controller>();
Assert.AreEqual($"umbraco/backoffice/api/{apiControllerName.ToLowerInvariant()}/{{action}}/{{id?}}", endpoint4.RoutePattern.RawText);
Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey("area"));
Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey("action"));
Assert.AreEqual(apiControllerName, endpoint4.RoutePattern.Defaults["controller"]);
}
private void AssertMinimalBackOfficeRoutes(EndpointDataSource route)
{
var endpoint1 = (RouteEndpoint)route.Endpoints[0];
Assert.AreEqual($"umbraco/{{action}}/{{id?}}", endpoint1.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.BackOfficeArea, endpoint1.RoutePattern.Defaults["area"]);
Assert.AreEqual("Default", endpoint1.RoutePattern.Defaults["action"]);
Assert.AreEqual(ControllerExtensions.GetControllerName<BackOfficeController>(), endpoint1.RoutePattern.Defaults["controller"]);
var endpoint2 = (RouteEndpoint)route.Endpoints[1];
var controllerName = ControllerExtensions.GetControllerName<AuthenticationController>();
Assert.AreEqual($"umbraco/backoffice/{Constants.Web.Mvc.BackOfficeArea.ToLowerInvariant()}/{controllerName.ToLowerInvariant()}/{{action}}/{{id?}}", endpoint2.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.BackOfficeArea, endpoint2.RoutePattern.Defaults["area"]);
Assert.IsFalse(endpoint2.RoutePattern.Defaults.ContainsKey("action"));
Assert.AreEqual(controllerName, endpoint2.RoutePattern.Defaults["controller"]);
}
private BackOfficeAreaRoutes GetBackOfficeAreaRoutes(RuntimeLevel level)
{
var routes = new BackOfficeAreaRoutes(
Mock.Of<IGlobalSettings>(x => x.UmbracoPath == "~/umbraco"),
Mock.Of<IHostingEnvironment>(x => x.ToAbsolute(It.IsAny<string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<IRuntimeState>(x => x.Level == level),
new UmbracoApiControllerTypeCollection(new[] { typeof(Testing1Controller) }));
return routes;
}
[IsBackOffice]
private class Testing1Controller : UmbracoApiController
{
}
}
}

View File

@@ -1,21 +1,18 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUglify.Helpers;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core;
using Umbraco.Extensions;
using Umbraco.Web.Common.Routing;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Routing
{
[TestFixture]
public class EndpointRouteBuilderExtensionsTests
{
@@ -110,41 +107,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Routing
Assert.AreEqual(controllerName, endpoint.RoutePattern.Defaults["controller"]);
}
private class TestRouteBuilder : IEndpointRouteBuilder
{
private readonly ServiceProvider _serviceProvider;
public TestRouteBuilder()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddMvc();
_serviceProvider = services.BuildServiceProvider();
}
public ICollection<EndpointDataSource> DataSources { get; } = new List<EndpointDataSource>();
public IServiceProvider ServiceProvider => _serviceProvider;
public IApplicationBuilder CreateApplicationBuilder()
{
return Mock.Of<IApplicationBuilder>();
}
}
private class TestServiceProvider : IServiceProvider
{
public object GetService(Type serviceType)
{
if (serviceType.Name == "MvcMarkerService")
{
// it's internal but we can force make it
return Activator.CreateInstance(serviceType);
}
return null;
}
}
private class Testing1Controller : ControllerBase
{

View File

@@ -0,0 +1,82 @@
using Microsoft.AspNetCore.Routing;
using Moq;
using NUnit.Framework;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Hosting;
using Umbraco.Extensions;
using Umbraco.Web.Common.Install;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Routing
{
[TestFixture]
public class InstallAreaRoutesTests
{
[TestCase(RuntimeLevel.BootFailed)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Boot)]
public void RuntimeState_No_Routes(RuntimeLevel level)
{
var routes = GetInstallAreaRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(0, endpoints.DataSources.Count);
}
[TestCase(RuntimeLevel.Install)]
[TestCase(RuntimeLevel.Upgrade)]
public void RuntimeState_Install(RuntimeLevel level)
{
var routes = GetInstallAreaRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(2, endpoints.DataSources.Count);
var route = endpoints.DataSources.First();
Assert.AreEqual(2, route.Endpoints.Count);
var endpoint1 = (RouteEndpoint)route.Endpoints[0];
Assert.AreEqual($"install/api/{{action}}/{{id?}}", endpoint1.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.InstallArea, endpoint1.RoutePattern.Defaults["area"]);
Assert.AreEqual("Index", endpoint1.RoutePattern.Defaults["action"]);
Assert.AreEqual(ControllerExtensions.GetControllerName<InstallApiController>(), endpoint1.RoutePattern.Defaults["controller"]);
var endpoint2 = (RouteEndpoint)route.Endpoints[1];
Assert.AreEqual($"install/{{action}}/{{id?}}", endpoint2.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.InstallArea, endpoint2.RoutePattern.Defaults["area"]);
Assert.AreEqual("Index", endpoint2.RoutePattern.Defaults["action"]);
Assert.AreEqual(ControllerExtensions.GetControllerName<InstallController>(), endpoint2.RoutePattern.Defaults["controller"]);
var fallbackRoute = endpoints.DataSources.Last();
Assert.AreEqual(1, fallbackRoute.Endpoints.Count);
Assert.AreEqual("Fallback {*path:nonfile}", fallbackRoute.Endpoints[0].ToString());
}
[Test]
public void RuntimeState_Run()
{
var routes = GetInstallAreaRoutes(RuntimeLevel.Run);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(1, endpoints.DataSources.Count);
var route = endpoints.DataSources.First();
Assert.AreEqual(1, route.Endpoints.Count);
Assert.AreEqual("install/{controller?}/{action?} HTTP: GET", route.Endpoints[0].ToString());
}
private InstallAreaRoutes GetInstallAreaRoutes(RuntimeLevel level)
{
var routes = new InstallAreaRoutes(
Mock.Of<IRuntimeState>(x => x.Level == level),
Mock.Of<IHostingEnvironment>(x => x.ToAbsolute(It.IsAny<string>()) == "/install" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<LinkGenerator>());
return routes;
}
}
}

View File

@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Routing
{
public class TestRouteBuilder : IEndpointRouteBuilder
{
private readonly ServiceProvider _serviceProvider;
public TestRouteBuilder()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddMvc();
_serviceProvider = services.BuildServiceProvider();
}
public ICollection<EndpointDataSource> DataSources { get; } = new List<EndpointDataSource>();
public IServiceProvider ServiceProvider => _serviceProvider;
public IApplicationBuilder CreateApplicationBuilder()
{
return Mock.Of<IApplicationBuilder>();
}
}
}

View File

@@ -5,7 +5,7 @@ using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.BackOffice.Controllers
{
[Area(Constants.Web.Mvc.BackOfficeArea)] // TODO: Maybe this could be applied with our Application Model conventions
[PluginController(Constants.Web.Mvc.BackOfficeArea)] // TODO: Maybe this could be applied with our Application Model conventions
//[ValidationFilter] // TODO: I don't actually think this is required with our custom Application Model conventions applied
[TypeFilter(typeof(AngularJsonOnlyConfigurationAttribute))] // TODO: This could be applied with our Application Model conventions
[IsBackOffice] // TODO: This could be applied with our Application Model conventions

View File

@@ -7,11 +7,12 @@ using Umbraco.Net;
using Umbraco.Web.BackOffice.Filters;
using Umbraco.Web.Common.ActionResults;
using Umbraco.Web.WebAssets;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.BackOffice.Controllers
{
[Area(Umbraco.Core.Constants.Web.Mvc.BackOfficeArea)]
[Area(Constants.Web.Mvc.BackOfficeArea)]
public class BackOfficeController : Controller
{
private readonly IRuntimeMinifier _runtimeMinifier;

View File

@@ -22,7 +22,7 @@ using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.BackOffice.Controllers
{
[DisableBrowserCache]
[Area(Umbraco.Core.Constants.Web.Mvc.BackOfficeArea)]
[Area(Constants.Web.Mvc.BackOfficeArea)]
public class PreviewController : Controller
{
private readonly UmbracoFeatures _features;

View File

@@ -47,7 +47,7 @@ namespace Umbraco.Web.BackOffice.Routing
case RuntimeLevel.Run:
MapMinimalBackOffice(endpoints);
endpoints.MapUmbracoRoute<PreviewController>(_umbracoPathSegment, Constants.Web.Mvc.BackOfficeArea, "preview");
endpoints.MapUmbracoRoute<PreviewController>(_umbracoPathSegment, Constants.Web.Mvc.BackOfficeArea, null);
AutoRouteBackOfficeControllers(endpoints);
break;
@@ -67,6 +67,7 @@ namespace Umbraco.Web.BackOffice.Routing
endpoints.MapUmbracoRoute<BackOfficeController>(_umbracoPathSegment, Constants.Web.Mvc.BackOfficeArea,
string.Empty,
"Default",
includeControllerNameInRoute: false,
constraints:
// Limit the action/id to only allow characters - this is so this route doesn't hog all other
// routes like: /umbraco/channels/word.aspx, etc...

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Routing;
using System;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Hosting;
using Umbraco.Core.Logging;
using Umbraco.Extensions;
using Umbraco.Web.Common.Routing;
@@ -14,19 +15,19 @@ namespace Umbraco.Web.Common.Install
public class InstallAreaRoutes : IAreaRoutes
{
private readonly IRuntimeState _runtime;
private readonly UriUtility _uriUtility;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly LinkGenerator _linkGenerator;
public InstallAreaRoutes(IRuntimeState runtime, UriUtility uriUtility, LinkGenerator linkGenerator)
public InstallAreaRoutes(IRuntimeState runtime, IHostingEnvironment hostingEnvironment, LinkGenerator linkGenerator)
{
_runtime = runtime;
_uriUtility = uriUtility;
_hostingEnvironment = hostingEnvironment;
_linkGenerator = linkGenerator;
}
public void CreateRoutes(IEndpointRouteBuilder endpoints)
{
var installPathSegment = _uriUtility.ToAbsolute(Core.Constants.SystemDirectories.Install);
var installPathSegment = _hostingEnvironment.ToAbsolute(Core.Constants.SystemDirectories.Install).TrimStart('/');
switch (_runtime.Level)
{