public void LoadProfiles_NoSetProfiles_ShouldReturnEmptyProfilesEnumerable(Database db, [Content] Item item, ITracker tracker, IProfileProvider provider) { //arrange tracker.IsActive.Returns(true); var fakeSiteContext = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore" }, { "startItem", item.Paths.FullPath.Remove(0, "/sitecore".Length) } }); fakeSiteContext.Database = db; using (new SiteContextSwitcher(fakeSiteContext)) { using (new TrackerSwitcher(tracker)) { var model = new VisitInformation(provider); model.LoadProfiles().Count().Should().Be(0); } } }
public void OnActionExecuting_RedirectEqualsCurrent_ShouldRedirectToRootPage(Database db, [Content] DbItem item, string afterLoginLink, [Frozen]IAccountsSettingsService accountsSettingsService, [Substitute]ActionExecutingContext filterContext, [Greedy]AccountsRedirectAuthenticatedAttribute redirectAuthenticatedAttribute) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore/content" }, { "startItem", item.Name } }) as SiteContext; siteContext.Database = db; accountsSettingsService.GetPageLinkOrDefault(Arg.Any<Item>(), Templates.AccountsSettings.Fields.AfterLoginPage, Arg.Any<Item>()).Returns(afterLoginLink); filterContext.HttpContext.Request.RawUrl.Returns(afterLoginLink); //Act using (new SiteContextSwitcher(siteContext)) using (new Sitecore.Security.Accounts.UserSwitcher(@"extranet\John", true)) { redirectAuthenticatedAttribute.OnActionExecuting(filterContext); } //Assert filterContext.Result.Should().BeOfType<RedirectResult>().Which.Url.Should().NotBe(afterLoginLink); }
public void LoadProfiles_SettingsIsEmpty_ShouldReturnExistentProfilesEnumerable([Content] Item item, CurrentInteraction currentInteraction, ITracker tracker, Profile profile) { var profileSettingItem = item.Add("profileSetting", new TemplateID(Templates.ProfilingSettings.ID)); var profileItem = item.Add("profile", new TemplateID(ProfileItem.TemplateID)); var provider = new ProfileProvider(); var fakeSiteContext = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore" }, { "startItem", profileSettingItem.Paths.FullPath.Remove(0, "/sitecore".Length) } }); fakeSiteContext.Database = item.Database; using (new SiteContextSwitcher(fakeSiteContext)) { provider.GetSiteProfiles().Count().Should().Be(0); } }
public void LoadProfiles_SettingWithProfiles_ShouldReturnExistentProfilesEnumerable(Db db, CurrentInteraction currentInteraction, ITracker tracker, Analytics.Tracking.Profile profile) { var profileItem = new DbItem("profile", ID.NewID, new TemplateID(ProfileItem.TemplateID)); db.Add(profileItem); var profileSettingItem = new DbItem("profileSetting", ID.NewID, new TemplateID(Templates.ProfilingSettings.ID)) { {Templates.ProfilingSettings.Fields.SiteProfiles, profileItem.ID.ToString()} }; db.Add(profileSettingItem); var provider = new ProfileProvider(); var fakeSiteContext = new FakeSiteContext(new StringDictionary { {"rootPath", "/sitecore"}, {"startItem", profileSettingItem.FullPath.Remove(0, "/sitecore".Length)} }) { Database = db.Database }; using (new SiteContextSwitcher(fakeSiteContext)) { var siteProfiles = provider.GetSiteProfiles(); siteProfiles.Count().Should().Be(1); } }
public void ShouldUseSite() { var fakeSite = new Sitecore.FakeDb.Sites.FakeSiteContext( new Sitecore.Collections.StringDictionary { { "name", "website" }, { "rootPath", "/sitecore" } }); //Arrange using (new FakeSiteContextSwitcher(fakeSite)) using (var db = new Db { new ItemBuilder().Build() }) { var item = db.GetItem("/sitecore/content/source"); var indexable = new SitecoreIndexableItem(item); var sut = new CurrentItemLinkField(); //Act var actual = sut.ComputeFieldValue(indexable); //Assert actual.Should().Be("/en/content/source.aspx"); } }
public void ShouldCreateSimpleFakeSiteContext() { // arrange & act var siteContext = new FakeSiteContext("mywebsite"); // assert siteContext.Name.Should().Be("mywebsite"); siteContext.Database.Should().BeNull(); }
public void ShouldCreateAdvancedFakeSiteContext() { // arrange & act var siteContext = new FakeSiteContext(new StringDictionary { { "name", "mywebsite" }, { "database", "web" } }); // assert siteContext.Name.Should().Be("mywebsite"); siteContext.Database.Name.Should().Be("web"); }
public FakeSite() { _fakeSiteContext = new Sitecore.FakeDb.Sites.FakeSiteContext( new Sitecore.Collections.StringDictionary { {"name", "website"}, {"database", "web"} }); _switcher = new FakeSiteContextSwitcher(_fakeSiteContext); }
public void OnActionExecuting_NotNormalMode_ShouldNotRedirect(FakeSiteContext siteContext, [Substitute]ActionExecutingContext filterContext, RedirectAuthenticatedAttribute redirectAuthenticatedAttribute) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); //Act using (new SiteContextSwitcher(siteContext)) { redirectAuthenticatedAttribute.OnActionExecuting(filterContext); } //Assert filterContext.Result.Should().BeNull(); }
public void ShouldSwitchContextSite() { // arrange var site = new FakeSiteContext("mywebsite"); // act using (new SiteContextSwitcher(site)) { // assert Context.Site.Name.Should().Be("mywebsite"); } Context.Site.Should().BeNull(); }
public void Get_DictionaryRootItemExists_ThrowConfigurationErrorException(Db db, [Content]DbItem item, DictionaryRepository repository) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary() { ["dictionaryPath"] = item.FullPath, ["database"] = "master" }); //Assert using (new SiteContextSwitcher(siteContext)) { repository.Get(siteContext).Root.ID.Should().Be(item.ID); } }
public void Logout_ShouldCallSitecoreLogout(Database db, [Content] DbItem item, IAccountRepository repo, INotificationService ns, IAccountsSettingsService acc) { var fakeSite = new FakeSiteContext(new StringDictionary { {"rootPath", "/sitecore/content"}, {"startItem", item.Name} }) as SiteContext; fakeSite.Database = db; using (new SiteContextSwitcher(fakeSite)) { var ctrl = new AccountsController(repo, ns, acc, null, null); ctrl.Logout(); repo.Received(1).Logout(); } }
public void Process_HandledException_DontSetView(FakeSiteContext siteContext, InvalidDatasourceItemExceptionProcessor processor, [Modest] ExceptionContext exceptionContext, [Substitute] ExceptionArgs exceptionArgs) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); exceptionArgs.ExceptionContext.ExceptionHandled = true; //Act using (new SiteContextSwitcher(siteContext)) { processor.Process(exceptionArgs); //Assert exceptionArgs.ExceptionContext.Result.Should().BeOfType<EmptyResult>(); } }
public void PageEditorError_Call_LogError(string errorMessage, FakeSiteContext siteContext, MemoryAppender appender, [RegisterView(Constants.InfoMessageView)] IView view, HtmlHelper htmlHelper) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); BasicConfigurator.Configure(appender); //Act using (new SiteContextSwitcher(siteContext)) { htmlHelper.PageEditorError(errorMessage); } //Assert appender.Events.Should().Contain(x => x.RenderedMessage.Contains(errorMessage)); }
public void PageEditorError_NormalMode_ReturnEmptyString(string errorMessage, FakeSiteContext siteContext) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Normal); //Act MvcHtmlString result; using (new SiteContextSwitcher(siteContext)) { result = AlertHtmlHelpers.PageEditorError(null, errorMessage); } //Assert result.ToString().Should().BeEmpty(); }
public void PageEditorError_EditMode_RenderErrorViewFriendlyMessage(string errorMessage, string friendlyMessage, FakeSiteContext siteContext, [RegisterView(Constants.InfoMessageView)] IView view, HtmlHelper helper) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); //Act MvcHtmlString result; using (new SiteContextSwitcher(siteContext)) { result = helper.PageEditorError(errorMessage, friendlyMessage, ID.NewID, ID.NewID); //Assert view.Received().Render(Arg.Is<ViewContext>(v => v.ViewData.Model.As<InfoMessage>().Type == InfoMessage.MessageType.Error), Arg.Any<TextWriter>()); } }
public void PageEditorError_EditMode_ReturnErrorBlock(string errorMessage, FakeSiteContext siteContext) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); //Act MvcHtmlString result; using (new SiteContextSwitcher(siteContext)) { result = HtmlHelperExtensions.PageEditorError(null, errorMessage); } //Assert result.ToString().Should().Contain(errorMessage).And.StartWith("<p").And.EndWith("</p>"); }
public void OnActionExecuting_NotAuthenticatedUser_ShouldNotRedirect(FakeSiteContext siteContext, [Substitute]ActionExecutingContext filterContext, RedirectAuthenticatedAttribute redirectAuthenticatedAttribute) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Normal); //Act using (new SiteContextSwitcher(siteContext)) using (new Sitecore.Security.Accounts.UserSwitcher(@"extranet\John", false)) { redirectAuthenticatedAttribute.OnActionExecuting(filterContext); } //Assert filterContext.Result.Should().BeNull(); }
public void Get_NotDictionaryRootItem_ThrowConfigurationErrorException(Db db, DictionaryRepository repository) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary() { ["dictionaryPath"] = "/sitecore/content/dictionaryPath", ["database"] = "master" }); //Assert using (new SiteContextSwitcher(siteContext)) { repository.Invoking(x => x.Get(siteContext)).ShouldThrow<ConfigurationErrorsException>(); } }
public void Current_HttpContextIsntInitialized_CreateNewRepository([Content]DbItem item) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary() { ["dictionaryPath"] = item.FullPath, ["database"] = "master" }); HttpContext.Current = null; using (new SiteContextSwitcher(siteContext)) { //Assert DictionaryPhraseRepository.Current.Should().BeOfType<DictionaryPhraseRepository>(); } }
public void Logout_ShouldRedirectUserToHomePage(Database db, [Content] DbItem item, IAccountRepository repo, INotificationService ns, IAccountsSettingsService acc) { var fakeSite = new FakeSiteContext(new StringDictionary { {"rootPath", "/sitecore/content"}, {"startItem", item.Name} }) as SiteContext; fakeSite.Database = db; Language.Current = Language.Invariant; using (new SiteContextSwitcher(fakeSite)) { var ctrl = new AccountsController(repo, ns, acc, null, null); var result = ctrl.Logout(); result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/"); } }
public void Process_DataSourceExceptionInNormalMode_HandleException(FakeSiteContext siteContext, InvalidDatasourceItemExceptionProcessor processor, [Modest] ExceptionContext exceptionContext, [Substitute] ExceptionArgs exceptionArgs, [Modest] InvalidDataSourceItemException exception) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Normal); exceptionArgs.ExceptionContext.ExceptionHandled = false; exceptionArgs.ExceptionContext.Exception = exception; //Act using (new SiteContextSwitcher(siteContext)) { processor.Process(exceptionArgs); //Assert exceptionArgs.ExceptionContext.ExceptionHandled.Should().BeTrue(); exceptionArgs.ExceptionContext.Result.Should().BeOfType<EmptyResult>(); } }
public void ForgotPasswordShouldReturnModelIfItsNotValid(PasswordResetInfo model, [Frozen] IAccountRepository repo, [NoAutoProperties] AccountsController controller) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(true); controller.ModelState.AddModelError("Error", "Error"); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); } }
public void Process_DataSourceExceptionInEditMode_SetView(Database db, FakeSiteContext siteContext, InvalidDatasourceItemExceptionProcessor processor, [Modest]ExceptionContext exceptionContext, [Substitute]ExceptionArgs exceptionArgs, [Modest]InvalidDataSourceItemException exception) { //Arrange typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(siteContext, DisplayMode.Edit); exceptionArgs.ExceptionContext.ExceptionHandled = false; exceptionArgs.ExceptionContext.Exception = exception; //Act using (new SiteContextSwitcher(siteContext)) { processor.Process(exceptionArgs); //Assert exceptionArgs.ExceptionContext.Result.Should().BeOfType<ViewResult>().Which.ViewName.Should().Be(ViewPath.InfoMessage); exceptionArgs.ExceptionContext.ExceptionHandled.Should().BeTrue(); } }
public void EditProfilePostShouldUpdateProfile(FakeSiteContext siteContext, string profileItemId, [Substitute] EditProfile editProfile, IUserProfileService userProfileService) { var user = Substitute.For<User>("extranet/John", true); user.Profile.Returns(Substitute.For<UserProfile>()); user.Profile.ProfileItemId = profileItemId; userProfileService.GetUserDefaultProfileId().Returns(profileItemId); userProfileService.ValidateProfile(Arg.Any<EditProfile>(), Arg.Any<ModelStateDictionary>()).Returns(true); using (new SiteContextSwitcher(siteContext)) using (new UserSwitcher(user)) { var accounController = new AccountsController(null, null, null, userProfileService); var result = accounController.EditProfile(editProfile); userProfileService.Received(1).SetProfile(user.Profile, editProfile); result.Should().BeOfType<ViewResult>().Which.Model.Should().BeOfType<InfoMessage>().Which.Type.Should().Be(InfoMessage.MessageType.Info); } }
public void Exists_UserExists_ShouldReturnTrue(FakeMembershipUser user, MembershipProvider membershipProvider, AccountRepository repo) { membershipProvider.GetUser(@"somedomain\John", Arg.Any<bool>()).Returns(user); var context = new FakeSiteContext(new StringDictionary { {"domain", "somedomain"} }); using (new Switcher<Domain, Domain>(new Domain("somedomain"))) { using (new MembershipSwitcher(membershipProvider)) { var exists = repo.Exists("John"); exists.Should().BeTrue(); } } }
public void EditProfilePostShouldReturnModelErrors(FakeSiteContext siteContext, ModelStateDictionary modelState, string profileItemId, IEnumerable<string> interests, [Substitute] EditProfile editProfile, IUserProfileService userProfileService) { var user = Substitute.For<User>("extranet/John", true); user.Profile.Returns(Substitute.For<UserProfile>()); user.Profile.ProfileItemId = profileItemId; userProfileService.GetUserDefaultProfileId().Returns(profileItemId); userProfileService.GetInterests().Returns(interests); userProfileService.ValidateProfile(Arg.Any<EditProfile>(), Arg.Do<ModelStateDictionary>(x => x.AddModelError("key", "error"))).Returns(false); using (new SiteContextSwitcher(siteContext)) using (new UserSwitcher(user)) { var accounController = new AccountsController(null, null, null, userProfileService); var result = accounController.EditProfile(editProfile); result.Should().BeOfType<ViewResult>().Which.ViewData.ModelState.Should().ContainKey("key").WhichValue.Errors.Should().Contain(e => e.ErrorMessage == "error"); } }
public void Get_AutocreateNotSet_ReturnFalse([Content]DbItem item, DictionaryRepository repository) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary() { ["dictionaryPath"] = item.FullPath, ["database"] = "master" }); using (new SiteContextSwitcher(siteContext)) { //Act var result = repository.Get(siteContext); //Assert result.AutoCreate.Should().BeFalse(); } }
public void ForgotPasswordShouldReturnModelIfUserNotExist(PasswordResetInfo model, [Frozen] IAccountRepository repo) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(false); var controller = new AccountsController(repo, null); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); result.Should().BeOfType<ViewResult>().Which.ViewData.ModelState.Should().ContainKey(nameof(model.Email)) .WhichValue.Errors.Should().Contain(x => x.ErrorMessage == Errors.UserDoesNotExist); } }
public void ForgotPasswordShoudCatchAndReturnViewWithError(PasswordResetInfo model, [Frozen] IAccountRepository repo, INotificationService notificationService, IAccountsSettingsService settingService) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).ThrowsForAnyArgs(new Exception("Error")); repo.Exists(Arg.Any<string>()).Returns(true); var controller = new AccountsController(repo, notificationService, settingService); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); result.Should().BeOfType<ViewResult>().Which.ViewData.ModelState.Should().ContainKey(nameof(model.Email)) .WhichValue.Errors.Should().Contain(x => x.ErrorMessage == "Error"); } }
public void Current_HttpContextHasNotDictionaryItem_CreateNewRepositoryAndCacheIt([Content]DbItem item, HttpContext context) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary() { ["dictionaryPath"] = item.FullPath, ["database"] = "master" }); HttpContext.Current = context; using (new SiteContextSwitcher(siteContext)) { //Act var result = DictionaryPhraseRepository.Current; //Assert result.Should().BeOfType<DictionaryPhraseRepository>(); HttpContext.Current.Items["DictionaryPhraseRepository.Current"].Should().Be(result); } }