2022-06-30 01:46:41 +02:00
|
|
|
|
using Bit.Core.IdentityServer;
|
2021-01-06 19:49:28 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-12-28 19:49:18 +01:00
|
|
|
|
using Microsoft.Extensions.Primitives;
|
2021-01-06 19:49:28 +01:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.IdentityServer
|
|
|
|
|
{
|
|
|
|
|
public class TokenRetrievalTests
|
|
|
|
|
{
|
2021-01-06 19:49:28 +01:00
|
|
|
|
private readonly Func<HttpRequest, string> _sut = TokenRetrieval.FromAuthorizationHeaderOrQueryString();
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RetrieveToken_FromHeader_ReturnsToken()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var headers = new HeaderDictionary
|
|
|
|
|
{
|
|
|
|
|
{ "Authorization", "Bearer test_value" },
|
|
|
|
|
{ "X-Test-Header", "random_value" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var request = Substitute.For<HttpRequest>();
|
|
|
|
|
|
|
|
|
|
request.Headers.Returns(headers);
|
|
|
|
|
|
|
|
|
|
// Act
|
2021-01-06 19:49:28 +01:00
|
|
|
|
var token = _sut(request);
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal("test_value", token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RetrieveToken_FromQueryString_ReturnsToken()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var queryString = new Dictionary<string, StringValues>
|
|
|
|
|
{
|
|
|
|
|
{ "access_token", "test_value" },
|
|
|
|
|
{ "test-query", "random_value" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var request = Substitute.For<HttpRequest>();
|
|
|
|
|
request.Query.Returns(new QueryCollection(queryString));
|
|
|
|
|
|
|
|
|
|
// Act
|
2021-01-06 19:49:28 +01:00
|
|
|
|
var token = _sut(request);
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal("test_value", token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RetrieveToken_HasBoth_ReturnsHeaderToken()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var queryString = new Dictionary<string, StringValues>
|
|
|
|
|
{
|
|
|
|
|
{ "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<HttpRequest>();
|
|
|
|
|
request.Headers.Returns(headers);
|
|
|
|
|
request.Query.Returns(new QueryCollection(queryString));
|
|
|
|
|
|
|
|
|
|
// Act
|
2021-01-06 19:49:28 +01:00
|
|
|
|
var token = _sut(request);
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal("header_token", token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RetrieveToken_NoToken_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var request = Substitute.For<HttpRequest>();
|
|
|
|
|
|
|
|
|
|
// Act
|
2021-01-06 19:49:28 +01:00
|
|
|
|
var token = _sut(request);
|
2020-12-28 19:49:18 +01:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Null(token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|