public void PermissionValidator_ValidateRole_UniqueCheckIgnoresDeleted()
        {
            var grain         = "app";
            var securableItem = "patientsafety";
            var name          = "admin";

            var existingPermission = new Permission
            {
                Grain         = grain,
                SecurableItem = securableItem,
                Name          = name,
                IsDeleted     = true
            };

            var mockPermissionStore = new Mock <IPermissionStore>()
                                      .SetupGetPermissions(new List <Permission> {
                existingPermission
            }).Create();
            var mockRoleStore = new Mock <IRoleStore>().Object;

            var permissionValidator = new PermissionValidator(new PermissionService(
                                                                  mockPermissionStore,
                                                                  new Mock <RoleService>(mockRoleStore, mockPermissionStore).Object));

            var validationResult = permissionValidator.Validate(new Permission
            {
                Grain         = grain,
                SecurableItem = securableItem,
                Name          = name
            });

            Assert.True(validationResult.IsValid);
        }
        public void PermissionValidator_ValidatePermission_ReturnsInvalidIfModelNotValid(string grain, string securableItem, string permissionName, int errorCount)
        {
            var existingPermission = new Permission
            {
                Id            = Guid.NewGuid(),
                Grain         = "app",
                SecurableItem = "patientsafety",
                Name          = "manageusers"
            };

            var mockPermissionStore = new Mock <IPermissionStore>()
                                      .SetupAddPermissions(new List <Permission>())
                                      .SetupGetPermissions(new List <Permission>
            {
                existingPermission
            })
                                      .Create();

            var mockRoleStore = new Mock <IRoleStore>().Object;

            var permissionValidator = new PermissionValidator(new PermissionService(
                                                                  mockPermissionStore,
                                                                  new Mock <RoleService>(mockRoleStore, mockPermissionStore).Object));
            var validationResult = permissionValidator.Validate(new Permission
            {
                Grain         = grain,
                SecurableItem = securableItem,
                Name          = permissionName
            });

            Assert.False(validationResult.IsValid);
            Assert.NotNull(validationResult.Errors);
            Assert.Equal(errorCount, validationResult.Errors.Count);
        }
示例#3
0
 public ActionResult Create(Permission permission)
 {
     // 以下是验证
     IValidator validator = new PermissionValidator();
     var results = validator.Validate(permission);
     var validationSucceeded = results.IsValid;
     var failures = results.Errors;
     ModelState.AddModelFluentErrors(failures);
     if (ModelState.IsValid)
     {
         PermissionRepository.Add(permission);
         PermissionRepository.Context.Commit();
         return RedirectToAction("Index");
     }
     return View(permission);
 }
        public void PermissionValidator_ValidatePermission_ReturnsValid()
        {
            var mockPermissionStore = new Mock <IPermissionStore>()
                                      .SetupAddPermissions(new List <Permission>())
                                      .SetupGetPermissions(new List <Permission>())
                                      .Create();

            var mockRoleStore = new Mock <IRoleStore>().Object;

            var permissionValidator = new PermissionValidator(new PermissionService(
                                                                  mockPermissionStore,
                                                                  new Mock <RoleService>(mockRoleStore, mockPermissionStore).Object));

            var validationResult = permissionValidator.Validate(new Permission
            {
                Grain         = "app",
                SecurableItem = "patientsafety",
                Name          = "manageusers"
            });

            Assert.True(validationResult.IsValid);
        }