Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModularTenantTests #14831

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace OrchardCore.Modules;

public static class ShellPipelineExtensions
{
private const string EndpointRouteBuilder = "__EndpointRouteBuilder";
public const string EndpointRouteBuilder = "__EndpointRouteBuilder";

private static readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphores = new();

Expand Down
110 changes: 110 additions & 0 deletions test/OrchardCore.Tests/Shell/ModularTenantTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using OrchardCore.DisplayManagement.FileProviders;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Builders;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Locking;
using OrchardCore.Locking.Distributed;
using OrchardCore.Modules;
using OrchardCore.Tests.Stubs;

namespace OrchardCore.Tests.Shell;

public class ModularTenantTests
{
[Fact]
public async Task ConfigureIStartupServicesBeforeRoutes()
{
var services = new ServiceCollection();

services.AddSingleton<IHostEnvironment>(new StubHostingEnvironment())
.AddSingleton<IWebHostEnvironment, FakeWebHostEnvironment>();

services.AddOrchardCore();
services.AddSingleton<IStartupFilter, TestStartupFilter>();
services.AddSingleton<IDistributedLock, LocalLock>()
.AddLogging()
;

var shellSettings = new ShellSettings().AsDefaultShell().AsRunning();

var shellContext = new ShellContext()
{
Settings = shellSettings,
ServiceProvider = services.BuildServiceProvider(),
};

await (await shellContext.CreateScopeAsync()).UsingAsync(scope =>
{
try
{
var builder = new ApplicationBuilder(scope.ShellContext.ServiceProvider);

builder.UseOrchardCore();

var app = builder.Build();

var httpContext = new DefaultHttpContext
{
RequestServices = new ShellScopeServices(scope.ShellContext.ServiceProvider)
};

app.Invoke(httpContext);
}
catch (Exception ex)
{
Assert.Fail("Expected no exception, but got: " + ex.Message);
}

return Task.CompletedTask;
});
}

internal sealed class FakeWebHostEnvironment : IWebHostEnvironment
{
public string ApplicationName { get; set; } = "Benchmark";

public IFileProvider ContentRootFileProvider { get; set; } = new FakeFileProvider();

public string ContentRootPath { get; set; }

public string EnvironmentName { get; set; }

public IFileProvider WebRootFileProvider { get; set; } = new FakeFileProvider();

public string WebRootPath { get; set; }
}

internal class FakeFileProvider : IFileProvider
{
public IDirectoryContents GetDirectoryContents(string subpath)
{
return new NotFoundDirectoryContents();
}

public IFileInfo GetFileInfo(string subpath)
{
return new ContentFileInfo("name", "content");
}

public IChangeToken Watch(string filter)
{
return new CancellationChangeToken(CancellationToken.None);
}
}

private class TestStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
if (app.Properties.TryGetValue(ShellPipelineExtensions.EndpointRouteBuilder, out var obj))
{
throw new InvalidOperationException($"{nameof(IStartupFilter.Configure)} must be in execution pipeline before {nameof(EndpointRoutingApplicationBuilderExtensions.UseRouting)} to 'Configure(...)' in the application startup code.");
}

next(app);
};
}
}
}