Example #1
0
        public ActionResult Create(Role role)
        {
            if (ViewData.ModelState.IsValid)
            {
                role.LastUpdateTimeStamp = DateTime.Now;
                role.LastUpdateUser = GetCurrentUser().Id;
                ActionConfirmation saveOrUpdateConfirmation =
                    _roleManagementService.SaveOrUpdate(role);

                if (saveOrUpdateConfirmation.WasSuccessful)
                {
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] =
                        saveOrUpdateConfirmation.Message;
                    return RedirectToAction("Index");
                }
            }
            else
            {
                role = null;
            }

            RoleFormViewModel viewModel =
                _roleManagementService.CreateFormViewModelFor(role);
            return View(viewModel);
        }
Example #2
0
        public void CanCompareRoles()
        {
            var instance = new Role {RoleName = "Role 1"};

            var instanceToCompareTo = new Role {RoleName = "Role 1"};

            instance.ShouldEqual(instanceToCompareTo);
        }
        public void CanDeleteRole()
        {
            // Establish Context
            var roleToDelete = new Role();

            roleRepository.Expect(r => r.Get(1))
                .Return(roleToDelete);

            // Act
            ActionConfirmation confirmation =
                roleManagementService.Delete(1);

            // Assert
            confirmation.ShouldNotBeNull();
            confirmation.WasSuccessful.ShouldBeTrue();
            confirmation.Value.ShouldBeNull();
        }
        public void CanCreateValidRoleFromForm()
        {
            // Establish Context
            var roleFromForm = new Role();
            var testUser = PersonInstanceFactory.CreateValidTransientPerson();
            testUser.SetAssignedIdTo(1);

            _authenticationProvider.Expect(r => r.GetLoggedInUser()).Return("testuser");
            _personManagementService.Expect(r => r.GetByUserName(Arg<string>.Is.Anything)).Return(testUser);
            _roleManagementService.Expect(r => r.SaveOrUpdate(roleFromForm))
                .Return(ActionConfirmation.CreateSuccessConfirmation("saved"));

            // Act
            RedirectToRouteResult redirectResult =
                _rolesController.Create(roleFromForm)
                    .AssertActionRedirect().ToAction("Index");

            // Assert
            _rolesController.TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()].ToString()
                .ShouldEqual("saved");
        }
        public ActionConfirmation SaveOrUpdate(Role role)
        {
            if (role.IsValid())
            {
                roleRepository.SaveOrUpdate(role);

                ActionConfirmation saveOrUpdateConfirmation = ActionConfirmation.CreateSuccessConfirmation(
                    "The role was successfully saved.");
                saveOrUpdateConfirmation.Value = role;

                return saveOrUpdateConfirmation;
            }
            else
            {
                roleRepository.DbContext.RollbackTransaction();

                return ActionConfirmation.CreateFailureConfirmation(
                    "The role could not be saved due to missing or invalid information.");
            }
        }
 public RoleFormViewModel CreateFormViewModelFor(Role role)
 {
     RoleFormViewModel viewModel = CreateFormViewModel();
     viewModel.Role = role;
     return viewModel;
 }
 private void TransferFormValuesTo(Role roleToUpdate, Role roleFromForm)
 {
     roleToUpdate.RoleName = roleFromForm.RoleName;
     roleToUpdate.LastUpdateTimeStamp = roleFromForm.LastUpdateTimeStamp;
     roleToUpdate.LastUpdateUser = roleFromForm.LastUpdateUser;
 }
        public ActionConfirmation UpdateWith(Role roleFromForm, int idOfRoleToUpdate)
        {
            Role roleToUpdate =
                roleRepository.Get(idOfRoleToUpdate);
            TransferFormValuesTo(roleToUpdate, roleFromForm);

            if (roleToUpdate.IsValid())
            {
                ActionConfirmation updateConfirmation = ActionConfirmation.CreateSuccessConfirmation(
                    "The role was successfully updated.");
                updateConfirmation.Value = roleToUpdate;

                return updateConfirmation;
            }
            else
            {
                roleRepository.DbContext.RollbackTransaction();

                return ActionConfirmation.CreateFailureConfirmation(
                    "The role could not be saved due to missing or invalid information.");
            }
        }
        public void CannotUpdateInvalidRoleFromForm()
        {
            // Establish Context
            var roleFromForm = new Role();
            var viewModelToExpect = new RoleFormViewModel();
            var testUser = PersonInstanceFactory.CreateValidTransientPerson();
            testUser.SetAssignedIdTo(1);

            _authenticationProvider.Expect(r => r.GetLoggedInUser()).Return("testuser");
            _personManagementService.Expect(r => r.GetByUserName(Arg<string>.Is.Anything)).Return(testUser);
            _roleManagementService.Expect(r => r.UpdateWith(roleFromForm, 0))
                .Return(ActionConfirmation.CreateFailureConfirmation("not updated"));
            _roleManagementService.Expect(r => r.CreateFormViewModelFor(roleFromForm))
                .Return(viewModelToExpect);

            // Act
            ViewResult result =
                _rolesController.Edit(roleFromForm).AssertViewRendered();

            // Assert
            result.ViewData.Model.ShouldNotBeNull();
            (result.ViewData.Model as RoleFormViewModel).ShouldNotBeNull();
        }
        public void CanUpdateWithValidRoleFromForm()
        {
            // Establish Context
            Role validRoleFromForm =
                RoleInstanceFactory.CreateValidTransientRole();

            // Intentionally empty to ensure successful transfer of values
            var roleFromDb = new Role();

            roleRepository.Expect(r => r.Get(1))
                .Return(roleFromDb);

            // Act
            ActionConfirmation confirmation =
                roleManagementService.UpdateWith(validRoleFromForm, 1);

            // Assert
            confirmation.ShouldNotBeNull();
            confirmation.WasSuccessful.ShouldBeTrue();
            confirmation.Value.ShouldNotBeNull();
            confirmation.Value.ShouldEqual(roleFromDb);
            confirmation.Value.ShouldEqual(validRoleFromForm);
        }
        public void CannotUpdateWithInvalidRoleFromForm()
        {
            // Establish Context
            var invalidRoleFromForm = new Role();

            // Intentionally empty to ensure successful transfer of values
            var roleFromDb = new Role();

            roleRepository.Expect(r => r.Get(1))
                .Return(roleFromDb);

            // Act
            ActionConfirmation confirmation =
                roleManagementService.UpdateWith(invalidRoleFromForm, 1);

            // Assert
            confirmation.ShouldNotBeNull();
            confirmation.WasSuccessful.ShouldBeFalse();
            confirmation.Value.ShouldBeNull();
        }
        public void CannotSaveOrUpdateInvalidRole()
        {
            // Establish Context
            var invalidRole = new Role();

            // Act
            ActionConfirmation confirmation =
                roleManagementService.SaveOrUpdate(invalidRole);

            // Assert
            confirmation.ShouldNotBeNull();
            confirmation.WasSuccessful.ShouldBeFalse();
            confirmation.Value.ShouldBeNull();
        }