Ejemplo n.º 1
0
        public PolicyResult Enforce(ISecurityContext context)
        {
            var innerPolicy = new RequireAllRolesPolicy(UserRole.Administrator);
            var result = innerPolicy.Enforce(context);

            return result.ViolationOccured ? PolicyResult.CreateFailureResult(this, result.Message) : PolicyResult.CreateSuccessResult(this);
        }
        public void Should_return_name_and_role()
        {
            // Arrange
            var roles = new List<object> { "Administrator" }.ToArray();
            var policy = new RequireAllRolesPolicy(roles);

            // Act
            var result = policy.ToString();

            // Assert
            Assert.That(result, Is.EqualTo("FluentSecurity.Policy.RequireAllRolesPolicy (Administrator)"));
        }
Ejemplo n.º 3
0
 public bool Equals(RequireAllRolesPolicy other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (RolesRequired.Count() != other.RolesRequired.Count())
     {
         return(false);
     }
     return(RolesRequired.All(role => other.RolesRequired.Contains(role)));
 }
        public void Should_be_equal()
        {
            var instance1 = new RequireAllRolesPolicy("Editor");
            var instance2 = new RequireAllRolesPolicy("Editor");
            Assert.That(instance1.Equals(instance2), Is.True);
            Assert.That(instance1 == instance2, Is.True);
            Assert.That(instance1 != instance2, Is.False);

            var instance3 = new RequireAllRolesPolicy(UserRole.Writer);
            var instance4 = new RequireAllRolesPolicy(UserRole.Writer);
            Assert.That(instance3.Equals(instance4), Is.True);
            Assert.That(instance3 == instance4, Is.True);
            Assert.That(instance3 != instance4, Is.False);
        }
        public void Should_not_be_equal_when_roles_differ()
        {
            var instance1 = new RequireAllRolesPolicy("Editor");
            var instance2 = new RequireAllRolesPolicy("Writer");
            Assert.That(instance1.Equals(instance2), Is.False);
            Assert.That(instance1 == instance2, Is.False);
            Assert.That(instance1 != instance2, Is.True);

            var instance3 = new RequireAllRolesPolicy(UserRole.Publisher);
            var instance4 = new RequireAllRolesPolicy(UserRole.Owner);
            Assert.That(instance3.Equals(instance4), Is.False);
            Assert.That(instance3 == instance4, Is.False);
            Assert.That(instance3 != instance4, Is.True);
        }
        public override void SetUp()
        {
            base.SetUp();

            // Arrange
            _requireAllRolesPolicy = new RequireAllRolesPolicy("Administrator");
            _denyAnonymousAccessPolicy = new DenyAnonymousAccessPolicy();
            _policies = new List<ISecurityPolicy>
                {
                    _denyAnonymousAccessPolicy
                };
        }
 public void Should_not_be_the_same_when_types_differ()
 {
     var instance1 = new RequireAllRolesPolicy("Editor", "Writer");
     var instance2 = new RequireAnyRolePolicy("Editor", "Writer");
     Assert.That(instance1.GetHashCode(), Is.Not.EqualTo(instance2.GetHashCode()));
 }
 public bool Equals(RequireAllRolesPolicy other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (RolesRequired.Count() != other.RolesRequired.Count()) return false;
     return RolesRequired.All(role => other.RolesRequired.Contains(role));
 }
 public void Should_not_be_equal_when_comparing_to_null()
 {
     var instance = new RequireAllRolesPolicy("Editor");
     Assert.That(instance.Equals(null), Is.False);
 }
        public void Should_not_be_the_same_when_roles_differ()
        {
            var instance1 = new RequireAllRolesPolicy("Editor");
            var instance2 = new RequireAllRolesPolicy("Writer");
            Assert.That(instance1.GetHashCode(), Is.Not.EqualTo(instance2.GetHashCode()));

            var instance3 = new RequireAllRolesPolicy(UserRole.Publisher);
            var instance4 = new RequireAllRolesPolicy(UserRole.Owner);
            Assert.That(instance3.GetHashCode(), Is.Not.EqualTo(instance4.GetHashCode()));
        }
        public void Should_return_expected_roles()
        {
            // Arrange
            var expectedRoles = new List<object> { "Administrator", "Editor" }.ToArray();
            var policy = new RequireAllRolesPolicy(expectedRoles);

            // Act
            var rolesRequired = policy.RolesRequired;

            // Assert
            Assert.That(rolesRequired, Is.EqualTo(expectedRoles));
        }
 protected override void Context()
 {
     // Arrange
     _requireAllRolesPolicy = new RequireAllRolesPolicy("Administrator");
     _denyAnonymousAccessPolicy = new DenyAnonymousAccessPolicy();
     Policies = new List<ISecurityPolicy>
     {
         _denyAnonymousAccessPolicy
     };
 }
        public void Should_be_the_same()
        {
            var instance1 = new RequireAllRolesPolicy("Editor");
            var instance2 = new RequireAllRolesPolicy("Editor");
            Assert.That(instance1.GetHashCode(), Is.EqualTo(instance2.GetHashCode()));

            var instance3 = new RequireAllRolesPolicy(UserRole.Writer);
            var instance4 = new RequireAllRolesPolicy(UserRole.Writer);
            Assert.That(instance3.GetHashCode(), Is.EqualTo(instance4.GetHashCode()));
        }
        public void Should_not_be_successful_when_isAuthenticated_is_true_and_roles_does_not_match()
        {
            // Arrange
            var policy = new RequireAllRolesPolicy("Role1", "Role2");
            const bool authenticated = true;
            var roles = new List<object> { "Role3", "Role4" }.ToArray();
            var context = TestDataFactory.CreateSecurityContext(authenticated, roles);

            // Act
            var result = policy.Enforce(context);

            // Assert
            Assert.That(result.ViolationOccured, Is.True);
            Assert.That(result.Message, Is.EqualTo("Access requires all of the following roles: Role1 and Role2."));
        }
        public void Should_not_be_successful_when_isAuthenticated_is_true_and_roles_are_null()
        {
            // Arrange
            var policy = new RequireAllRolesPolicy(new object[1]);
            const bool authenticated = true;
            IEnumerable<object> roles = null;
            var context = TestDataFactory.CreateSecurityContext(authenticated, roles);

            // Act
            var result = policy.Enforce(context);

            // Assert
            Assert.That(result.ViolationOccured, Is.True);
            Assert.That(result.Message, Is.EqualTo("Access denied"));
        }
        public void Should_not_be_successful_when_isAuthenticated_is_false()
        {
            // Arrange
            var policy = new RequireAllRolesPolicy(new object[1]);
            const bool authenticated = false;
            var context = TestDataFactory.CreateSecurityContext(authenticated);

            // Act
            var result = policy.Enforce(context);

            // Assert
            Assert.That(result.ViolationOccured, Is.True);
            Assert.That(result.Message, Is.EqualTo("Anonymous access denied"));
        }
        public void Should_be_successful_when_isAuthenticated_is_true_and_user_has_all_required_roles()
        {
            // Arrange
            var requiredRoles = new List<object> {
                UserRole.Writer,
                UserRole.Publisher
            };

            var policy = new RequireAllRolesPolicy(requiredRoles.ToArray());

            const bool authenticated = true;
            var roles = new List<object> {
                UserRole.Writer,
                UserRole.Publisher
            };
            var context = TestDataFactory.CreateSecurityContext(authenticated, roles.ToArray());

            // Act
            var result = policy.Enforce(context);

            // Assert
            Assert.That(result.ViolationOccured, Is.False);
        }
        public void Should_resolve_authentication_status_and_roles_exactly_once()
        {
            // Arrange
            var roles = new object[1];
            var policy = new RequireAllRolesPolicy(roles);
            var context = new Mock<ISecurityContext>();
            context.Setup(x => x.CurrentUserIsAuthenticated()).Returns(true);
            context.Setup(x => x.CurrentUserRoles()).Returns(roles);

            // Act
            var result = policy.Enforce(context.Object);

            // Assert
            Assert.That(result.ViolationOccured, Is.False);
            context.Verify(x => x.CurrentUserIsAuthenticated(), Times.Exactly(1), "The authentication status should be resolved at most once.");
            context.Verify(x => x.CurrentUserRoles(), Times.Exactly(1), "The roles should be resolved at most once.");
        }