public void UserProfileEmailMustBeSet()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var actualProfile = new UserProfile {UserName = "******"};
            var userProfileDbSet = new FakeDbSet<UserProfile>(new[]
                {
                    actualProfile
                });
            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet);

            var sut = new AccountService(() => mockContext.Object);
            var upsertUserProfile = new UserProfile
                {
                    UserName = "******",
                    Email = "testemail"
                };

            //Act
            sut.UpsertUserProfile(upsertUserProfile);

            //Assert
            Assert.AreEqual("testemail", actualProfile.Email);
        }
        public void MustFindMatchingUserViaUserName()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var userId = 3;
            var userProfileDbSet = new FakeDbSet<UserProfile>(new[]
                {
                    new UserProfile {UserId = 4, UserName = "******"},
                    new UserProfile {UserId = userId, UserName = "******"}
                });
            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet);

            var sut = new AccountService(() => mockContext.Object);
            var upsertUserProfile = new UserProfile {UserName = "******"};
           
            //Act
            sut.UpsertUserProfile(upsertUserProfile);

            //Assert
            mockContext
                .Verify(context => context.SetState(
                    It.Is<UserProfile>(profile => profile.UserId == userId),
                    It.IsAny<EntityState>()));
        }
Example #3
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    var profile = new UserProfile
                        {
                            Email = model.Email,
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            UserName = model.UserName,
                        };
                    accountService.UpsertUserProfile(profile);
                    return RedirectToAction("Index", "Dashboard");
                }
                catch (MembershipCreateUserException e)
                {
                    var resultName = Enum.GetName(typeof (MembershipCreateStatus), e.StatusCode);
                    var resourceName = "MembershipCreateStatus_" + resultName;
                    ModelState.AddModelError(string.Empty, resources.GetString(resourceName));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Example #4
0
        public void UpsertUserProfile(UserProfile user)
        {
            using (var dbContext = dataContextFactory())
            {
                var existing = dbContext
                    .UserProfiles
                    .FirstOrDefault(userprofile => userprofile.UserName == user.UserName);
                if (existing == null)
                {
                    existing = dbContext.UserProfiles.Create();
                }
                else
                {
                    dbContext.SetState(existing, EntityState.Modified);
                }
                existing.Email = user.Email;
                existing.FirstName = user.FirstName;
                existing.LastName = user.LastName;
                existing.UserName = user.UserName;

                dbContext.SaveChanges();
            }
        }
        public void TheFoundUserProfileMustSetTheStateToModified()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var userProfileDbSet = new FakeDbSet<UserProfile>(new[]
                {
                    new UserProfile {UserName = "******"}
                });
            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet);

            var sut = new AccountService(() => mockContext.Object);
            var upsertUserProfile = new UserProfile {UserName = "******"};

            //Act
            sut.UpsertUserProfile(upsertUserProfile);

            //Assert
            mockContext
                .Verify(context => context.SetState(
                    It.IsAny<UserProfile>(),
                    It.Is<EntityState>(state => state == EntityState.Modified)));
        }
        public void GetUserMustReturnMatchingUserName()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var userProfile = new UserProfile
                {
                    UserId = 3,
                    UserName = "******"
                };
            var userProfileDbSet = new FakeDbSet<UserProfile>(new[]
                {
                    userProfile,
                    new UserProfile {UserId = 4, UserName = "******"}
                });
            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet);

            var sut = new AccountService(() => mockContext.Object);

            //Act
            var match = sut.GetUser("username");

            //Assert
            Assert.AreEqual(3, match.UserId);
        }
        public void SaveChangesMustBeCalled()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var userProfileDbSet = new FakeDbSet<UserProfile>(new[]
                {
                    new UserProfile{UserName = "******"}
                });

            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet);

            var sut = new AccountService(() => mockContext.Object);
            var upsertUserProfile = new UserProfile
            {
                UserName = "******",
            };

            //Act
            sut.UpsertUserProfile(upsertUserProfile);

            //Assert
            mockContext.Verify(context => context.SaveChanges());
        }
        public void NewUserProfileUserNameMustBeSet()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var actualProfile = new UserProfile();
            var userProfileDbSet = new Mock<FakeDbSet<UserProfile>>();
            userProfileDbSet
                .Setup(helper => helper.Create())
                .Returns(actualProfile);
            mockContext
                .Setup(context => context.UserProfiles)
                .Returns(userProfileDbSet.Object);

            var sut = new AccountService(() => mockContext.Object);
            var upsertUserProfile = new UserProfile
            {
                UserName = "******",
            };

            //Act
            sut.UpsertUserProfile(upsertUserProfile);

            //Assert
            Assert.AreEqual("username", actualProfile.UserName);
        }
Example #9
0
        public ActionResult Manage(PasswordModel model)
        {
            ViewBag.ReturnUrl = Url.Action("Manage");

            if (ModelState.IsValid)
            {
                var username = User.Identity.Name;
                var userProfile = new UserProfile
                    {
                        Email = model.Email,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        UserId = WebSecurity.GetUserId(username),
                        UserName = username
                    };
                accountService.UpsertUserProfile(userProfile);

                // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                if (model.ChangePassword)
                {
                    try
                    {
                        if (WebSecurity.Login(username, model.OldPassword))
                        {
                            WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                        }
                        else
                        {
                            ModelState.AddModelError("", resources.GetString("Manage_PasswordChangeFailed"));
                        }
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("", resources.GetString("Manage_PasswordChangeFailed"));
                    }
                }

                ViewBag.StatusMessage = ModelState.IsValid
                                            ? resources.GetString("Manage_Succeeded")
                                            : resources.GetString("Manage_Failed");
            }
            return View(model);
        }