Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.New.Cms.Core/Services/InstallServiceTests.cs
Mole 748fb7d1f7 Implement new backoffice installer (#12790)
* Add new BackOfficeApi project

* Add swagger

* Add and route new install controller

* Add new install steps

* Add Setup endpoint

* Add missing RequiresExecution methods

* Fix nullability of databasemodel

* Move user information to separate model

* Remove ping method

* Add view models install data

* Move mapping folder

* Move ViewModels

* Add settings endpoint

* Remove unused binderprovider

* Postfix RequiresExecution with async

* Update NewDatabaseUpgradeStep to not depend on install step

* Add installstep collection

* Move registration into backoffice project

* Add InstallService

* Use service in controller

* Add upgrade to install service and use in controller

* Correctly check is database is configured

* Reorganize

* Reorganize into new core and infrastructure

* Rename steps

* Rename BackofficeApi to MangementApi

* Make install step an interface instead of abstract class

* Rename InstallStep to create CreateUserStep

* Move restart runtime and sign in user into install steps

* Move install service into new core project

* Map controllers in composer

* Restrict access to installcontroller based on runtime level

* Use FireAndForget when logging install

* Use actionresult instead of iactionresult

* Set new projects as not packable

* Link to backoffice in 201 response when installed

* Register installations

* Add custom backoffice routing template token

* Move umbraco path trimming out of application convention

* Make it easier to route to backoffice api

* Make swagger version aware and move behind backoffice path

* Obsolete old install classes

* Move maps into single file

This is all mappint to/from viewmodels in some manner

* Remove usage of InstallSetupResult

* Move new projects to the src folder

* Remove InstallationType from IInstallStep

This upgrade steps should implement their own IUpgradeStep interface

* Remove upgrade from service and controller

This should be its own service and controller

* Add xml docs

* Remove internals visible to

* Disable package validation for new projects

Quite the gotcha here, if the projects are brand new, there is no nuget packages to compare with, this causes the build to fail.

* Add ValidateDatabase endpoint

* Remove project references to new backoffice

We don't actually want to depend on this yet, it's just needed for testing/development

* Obsolete installationtype

* Add DatabaseSettingsFactory tests

* Add InstallServiceTests

* Fix InstallServiceTests

* Test RequireRuntimeLevelAttribute

* Implement new backoffice upgrader (#12818)

* Add UpgradeSettingsModel and viewmodel

* Add upgrade/settings endpoint

* Implement upgrade steps

* Add upgrade step collection

* Add UpgradeService

* Add authorize endpoint to UpgradeController

* Fix interface

* Add upgrade service tests

* Remove runtime check in databaseinstallstep

* Move RequireRuntimeLevel to controller

* Add a readme to the new backoffice part

* BackOffice not Backoffice

* Add conditional project references

* Fixes based on review

* Fix up

* Move running of steps into its own method in UpgradeService

* Make services transient

* More fixup

* Log exceptions when running steps
2022-08-29 09:50:48 +02:00

112 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Services;
using Umbraco.New.Cms.Core.Installer;
using Umbraco.New.Cms.Core.Models.Installer;
using Umbraco.New.Cms.Core.Services.Installer;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.New.Cms.Core.Services;
[TestFixture]
public class InstallServiceTests
{
[Test]
public void RequiresInstallRuntimeToInstall()
{
var runtimeStateMock = new Mock<IRuntimeState>();
runtimeStateMock.Setup(x => x.Level).Returns(RuntimeLevel.Run);
var stepCollection = new NewInstallStepCollection(Enumerable.Empty<IInstallStep>);
var sut = new InstallService(Mock.Of<ILogger<InstallService>>(), stepCollection, runtimeStateMock.Object);
Assert.ThrowsAsync<InvalidOperationException>(async () => await sut.Install(new InstallData()));
}
[Test]
public async Task OnlyRunsStepsThatRequireExecution()
{
var steps = new[]
{
new TestInstallStep { ShouldRun = true },
new TestInstallStep { ShouldRun = false },
new TestInstallStep { ShouldRun = true },
};
var sut = CreateInstallService(steps);
await sut.Install(new InstallData());
foreach (var step in steps)
{
Assert.AreEqual(step.ShouldRun, step.HasRun);
}
}
[Test]
public async Task StepsRunInCollectionOrder()
{
List<TestInstallStep> runOrder = new List<TestInstallStep>();
var steps = new[]
{
new TestInstallStep { Id = 1 },
new TestInstallStep { Id = 2 },
new TestInstallStep { Id = 3 },
};
// Add an method delegate that will add the step itself, that way we can know the executed order.
foreach (var step in steps)
{
step.AdditionalExecution = _ =>
{
runOrder.Add(step);
return Task.CompletedTask;
};
}
var sut = CreateInstallService(steps);
await sut.Install(new InstallData());
// The ID's are strictly not necessary, but it makes potential debugging easier.
var expectedRunOrder = steps.Select(x => x.Id);
var actualRunOrder = runOrder.Select(x => x.Id);
Assert.AreEqual(expectedRunOrder, actualRunOrder);
}
private InstallService CreateInstallService(IEnumerable<IInstallStep> steps)
{
var logger = Mock.Of<ILogger<InstallService>>();
var stepCollection = new NewInstallStepCollection(() => steps);
var runtimeStateMock = new Mock<IRuntimeState>();
runtimeStateMock.Setup(x => x.Level).Returns(RuntimeLevel.Install);
return new InstallService(logger, stepCollection, runtimeStateMock.Object);
}
private class TestInstallStep : IInstallStep
{
public bool HasRun;
public bool ShouldRun = true;
public int Id;
public Func<InstallData, Task> AdditionalExecution;
public Task ExecuteAsync(InstallData model)
{
HasRun = true;
AdditionalExecution?.Invoke(model);
return Task.CompletedTask;
}
public Task<bool> RequiresExecutionAsync(InstallData model) => Task.FromResult(ShouldRun);
}
}