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 System.Threading.Tasks;
|
|
|
|
|
|
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;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Composing;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
using Umbraco.Core;
|
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-02 18:10:29 +10:00
|
|
|
|
using Umbraco.Web.Common.Builder;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
using Umbraco.Web.Common.Controllers;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
using Umbraco.Web.Editors;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration.TestServerTest
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2020-06-30 20:11:39 +02:00
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console, Boot = false)]
|
2020-06-17 16:39:28 +02:00
|
|
|
|
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest
|
|
|
|
|
|
{
|
|
|
|
|
|
[SetUp]
|
2020-09-02 18:10:29 +10:00
|
|
|
|
public override Task 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
|
|
|
|
|
|
var factory = new UmbracoWebApplicationFactory<UmbracoTestServerTestBase>(CreateHostBuilder);
|
|
|
|
|
|
|
|
|
|
|
|
// additional host configuration for web server integration tests
|
|
|
|
|
|
Factory = factory.WithWebHostBuilder(builder =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// Executes after the standard ConfigureServices method
|
|
|
|
|
|
builder.ConfigureTestServices(services =>
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddAuthentication("Test").AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
|
|
|
|
|
|
});
|
2020-06-17 16:39:28 +02:00
|
|
|
|
});
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
|
|
Client = Factory.CreateClient(new WebApplicationFactoryClientOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
AllowAutoRedirect = false
|
|
|
|
|
|
});
|
|
|
|
|
|
|
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
|
|
|
|
return Task.CompletedTask;
|
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 =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// need to configure the IWebHostEnvironment too
|
|
|
|
|
|
builder.ConfigureServices((c, s) => {
|
|
|
|
|
|
c.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// call startup
|
|
|
|
|
|
builder.Configure(app =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Services = app.ApplicationServices;
|
|
|
|
|
|
Configure(app);
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
.UseEnvironment(Environments.Development);
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
|
protected HttpClient Client { get; private set; }
|
|
|
|
|
|
protected LinkGenerator LinkGenerator { get; private set; }
|
|
|
|
|
|
protected WebApplicationFactory<UmbracoTestServerTestBase> Factory { get; private set; }
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
2020-09-02 18:10:29 +10:00
|
|
|
|
public override void TearDown()
|
2020-06-17 16:39:28 +02:00
|
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
|
base.TearDown();
|
2020-06-18 14:40:30 +02:00
|
|
|
|
|
|
|
|
|
|
Factory.Dispose();
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
if (Current.IsInitialized)
|
|
|
|
|
|
{
|
2020-07-06 13:01:25 +02:00
|
|
|
|
Current.IsInitialized = false;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region IStartup
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
//services.AddSingleton(TestHelper.DbProviderFactoryCreator);
|
|
|
|
|
|
//var webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
|
|
|
|
|
//services.AddRequiredNetCoreServices(TestHelper, webHostEnvironment);
|
|
|
|
|
|
|
|
|
|
|
|
var umbracoBuilder = services.AddUmbraco(TestHelper.GetWebHostEnvironment(), Configuration);
|
|
|
|
|
|
umbracoBuilder
|
|
|
|
|
|
.WithConfiguration()
|
|
|
|
|
|
.WithTestCore(TestHelper, UmbracoContainer, UseTestLocalDb) // This is the important one!
|
|
|
|
|
|
.WithWebComponents()
|
|
|
|
|
|
.WithRuntimeMinifier()
|
|
|
|
|
|
.WithBackOffice()
|
|
|
|
|
|
.WithBackOfficeIdentity()
|
|
|
|
|
|
//.WithMiniProfiler()
|
|
|
|
|
|
.WithMvcAndRazor(mvcBuilding: mvcBuilder =>
|
|
|
|
|
|
{
|
|
|
|
|
|
mvcBuilder.AddApplicationPart(typeof(ContentController).Assembly);
|
|
|
|
|
|
})
|
|
|
|
|
|
.WithWebServer()
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Configure(IApplicationBuilder app)
|
|
|
|
|
|
{
|
|
|
|
|
|
app.UseUmbraco();
|
2020-06-17 16:39:28 +02:00
|
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-17 16:39:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|