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;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2020-12-28 19:49:18 +01:00
|
|
|
|
public class SelfHostedAttributeTests
|
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Fact]
|
2020-12-28 19:49:18 +01:00
|
|
|
|
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)));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
[Fact]
|
2020-12-28 19:49:18 +01:00
|
|
|
|
public void NotSelfHosted_Success_When_NotSelfHosted()
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
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
|
|
|
|
|
sha.OnActionExecuting(GetContext(selfHosted: false));
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
// Assert
|
|
|
|
|
// The Assert here is just NOT throwing an exception
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
|
2021-01-06 19:49:28 +01:00
|
|
|
|
[Fact]
|
2020-12-28 19:49:18 +01:00
|
|
|
|
public void SelfHosted_Success_When_SelfHosted()
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
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
|
|
|
|
|
sha.OnActionExecuting(GetContext(selfHosted: true));
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
// The Assert here is just NOT throwing an exception
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SelfHosted_Throws_When_NotSelfHosted()
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2020-12-28 19:49:18 +01:00
|
|
|
|
// Arrange
|
|
|
|
|
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)));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This generates a ActionExecutingContext with the needed injected
|
|
|
|
|
// service with the given value.
|
|
|
|
|
private ActionExecutingContext GetContext(bool selfHosted)
|
|
|
|
|
{
|
|
|
|
|
IServiceCollection services = new ServiceCollection();
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2020-12-28 19:49:18 +01:00
|
|
|
|
var globalSettings = new GlobalSettings
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2020-12-28 19:49:18 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|