Ejemplo n.º 1
0
        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");
              }
        }
Ejemplo n.º 2
0
 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 ForgotPasswordShouldRedirectLoggedUser(PasswordResetInfo model, IAccountRepository repo, INotificationService ns)
 {
     var fakeSite = new FakeSiteContext(new StringDictionary
       {
     {
       "displayMode", "normal"
     }
       }) as SiteContext;
       using (new SiteContextSwitcher(fakeSite))
       {
     using (new UserSwitcher(@"extranet\fake", true))
     {
       repo.RestorePassword(Arg.Any<string>()).Returns("new password");
       repo.Exists(Arg.Any<string>()).Returns(true);
       var controller = new AccountsController(repo, ns);
       var result = controller.ForgotPassword(model);
       result.Should().BeOfType<RedirectResult>();
     }
       }
 }
Ejemplo n.º 4
0
        public void EditProfilePostShouldReturnInfoMessageIfProfileDoesntMatch(string siteProfileId, 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(siteProfileId);

              var fakeSite = new FakeSiteContext(new StringDictionary
              {
            {"displayMode", "normal"}
              }) as SiteContext;

              using (new SiteContextSwitcher(fakeSite))
              using (new UserSwitcher(user))
              {
            var accounController = new AccountsController(null, null, null, userProfileService);
            var result = accounController.EditProfile(editProfile);
            result.Should().BeOfType<ViewResult>().Which.Model.Should().BeOfType<InfoMessage>().Which.Type.Should().Be(InfoMessage.MessageType.Error);
              }
        }
        public void LogoutShouldRedirectUserToHomePage(Database db, [Content] DbItem item, IAccountRepository repo, INotificationService ns)
        {
            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);
            var result = ctrl.Logout();
            result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/");
              }
        }
 public void LogoutShouldCallSitecoreLogout(Database db, [Content] DbItem item, IAccountRepository repo, INotificationService ns)
 {
     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);
     ctrl.Logout();
     repo.Received(1).Logout();
       }
 }
 public void ForgotPasswordShouldReturnSuccessView([Frozen] IAccountRepository repo, INotificationService ns, PasswordResetInfo model)
 {
     var fakeSite = new FakeSiteContext(new StringDictionary
       {
     {
       "displayMode", "normal"
     }
       }) as SiteContext;
       using (new SiteContextSwitcher(fakeSite))
       {
     var controller = new AccountsController(repo, ns);
     repo.RestorePassword(Arg.Any<string>()).Returns("new password");
     repo.Exists(Arg.Any<string>()).Returns(true);
     var result = controller.ForgotPassword(model);
     result.Should().BeOfType<ViewResult>().Which.ViewName.Should().Be("ForgotPasswordSuccess");
       }
 }
Ejemplo n.º 8
0
        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);
            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>();
              }
        }
Ejemplo n.º 9
0
        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);

              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>());
              }
        }
Ejemplo n.º 10
0
        public void LoginShouldRedirectToRootIfReturnUrlNotSet(Database db, [Content] DbItem item, [Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, 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;
            var controller = new AccountsController(repo, service, accountSetting, null);
            repo.Login(string.Empty, string.Empty).ReturnsForAnyArgs(x => true);
            var result = controller.Login(info);
            result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be("/");
              }
        }
Ejemplo n.º 11
0
 public void LoginShouldReturnViewModelIfModelStateNotValid([Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, IAccountsSettingsService accountSetting)
 {
     var controller = new AccountsController(repo, service, accountSetting, null);
       controller.ModelState.AddModelError("Error", "Error");
       var result = controller.Login(info);
       result.Should().BeOfType<ViewResult>();
 }
Ejemplo n.º 12
0
 public void LoginShouldRedirectToReturnUrlIfLoggedIn([Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, IAccountsSettingsService accountSetting)
 {
     var controller = new AccountsController(repo, service, accountSetting, null);
       repo.Login(string.Empty, string.Empty).ReturnsForAnyArgs(x => true);
       var result = controller.Login(info);
       result.Should().BeOfType<RedirectResult>().Which.Url.Should().Be(info.ReturnUrl);
 }
Ejemplo n.º 13
0
 public void LoginDialogShouldRedirectIfLoggedIn(Database db, [Content] DbItem item, [Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, IAccountsSettingsService accountSetting)
 {
     var controller = new AccountsController(repo, service, accountSetting, null);
       repo.Login(string.Empty, string.Empty).ReturnsForAnyArgs(x => true);
       var result = controller.LoginDialog(info);
       result.Should().BeOfType<JsonResult>();
       ((result as JsonResult).Data as LoginResult).RedirectUrl.Should().BeEquivalentTo(info.ReturnUrl);
 }
Ejemplo n.º 14
0
        public void EditProfileShouldReturnEmptyViewForEditMode(Database db, IUserProfileService userProfileService)
        {
            userProfileService.GetEmptyProfile().Returns(new EditProfile());

              var fakeSite = new FakeSiteContext(new StringDictionary
              {
            {"rootPath", "/sitecore/content"}
              }) as SiteContext;
              fakeSite.Database = db;
              typeof(SiteContext).GetField("displayMode", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(fakeSite, DisplayMode.Edit);

              Language.Current = Language.Invariant;

              using (new SiteContextSwitcher(fakeSite))
              {
            var accounController = new AccountsController(null, null, null, userProfileService);
            var result = accounController.EditProfile();
            result.Should().BeOfType<ViewResult>().Which.Model.ShouldBeEquivalentTo(new EditProfile());
              }
        }
        public void RegisterShouldCallRegisterUserAndRedirectToHomePage(Database db, [Content] DbItem item, RegistrationInfo registrationInfo, [Frozen] IAccountRepository repo, [Frozen] INotificationService notifyService)
        {
            repo.Exists(Arg.Any<string>()).Returns(false);
              var controller = new AccountsController(repo, notifyService);

              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("/");

            repo.Received(1).RegisterUser(registrationInfo);
              }
        }
Ejemplo n.º 16
0
        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);

              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 RegisterShouldReturnErrorIfRegistrationThrowsMembershipException(RegistrationInfo registrationInfo, MembershipCreateUserException exception, [Frozen] IAccountRepository repo, [Frozen] INotificationService notifyService)
        {
            repo.When(x => x.RegisterUser(Arg.Any<RegistrationInfo>())).Do(x => { throw new MembershipCreateUserException(); });
              var controller = new AccountsController(repo, notifyService);

              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);
        }
Ejemplo n.º 18
0
 public void ShouldAddErrorToModelStateIfNotLoggedIn([Frozen] IAccountRepository repo, LoginInfo info, INotificationService service, IAccountsSettingsService accountSetting)
 {
     repo.Login(string.Empty, string.Empty).ReturnsForAnyArgs(x => false);
       var controller = new AccountsController(repo, service, accountSetting, null);
       var result = controller.Login(info);
       controller.ModelState.IsValid.Should().BeFalse();
       controller.ModelState.Keys.Should().Contain("invalidCredentials");
 }
 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);
       }
 }
Ejemplo n.º 20
0
        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);
              }
        }