Ejemplo n.º 1
0
        public void Is_Back_Office_Request(string input, string virtualPath, bool expected)
        {
            SystemDirectories.Root = virtualPath;
            var globalConfig = SettingsForTests.GenerateMockGlobalSettings();
            var source       = new Uri(input);

            Assert.AreEqual(expected, source.IsBackOfficeRequest(virtualPath, globalConfig));
        }
        public void ParseLocalLinks(string input, string result)
        {
            var serviceCtxMock = new TestObjects(null).GetServiceContextMock();

            //setup a mock entity service from the service context to return an integer for a GUID
            var entityService = Mock.Get(serviceCtxMock.EntityService);
            //entityService.Setup(x => x.GetId(It.IsAny<Guid>(), It.IsAny<UmbracoObjectTypes>()))
            //    .Returns((Guid id, UmbracoObjectTypes objType) =>
            //    {
            //        return Attempt.Succeed(1234);
            //    });

            //setup a mock url provider which we'll use fo rtesting
            var testUrlProvider = new Mock <IUrlProvider>();

            testUrlProvider
            .Setup(x => x.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlProviderMode>(), It.IsAny <string>(), It.IsAny <Uri>()))
            .Returns((UmbracoContext umbCtx, IPublishedContent content, UrlProviderMode mode, string culture, Uri url) => UrlInfo.Url("/my-test-url"));

            var globalSettings = SettingsForTests.GenerateMockGlobalSettings();

            var contentType      = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing);
            var publishedContent = Mock.Of <IPublishedContent>();

            Mock.Get(publishedContent).Setup(x => x.Id).Returns(1234);
            Mock.Get(publishedContent).Setup(x => x.ContentType).Returns(contentType);
            var contentCache = Mock.Of <IPublishedContentCache>();

            Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent);
            Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent);
            var snapshot = Mock.Of <IPublishedSnapshot>();

            Mock.Get(snapshot).Setup(x => x.Content).Returns(contentCache);
            var snapshotService = Mock.Of <IPublishedSnapshotService>();

            Mock.Get(snapshotService).Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot);

            using (var umbCtx = UmbracoContext.EnsureContext(
                       Umbraco.Web.Composing.Current.UmbracoContextAccessor,
                       Mock.Of <HttpContextBase>(),
                       snapshotService,
                       new Mock <WebSecurity>(null, null, globalSettings).Object,
                       //setup a quick mock of the WebRouting section
                       Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "AutoLegacy")),
                       //pass in the custom url provider
                       new[] { testUrlProvider.Object },
                       globalSettings,
                       new TestVariationContextAccessor(),
                       true))
            {
                var output = TemplateUtilities.ParseInternalLinks(input, umbCtx.UrlProvider);

                Assert.AreEqual(result, output);
            }
        }
Ejemplo n.º 3
0
        public void Umbraco_Mvc_Area(string path, string rootPath, string outcome)
        {
            var globalSettings = SettingsForTests.GenerateMockGlobalSettings();

            var globalSettingsMock = Mock.Get(globalSettings);

            globalSettingsMock.Setup(x => x.Path).Returns(() => Current.IOHelper.ResolveUrl(path));

            SystemDirectories.Root = rootPath;
            Assert.AreEqual(outcome, globalSettings.GetUmbracoMvcAreaNoCache());
        }
Ejemplo n.º 4
0
        public void SetUp()
        {
            // fixme - bad in a unit test - but Udi has a static ctor that wants it?!
            var container      = new Mock <IFactory>();
            var globalSettings = SettingsForTests.GenerateMockGlobalSettings();

            container.Setup(x => x.GetInstance(typeof(TypeLoader))).Returns(
                new TypeLoader(NullCacheProvider.Instance, LocalTempStorage.Default, new ProfilingLogger(Mock.Of <ILogger>(), Mock.Of <IProfiler>())));
            Current.Factory = container.Object;

            Udi.ResetUdiTypes();
        }
Ejemplo n.º 5
0
        public void SetUp()
        {
            // FIXME: bad in a unit test - but Udi has a static ctor that wants it?!
            var container      = new Mock <IFactory>();
            var globalSettings = SettingsForTests.GenerateMockGlobalSettings();

            container.Setup(x => x.GetInstance(typeof(TypeLoader))).Returns(
                new TypeLoader(NoAppCache.Instance, IOHelper.MapPath("~/App_Data/TEMP"), new ProfilingLogger(Mock.Of <ILogger>(), Mock.Of <IProfiler>())));
            Current.Factory = container.Object;

            Udi.ResetUdiTypes();
        }
Ejemplo n.º 6
0
        public void ParseLocalLinks(string input, string result)
        {
            //setup a mock url provider which we'll use for testing
            var testUrlProvider = new Mock <IUrlProvider>();

            testUrlProvider
            .Setup(x => x.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>()))
            .Returns((UmbracoContext umbCtx, IPublishedContent content, UrlMode mode, string culture, Uri url) => UrlInfo.Url("/my-test-url"));

            var globalSettings = SettingsForTests.GenerateMockGlobalSettings();

            var contentType      = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing);
            var publishedContent = Mock.Of <IPublishedContent>();

            Mock.Get(publishedContent).Setup(x => x.Id).Returns(1234);
            Mock.Get(publishedContent).Setup(x => x.ContentType).Returns(contentType);
            var contentCache = Mock.Of <IPublishedContentCache>();

            Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent);
            Mock.Get(contentCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent);
            var snapshot = Mock.Of <IPublishedSnapshot>();

            Mock.Get(snapshot).Setup(x => x.Content).Returns(contentCache);
            var snapshotService = Mock.Of <IPublishedSnapshotService>();

            Mock.Get(snapshotService).Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot);
            var media = Mock.Of <IPublishedContent>();

            Mock.Get(media).Setup(x => x.Url).Returns("/media/1001/my-image.jpg");
            var mediaCache = Mock.Of <IPublishedMediaCache>();

            Mock.Get(mediaCache).Setup(x => x.GetById(It.IsAny <Guid>())).Returns(media);

            var umbracoContextAccessor = new TestUmbracoContextAccessor();
            var umbracoContextFactory  = new UmbracoContextFactory(
                umbracoContextAccessor,
                snapshotService,
                new TestVariationContextAccessor(),
                new TestDefaultCultureAccessor(),
                Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
                globalSettings,
                new UrlProviderCollection(new[] { testUrlProvider.Object }),
                new MediaUrlProviderCollection(Enumerable.Empty <IMediaUrlProvider>()),
                Mock.Of <IUserService>());

            using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>()))
            {
                var output = TemplateUtilities.ParseInternalLinks(input, reference.UmbracoContext.UrlProvider, mediaCache);

                Assert.AreEqual(result, output);
            }
        }
        public void SetApplicationUrlFromWrSettingsSsl()
        {
            var settings = Mock.Of <IUmbracoSettingsSection>(section =>
                                                             section.WebRouting == Mock.Of <IWebRoutingSection>(wrSection => wrSection.UmbracoApplicationUrl == "httpx://whatever.com/umbraco/"));

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseHttps).Returns(true);



            var url = ApplicationUrlHelper.TryGetApplicationUrl(settings, Mock.Of <ILogger>(), globalConfig.Object, Mock.Of <IServerRegistrar>());

            Assert.AreEqual("httpx://whatever.com/umbraco", url);
        }
        public void SetApplicationUrlWhenNoSettings()
        {
            // no applicable settings, cannot set URL

            var settings = Mock.Of <IUmbracoSettingsSection>(section =>
                                                             section.WebRouting == Mock.Of <IWebRoutingSection>(wrSection => wrSection.UmbracoApplicationUrl == (string)null));

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseHttps).Returns(true);

            var url = ApplicationUrlHelper.TryGetApplicationUrl(settings, Mock.Of <ILogger>(), globalConfig.Object, Mock.Of <IServerRegistrar>());

            // still NOT set
            Assert.IsNull(url);
        }
        public void SetApplicationUrlFromStSettingsSsl()
        {
            var settings = Mock.Of <IUmbracoSettingsSection>(section =>
                                                             section.WebRouting == Mock.Of <IWebRoutingSection>(wrSection => wrSection.UmbracoApplicationUrl == (string)null) &&
                                                             section.ScheduledTasks == Mock.Of <IScheduledTasksSection>(tasksSection => tasksSection.BaseUrl == "mycoolhost.com/umbraco/"));

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseHttps).Returns(true);



            var url = ApplicationUrlHelper.TryGetApplicationUrl(settings, Mock.Of <ILogger>(), globalConfig.Object, Mock.Of <IServerRegistrar>());

            Assert.AreEqual("https://mycoolhost.com/umbraco", url);
        }
        public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool trailingSlash)
        {
            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var requestMock = Mock.Get(settings.RequestHandler);

            requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash);

            UriUtility.SetAppDomainAppVirtualPath("/");

            var expectedUri = NewUri(expectedUrl);
            var sourceUri   = NewUri(sourceUrl);
            var resultUri   = UriUtility.UriFromUmbraco(sourceUri, globalConfig.Object, settings.RequestHandler);

            Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
        }
Ejemplo n.º 11
0
        public static IUmbracoContextFactory Create(IGlobalSettings globalSettings                 = null, IUrlProvider urlProvider = null,
                                                    IMediaUrlProvider mediaUrlProvider             = null,
                                                    IUmbracoContextAccessor umbracoContextAccessor = null)
        {
            if (globalSettings == null)
            {
                globalSettings = SettingsForTests.GenerateMockGlobalSettings();
            }
            if (urlProvider == null)
            {
                urlProvider = Mock.Of <IUrlProvider>();
            }
            if (mediaUrlProvider == null)
            {
                mediaUrlProvider = Mock.Of <IMediaUrlProvider>();
            }
            if (umbracoContextAccessor == null)
            {
                umbracoContextAccessor = new TestUmbracoContextAccessor();
            }

            var contentCache = new Mock <IPublishedContentCache>();
            var mediaCache   = new Mock <IPublishedMediaCache>();
            var snapshot     = new Mock <IPublishedSnapshot>();

            snapshot.Setup(x => x.Content).Returns(contentCache.Object);
            snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
            var snapshotService = new Mock <IPublishedSnapshotService>();

            snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())).Returns(snapshot.Object);

            var umbracoContextFactory = new UmbracoContextFactory(
                umbracoContextAccessor,
                snapshotService.Object,
                new TestVariationContextAccessor(),
                new TestDefaultCultureAccessor(),
                Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
                globalSettings,
                new UrlProviderCollection(new[] { urlProvider }),
                new MediaUrlProviderCollection(new[] { mediaUrlProvider }),
                Mock.Of <IUserService>());

            return(umbracoContextFactory);
        }
Ejemplo n.º 12
0
        public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool directoryUrls, bool trailingSlash)
        {
            ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", directoryUrls ? "true" : "false");

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseDirectoryUrls).Returns(directoryUrls);

            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var requestMock = Mock.Get(settings.RequestHandler);

            requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash);

            UriUtility.SetAppDomainAppVirtualPath("/");

            var expectedUri = NewUri(expectedUrl);
            var sourceUri   = NewUri(sourceUrl);
            var resultUri   = UriUtility.UriFromUmbraco(sourceUri, globalConfig.Object, settings.RequestHandler);

            Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
        }
        public void SetApplicationUrlViaProvider()
        {
            // no applicable settings, but a provider

            var settings = Mock.Of <IUmbracoSettingsSection>(section =>
                                                             section.WebRouting == Mock.Of <IWebRoutingSection>(wrSection => wrSection.UmbracoApplicationUrl == (string)null));

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseHttps).Returns(true);

            ApplicationUrlHelper.ApplicationUrlProvider = request => "http://server1.com/umbraco";



            var state = new RuntimeState(Mock.Of <ILogger>(), settings, globalConfig.Object, new Lazy <IMainDom>(), new Lazy <IServerRegistrar>(() => Mock.Of <IServerRegistrar>()));

            state.EnsureApplicationUrl();

            Assert.AreEqual("http://server1.com/umbraco", state.ApplicationUrl.ToString());
        }
Ejemplo n.º 14
0
        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 = SettingsForTests.GenerateMockGlobalSettings();

            // 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 = (UmbracoBackOfficeIdentity)owinContext.Authentication.User.Identity;

            var webSecurity = new Mock <WebSecurity>(null, null, globalSettings);

            //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]));
            }
            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 == "*****@*****.**" &&
                                     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())
            .Returns(() => true);
            webSecurity.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 umbCtx = new UmbracoContext(httpContext,
                                            publishedSnapshotService.Object,
                                            webSecurity.Object,
                                            Mock.Of <IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of <IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
                                            Enumerable.Empty <IUrlProvider>(),
                                            Enumerable.Empty <IMediaUrlProvider>(),
                                            globalSettings,
                                            new TestVariationContextAccessor());

            //replace it
            umbracoContextAccessor.UmbracoContext = umbCtx;

            var urlHelper = new Mock <IUrlProvider>();

            urlHelper.Setup(provider => provider.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlProviderMode>(), It.IsAny <string>(), It.IsAny <Uri>()))
            .Returns(UrlInfo.Url("/hello/world/1234"));

            var membershipHelper = new MembershipHelper(umbCtx.HttpContext, Mock.Of <IPublishedMemberCache>(), Mock.Of <MembershipProvider>(), Mock.Of <RoleProvider>(), Mock.Of <IMemberService>(), Mock.Of <IMemberTypeService>(), Mock.Of <IUserService>(), Mock.Of <IPublicAccessService>(), Mock.Of <AppCaches>(), Mock.Of <ILogger>());

            var umbHelper = new UmbracoHelper(Mock.Of <IPublishedContent>(),
                                              Mock.Of <ITagQuery>(),
                                              Mock.Of <ICultureDictionaryFactory>(),
                                              Mock.Of <IUmbracoComponentRenderer>(),
                                              Mock.Of <IPublishedContentQuery>(),
                                              membershipHelper);

            return(CreateController(controllerType, request, umbracoContextAccessor, umbHelper));
        }