public void EditProfilePostShouldUpdateProfile(FakeSiteContext siteContext, string profileItemId, [Substitute] EditProfile editProfile, [Frozen] 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 accountsController = new AccountsController(null, null, null, userProfileService, null); accountsController.ControllerContext = Substitute.For <ControllerContext>(); accountsController.ControllerContext.HttpContext.Returns(Substitute.For <HttpContextBase>()); accountsController.ControllerContext.HttpContext.Session.Returns(Substitute.For <HttpSessionStateBase>()); accountsController.ControllerContext.HttpContext.Request.Returns(Substitute.For <HttpRequestBase>()); accountsController.ControllerContext.HttpContext.Request.RawUrl.Returns("/"); var result = accountsController.EditProfile(editProfile); userProfileService.Received(1).SetProfile(user.Profile, editProfile); accountsController.Session["EditProfileMessage"].Should().BeOfType <InfoMessage>().Which.Type.Should().Be(InfoMessage.MessageType.Info); result.Should().BeOfType <RedirectResult>(); } }
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_AuthenticatedUser_ShouldRedirect(Database db, [Content] DbItem item, [Substitute] ActionExecutingContext filterContext, RedirectAuthenticatedAttribute redirectAuthenticatedAttribute) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore/content" }, { "startItem", item.Name } }) as SiteContext; siteContext.Database = db; //Act using (new SiteContextSwitcher(siteContext)) using (new Sitecore.Security.Accounts.UserSwitcher(@"extranet\John", true)) { redirectAuthenticatedAttribute.OnActionExecuting(filterContext); } //Assert filterContext.Result.Should().BeOfType <RedirectResult>(); }
public void LoadProfiles_SettingsIsEmpty_ShouldReturnExistentProfilesEnumerable([Content] Item item, CurrentInteraction currentInteraction, ITracker tracker, Analytics.Tracking.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 RegisterShouldReturnErrorIfRegistrationThrowsMembershipException(Database db, [Content] DbItem item, Item profileItem, RegistrationInfo registrationInfo, MembershipCreateUserException exception, [Frozen] IAccountRepository repo, [Frozen] INotificationService notifyService, [Frozen] IAccountsSettingsService accountsSettingsService, [Frozen] IUserProfileService userProfileService) { repo.When(x => x.RegisterUser(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())).Do(x => { throw new MembershipCreateUserException(); }); userProfileService.GetUserDefaultProfileId().Returns(profileItem.ID.ToString()); var controller = new AccountsController(repo, notifyService, accountsSettingsService, userProfileService, null); 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 result = controller.Register(registrationInfo); result.Should().BeOfType <ViewResult>().Which.Model.Should().Be(registrationInfo); result.Should().BeOfType <ViewResult>().Which.ViewData.ModelState.Should().ContainKey(nameof(registrationInfo.Email)) .WhichValue.Errors.Should().Contain(x => x.ErrorMessage == exception.Message); } }
public void LoginShouldAddModelStateErrorIfNotLoggedIn(Database db, [Content] DbItem item, [Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, [Frozen] IAccountsSettingsService accountSetting) { accountSetting.GetPageLinkOrDefault(Arg.Any <Item>(), Arg.Any <ID>(), Arg.Any <Item>()).Returns("/"); 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)) { info.ReturnUrl = null; info.Email = null; info.Password = null; var controller = new AccountsController(repo, service, accountSetting, null, null); repo.Login(string.Empty, string.Empty).ReturnsForAnyArgs(x => true); var result = controller.Login(info); result.Should().BeOfType <RedirectResult>().Which.Url.Should().Be("/"); } }
public void ShouldLoadSite() { //Arrange var fakeSite = new FakeSiteContext( new Sitecore.Collections.StringDictionary { { "name", "website" }, { "database", "web" }, { "cdTargetHostName", "cdsite" } }); // switch the context site 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(); sut.Site = Context.Site.Name; //Act var actual = sut.ComputeFieldValue(indexable); //Assert actual.Should().Be("//cdsite/en/sitecore/content/source.aspx"); } }
public void ExperienceData_InitializedTrackerAndNormalMode_ReturnExperienceData(IKeyBehaviorCache keyBehaviorCache, Session session, CurrentInteraction currentInteraction, ITracker tracker, Contact contact, [Frozen] IProfileProvider profileProvider, [Frozen] IDemoStateService demoState, [Frozen] IExperienceDataFactory dataFactory, [Greedy] DemoController sut) { demoState.IsDemoEnabled.Returns(true); dataFactory.Get().Returns(new ExperienceData()); tracker.Interaction.Returns(currentInteraction); tracker.Session.Returns(session); var attachments = new Dictionary <string, object> { ["KeyBehaviorCache"] = new Analytics.Tracking.KeyBehaviorCache(keyBehaviorCache) }; contact.Attachments.Returns(attachments); tracker.Contact.Returns(contact); var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { using (new TrackerSwitcher(tracker)) { var result = sut.ExperienceData(); result.Should().BeOfType <ViewResult>().Which.Model.Should().BeOfType <ExperienceData>(); } } }
public void ExperienceData_InitializedTrackerAndPreviewMode_ReturnEmptyResult(IKeyBehaviorCache keyBehaviorCache, Session session, CurrentInteraction currentInteraction, ITracker tracker, Contact contact, [Frozen] IProfileProvider profileProvider, [Frozen] IDemoStateService demoState, [Greedy] DemoController sut) { demoState.IsDemoEnabled.Returns(true); tracker.Interaction.Returns(currentInteraction); tracker.Session.Returns(session); var attachments = new Dictionary <string, object> { ["KeyBehaviorCache"] = new Analytics.Tracking.KeyBehaviorCache(keyBehaviorCache) }; contact.Attachments.Returns(attachments); tracker.Contact.Returns(contact); var fakeSite = new FakeSiteContext(new StringDictionary { { "enablePreview", "true" }, { "masterDatabase", "master" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Remember); using (new TrackerSwitcher(tracker)) { sut.ExperienceData().Should().BeOfType <EmptyResult>(); } } }
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 RegisterShouldCallRegisterUserAndRedirectToHomePage(Database db, [Content] DbItem item, Item profileItem, RegistrationInfo registrationInfo, [Frozen] IAccountRepository repo, [Frozen] INotificationService notifyService, [Frozen] IAccountsSettingsService accountsSettingsService, [Frozen] IUserProfileService userProfileService) { accountsSettingsService.GetPageLinkOrDefault(Arg.Any <Item>(), Arg.Any <ID>(), Arg.Any <Item>()).Returns("/redirect"); repo.Exists(Arg.Any <string>()).Returns(false); userProfileService.GetUserDefaultProfileId().Returns(profileItem.ID.ToString()); var controller = new AccountsController(repo, notifyService, accountsSettingsService, userProfileService, null); 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 result = controller.Register(registrationInfo); result.Should().BeOfType <RedirectResult>().Which.Url.Should().Be("/redirect"); repo.Received(1).RegisterUser(registrationInfo.Email, registrationInfo.Password, Arg.Any <string>()); } }
public void OnActionExecuting_AuthenticatedUser_ShouldRedirect(Database db, [Content] DbItem item, [Substitute] ActionExecutingContext filterContext, string url) { //Arrange var siteContext = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore/content" }, { "startItem", item.Name } }) as SiteContext; siteContext.Database = db; var urlService = Substitute.For <IGetRedirectUrlService>(); urlService.GetRedirectUrl(AuthenticationStatus.Authenticated).Returns(url); var redirectAuthenticatedAttribute = new RedirectAuthenticatedAttribute(urlService); //Act using (new SiteContextSwitcher(siteContext)) using (new Sitecore.Security.Accounts.UserSwitcher(@"extranet\John", true)) { redirectAuthenticatedAttribute.OnActionExecuting(filterContext); } //Assert filterContext.Result.Should().BeOfType <RedirectResult>(); ((RedirectResult)filterContext.Result).Url.Should().Be(url); }
public void HasMatchingPattern_HistoricProfileAndItemExists_ShouldReturnTrue([Content] Item profileItem, Contact contact, ITracker tracker, Analytics.Tracking.Profile profile) { //Arrange tracker.Contact.Returns(contact); var behaviorPattern = Substitute.For <IBehaviorProfileContext>(); behaviorPattern.PatternId.Returns(profileItem.ID); contact.BehaviorProfiles[Arg.Is(profileItem.ID)].Returns(behaviorPattern); var pattern = profileItem.Add("fakePattern", new TemplateID(PatternCardItem.TemplateID)); profile.PatternId = pattern.ID.Guid; var fakeSiteContext = new FakeSiteContext("fake") { Database = Database.GetDatabase("master") }; using (new TrackerSwitcher(tracker)) { using (new SiteContextSwitcher(fakeSiteContext)) { var provider = new ProfileProvider(); provider.HasMatchingPattern(new ProfileItem(profileItem), ProfilingTypes.Historic).Should().BeTrue(); } } }
public AccountRepositoryTests() { FakeSiteContext = new FakeSiteContext(new Collections.StringDictionary { { "rootPath", "/sitecore/content/" + Templates.SiteName.Name } });; HttpContext.Current = new HttpContext(new HttpRequest(null, "http://local", null), new HttpResponse(null)); Context.Site = FakeSiteContext; }
private void MockContexts(Database db, string httpRequestUrl = "http://local/") { var fakeSiteContext = new FakeSiteContext("fake"); HttpContext.Current = new HttpContext(new HttpRequest(null, httpRequestUrl, null), new HttpResponse(null)); fakeSiteContext.Database = db; Context.Site = fakeSiteContext; Context.Items["__visitorContext"] = new MockVisitorContext("1", "fake", "1"); }
public void Save_SpecPassed_ShouldBeSavedCorrectly(string settingName, [Frozen(As = typeof(SiteContext))] FakeSiteContext sitecoreContext, FakeSiteContextSwitcher siteContextSwitcher, Db db, [Greedy] SettingsRepository repo) { CreateItem(db, PresetsRoot + sitecoreContext.Name); repo.Save(settingName, EmptySpecification); var itemPath = String.Concat(PresetsRoot, sitecoreContext.Name, "/", settingName); var savedSpec = db.GetItem(itemPath)[Templates.Preset.Fields.Specification]; JObject.Parse(savedSpec).ToString().Should().Be(EmptySpecification.ToString()); }
public void ShouldCreateSimpleFakeSiteContext() { // arrange & act var siteContext = new FakeSiteContext("mywebsite"); // assert siteContext.Name.Should().Be("mywebsite"); siteContext.Database.Should().BeNull(); }
public void ContactSettingsRepository_NoPresets_ShouldReturnEmptyCollection(Db db, [Greedy] ContactSettingsRepository repo) { var siteContext = new FakeSiteContext("siteName"); using (new FakeSiteContextSwitcher(siteContext)) { CreateItem(db, $"/sitecore/client/Applications/ExperienceGenerator/Common/Contacts/{siteContext.Name}"); repo.GetPresets().Count.Should().Be(0); } }
public void GetSupportedLanguages_NoneSelected_ShouldReturnEmptyList(Db db, [Content] DbTemplate template, DbItem item, string rootName, ISiteDefinitionsProvider siteProvider, BaseLinkManager linkManager) { // Arrange template.BaseIDs = new[] { Templates.Site.ID, Feature.Language.Templates.LanguageSettings.ID }; var languageItem = new DbItem("en"); db.Add(languageItem); var siteRootId = ID.NewID; var siteRootItem = new DbItem(rootName, siteRootId, template.ID) { new DbField(Feature.Language.Templates.LanguageSettings.Fields.SupportedLanguages) { { "en", "" } } }; siteRootItem.Add(item); db.Add(siteRootItem); var contextItem = db.GetItem(item.ID); var siteContext = new Mock <Foundation.Multisite.SiteContext>(siteProvider); siteContext.Setup(x => x.GetSiteDefinition(contextItem)).Returns(new SiteDefinition { Item = db.GetItem(siteRootId) }); var repository = new LanguageRepository(siteContext.Object, linkManager); var fakeSite = new FakeSiteContext(new StringDictionary() { { "name", "fake" }, { "database", "master" }, { "rootPath", siteRootItem.FullPath }, { "hostName", "local" } }); using (new FakeSiteContextSwitcher(fakeSite)) { using (new ContextItemSwitcher(contextItem)) { // Act var supportedLanguages = repository.GetSupportedLanguages(); // Assert supportedLanguages.Count().Should().Be(0); } } }
public FakeSite() { _fakeSiteContext = new Sitecore.FakeDb.Sites.FakeSiteContext( new Sitecore.Collections.StringDictionary { { "name", "website" }, { "database", "web" } }); _switcher = new FakeSiteContextSwitcher(_fakeSiteContext); }
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 void Save_OnePresent_ShouldReturnSingleSetting(Db db, [Greedy] ContactSettingsRepository repo) { var siteContext = new FakeSiteContext("siteName"); using (new FakeSiteContextSwitcher(siteContext)) { CreateItem(db, $"/sitecore/client/Applications/ExperienceGenerator/Common/Contacts/{siteContext.Name}"); repo.Save("name", EmptySpecification); repo.GetPresets().Count.Should().Be(1); } }
private static SiteContext BuildUpSiteContext(Db db, string body = "restore password $pass$ body", string from = "*****@*****.**", string subj = "restore password subject", ID outcomeID = null) { var template = ID.NewID; if (outcomeID == (ID)null) { outcomeID = ID.NewID; } db.Add(new DbItem("siteroot") { TemplateID = Templates.AccountsSettings.ID, Fields = { new DbField("ForgotPasswordMailTemplate", Templates.AccountsSettings.Fields.ForgotPasswordMailTemplate) { Value = template.ToString() }, new DbField("RegisterOutcome", Templates.AccountsSettings.Fields.RegisterOutcome) { Value = outcomeID.ToString() } }, Children = { new DbItem("mailtemplate", template, Templates.MailTemplate.ID) { { Templates.MailTemplate.Fields.Body, body }, { Templates.MailTemplate.Fields.From, from }, { Templates.MailTemplate.Fields.Subject, subj } }, new DbItem("outcome", outcomeID) } }); var fakeSite = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore/content/siteroot" }, { "database", "master" } }) as SiteContext; Context.Item = db.GetItem(template); return(fakeSite); }
public void ForgotPassword_ValidData_ShouldReturnViewWithoutModel([Frozen] IAccountRepository repo, [NoAutoProperties] AccountsController controller) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { var result = controller.ForgotPassword(); result.Should().BeOfType <ViewResult>().Which.Model.Should().BeNull(); } }
public void Register_ShouldReturnViewWithoutModel(Database db, [Content] DbItem item, [Frozen] IAccountRepository repo, [NoAutoProperties] AccountsController controller) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { var result = controller.Register(); result.Should().BeOfType <ViewResult>().Which.Model.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 Save_SpecPassed_ShouldBeSavedCorrectly(Db db, [Greedy] ContactSettingsRepository repo) { var siteContext = new FakeSiteContext("siteName"); using (new FakeSiteContextSwitcher(siteContext)) { CreateItem(db, $"/sitecore/client/Applications/ExperienceGenerator/Common/Contacts/{siteContext.Name}"); repo.Save("name", EmptySpecification); var savedSpec = db.GetItem($"/sitecore/client/Applications/ExperienceGenerator/Common/Contacts/{siteContext.Name}/name")[ Templates.Preset.Fields.Specification]; JArray.Parse(savedSpec).ToString().Should().Be(EmptySpecification.ToString()); } }
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(); }
private SiteContext BuildSiteContext(Db db, Db coreDb, ID profileId, ID interestFolder, IEnumerable <string> interests) { var contextId = ID.NewID; var interestItem = new DbItem("InterestsFolder", interestFolder); interests.Select(i => new DbItem(i) { TemplateID = Templates.Interest.ID, Fields = { new DbField("Title", Templates.Interest.Fields.Title) { Value = i } } }) .ToList().ForEach(x => interestItem.Add(x)); db.Add(new DbItem("siteroot") { TemplateID = Templates.ProfileSettigs.ID, Fields = { new DbField("UserProfile", Templates.ProfileSettigs.Fields.UserProfile) { Value = profileId.ToString() }, new DbField("InterestsFolder", Templates.ProfileSettigs.Fields.InterestsFolder) { Value = interestFolder.ToString() } }, Children = { interestItem } }); coreDb.Add(new DbItem("UserProfile", profileId)); var fakeSite = new FakeSiteContext(new StringDictionary { { "rootPath", "/sitecore/content/siteroot" }, { "database", "master" } }) as SiteContext; Context.Item = db.GetItem(interestFolder); return(fakeSite); }