mirror of
https://github.com/bitwarden/server.git
synced 2024-12-12 15:26:48 +01:00
aad36496e4
* Sorted usings * Added CoreHelpersTests * Added CloneObject test * Added comments to SelfHostedAttributeTests * Changed private variable declaration to be inline with the rest of the project * Changed to SUT naming scheme * Scaffolded AppleIapServiceTests * Scaffolded I18nServiceTests.cs * Scaffolded I18nViewLocalizerTests * Scaffolded LocalSendStorageServiceTests * Added tests to ReadableBytesSize_Success * Add more CleanCertificateThumbprint tests * Added more tests for the ExtendQuery method * Removed AppleIapServiceTests * Removed I18nServiceTests * Removed I18nViewLocalizerTests * Removed LocalSendStorageServiceTests * Converted Batch_Success to theory with InlineData * Simplified Batch_Success so there is less branching and more accuracy * Switched to using size variable to format readable bytes for any number lower than 1024 * Fixed test data to work in all locales
97 lines
2.5 KiB
C#
97 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Bit.Core.IdentityServer;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Primitives;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.IdentityServer
|
|
{
|
|
public class TokenRetrievalTests
|
|
{
|
|
private readonly Func<HttpRequest, string> _sut = 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<HttpRequest>();
|
|
|
|
request.Headers.Returns(headers);
|
|
|
|
// Act
|
|
var token = _sut(request);
|
|
|
|
// 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
|
|
var token = _sut(request);
|
|
|
|
// 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
|
|
var token = _sut(request);
|
|
|
|
// Assert
|
|
Assert.Equal("header_token", token);
|
|
}
|
|
|
|
[Fact]
|
|
public void RetrieveToken_NoToken_ReturnsNull()
|
|
{
|
|
// Arrange
|
|
var request = Substitute.For<HttpRequest>();
|
|
|
|
// Act
|
|
var token = _sut(request);
|
|
|
|
// Assert
|
|
Assert.Null(token);
|
|
}
|
|
}
|
|
}
|