Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/BackOfficeAreaRoutesTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

100 lines
4.6 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.BackOffice.Routing;
using Umbraco.Cms.Web.Common.Attributes;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Extensions;
using Constants = Umbraco.Cms.Core.Constants;
using static Umbraco.Cms.Core.Constants.Web.Routing;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Routing
{
[TestFixture]
public class BackOfficeAreaRoutesTests
{
[TestCase(RuntimeLevel.BootFailed)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Boot)]
public void RuntimeState_No_Routes(RuntimeLevel level)
{
BackOfficeAreaRoutes routes = GetBackOfficeAreaRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(0, endpoints.DataSources.Count);
}
[Test]
[TestCase(RuntimeLevel.Run)]
[TestCase(RuntimeLevel.Upgrade)]
[TestCase(RuntimeLevel.Install)]
public void RuntimeState_All_Routes(RuntimeLevel level)
{
BackOfficeAreaRoutes routes = GetBackOfficeAreaRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(1, endpoints.DataSources.Count);
EndpointDataSource route = endpoints.DataSources.First();
Assert.AreEqual(3, route.Endpoints.Count);
AssertMinimalBackOfficeRoutes(route);
var endpoint4 = (RouteEndpoint)route.Endpoints[2];
string apiControllerName = ControllerExtensions.GetControllerName<Testing1Controller>();
Assert.AreEqual($"umbraco/backoffice/api/{apiControllerName.ToLowerInvariant()}/{{action}}/{{id?}}", endpoint4.RoutePattern.RawText);
Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey(AreaToken));
Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey(ActionToken));
Assert.AreEqual(apiControllerName, endpoint4.RoutePattern.Defaults[ControllerToken]);
}
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[AreaToken]);
Assert.AreEqual("Default", endpoint1.RoutePattern.Defaults[ActionToken]);
Assert.AreEqual(ControllerExtensions.GetControllerName<BackOfficeController>(), endpoint1.RoutePattern.Defaults[ControllerToken]);
Assert.AreEqual(endpoint1.RoutePattern.Defaults[AreaToken], typeof(BackOfficeController).GetCustomAttribute<AreaAttribute>(false).RouteValue);
var endpoint2 = (RouteEndpoint)route.Endpoints[1];
string controllerName = ControllerExtensions.GetControllerName<AuthenticationController>();
Assert.AreEqual($"umbraco/backoffice/{Constants.Web.Mvc.BackOfficeApiArea.ToLowerInvariant()}/{controllerName.ToLowerInvariant()}/{{action}}/{{id?}}", endpoint2.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.BackOfficeApiArea, endpoint2.RoutePattern.Defaults[AreaToken]);
Assert.IsFalse(endpoint2.RoutePattern.Defaults.ContainsKey(ActionToken));
Assert.AreEqual(controllerName, endpoint2.RoutePattern.Defaults[ControllerToken]);
Assert.AreEqual(endpoint1.RoutePattern.Defaults[AreaToken], typeof(BackOfficeController).GetCustomAttribute<AreaAttribute>(false).RouteValue);
}
private BackOfficeAreaRoutes GetBackOfficeAreaRoutes(RuntimeLevel level)
{
var globalSettings = new GlobalSettings();
var routes = new BackOfficeAreaRoutes(
Options.Create(globalSettings),
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
{
}
}
}