Move test projects from src/ to tests/ (#11357)

* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
This commit is contained in:
Paul Johnson
2021-10-18 08:14:04 +01:00
committed by GitHub
parent c005673a96
commit 00133e880d
752 changed files with 650 additions and 1844 deletions

View File

@@ -0,0 +1,89 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Linq;
using System.Security.Claims;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions
{
[TestFixture]
public class ClaimsPrincipalExtensionsTests
{
[Test]
public void Get_Remaining_Ticket_Seconds()
{
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
"test",
"test",
Enumerable.Empty<int>(),
Enumerable.Empty<int>(),
"en-US",
Guid.NewGuid().ToString(),
Enumerable.Empty<string>(),
Enumerable.Empty<string>());
var principal = new ClaimsPrincipal(backOfficeIdentity);
var expireSeconds = 99;
var elapsedSeconds = 3;
var remainingSeconds = expireSeconds - elapsedSeconds;
DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset then = now.AddSeconds(elapsedSeconds);
var expires = now.AddSeconds(expireSeconds).ToString("o");
backOfficeIdentity.AddClaim(new Claim(
Constants.Security.TicketExpiresClaimType,
expires,
ClaimValueTypes.DateTime,
Constants.Security.BackOfficeAuthenticationType,
Constants.Security.BackOfficeAuthenticationType,
backOfficeIdentity));
var ticketRemainingSeconds = principal.GetRemainingAuthSeconds(then);
Assert.AreEqual(remainingSeconds, ticketRemainingSeconds);
}
[Test]
public void AddOrUpdateClaim__Should_ensure_a_claim_is_not_added_twice()
{
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
"test",
"test",
Enumerable.Empty<int>(),
Enumerable.Empty<int>(),
"en-US",
Guid.NewGuid().ToString(),
Enumerable.Empty<string>(),
Enumerable.Empty<string>());
var expireSeconds = 99;
DateTimeOffset now = DateTimeOffset.Now;
var expires = now.AddSeconds(expireSeconds).ToString("o");
var claim = new Claim(
Constants.Security.TicketExpiresClaimType,
expires,
ClaimValueTypes.DateTime,
Constants.Security.BackOfficeAuthenticationType,
Constants.Security.BackOfficeAuthenticationType,
backOfficeIdentity);
backOfficeIdentity.AddOrUpdateClaim(claim);
backOfficeIdentity.AddOrUpdateClaim(claim);
Assert.AreEqual(1, backOfficeIdentity.Claims.Count(x=>x.Type == Constants.Security.TicketExpiresClaimType));
}
}
}

View File

@@ -0,0 +1,83 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.NUnit3;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Notifications;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions
{
[TestFixture]
public class UmbracoBuilderExtensionsTests
{
[Test, Customization]
public void AddNotificationsFromAssembly_Should_AddNotificationHandler_To_ServicesCollection(IUmbracoBuilder sut)
{
sut.AddNotificationsFromAssembly<CustomizationAttribute>();
var expectedHandlerType = typeof(INotificationHandler<ContentPublishedNotification>);
var handler = sut.Services.SingleOrDefault(x => x.ServiceType == expectedHandlerType);
Assert.NotNull(handler);
Assert.That(handler.ImplementationType, Is.EqualTo(typeof(StubNotificationHandler)));
}
[Test, Customization]
public void AddNotificationsFromAssembly_Should_AddAsyncNotificationHandler_To_ServicesCollection(IUmbracoBuilder sut)
{
sut.AddNotificationsFromAssembly<CustomizationAttribute>();
var expectedHandlerType = typeof(INotificationAsyncHandler<ContentPublishedNotification>);
var handler = sut.Services.SingleOrDefault(x => x.ServiceType == expectedHandlerType);
Assert.NotNull(handler);
Assert.That(handler.ImplementationType, Is.EqualTo(typeof(StubNotificationHandler)));
}
private class CustomizationAttribute : AutoDataAttribute
{
public CustomizationAttribute() : base(() => {
var fixture = new Fixture();
var stub = new UmbracoBuildStub();
fixture.Inject((IUmbracoBuilder)stub);
return fixture;
}){}
}
private class UmbracoBuildStub : IUmbracoBuilder
{
public IServiceCollection Services { get; }
public IConfiguration Config { get; }
public TypeLoader TypeLoader { get; }
public ILoggerFactory BuilderLoggerFactory { get; }
public IHostingEnvironment BuilderHostingEnvironment { get; }
public IProfiler Profiler { get; }
public AppCaches AppCaches { get; }
public TBuilder WithCollectionBuilder<TBuilder>() where TBuilder : ICollectionBuilder, new() => default;
public UmbracoBuildStub() => Services = new ServiceCollection();
public void Build() {}
}
private class StubNotificationHandler
: INotificationHandler<ContentPublishedNotification>
, INotificationAsyncHandler<ContentPublishedNotification>
{
public void Handle(ContentPublishedNotification notification) { }
public Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken) => Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,160 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using NUnit.Framework;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions
{
[TestFixture]
public class UriExtensionsTests
{
[TestCase("http://www.domain.com/foo/bar", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar#hop", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar?q=2#hop", "/", "http://www.domain.com/?q=2")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
[TestCase("http://www.domain.com/foo#bang", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3#bang", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
public void RewritePath(string input, string path, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", typeof(ArgumentException))]
[TestCase("http://www.domain.com", "path/to/page/", typeof(ArgumentException))]
public void RewritePath_Exceptions(string input, string path, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path);
});
}
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
public void RewritePathAndQuery(string input, string path, string query, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path, query);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", "", typeof(ArgumentException))]
[TestCase("http://www.domain.com/", "/path/to/page/", "x=27", typeof(ArgumentException))]
public void RewritePathAndQuery_Exceptions(string input, string path, string query, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path, query);
});
}
[TestCase("http://www.domain.com", "/")]
[TestCase("http://www.domain.com/", "/")]
[TestCase("http://www.domain.com/foo", "/foo")]
[TestCase("http://www.domain.com/foo/", "/foo/")]
[TestCase("http://www.domain.com/foo/bar", "/foo/bar")]
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar%20nix")]
[TestCase("http://www.domain.com/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/", "/")]
[TestCase("/foo", "/foo")]
[TestCase("/foo/", "/foo/")]
[TestCase("/foo/bar", "/foo/bar")]
[TestCase("/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/foo%20bar/pof", "/foo%20bar/pof")]
public void GetSafeAbsolutePath(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePath();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
public void GetAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
[TestCase("/foo%20bar/pof", "/foo bar/pof")]
public void GetSafeAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to/?q=3")]
public void EndPathWithSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.EndPathWithSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to?q=3")]
public void TrimPathEndSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.TrimPathEndSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("/foo/bar", "http://www.domain.com", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang", "http://www.domain.com/foo/bar")]
[TestCase("/", "http://www.domain.com/dang/dang", "http://www.domain.com/")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar?k=6#yop", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar?k=6")]
public void MakeAbsolute(string input, string reference, string expected)
{
var source = new Uri(input, UriKind.Relative);
var absolute = new Uri(reference);
var output = source.MakeAbsolute(absolute);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com:666/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com:666", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
public void WithoutPort(string input, string expected)
{
var source = new Uri(input);
var output = source.WithoutPort();
Assert.AreEqual(expected, output.ToString());
}
}
}