2022-06-30 01:46:41 +02:00
|
|
|
|
using Bit.Core.Exceptions;
|
2021-02-22 22:35:16 +01:00
|
|
|
|
using Bit.Core.Settings;
|
2021-01-06 19:49:28 +01:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
2020-12-28 19:49:18 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2021-01-06 19:49:28 +01:00
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-12-28 19:49:18 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class SelfHostedAttributeTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NotSelfHosted_Throws_When_SelfHosted()
|
|
|
|
|
{
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Arrange
|
2020-12-28 19:49:18 +01:00
|
|
|
|
var sha = new SelfHostedAttribute { NotSelfHostedOnly = true };
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Act & Assert
|
2020-12-28 19:49:18 +01:00
|
|
|
|
Assert.Throws<BadRequestException>(() => sha.OnActionExecuting(GetContext(selfHosted: true)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NotSelfHosted_Success_When_NotSelfHosted()
|
|
|
|
|
{
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Arrange
|
2020-12-28 19:49:18 +01:00
|
|
|
|
var sha = new SelfHostedAttribute { NotSelfHostedOnly = true };
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Act
|
2020-12-28 19:49:18 +01:00
|
|
|
|
sha.OnActionExecuting(GetContext(selfHosted: false));
|
2021-01-06 19:49:28 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
// The Assert here is just NOT throwing an exception
|
2020-12-28 19:49:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SelfHosted_Success_When_SelfHosted()
|
|
|
|
|
{
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Arrange
|
2020-12-28 19:49:18 +01:00
|
|
|
|
var sha = new SelfHostedAttribute { SelfHostedOnly = true };
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Act
|
2020-12-28 19:49:18 +01:00
|
|
|
|
sha.OnActionExecuting(GetContext(selfHosted: true));
|
2021-01-06 19:49:28 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
// The Assert here is just NOT throwing an exception
|
2020-12-28 19:49:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SelfHosted_Throws_When_NotSelfHosted()
|
|
|
|
|
{
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Arrange
|
2020-12-28 19:49:18 +01:00
|
|
|
|
var sha = new SelfHostedAttribute { SelfHostedOnly = true };
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Act & Assert
|
2020-12-28 19:49:18 +01:00
|
|
|
|
Assert.Throws<BadRequestException>(() => sha.OnActionExecuting(GetContext(selfHosted: false)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This generates a ActionExecutingContext with the needed injected
|
|
|
|
|
// service with the given value.
|
|
|
|
|
private ActionExecutingContext GetContext(bool selfHosted)
|
|
|
|
|
{
|
|
|
|
|
IServiceCollection services = new ServiceCollection();
|
|
|
|
|
|
|
|
|
|
var globalSettings = new GlobalSettings
|
|
|
|
|
{
|
|
|
|
|
SelfHosted = selfHosted
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.AddSingleton(globalSettings);
|
|
|
|
|
|
|
|
|
|
var httpContext = new DefaultHttpContext();
|
|
|
|
|
httpContext.RequestServices = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
var context = Substitute.For<ActionExecutingContext>(
|
|
|
|
|
Substitute.For<ActionContext>(httpContext,
|
|
|
|
|
new RouteData(),
|
|
|
|
|
Substitute.For<ActionDescriptor>()),
|
|
|
|
|
new List<IFilterMetadata>(),
|
|
|
|
|
new Dictionary<string, object>(),
|
|
|
|
|
Substitute.For<Controller>());
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|