Exemple #1
0
        public void AddRoleMember_ValidationResult_AlreadyExists()
        {
            var resources = new Resources();

            using (resources.Context.Database.BeginTransaction())
            {
                // Given
                TestHelper.RemoveRoles(resources.Context);
                var role         = TestHelper.CreateRole(resources.Context);
                var roleMember   = TestHelper.CreateRoleMember(resources.Context, role, "testuser");
                var requestModel = new AddRoleMemberRequestModel {
                    RoleId = role.RoleId, Username = roleMember.Username, CreateUser = "******"
                };

                // When
                var result = resources.Controller.AddRoleMember(requestModel);

                // Then
                Assert.IsFalse(result.IsValid);

                var savedRolemembers = resources.Context.Roles.First(r => r.RoleId == requestModel.RoleId).RoleMembers;

                Assert.AreEqual(1, savedRolemembers.Count);
                Assert.IsTrue(savedRolemembers.Any(r => r.RoleId == roleMember.RoleId));
                Assert.IsTrue(savedRolemembers.Any(r => r.Username == roleMember.Username));
            }
        }
        public ValidationResult AddRoleMember([FromBody] AddRoleMemberRequestModel requestModel)
        {
            var response = new ValidationResult();

            var existingRole = Context.Roles.Include(r => r.RoleMembers).FirstOrDefault(r => r.RoleId == requestModel.RoleId);

            if (existingRole == null)
            {
                response.InValidate("", "The role does not exist");
                return(response);
            }

            if (existingRole.RoleMembers.FirstOrDefault(r => r.Username.ToUpper() == requestModel.Username.ToUpper()) != null)
            {
                response.InValidate("", "The rolemember already exists");
                return(response);
            }

            var rolemember = new RoleMember
            {
                RoleId     = requestModel.RoleId,
                Username   = requestModel.Username,
                CreateDate = DateTime.Now,
                CreateUser = requestModel.CreateUser
            };

            existingRole.RoleMembers.Add(rolemember);
            Context.SaveChanges();

            return(response);
        }
        public void AddMember_Successful_ValidationResult()
        {
            // Given
            var resources    = new Resources();
            var requestModel = new AddRoleMemberRequestModel {
                RoleId = 1, Username = "******", CreateUser = resources.TestUsername
            };

            resources.MockApiCaller.AddMockResponse("WebApi:Role:AddRoleMember", requestModel, new ValidationResult());

            // When
            var result = resources.Controller.AddRoleMember(requestModel) as JsonResult;

            // Then
            Assert.IsNotNull(result);
            var resultModel = result.Value as ValidationResult;

            Assert.IsTrue(resultModel.IsValid);
            Assert.AreEqual(0, resultModel.Messages.Count);
        }
        public void AddMember_AlreadyExists_ValidationResult()
        {
            // Given
            var resources    = new Resources();
            var requestModel = new AddRoleMemberRequestModel {
                RoleId = 1, Username = "******", CreateUser = resources.TestUsername
            };
            var validationResult = new ValidationResult();

            validationResult.InValidate("", "The member already exists");
            resources.MockApiCaller.AddMockResponse("WebApi:Role:AddRoleMember", requestModel, validationResult);

            // When
            var result = resources.Controller.AddRoleMember(requestModel) as JsonResult;

            // Then
            Assert.IsNotNull(result);
            var resultModel = result.Value as ValidationResult;

            Assert.IsFalse(resultModel.IsValid);
            Assert.AreEqual(1, resultModel.Messages.Count);
            Assert.AreEqual("The member already exists", resultModel.Messages[0].Message);
        }
        public JsonResult AddRoleMember(AddRoleMemberRequestModel requestModel)
        {
            requestModel.CreateUser = CurrentUser.Username;

            return(Json(WebApiCaller.PostAsync <ValidationResult>("WebApi:Role:AddRoleMember", requestModel)));
        }