2020-06-17 16:39:28 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Net.Http;
|
2020-09-02 18:10:29 +10:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-06-17 16:39:28 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-09-02 18:10:29 +10:00
|
|
|
using Microsoft.AspNetCore.TestHost;
|
2020-06-17 16:39:28 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-12-21 15:58:47 +11:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-06-17 16:39:28 +02:00
|
|
|
using NUnit.Framework;
|
2020-09-02 18:10:29 +10:00
|
|
|
using Umbraco.Core;
|
2020-12-21 15:58:47 +11:00
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
using Umbraco.Core.DependencyInjection;
|
2020-06-17 16:39:28 +02:00
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2020-06-30 20:11:39 +02:00
|
|
|
using Umbraco.Tests.Testing;
|
2020-06-17 16:39:28 +02:00
|
|
|
using Umbraco.Web;
|
2020-09-22 19:59:01 +02:00
|
|
|
using Umbraco.Web.BackOffice.Controllers;
|
2020-12-21 15:58:47 +11:00
|
|
|
using Umbraco.Web.Common.Controllers;
|
|
|
|
|
using Umbraco.Web.Website.Controllers;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration.TestServerTest
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2020-10-16 09:54:03 +02:00
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console, Boot = true)]
|
2020-06-17 16:39:28 +02:00
|
|
|
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest
|
|
|
|
|
{
|
|
|
|
|
[SetUp]
|
2020-10-05 10:02:11 +02:00
|
|
|
public override void Setup()
|
2020-06-17 16:39:28 +02:00
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
InMemoryConfiguration["ConnectionStrings:" + Constants.System.UmbracoConnectionName] = null;
|
|
|
|
|
InMemoryConfiguration["Umbraco:CMS:Hosting:Debug"] = "true";
|
|
|
|
|
|
|
|
|
|
// create new WebApplicationFactory specifying 'this' as the IStartup instance
|
2020-12-17 12:18:28 +00:00
|
|
|
var factory = new UmbracoWebApplicationFactory<UmbracoTestServerTestBase>(CreateHostBuilder, BeforeHostStart);
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
// additional host configuration for web server integration tests
|
|
|
|
|
Factory = factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
// Executes after the standard ConfigureServices method
|
|
|
|
|
builder.ConfigureTestServices(services =>
|
|
|
|
|
{
|
2020-11-26 16:52:03 +11:00
|
|
|
// Add a test auth scheme with a test auth handler to authn and assign the user
|
|
|
|
|
services.AddAuthentication(TestAuthHandler.TestAuthenticationScheme)
|
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.TestAuthenticationScheme, options => { });
|
2020-09-02 18:10:29 +10:00
|
|
|
});
|
2020-06-17 16:39:28 +02:00
|
|
|
});
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
Client = Factory.CreateClient(new WebApplicationFactoryClientOptions
|
2020-09-04 00:27:43 +10:00
|
|
|
{
|
|
|
|
|
AllowAutoRedirect = false
|
2020-09-02 18:10:29 +10:00
|
|
|
});
|
|
|
|
|
|
2020-06-17 16:39:28 +02:00
|
|
|
LinkGenerator = Factory.Services.GetRequiredService<LinkGenerator>();
|
2020-08-31 11:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
public override IHostBuilder CreateHostBuilder()
|
2020-08-31 11:31:56 +02:00
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
var builder = base.CreateHostBuilder();
|
|
|
|
|
builder.ConfigureWebHost(builder =>
|
2020-09-04 00:27:43 +10:00
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
// need to configure the IWebHostEnvironment too
|
2020-09-04 00:27:43 +10:00
|
|
|
builder.ConfigureServices((c, s) =>
|
|
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
c.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// call startup
|
|
|
|
|
builder.Configure(app =>
|
|
|
|
|
{
|
|
|
|
|
Configure(app);
|
|
|
|
|
});
|
2020-12-16 01:54:49 +00:00
|
|
|
|
|
|
|
|
}).UseEnvironment(Environments.Development);
|
2020-09-04 00:27:43 +10:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
return builder;
|
2020-06-17 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 12:55:23 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Prepare a url before using <see cref="Client"/>.
|
|
|
|
|
/// This returns the url but also sets the HttpContext.request into to use this url.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The string URL of the controller action.</returns>
|
2020-06-17 16:39:28 +02:00
|
|
|
protected string PrepareUrl<T>(Expression<Func<T, object>> methodSelector)
|
|
|
|
|
where T : UmbracoApiController
|
|
|
|
|
{
|
|
|
|
|
var url = LinkGenerator.GetUmbracoApiService<T>(methodSelector);
|
|
|
|
|
|
2020-10-21 16:51:00 +11:00
|
|
|
var backofficeSecurityFactory = GetRequiredService<IBackOfficeSecurityFactory>();
|
2020-06-17 16:39:28 +02:00
|
|
|
var umbracoContextFactory = GetRequiredService<IUmbracoContextFactory>();
|
|
|
|
|
var httpContextAccessor = GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
|
|
|
|
|
httpContextAccessor.HttpContext = new DefaultHttpContext
|
|
|
|
|
{
|
|
|
|
|
Request =
|
|
|
|
|
{
|
|
|
|
|
Scheme = "https",
|
|
|
|
|
Host = new HostString("localhost", 80),
|
|
|
|
|
Path = url,
|
|
|
|
|
QueryString = new QueryString(string.Empty)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-21 16:51:00 +11:00
|
|
|
backofficeSecurityFactory.EnsureBackOfficeSecurity();
|
2020-06-17 16:39:28 +02:00
|
|
|
umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
protected HttpClient Client { get; private set; }
|
2020-12-17 16:27:28 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
protected LinkGenerator LinkGenerator { get; private set; }
|
2020-12-17 16:27:28 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
protected WebApplicationFactory<UmbracoTestServerTestBase> Factory { get; private set; }
|
2020-06-17 16:39:28 +02:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2020-12-11 18:20:07 +00:00
|
|
|
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
2020-12-21 15:58:47 +11:00
|
|
|
var typeLoader = services.AddTypeLoader(
|
|
|
|
|
GetType().Assembly,
|
|
|
|
|
TestHelper.GetWebHostEnvironment(),
|
|
|
|
|
TestHelper.GetHostingEnvironment(),
|
|
|
|
|
TestHelper.ConsoleLoggerFactory,
|
|
|
|
|
AppCaches.NoCache,
|
|
|
|
|
Configuration,
|
|
|
|
|
TestHelper.Profiler);
|
2020-11-20 12:24:16 +00:00
|
|
|
|
|
|
|
|
var builder = new UmbracoBuilder(services, Configuration, typeLoader);
|
2020-12-11 18:20:07 +00:00
|
|
|
|
2020-11-20 12:24:16 +00:00
|
|
|
builder
|
2020-11-19 09:06:04 +00:00
|
|
|
.AddConfiguration()
|
2020-11-19 20:05:28 +00:00
|
|
|
.AddTestCore(TestHelper) // This is the important one!
|
2020-11-19 09:06:04 +00:00
|
|
|
.AddWebComponents()
|
|
|
|
|
.AddRuntimeMinifier()
|
|
|
|
|
.AddBackOffice()
|
|
|
|
|
.AddBackOfficeIdentity()
|
2020-11-26 16:52:03 +11:00
|
|
|
.AddBackOfficeAuthorizationPolicies(TestAuthHandler.TestAuthenticationScheme)
|
2020-11-19 09:06:04 +00:00
|
|
|
.AddPreviewSupport()
|
|
|
|
|
.AddMvcAndRazor(mvcBuilding: mvcBuilder =>
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
2020-12-21 15:58:47 +11:00
|
|
|
// Adds Umbraco.Web.BackOffice
|
2020-09-04 00:27:43 +10:00
|
|
|
mvcBuilder.AddApplicationPart(typeof(ContentController).Assembly);
|
2020-12-21 15:58:47 +11:00
|
|
|
|
|
|
|
|
// Adds Umbraco.Web.Common
|
|
|
|
|
mvcBuilder.AddApplicationPart(typeof(RenderController).Assembly);
|
|
|
|
|
|
|
|
|
|
// Adds Umbraco.Web.Website
|
|
|
|
|
mvcBuilder.AddApplicationPart(typeof(SurfaceController).Assembly);
|
2020-09-02 18:10:29 +10:00
|
|
|
})
|
2020-11-19 09:06:04 +00:00
|
|
|
.AddWebServer()
|
2020-09-02 18:10:29 +10:00
|
|
|
.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Configure(IApplicationBuilder app)
|
2020-09-04 00:27:43 +10:00
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
app.UseUmbraco();
|
2020-12-21 15:58:47 +11:00
|
|
|
app.UseUmbracoBackOffice();
|
|
|
|
|
app.UseUmbracoWebsite();
|
2020-06-17 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|