Files
Umbraco-CMS/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs

169 lines
8.6 KiB
C#
Raw Normal View History

2017-09-23 10:08:18 +02:00
using System;
2017-09-19 15:51:47 +02:00
using System.Collections.Generic;
2017-09-14 19:29:12 +02:00
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Security;
using Moq;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Security;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi;
using Umbraco.Core.Logging;
2018-04-30 15:11:01 +02:00
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web.Security.Providers;
2017-09-14 19:29:12 +02:00
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)
{
2017-09-22 15:23:46 +02:00
// default
if (!typeof (UmbracoApiControllerBase).IsAssignableFrom(controllerType))
return base.Create(request, controllerDescriptor, controllerType);
var owinContext = request.TryGetOwinContext().Result;
2017-09-23 10:08:18 +02:00
2017-09-22 15:23:46 +02:00
var mockedUserService = Mock.Of<IUserService>();
var mockedContentService = Mock.Of<IContentService>();
var mockedMediaService = Mock.Of<IMediaService>();
var mockedEntityService = Mock.Of<IEntityService>();
2018-03-27 17:59:53 +02:00
var mockedMemberService = Mock.Of<IMemberService>();
var mockedMemberTypeService = Mock.Of<IMemberTypeService>();
var mockedDataTypeService = Mock.Of<IDataTypeService>();
var mockedContentTypeService = Mock.Of<IContentTypeService>();
2017-09-22 15:23:46 +02:00
2019-01-04 16:02:10 +01:00
var serviceContext = ServiceContext.CreatePartial(
2017-09-22 15:23:46 +02:00
userService: mockedUserService,
contentService: mockedContentService,
mediaService: mockedMediaService,
entityService: mockedEntityService,
2018-03-27 17:59:53 +02:00
memberService: mockedMemberService,
memberTypeService: mockedMemberTypeService,
dataTypeService: mockedDataTypeService,
contentTypeService: mockedContentTypeService,
localizedTextService:Mock.Of<ILocalizedTextService>());
2017-09-22 15:23:46 +02:00
var globalSettings = SettingsForTests.GenerateMockGlobalSettings();
2017-09-22 15:23:46 +02:00
// FIXME: v8?
2017-09-22 15:23:46 +02:00
////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>()
};
2017-09-22 15:23:46 +02:00
//httpcontext with an auth'd user
var httpContext = Mock.Of<HttpContextBase>(
http => http.User == owinContext.Authentication.User
2017-09-23 10:08:18 +02:00
//ensure the request exists with a cookies collection
2018-09-06 17:37:47 +10:00
&& http.Request == Mock.Of<HttpRequestBase>(r => r.Cookies == new HttpCookieCollection()
&& r.RequestContext == new System.Web.Routing.RequestContext
{
RouteData = new System.Web.Routing.RouteData()
})
2017-09-23 10:08:18 +02:00
//ensure the request exists with an items collection
&& http.Items == httpContextItems);
2017-09-22 15:23:46 +02:00
//chuck it into the props since this is what MS does when hosted and it's needed there
2017-09-23 10:08:18 +02:00
request.Properties["MS_HttpContext"] = httpContext;
2017-09-22 15:23:46 +02:00
var backofficeIdentity = (UmbracoBackOfficeIdentity) owinContext.Authentication.User.Identity;
var webSecurity = new Mock<WebSecurity>(null, null, globalSettings);
2017-09-22 15:23:46 +02:00
//mock CurrentUser
var groups = new List<ReadOnlyUserGroup>();
for (var index = 0; index < backofficeIdentity.Roles.Length; index++)
{
var role = backofficeIdentity.Roles[index];
groups.Add(new ReadOnlyUserGroup(index + 1, role, "icon-user", null, null, role, new string[0], new string[0]));
2017-09-14 19:29:12 +02:00
}
2017-09-22 15:23:46 +02:00
webSecurity.Setup(x => x.CurrentUser)
.Returns(Mock.Of<IUser>(u => u.IsApproved == true
&& u.IsLockedOut == false
&& u.AllowedSections == backofficeIdentity.AllowedApplications
&& u.Groups == groups
&& u.Email == "admin@admin.com"
&& u.Id == (int) backofficeIdentity.Id
&& u.Language == "en"
&& u.Name == backofficeIdentity.RealName
&& u.StartContentIds == backofficeIdentity.StartContentNodes
&& u.StartMediaIds == backofficeIdentity.StartMediaNodes
&& u.Username == backofficeIdentity.Username));
//mock Validate
webSecurity.Setup(x => x.ValidateCurrentUser())
2017-09-23 10:08:18 +02:00
.Returns(() => true);
2017-09-22 15:23:46 +02:00
webSecurity.Setup(x => x.UserHasSectionAccess(It.IsAny<string>(), It.IsAny<IUser>()))
.Returns(() => true);
2018-04-27 11:38:50 +10:00
var publishedSnapshot = new Mock<IPublishedSnapshot>();
2017-10-31 12:50:30 +01:00
publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
2017-10-31 12:48:24 +01:00
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(publishedSnapshot.Object);
2017-09-22 15:23:46 +02:00
var umbracoContextAccessor = Umbraco.Web.Composing.Current.UmbracoContextAccessor;
2019-02-15 12:19:42 +11:00
var umbCtx = new UmbracoContext(httpContext,
2017-10-31 12:48:24 +01:00
publishedSnapshotService.Object,
2017-09-22 15:23:46 +02:00
webSecurity.Object,
2019-02-14 12:11:06 +01:00
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
2017-09-22 15:23:46 +02:00
Enumerable.Empty<IUrlProvider>(),
2019-04-15 15:57:35 +02:00
Enumerable.Empty<IMediaUrlProvider>(),
globalSettings,
2019-02-15 12:19:42 +11:00
new TestVariationContextAccessor());
2019-02-14 12:11:06 +01:00
2019-02-15 12:19:42 +11:00
//replace it
umbracoContextAccessor.UmbracoContext = umbCtx;
2017-09-23 10:08:18 +02:00
2017-09-22 15:23:46 +02:00
var urlHelper = new Mock<IUrlProvider>();
2019-04-16 20:03:07 +02:00
urlHelper.Setup(provider => provider.GetUrl(It.IsAny<UmbracoContext>(), It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
2018-12-18 22:02:39 +11:00
.Returns(UrlInfo.Url("/hello/world/1234"));
2017-09-22 15:23:46 +02:00
var membershipHelper = new MembershipHelper(umbCtx.HttpContext, Mock.Of<IPublishedMemberCache>(), Mock.Of<MembersMembershipProvider>(), Mock.Of<RoleProvider>(), Mock.Of<IMemberService>(), Mock.Of<IMemberTypeService>(), Mock.Of<IUserService>(), Mock.Of<IPublicAccessService>(), AppCaches.Disabled, Mock.Of<ILogger>());
2017-09-22 15:23:46 +02:00
var umbHelper = new UmbracoHelper(Mock.Of<IPublishedContent>(),
2017-09-22 15:23:46 +02:00
Mock.Of<ITagQuery>(),
Mock.Of<ICultureDictionaryFactory>(),
2017-09-22 15:23:46 +02:00
Mock.Of<IUmbracoComponentRenderer>(),
Mock.Of<IPublishedContentQuery>(),
membershipHelper);
2017-09-22 15:23:46 +02:00
return CreateController(controllerType, request, umbracoContextAccessor, umbHelper);
2017-09-14 19:29:12 +02:00
}
protected abstract ApiController CreateController(Type controllerType, HttpRequestMessage msg, IUmbracoContextAccessor umbracoContextAccessor, UmbracoHelper helper);
2017-09-14 19:29:12 +02:00
}
2017-09-23 10:08:18 +02:00
}