Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/InstallAreaRoutesTests.cs
Bjarke Berg 2d564a11b9 Maintenance page when in upgrade state (#13551)
* Added functionality to show maintenance page and fixed issues with showing custom api controllers and 404 page, when umbraco had been in install or upgrade state

* Fixed Tests

* Fixed typo

* Fixed issue with login screen redirecting to website when in upgrade state, instead of backoffice
2022-12-14 08:14:19 +01:00

99 lines
3.9 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.BackOffice.Install;
using Umbraco.Extensions;
using static Umbraco.Cms.Core.Constants.Web.Routing;
namespace Umbraco.Cms.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(1, 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[AreaToken]);
Assert.AreEqual("Index", endpoint1.RoutePattern.Defaults[ActionToken]);
Assert.AreEqual(
ControllerExtensions.GetControllerName<InstallApiController>(),
endpoint1.RoutePattern.Defaults[ControllerToken]);
Assert.AreEqual(
endpoint1.RoutePattern.Defaults[AreaToken],
typeof(InstallApiController).GetCustomAttribute<AreaAttribute>(false).RouteValue);
var endpoint2 = (RouteEndpoint)route.Endpoints[1];
Assert.AreEqual("install/{action}/{id?}", endpoint2.RoutePattern.RawText);
Assert.AreEqual(Constants.Web.Mvc.InstallArea, endpoint2.RoutePattern.Defaults[AreaToken]);
Assert.AreEqual("Index", endpoint2.RoutePattern.Defaults[ActionToken]);
Assert.AreEqual(
ControllerExtensions.GetControllerName<InstallController>(),
endpoint2.RoutePattern.Defaults[ControllerToken]);
Assert.AreEqual(
endpoint2.RoutePattern.Defaults[AreaToken],
typeof(InstallController).GetCustomAttribute<AreaAttribute>(false).RouteValue);
var dataSource = endpoints.DataSources.Last();
Assert.AreEqual(2, dataSource.Endpoints.Count);
Assert.AreEqual("Route: install/api/{action}/{id?}", dataSource.Endpoints[0].ToString());
Assert.AreEqual("Route: install/{action}/{id?}", dataSource.Endpoints[1].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(2, route.Endpoints.Count);
var routeEndpoint = (RouteEndpoint)route.Endpoints[0];
Assert.AreEqual("install/api/{action}/{id?}", routeEndpoint.RoutePattern.RawText);
routeEndpoint = (RouteEndpoint)route.Endpoints[1];
Assert.AreEqual("install/{action}/{id?}", routeEndpoint.RoutePattern.RawText);
}
private InstallAreaRoutes GetInstallAreaRoutes(RuntimeLevel level) =>
new(
Mock.Of<IRuntimeState>(x => x.Level == level),
Mock.Of<IHostingEnvironment>(x =>
x.ToAbsolute(It.IsAny<string>()) == "/install" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<LinkGenerator>());
}