1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

Added Services and Utilities Tests to Core.Test project (#1068)

* 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
This commit is contained in:
Justin Baur 2021-01-06 13:49:28 -05:00 committed by GitHub
parent d96da37e2a
commit aad36496e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 250 additions and 17 deletions

View File

@ -349,7 +349,7 @@ namespace Bit.Core.Utilities
}
else
{
return absoluteSize.ToString("0 Bytes"); // Byte
return size.ToString("0 Bytes"); // Byte
}
// Divide by 1024 to get fractional value

View File

@ -1,16 +1,16 @@
using System;
using Microsoft.AspNetCore.Http;
using Bit.Core.IdentityServer;
using Xunit;
using NSubstitute;
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 Func<HttpRequest, string> Retrieve = TokenRetrieval.FromAuthorizationHeaderOrQueryString();
private readonly Func<HttpRequest, string> _sut = TokenRetrieval.FromAuthorizationHeaderOrQueryString();
[Fact]
public void RetrieveToken_FromHeader_ReturnsToken()
@ -27,7 +27,7 @@ namespace Bit.Core.Test.IdentityServer
request.Headers.Returns(headers);
// Act
var token = Retrieve(request);
var token = _sut(request);
// Assert
Assert.Equal("test_value", token);
@ -47,7 +47,7 @@ namespace Bit.Core.Test.IdentityServer
request.Query.Returns(new QueryCollection(queryString));
// Act
var token = Retrieve(request);
var token = _sut(request);
// Assert
Assert.Equal("test_value", token);
@ -74,7 +74,7 @@ namespace Bit.Core.Test.IdentityServer
request.Query.Returns(new QueryCollection(queryString));
// Act
var token = Retrieve(request);
var token = _sut(request);
// Assert
Assert.Equal("header_token", token);
@ -87,7 +87,7 @@ namespace Bit.Core.Test.IdentityServer
var request = Substitute.For<HttpRequest>();
// Act
var token = Retrieve(request);
var token = _sut(request);
// Assert
Assert.Null(token);

View File

@ -0,0 +1,220 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Utilities;
using Xunit;
namespace Bit.Core.Test.Utilities
{
public class CoreHelpersTests
{
public static IEnumerable<object[]> _epochTestCases = new[]
{
new object[] {new DateTime(2020, 12, 30, 11, 49, 12, DateTimeKind.Utc), 1609328952000L},
};
[Fact]
public void GenerateComb_Success()
{
// Arrange & Act
var comb = CoreHelpers.GenerateComb();
// Assert
Assert.NotEqual(Guid.Empty, comb);
// TODO: Add more asserts to make sure important aspects of
// the comb are working properly
}
[Theory]
[InlineData(2, 5, new[] { 1, 2, 3, 4, 5, 6, 7, 8 , 9, 0 })]
[InlineData(2, 3, new[] { 1, 2, 3, 4, 5 })]
[InlineData(2, 1, new[] { 1, 2 })]
[InlineData(1, 1, new[] { 1 })]
[InlineData(2, 2, new[] { 1, 2, 3 })]
public void Batch_Success(int batchSize, int totalBatches, int[] collection)
{
// Arrange
var remainder = collection.Length % batchSize;
// Act
var batches = collection.Batch(batchSize);
// Assert
Assert.Equal(totalBatches, batches.Count());
foreach (var batch in batches.Take(totalBatches - 1))
{
Assert.Equal(batchSize, batch.Count());
}
Assert.Equal(batches.Last().Count(), remainder == 0 ? batchSize : remainder);
}
[Fact]
public void ToGuidIdArrayTVP_Success()
{
// Arrange
var item0 = Guid.NewGuid();
var item1 = Guid.NewGuid();
var ids = new[] { item0, item1 };
// Act
var dt = ids.ToGuidIdArrayTVP();
// Assert
Assert.Single(dt.Columns);
Assert.Equal("GuidId", dt.Columns[0].ColumnName);
Assert.Equal(2, dt.Rows.Count);
Assert.Equal(item0, dt.Rows[0][0]);
Assert.Equal(item1, dt.Rows[1][0]);
}
// TODO: Test the other ToArrayTVP Methods
[Theory]
[InlineData("12345&6789", "123456789")]
[InlineData("abcdef", "ABCDEF")]
[InlineData("1!@#$%&*()_+", "1")]
[InlineData("\u00C6123abc\u00C7", "123ABC")]
[InlineData("123\u00C6ABC", "123ABC")]
[InlineData("\r\nHello", "E")]
[InlineData("\tdef", "DEF")]
[InlineData("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV1234567890", "ABCDEFABCDEF1234567890")]
public void CleanCertificateThumbprint_Success(string input, string output)
{
// Arrange & Act
var sanitizedInput = CoreHelpers.CleanCertificateThumbprint(input);
// Assert
Assert.Equal(output, sanitizedInput);
}
// TODO: Add more tests
[Theory]
[MemberData(nameof(_epochTestCases))]
public void ToEpocMilliseconds_Success(DateTime date, long milliseconds)
{
// Act & Assert
Assert.Equal(milliseconds, CoreHelpers.ToEpocMilliseconds(date));
}
[Theory]
[MemberData(nameof(_epochTestCases))]
public void FromEpocMilliseconds(DateTime date, long milliseconds)
{
// Act & Assert
Assert.Equal(date, CoreHelpers.FromEpocMilliseconds(milliseconds));
}
[Fact]
public void SecureRandomString_Success()
{
// Arrange & Act
var @string = CoreHelpers.SecureRandomString(8);
// Assert
// TODO: Should probably add more Asserts down the line
Assert.Equal(8, @string.Length);
}
[Theory]
[InlineData(1, "1 Bytes")]
[InlineData(-5L, "-5 Bytes")]
[InlineData(1023L, "1023 Bytes")]
[InlineData(1024L, "1 KB")]
[InlineData(1025L, "1 KB")]
[InlineData(-1023L, "-1023 Bytes")]
[InlineData(-1024L, "-1 KB")]
[InlineData(-1025L, "-1 KB")]
[InlineData(1048575L, "1024 KB")]
[InlineData(1048576L, "1 MB")]
[InlineData(1048577L, "1 MB")]
[InlineData(-1048575L, "-1024 KB")]
[InlineData(-1048576L, "-1 MB")]
[InlineData(-1048577L, "-1 MB")]
[InlineData(1073741823L, "1024 MB")]
[InlineData(1073741824L, "1 GB")]
[InlineData(1073741825L, "1 GB")]
[InlineData(-1073741823L, "-1024 MB")]
[InlineData(-1073741824L, "-1 GB")]
[InlineData(-1073741825L, "-1 GB")]
[InlineData(long.MaxValue, "8589934592 GB")]
public void ReadableBytesSize_Success(long size, string readable)
{
// Act & Assert
Assert.Equal(readable, CoreHelpers.ReadableBytesSize(size));
}
[Fact]
public void CloneObject_Success()
{
var orignial = new { Message = "Message" };
var copy = CoreHelpers.CloneObject(orignial);
Assert.Equal(orignial.Message, copy.Message);
}
[Fact]
public void ExtendQuery_AddNewParameter_Success()
{
// Arrange
var uri = new Uri("https://bitwarden.com/?param1=value1");
// Act
var newUri = CoreHelpers.ExtendQuery(uri,
new Dictionary<string, string> { { "param2", "value2" } });
// Assert
Assert.Equal("https://bitwarden.com/?param1=value1&param2=value2", newUri.ToString());
}
[Fact]
public void ExtendQuery_AddTwoNewParameters_Success()
{
// Arrange
var uri = new Uri("https://bitwarden.com/?param1=value1");
// Act
var newUri = CoreHelpers.ExtendQuery(uri,
new Dictionary<string, string>
{
{ "param2", "value2" },
{ "param3", "value3" }
});
// Assert
Assert.Equal("https://bitwarden.com/?param1=value1&param2=value2&param3=value3", newUri.ToString());
}
[Fact]
public void ExtendQuery_AddExistingParameter_Success()
{
// Arrange
var uri = new Uri("https://bitwarden.com/?param1=value1&param2=value2");
// Act
var newUri = CoreHelpers.ExtendQuery(uri,
new Dictionary<string, string> { { "param1", "test_value" } });
// Assert
Assert.Equal("https://bitwarden.com/?param1=test_value&param2=value2", newUri.ToString());
}
[Fact]
public void ExtendQuery_AddNoParameters_Success()
{
// Arrange
const string startingUri = "https://bitwarden.com/?param1=value1";
var uri = new Uri(startingUri);
// Act
var newUri = CoreHelpers.ExtendQuery(uri, new Dictionary<string, string>());
// Assert
Assert.Equal(startingUri, newUri.ToString());
}
}
}

View File

@ -1,15 +1,14 @@
using Bit.Core.Utilities;
using System.Collections.Generic;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
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
{
@ -18,33 +17,47 @@ namespace Bit.Core.Test.Utilities
[Fact]
public void NotSelfHosted_Throws_When_SelfHosted()
{
// Arrange
var sha = new SelfHostedAttribute { NotSelfHostedOnly = true };
// Act & Assert
Assert.Throws<BadRequestException>(() => sha.OnActionExecuting(GetContext(selfHosted: true)));
}
[Fact]
public void NotSelfHosted_Success_When_NotSelfHosted()
{
// Arrange
var sha = new SelfHostedAttribute { NotSelfHostedOnly = true };
// Act
sha.OnActionExecuting(GetContext(selfHosted: false));
// Assert
// The Assert here is just NOT throwing an exception
}
[Fact]
public void SelfHosted_Success_When_SelfHosted()
{
// Arrange
var sha = new SelfHostedAttribute { SelfHostedOnly = true };
// Act
sha.OnActionExecuting(GetContext(selfHosted: true));
// Assert
// The Assert here is just NOT throwing an exception
}
[Fact]
public void SelfHosted_Throws_When_NotSelfHosted()
{
// Arrange
var sha = new SelfHostedAttribute { SelfHostedOnly = true };
// Act & Assert
Assert.Throws<BadRequestException>(() => sha.OnActionExecuting(GetContext(selfHosted: false)));
}