コード例 #1
0
 public bool Equals(RequireAnyRolePolicy other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (RolesRequired.Count() != other.RolesRequired.Count())
     {
         return(false);
     }
     return(RolesRequired.All(role => other.RolesRequired.Contains(role)));
 }
コード例 #2
0
        public void Should_not_be_equal_when_roles_differ()
        {
            var instance1 = new RequireAnyRolePolicy("Editor");
            var instance2 = new RequireAnyRolePolicy("Writer");
            Assert.That(instance1.Equals(instance2), Is.False);
            Assert.That(instance1 == instance2, Is.False);
            Assert.That(instance1 != instance2, Is.True);

            var instance3 = new RequireAnyRolePolicy(UserRole.Publisher);
            var instance4 = new RequireAnyRolePolicy(UserRole.Owner);
            Assert.That(instance3.Equals(instance4), Is.False);
            Assert.That(instance3 == instance4, Is.False);
            Assert.That(instance3 != instance4, Is.True);
        }
コード例 #3
0
        public void Should_be_equal()
        {
            var instance1 = new RequireAnyRolePolicy("Editor");
            var instance2 = new RequireAnyRolePolicy("Editor");
            Assert.That(instance1.Equals(instance2), Is.True);
            Assert.That(instance1 == instance2, Is.True);
            Assert.That(instance1 != instance2, Is.False);

            var instance3 = new RequireAnyRolePolicy(UserRole.Writer);
            var instance4 = new RequireAnyRolePolicy(UserRole.Writer);
            Assert.That(instance3.Equals(instance4), Is.True);
            Assert.That(instance3 == instance4, Is.True);
            Assert.That(instance3 != instance4, Is.False);
        }
コード例 #4
0
        public void Should_return_name_and_roles()
        {
            // Arrange
            var roles = new List<object> { "Writer", "Editor", "Administrator" }.ToArray();
            var policy = new RequireAnyRolePolicy(roles);

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

            // Assert
            Assert.That(result, Is.EqualTo("FluentSecurity.Policy.RequireAnyRolePolicy (Writer or Editor or Administrator)"));
        }
コード例 #5
0
 public void Should_not_be_equal_when_comparing_to_null()
 {
     var instance = new RequireAnyRolePolicy("Editor");
     Assert.That(instance.Equals(null), Is.False);
 }
コード例 #6
0
        public void Should_return_expected_roles()
        {
            // Arrange
            var expectedRoles = new List<object> { "Administrator", "Editor" }.ToArray();
            var policy = new RequireAnyRolePolicy(expectedRoles);

            // Act
            var rolesRequired = policy.RolesRequired;

            // Assert
            Assert.That(rolesRequired, Is.EqualTo(expectedRoles));
        }
コード例 #7
0
 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()));
 }
コード例 #8
0
        public void Should_not_be_the_same_when_roles_differ()
        {
            var instance1 = new RequireAnyRolePolicy("Editor");
            var instance2 = new RequireAnyRolePolicy("Writer");
            Assert.That(instance1.GetHashCode(), Is.Not.EqualTo(instance2.GetHashCode()));

            var instance3 = new RequireAnyRolePolicy(UserRole.Publisher);
            var instance4 = new RequireAnyRolePolicy(UserRole.Owner);
            Assert.That(instance3.GetHashCode(), Is.Not.EqualTo(instance4.GetHashCode()));
        }
コード例 #9
0
        public void Should_be_the_same()
        {
            var instance1 = new RequireAnyRolePolicy("Editor");
            var instance2 = new RequireAnyRolePolicy("Editor");
            Assert.That(instance1.GetHashCode(), Is.EqualTo(instance2.GetHashCode()));

            var instance3 = new RequireAnyRolePolicy(UserRole.Writer);
            var instance4 = new RequireAnyRolePolicy(UserRole.Writer);
            Assert.That(instance3.GetHashCode(), Is.EqualTo(instance4.GetHashCode()));
        }
コード例 #10
0
        public void Should_resolve_authentication_status_and_roles_exactly_once()
        {
            // Arrange
            var roles = new object[1];
            var policy = new RequireAnyRolePolicy(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.");
        }
コード例 #11
0
        public void Should_not_be_successful_when_isAuthenticated_is_true_and_roles_does_not_match()
        {
            // Arrange
            var policy = new RequireAnyRolePolicy("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 one of the following roles: Role1 or Role2."));
        }
コード例 #12
0
        public void Should_not_be_successful_when_isAuthenticated_is_true_and_roles_are_null()
        {
            // Arrange
            var policy = new RequireAnyRolePolicy(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"));
        }
コード例 #13
0
        public void Should_not_be_successful_when_isAuthenticated_is_false()
        {
            // Arrange
            var policy = new RequireAnyRolePolicy(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"));
        }
コード例 #14
0
        public void Should_be_successful_when_isAuthenticated_is_true_and_user_has_at_least_one_matching_role()
        {
            // Arrange
            var requiredRoles = new List<object> {
                UserRole.Writer,
                UserRole.Publisher
            };

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

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

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

            // Assert
            Assert.That(result.ViolationOccured, Is.False);
        }
コード例 #15
0
 public bool Equals(RequireAnyRolePolicy other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (RolesRequired.Count() != other.RolesRequired.Count()) return false;
     return RolesRequired.All(role => other.RolesRequired.Contains(role));
 }
コード例 #16
0
 protected override void Context()
 {
     // Arrange
     _requireAnyRolePolicy = new RequireAnyRolePolicy("Administrator");
     _denyAnonymousAccessPolicy = new DenyAnonymousAccessPolicy();
     Policies = new List<ISecurityPolicy>
     {
         _denyAnonymousAccessPolicy
     };
 }