Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/PreviewRoutesTests.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

76 lines
3.2 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.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 PreviewRoutesTests
{
[TestCase(RuntimeLevel.BootFailed)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Boot)]
public void RuntimeState_No_Routes(RuntimeLevel level)
{
PreviewRoutes routes = GetRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(0, endpoints.DataSources.Count);
}
[TestCase(RuntimeLevel.Run)]
[TestCase(RuntimeLevel.Upgrade)]
[TestCase(RuntimeLevel.Install)]
public void RuntimeState_All_Routes(RuntimeLevel level)
{
PreviewRoutes routes = GetRoutes(level);
var endpoints = new TestRouteBuilder();
routes.CreateRoutes(endpoints);
Assert.AreEqual(2, endpoints.DataSources.Count);
EndpointDataSource route = endpoints.DataSources.First();
Assert.AreEqual(2, route.Endpoints.Count);
var endpoint0 = (RouteEndpoint)route.Endpoints[0];
Assert.AreEqual($"{routes.GetPreviewHubRoute()}/negotiate", endpoint0.RoutePattern.RawText);
var endpoint1 = (RouteEndpoint)route.Endpoints[1];
Assert.AreEqual($"{routes.GetPreviewHubRoute()}", endpoint1.RoutePattern.RawText);
var endpoint3 = (RouteEndpoint)endpoints.DataSources.Last().Endpoints[0];
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[ActionToken]);
Assert.AreEqual(previewControllerName, endpoint3.RoutePattern.Defaults[ControllerToken]);
Assert.AreEqual(endpoint3.RoutePattern.Defaults["area"], typeof(PreviewController).GetCustomAttribute<AreaAttribute>(false).RouteValue);
}
private PreviewRoutes GetRoutes(RuntimeLevel level)
{
var globalSettings = new GlobalSettings();
var routes = new PreviewRoutes(
Options.Create(globalSettings),
Mock.Of<IHostingEnvironment>(x =>
x.ToAbsolute(It.IsAny<string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<IRuntimeState>(x => x.Level == level));
return routes;
}
}
}