public void SetUp()
 {
     Mock = new MockRepository();
     ProfileService = Mock.DynamicMock<IProfileService>();
     ProfileController = new ProfileController(ProfileService);
     Profile = new ProfileModelDto { ID = 1, Email = "[email protected]", Role = "admin", LoginName = "testuser" };
     ProfileList = new[]
         {
             new ProfileModelDto { ID=1},
             new ProfileModelDto { ID=2}
         };
 }
        public void SetUp()
        {
            Mock = new MockRepository();
            GroupService = Mock.DynamicMock<IGroupService>();
            ProfileService = Mock.DynamicMock<IProfileService>();
            GroupController = new GroupController(GroupService, JournalService);

            SampleGroup = new GroupModelDto { ID = 1, GroupName = "test" };
            SampleProfile = new ProfileModelDto { ID = 1, LoginName = "test" };
            SampleGroupList = new List<GroupModelDto>
                                  {
                                      new GroupModelDto(),
                                      new GroupModelDto()
                                  };
        }
        public int AddProfile(ProfileModelDto profile)
        {
            try
            {
                int id = -1;
                DataAccess.InTransaction(session =>
                    {
                        id = (int)session.Save(ProfileModelDto.UnMap(profile));
                    });

                return id;
            }
            catch (Exception ex)
            {
                Logger.Error("Error : ProfileService.AddProfile - {0}", ex.Message);
                return -1;
            }
        }
示例#4
0
        public void Can_convert_entity_to_form_collection()
        {
            #region Arrange
            var profile = new ProfileModelDto { Email = "[email protected]", ID = 1, IsActive = true,DisplayName="test", LoginName = "test", Role = "test" };
            #endregion

            #region Act

            var form = TestHelper.ConvertEntityToFormCollection(profile);

            #endregion

            #region Assert
            Assert.That(form.Count,Is.EqualTo(6));
            Assert.That(form["Email"], Is.EqualTo("[email protected]"));
            Assert.That(form["IsActive"], Is.EqualTo("True"));
            Assert.That(form["LoginName"], Is.EqualTo("test"));
            Assert.That(form["DisplayName"], Is.EqualTo("test"));
            Assert.That(form["ID"], Is.EqualTo("1"));
            Assert.That(form["Role"], Is.EqualTo("test"));
            #endregion
        }
        public void Can_add_profile()
        {
            #region Arrange
            var profile = new ProfileModelDto { LoginName = "new profile", Email = "[email protected]" };
            #endregion

            #region Act

            int id = new ProfileService().AddProfile(profile);
            ProfileModel profileModel = null;
            DataAccess.InTransaction(session =>
            {
                profileModel = session.Get<ProfileModel>(id);
            });
            #endregion

            #region Assert
            Assert.That(id,Is.GreaterThan(-1));
            Assert.That(profileModel,Is.Not.Null);
            Assert.That(profileModel.LoginName, Is.EqualTo("new profile"));
            #endregion
        }
        public void Updates_role_deletes_user_from_existing_roles()
        {
            #region Arrange

            var roleProvider = _mocker.DynamicMock<IRoleProvider>();
            var profile = new ProfileModelDto { Role = "admin", LoginName = "user" };
            var service = new ProfileService(roleProvider);

            #endregion

            #region Act
            using (_mocker.Record())
            {
                Expect.Call(roleProvider.RoleExists("admin")).Return(true);
                Expect.Call(roleProvider.IsUserInRole("user", "admin")).Return(false);
                Expect.Call(roleProvider.GetRolesForUser("user")).Return(new[] { "unusedRole" });
                Expect.Call(() => roleProvider.RemoveUserFromRole("user", "unusedRole")).Repeat.Once();
                Expect.Call(() => roleProvider.AddUserToRole("user", "admin"));
            }

            using (_mocker.Playback())
            {
                service.UpdateRole(profile, false);
            }

            #endregion

            #region Assert
            #endregion
        }
        public void If_role_parameter_empty_reset_user_roles()
        {
            #region Arrange

            var roleProvider = _mocker.DynamicMock<IRoleProvider>();
            var profile = new ProfileModelDto { Role = "", LoginName = "user" };
            var service = new ProfileService(roleProvider);

            #endregion

            #region Act
            using (_mocker.Record())
            {
                Expect.Call(roleProvider.GetRolesForUser("user")).Return(new[] { "unusedRole" });
                Expect.Call(() => roleProvider.RemoveUserFromRole("user", "unusedRole"));
            }

            using (_mocker.Playback())
            {
                service.UpdateRole(profile, false);
            }

            #endregion
        }
        public void If_no_role_dont_update()
        {
            #region Arrange

            var roleProvider = _mocker.DynamicMock<IRoleProvider>();
            var profile = new ProfileModelDto { Role = "admin", LoginName = "user" };
            var service = new ProfileService(roleProvider);

            #endregion

            #region Act
            using (_mocker.Record())
            {
                Expect.Call(roleProvider.RoleExists("admin")).Return(false);
                Expect.Call(() => roleProvider.AddUserToRole("user", "admin")).Repeat.Never();
                Expect.Call(() => roleProvider.RemoveUserFromRole("user", "unusedRole")).Repeat.Never().IgnoreArguments();
            }

            using (_mocker.Playback())
            {
                service.UpdateRole(profile, false);
            }

            #endregion
        }
        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            try
            {
                var profile = new ProfileModelDto { LoginName = userName, Email = email, Role = "basicuser" };
                AddProfile(profile);
                UpdateRole(profile, false);

                MembershipCreateStatus status;
                if (Membership.Provider.RequiresQuestionAndAnswer)
                    Membership.Provider.CreateUser(profile.LoginName, password, email, "this is sample question", "this is answer", true, null, out status);
                else
                    Membership.Provider.CreateUser(profile.LoginName, password, email, null, null, true, null, out status);
                return status;
            }
            catch (Exception ex)
            {
                Logger.Error("Error : ProfileService.CreateUser - {0}", ex.Message);
                return MembershipCreateStatus.ProviderError;
            }
        }
        public bool UpdateRole(ProfileModelDto profile,bool createIfNotExist)
        {
            var role = profile.Role;
            var userName = profile.LoginName;
            try
            {
                if (!String.IsNullOrWhiteSpace(role))
                {
                    if (_roleProvider.RoleExists(role) && !_roleProvider.IsUserInRole(userName, role))
                    {
                        DeleteUserFromRoles(userName);
                        _roleProvider.AddUserToRole(userName, role);
                    }
                    else if (!_roleProvider.RoleExists(role) && createIfNotExist)
                    {
                        _roleProvider.CreateRole(role);
                        DeleteUserFromRoles(userName);
                        _roleProvider.AddUserToRole(userName, role);
                    }

                }
                else
                {
                    DeleteUserFromRoles(userName);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error("Error : ProfileService.UpdateRole - {0}", ex.Message);
                return false;
            }
        }
 public bool UpdateProfile(ProfileModelDto profile)
 {
     try
     {
         DataAccess.InTransaction(session => session.Update(ProfileModelDto.UnMap(profile)));
         return true;
     }
     catch (Exception ex)
     {
         Logger.Error("Error : ProfileService.UpdateProfile - {0}", ex.Message);
         return false;
     }
 }