From 6803b61b9b59b61656ae465631bdfae17dbfc90a Mon Sep 17 00:00:00 2001 From: Justin Baur Date: Mon, 28 Dec 2020 13:49:18 -0500 Subject: [PATCH] Core Tests (#1062) * Added tests for SelfHostedAttribute * Added Token Retrieval Tests --- .../IdentityServer/TokenRetrievalTests.cs | 96 +++++++++++++++++++ .../Utilities/SelfHostedAttributeTests.cs | 79 +++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 test/Core.Test/IdentityServer/TokenRetrievalTests.cs create mode 100644 test/Core.Test/Utilities/SelfHostedAttributeTests.cs diff --git a/test/Core.Test/IdentityServer/TokenRetrievalTests.cs b/test/Core.Test/IdentityServer/TokenRetrievalTests.cs new file mode 100644 index 0000000000..1375ff1660 --- /dev/null +++ b/test/Core.Test/IdentityServer/TokenRetrievalTests.cs @@ -0,0 +1,96 @@ +using System; +using Microsoft.AspNetCore.Http; +using Bit.Core.IdentityServer; +using Xunit; +using NSubstitute; +using System.Collections.Generic; +using Microsoft.Extensions.Primitives; + +namespace Bit.Core.Test.IdentityServer +{ + public class TokenRetrievalTests + { + private Func Retrieve = TokenRetrieval.FromAuthorizationHeaderOrQueryString(); + + [Fact] + public void RetrieveToken_FromHeader_ReturnsToken() + { + // Arrange + var headers = new HeaderDictionary + { + { "Authorization", "Bearer test_value" }, + { "X-Test-Header", "random_value" } + }; + + var request = Substitute.For(); + + request.Headers.Returns(headers); + + // Act + var token = Retrieve(request); + + // Assert + Assert.Equal("test_value", token); + } + + [Fact] + public void RetrieveToken_FromQueryString_ReturnsToken() + { + // Arrange + var queryString = new Dictionary + { + { "access_token", "test_value" }, + { "test-query", "random_value" } + }; + + var request = Substitute.For(); + request.Query.Returns(new QueryCollection(queryString)); + + // Act + var token = Retrieve(request); + + // Assert + Assert.Equal("test_value", token); + } + + [Fact] + public void RetrieveToken_HasBoth_ReturnsHeaderToken() + { + // Arrange + var queryString = new Dictionary + { + { "access_token", "query_string_token" }, + { "test-query", "random_value" } + }; + + var headers = new HeaderDictionary + { + { "Authorization", "Bearer header_token" }, + { "X-Test-Header", "random_value" } + }; + + var request = Substitute.For(); + request.Headers.Returns(headers); + request.Query.Returns(new QueryCollection(queryString)); + + // Act + var token = Retrieve(request); + + // Assert + Assert.Equal("header_token", token); + } + + [Fact] + public void RetrieveToken_NoToken_ReturnsNull() + { + // Arrange + var request = Substitute.For(); + + // Act + var token = Retrieve(request); + + // Assert + Assert.Null(token); + } + } +} diff --git a/test/Core.Test/Utilities/SelfHostedAttributeTests.cs b/test/Core.Test/Utilities/SelfHostedAttributeTests.cs new file mode 100644 index 0000000000..28e8b02641 --- /dev/null +++ b/test/Core.Test/Utilities/SelfHostedAttributeTests.cs @@ -0,0 +1,79 @@ + +using Bit.Core.Utilities; +using Bit.Core.Exceptions; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Xunit; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.Mvc.Abstractions; + +namespace Bit.Core.Test.Utilities +{ + public class SelfHostedAttributeTests + { + [Fact] + public void NotSelfHosted_Throws_When_SelfHosted() + { + var sha = new SelfHostedAttribute { NotSelfHostedOnly = true }; + + Assert.Throws(() => sha.OnActionExecuting(GetContext(selfHosted: true))); + } + + [Fact] + public void NotSelfHosted_Success_When_NotSelfHosted() + { + var sha = new SelfHostedAttribute { NotSelfHostedOnly = true }; + + sha.OnActionExecuting(GetContext(selfHosted: false)); + } + + + [Fact] + public void SelfHosted_Success_When_SelfHosted() + { + var sha = new SelfHostedAttribute { SelfHostedOnly = true }; + + sha.OnActionExecuting(GetContext(selfHosted: true)); + } + + [Fact] + public void SelfHosted_Throws_When_NotSelfHosted() + { + var sha = new SelfHostedAttribute { SelfHostedOnly = true }; + + Assert.Throws(() => 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( + Substitute.For(httpContext, + new RouteData(), + Substitute.For()), + new List(), + new Dictionary(), + Substitute.For()); + + return context; + } + } +} \ No newline at end of file