Search in sources :

Example 1 with TestAuthentication

use of org.springframework.security.authentication.TestAuthentication in project spring-security by spring-projects.

the class Jsr250AuthorizationManagerTests method checkDoSomethingWhenNoJsr250AnnotationsThenNullDecision.

@Test
public void checkDoSomethingWhenNoJsr250AnnotationsThenNullDecision() throws Exception {
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomething");
    Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager();
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, methodInvocation);
    assertThat(decision).isNull();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Example 2 with TestAuthentication

use of org.springframework.security.authentication.TestAuthentication in project spring-security by spring-projects.

the class Jsr250AuthorizationManagerTests method checkRolesAllowedUserOrAdminWhenRoleAdminThenGrantedDecision.

@Test
public void checkRolesAllowedUserOrAdminWhenRoleAdminThenGrantedDecision() throws Exception {
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "rolesAllowedUserOrAdmin");
    Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager();
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedAdmin, methodInvocation);
    assertThat(decision).isNotNull();
    assertThat(decision.isGranted()).isTrue();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Example 3 with TestAuthentication

use of org.springframework.security.authentication.TestAuthentication in project spring-security by spring-projects.

the class PostAuthorizeAuthorizationManagerTests method checkDoSomethingWhenNoPostAuthorizeAnnotationThenNullDecision.

@Test
public void checkDoSomethingWhenNoPostAuthorizeAnnotationThenNullDecision() throws Exception {
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomething", new Class[] {}, new Object[] {});
    PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
    MethodInvocationResult result = new MethodInvocationResult(methodInvocation, null);
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, result);
    assertThat(decision).isNull();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Example 4 with TestAuthentication

use of org.springframework.security.authentication.TestAuthentication in project spring-security by spring-projects.

the class PostAuthorizeAuthorizationManagerTests method checkDoSomethingListWhenReturnObjectNotContainsGrantThenDeniedDecision.

@Test
public void checkDoSomethingListWhenReturnObjectNotContainsGrantThenDeniedDecision() throws Exception {
    List<String> list = Collections.singletonList("deny");
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomethingList", new Class[] { List.class }, new Object[] { list });
    MethodInvocationResult result = new MethodInvocationResult(methodInvocation, list);
    PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, result);
    assertThat(decision).isNotNull();
    assertThat(decision.isGranted()).isFalse();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Example 5 with TestAuthentication

use of org.springframework.security.authentication.TestAuthentication in project spring-security by spring-projects.

the class PostAuthorizeAuthorizationManagerTests method checkDoSomethingStringWhenArgIsNotGrantThenDeniedDecision.

@Test
public void checkDoSomethingStringWhenArgIsNotGrantThenDeniedDecision() throws Exception {
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomethingString", new Class[] { String.class }, new Object[] { "deny" });
    MethodInvocationResult result = new MethodInvocationResult(methodInvocation, null);
    PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, result);
    assertThat(decision).isNotNull();
    assertThat(decision.isGranted()).isFalse();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)16 MockMethodInvocation (org.springframework.security.access.intercept.method.MockMethodInvocation)16 TestAuthentication (org.springframework.security.authentication.TestAuthentication)16 AuthorizationDecision (org.springframework.security.authorization.AuthorizationDecision)16