Esempio n. 1
0
        public IActionResult Create(AdminRoleCreateViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                // validate if new role can be inserted due name (avoid duplicated roles).
                bool canInsert = this.roleService.CanInsert(model.Name);

                if (canInsert)
                {
                    // create claims and insert too.
                    var claims = model.Claims.Where(x => x.Check == true).Select(x => new Claim(x.Type, x.Value)).ToList();

                    // insert new role and redirect.
                    this.roleService.Insert(model.Name, claims);
                    return(this.RedirectToAction("Index"));
                }
                else
                {
                    // add model error and return view with model.
                    this.ModelState.AddModelError(string.Empty, this.localizer["There's already a role with the provided name."]);
                    return(this.View(model));
                }
            }
            else
            {
                return(this.View(model));
            }
        }
Esempio n. 2
0
        public IActionResult Create()
        {
            // create model.
            var model = new AdminRoleCreateViewModel();

            model.Claims = this.GetClaims();

            // pass it to view.
            return(this.View(model));
        }
Esempio n. 3
0
        public void CreatePostInvalidModelState()
        {
            // setup
            var model = new AdminRoleCreateViewModel {
                Name = "Test"
            };

            this.controller = new RoleController(this.roleService.Object, this.localizer.Object);
            this.controller.ModelState.AddModelError(string.Empty, "There's already a role with the provided name.");

            // action
            var result = (this.controller.Create(model) as ViewResult).Model as AdminRoleCreateViewModel;

            // assert
            Assert.IsType(typeof(AdminRoleCreateViewModel), result);
        }
Esempio n. 4
0
        public void CreatePostOk()
        {
            // setup
            var model = new AdminRoleCreateViewModel {
                Name = "Test"
            };

            this.roleService.Setup(x => x.CanInsert(model.Name)).Returns(true);
            this.controller = new RoleController(this.roleService.Object, this.localizer.Object);

            // action
            var result = this.controller.Create(model);

            // assert
            Assert.IsType(typeof(RedirectToActionResult), result);
        }
Esempio n. 5
0
        public void CreatePostInvalidName()
        {
            // setup
            var localizedString = new LocalizedString("There's already a role with the provided name.", "There's already a role with the provided name.");
            var model           = new AdminRoleCreateViewModel {
                Name = "Test"
            };

            this.roleService.Setup(x => x.CanInsert(model.Name)).Returns(false);
            this.localizer.Setup(x => x["There's already a role with the provided name."]).Returns(localizedString);
            this.controller = new RoleController(this.roleService.Object, this.localizer.Object);

            // action
            var result = (this.controller.Create(model) as ViewResult).Model as AdminRoleCreateViewModel;

            // assert
            Assert.IsType(typeof(AdminRoleCreateViewModel), result);
            Assert.False(this.controller.ModelState.IsValid);
        }