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:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using Microsoft.Owin.Extensions;
|
||||
using Owin;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
public static class AuthenticateEverythingExtensions
|
||||
{
|
||||
public static IAppBuilder AuthenticateEverything(this IAppBuilder app)
|
||||
{
|
||||
if (app == null)
|
||||
throw new ArgumentNullException("app");
|
||||
app.Use(typeof(AuthenticateEverythingMiddleware), (object)app, (object)new AuthenticateEverythingMiddleware.AuthenticateEverythingAuthenticationOptions());
|
||||
app.UseStageMarker(PipelineStage.Authenticate);
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Owin;
|
||||
using Microsoft.Owin.Security;
|
||||
using Microsoft.Owin.Security.Infrastructure;
|
||||
using Owin;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Security;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
/// <summary>
|
||||
/// Ensures there's an admin user assigned to the request
|
||||
/// </summary>
|
||||
public class AuthenticateEverythingMiddleware : AuthenticationMiddleware<AuthenticationOptions>
|
||||
{
|
||||
public AuthenticateEverythingMiddleware(OwinMiddleware next, IAppBuilder app, AuthenticationOptions options)
|
||||
: base(next, options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override AuthenticationHandler<AuthenticationOptions> CreateHandler()
|
||||
{
|
||||
return new AuthenticateEverythingHandler();
|
||||
}
|
||||
|
||||
public class AuthenticateEverythingHandler : AuthenticationHandler<AuthenticationOptions>
|
||||
{
|
||||
protected override Task<AuthenticationTicket> AuthenticateCoreAsync()
|
||||
{
|
||||
var securityStamp = Guid.NewGuid().ToString();
|
||||
|
||||
var identity = new ClaimsIdentity();
|
||||
identity.AddRequiredClaims(
|
||||
Cms.Core.Constants.Security.SuperUserIdAsString,
|
||||
"admin",
|
||||
"Admin",
|
||||
new[] { -1 },
|
||||
new[] { -1 },
|
||||
"en-US",
|
||||
securityStamp,
|
||||
new[] { "content", "media", "members" },
|
||||
new[] { "admin" });
|
||||
|
||||
return Task.FromResult(new AuthenticationTicket(
|
||||
identity,
|
||||
new AuthenticationProperties()
|
||||
{
|
||||
ExpiresUtc = DateTime.Now.AddDays(1)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public class AuthenticateEverythingAuthenticationOptions : AuthenticationOptions
|
||||
{
|
||||
public AuthenticateEverythingAuthenticationOptions()
|
||||
: base("AuthenticateEverything")
|
||||
{
|
||||
AuthenticationMode = AuthenticationMode.Active;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Web.Http.Dispatcher;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
public class SpecificAssemblyResolver : IAssembliesResolver
|
||||
{
|
||||
private readonly Assembly[] _assemblies;
|
||||
|
||||
public SpecificAssemblyResolver(Assembly[] assemblies)
|
||||
{
|
||||
_assemblies = assemblies;
|
||||
}
|
||||
|
||||
public ICollection<Assembly> GetAssemblies()
|
||||
{
|
||||
return _assemblies;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Web;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
public class TestControllerActivator : TestControllerActivatorBase
|
||||
{
|
||||
private readonly Func<HttpRequestMessage, IUmbracoContextAccessor, ApiController> _factory;
|
||||
|
||||
public TestControllerActivator(Func<HttpRequestMessage, IUmbracoContextAccessor, ApiController> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
protected override ApiController CreateController(Type controllerType, HttpRequestMessage msg, IUmbracoContextAccessor umbracoContextAccessor)
|
||||
{
|
||||
return _factory(msg, umbracoContextAccessor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using Moq;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Models.Membership;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Cms.Core.Routing;
|
||||
using Umbraco.Cms.Core.Security;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Cms.Tests.Common;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Tests.TestHelpers.Entities;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.WebApi;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to mock all of the services required for re-mocking and testing controllers
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A more complete version of this is found in the Umbraco REST API project but this has the basics covered
|
||||
/// </remarks>
|
||||
public abstract class TestControllerActivatorBase : DefaultHttpControllerActivator, IHttpControllerActivator
|
||||
{
|
||||
IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
|
||||
{
|
||||
// default
|
||||
if (!typeof (UmbracoApiControllerBase).IsAssignableFrom(controllerType))
|
||||
return base.Create(request, controllerDescriptor, controllerType);
|
||||
|
||||
var owinContext = request.TryGetOwinContext().Result;
|
||||
|
||||
var mockedUserService = Mock.Of<IUserService>();
|
||||
var mockedContentService = Mock.Of<IContentService>();
|
||||
var mockedMediaService = Mock.Of<IMediaService>();
|
||||
var mockedEntityService = Mock.Of<IEntityService>();
|
||||
var mockedMemberService = Mock.Of<IMemberService>();
|
||||
var mockedMemberTypeService = Mock.Of<IMemberTypeService>();
|
||||
var mockedDataTypeService = Mock.Of<IDataTypeService>();
|
||||
var mockedContentTypeService = Mock.Of<IContentTypeService>();
|
||||
|
||||
var serviceContext = ServiceContext.CreatePartial(
|
||||
userService: mockedUserService,
|
||||
contentService: mockedContentService,
|
||||
mediaService: mockedMediaService,
|
||||
entityService: mockedEntityService,
|
||||
memberService: mockedMemberService,
|
||||
memberTypeService: mockedMemberTypeService,
|
||||
dataTypeService: mockedDataTypeService,
|
||||
contentTypeService: mockedContentTypeService,
|
||||
localizedTextService:Mock.Of<ILocalizedTextService>());
|
||||
|
||||
var globalSettings = new GlobalSettings();
|
||||
|
||||
// FIXME: v8?
|
||||
////new app context
|
||||
//var dbCtx = new Mock<DatabaseContext>(Mock.Of<IDatabaseFactory>(), Mock.Of<ILogger>(), Mock.Of<ISqlSyntaxProvider>(), "test");
|
||||
////ensure these are set so that the appctx is 'Configured'
|
||||
//dbCtx.Setup(x => x.CanConnect).Returns(true);
|
||||
//dbCtx.Setup(x => x.IsDatabaseConfigured).Returns(true);
|
||||
//var appCtx = ApplicationContext.EnsureContext(
|
||||
// dbCtx.Object,
|
||||
// //pass in mocked services
|
||||
// serviceContext,
|
||||
// CacheHelper.CreateDisabledCacheHelper(),
|
||||
// new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()),
|
||||
// true);
|
||||
|
||||
var httpContextItems = new Dictionary<string, object>
|
||||
{
|
||||
//add the special owin environment to the httpcontext items, this is how the GetOwinContext works
|
||||
["owin.Environment"] = new Dictionary<string, object>()
|
||||
};
|
||||
|
||||
//httpcontext with an auth'd user
|
||||
var httpContext = Mock.Of<HttpContextBase>(
|
||||
http => http.User == owinContext.Authentication.User
|
||||
//ensure the request exists with a cookies collection
|
||||
&& http.Request == Mock.Of<HttpRequestBase>(r => r.Cookies == new HttpCookieCollection()
|
||||
&& r.RequestContext == new System.Web.Routing.RequestContext
|
||||
{
|
||||
RouteData = new System.Web.Routing.RouteData()
|
||||
})
|
||||
//ensure the request exists with an items collection
|
||||
&& http.Items == httpContextItems);
|
||||
//chuck it into the props since this is what MS does when hosted and it's needed there
|
||||
request.Properties["MS_HttpContext"] = httpContext;
|
||||
|
||||
var backofficeIdentity = (ClaimsIdentity) owinContext.Authentication.User.Identity;
|
||||
|
||||
var backofficeSecurity = new Mock<IBackOfficeSecurity>();
|
||||
|
||||
//mock CurrentUser
|
||||
var groups = new List<ReadOnlyUserGroup>();
|
||||
for (var index = 0; index < backofficeIdentity.GetRoles().Length; index++)
|
||||
{
|
||||
var role = backofficeIdentity.GetRoles()[index];
|
||||
groups.Add(new ReadOnlyUserGroup(index + 1, role, "icon-user", null, null, role, new string[0], new string[0]));
|
||||
}
|
||||
var mockUser = MockedUser.GetUserMock();
|
||||
mockUser.Setup(x => x.IsApproved).Returns(true);
|
||||
mockUser.Setup(x => x.IsLockedOut).Returns(false);
|
||||
mockUser.Setup(x => x.AllowedSections).Returns(backofficeIdentity.GetAllowedApplications());
|
||||
mockUser.Setup(x => x.Groups).Returns(groups);
|
||||
mockUser.Setup(x => x.Email).Returns("admin@admin.com");
|
||||
mockUser.Setup(x => x.Id).Returns((int)backofficeIdentity.GetId());
|
||||
mockUser.Setup(x => x.Language).Returns("en");
|
||||
mockUser.Setup(x => x.Name).Returns(backofficeIdentity.GetRealName());
|
||||
mockUser.Setup(x => x.StartContentIds).Returns(backofficeIdentity.GetStartContentNodes());
|
||||
mockUser.Setup(x => x.StartMediaIds).Returns(backofficeIdentity.GetStartMediaNodes());
|
||||
mockUser.Setup(x => x.Username).Returns(backofficeIdentity.GetUsername());
|
||||
backofficeSecurity.Setup(x => x.CurrentUser)
|
||||
.Returns(mockUser.Object);
|
||||
|
||||
//mock Validate
|
||||
backofficeSecurity.Setup(x => x.UserHasSectionAccess(It.IsAny<string>(), It.IsAny<IUser>()))
|
||||
.Returns(() => true);
|
||||
|
||||
var publishedSnapshot = new Mock<IPublishedSnapshot>();
|
||||
publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
|
||||
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
|
||||
publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(publishedSnapshot.Object);
|
||||
|
||||
var umbracoContextAccessor = Umbraco.Web.Composing.Current.UmbracoContextAccessor;
|
||||
|
||||
var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext);
|
||||
var umbCtx = new UmbracoContext(httpContextAccessor,
|
||||
publishedSnapshotService.Object,
|
||||
backofficeSecurity.Object,
|
||||
globalSettings,
|
||||
TestHelper.GetHostingEnvironment(),
|
||||
new TestVariationContextAccessor(),
|
||||
TestHelper.UriUtility,
|
||||
new AspNetCookieManager(httpContextAccessor));
|
||||
|
||||
//replace it
|
||||
umbracoContextAccessor.UmbracoContext = umbCtx;
|
||||
|
||||
var urlHelper = new Mock<IUrlProvider>();
|
||||
urlHelper.Setup(provider => provider.GetUrl(It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
||||
.Returns(UrlInfo.Url("/hello/world/1234"));
|
||||
|
||||
return CreateController(controllerType, request, umbracoContextAccessor);
|
||||
}
|
||||
|
||||
protected abstract ApiController CreateController(Type controllerType, HttpRequestMessage msg, IUmbracoContextAccessor umbracoContextAccessor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using System.Web.Http.ExceptionHandling;
|
||||
using System.Web.Http.Tracing;
|
||||
using Owin;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.Hosting;
|
||||
using Umbraco.Web.WebApi;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
/// <summary>
|
||||
/// Startup class for the self-hosted web server works for OWIN and WebAPI
|
||||
/// </summary>
|
||||
public class TestStartup
|
||||
{
|
||||
private readonly Func<HttpRequestMessage, IUmbracoContextAccessor, ApiController> _controllerFactory;
|
||||
private readonly Action<HttpConfiguration> _initialize;
|
||||
|
||||
public TestStartup(Action<HttpConfiguration> initialize, Func<HttpRequestMessage, IUmbracoContextAccessor, ApiController> controllerFactory)
|
||||
{
|
||||
_controllerFactory = controllerFactory;
|
||||
_initialize = initialize;
|
||||
}
|
||||
|
||||
public void Configuration(IAppBuilder app)
|
||||
{
|
||||
var httpConfig = new HttpConfiguration();
|
||||
|
||||
// TODO: Enable this if you can't see the errors produced
|
||||
// var traceWriter = httpConfig.EnableSystemDiagnosticsTracing();
|
||||
// traceWriter.IsVerbose = true;
|
||||
// traceWriter.MinimumLevel = TraceLevel.Debug;
|
||||
|
||||
httpConfig.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
|
||||
|
||||
// Add in a simple exception tracer so we can see what is causing the 500 Internal Server Error
|
||||
httpConfig.Services.Add(typeof (IExceptionLogger), new TraceExceptionLogger());
|
||||
|
||||
httpConfig.Services.Replace(typeof (IAssembliesResolver), new SpecificAssemblyResolver(new[] { typeof (AspNetApplicationShutdownRegistry).Assembly }));
|
||||
httpConfig.Services.Replace(typeof (IHttpControllerActivator), new TestControllerActivator(_controllerFactory));
|
||||
httpConfig.Services.Replace(typeof (IHttpControllerSelector), new NamespaceHttpControllerSelector(httpConfig));
|
||||
|
||||
//auth everything
|
||||
app.AuthenticateEverything();
|
||||
|
||||
_initialize(httpConfig);
|
||||
|
||||
app.UseWebApi(httpConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Diagnostics;
|
||||
using System.Web.Http.ExceptionHandling;
|
||||
|
||||
namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
{
|
||||
/// <summary>
|
||||
/// Traces any errors for WebApi to the output window
|
||||
/// </summary>
|
||||
public class TraceExceptionLogger : ExceptionLogger
|
||||
{
|
||||
public override void Log(ExceptionLoggerContext context)
|
||||
{
|
||||
Trace.TraceError(context.ExceptionContext.Exception.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user