Search in sources :

Example 1 with SecurityContext

use of org.springframework.boot.actuate.endpoint.SecurityContext in project spring-boot by spring-projects.

the class CachingOperationInvokerTests method assertCacheIsUsed.

private void assertCacheIsUsed(Map<String, Object> parameters, Principal principal) {
    OperationInvoker target = mock(OperationInvoker.class);
    Object expected = new Object();
    SecurityContext securityContext = mock(SecurityContext.class);
    if (principal != null) {
        given(securityContext.getPrincipal()).willReturn(principal);
    }
    InvocationContext context = new InvocationContext(securityContext, parameters);
    given(target.invoke(context)).willReturn(expected);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    Object response = invoker.invoke(context);
    assertThat(response).isSameAs(expected);
    then(target).should().invoke(context);
    Object cachedResponse = invoker.invoke(context);
    assertThat(cachedResponse).isSameAs(response);
    then(target).shouldHaveNoMoreInteractions();
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext)

Example 2 with SecurityContext

use of org.springframework.boot.actuate.endpoint.SecurityContext in project spring-boot by spring-projects.

the class CachingOperationInvokerTests method targetAlwaysInvokedWithDifferentPrincipals.

@Test
void targetAlwaysInvokedWithDifferentPrincipals() {
    OperationInvoker target = mock(OperationInvoker.class);
    Map<String, Object> parameters = new HashMap<>();
    SecurityContext securityContext = mock(SecurityContext.class);
    given(securityContext.getPrincipal()).willReturn(mock(Principal.class), mock(Principal.class), mock(Principal.class));
    InvocationContext context = new InvocationContext(securityContext, parameters);
    Object result1 = new Object();
    Object result2 = new Object();
    Object result3 = new Object();
    given(target.invoke(context)).willReturn(result1, result2, result3);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    assertThat(invoker.invoke(context)).isEqualTo(result1);
    assertThat(invoker.invoke(context)).isEqualTo(result2);
    assertThat(invoker.invoke(context)).isEqualTo(result3);
    then(target).should(times(3)).invoke(context);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) HashMap(java.util.HashMap) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 3 with SecurityContext

use of org.springframework.boot.actuate.endpoint.SecurityContext in project spring-boot by spring-projects.

the class CachingOperationInvokerTests method invokeWithUniquePrincipal.

private void invokeWithUniquePrincipal(CachingOperationInvoker invoker) {
    SecurityContext securityContext = mock(SecurityContext.class);
    Principal principal = mock(Principal.class);
    given(securityContext.getPrincipal()).willReturn(principal);
    InvocationContext context = new InvocationContext(securityContext, Collections.emptyMap());
    ((Mono<?>) invoker.invoke(context)).block();
}
Also used : Mono(reactor.core.publisher.Mono) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Principal(java.security.Principal)

Example 4 with SecurityContext

use of org.springframework.boot.actuate.endpoint.SecurityContext in project spring-boot by spring-projects.

the class CachingOperationInvokerTests method targetInvokedWhenCalledWithAndWithoutPrincipal.

@Test
void targetInvokedWhenCalledWithAndWithoutPrincipal() {
    OperationInvoker target = mock(OperationInvoker.class);
    Map<String, Object> parameters = new HashMap<>();
    SecurityContext anonymous = mock(SecurityContext.class);
    SecurityContext authenticated = mock(SecurityContext.class);
    given(authenticated.getPrincipal()).willReturn(mock(Principal.class));
    InvocationContext anonymousContext = new InvocationContext(anonymous, parameters);
    Object anonymousResult = new Object();
    given(target.invoke(anonymousContext)).willReturn(anonymousResult);
    InvocationContext authenticatedContext = new InvocationContext(authenticated, parameters);
    Object authenticatedResult = new Object();
    given(target.invoke(authenticatedContext)).willReturn(authenticatedResult);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    assertThat(invoker.invoke(anonymousContext)).isEqualTo(anonymousResult);
    assertThat(invoker.invoke(authenticatedContext)).isEqualTo(authenticatedResult);
    assertThat(invoker.invoke(anonymousContext)).isEqualTo(anonymousResult);
    assertThat(invoker.invoke(authenticatedContext)).isEqualTo(authenticatedResult);
    then(target).should().invoke(anonymousContext);
    then(target).should().invoke(authenticatedContext);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) HashMap(java.util.HashMap) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Aggregations

InvocationContext (org.springframework.boot.actuate.endpoint.InvocationContext)4 SecurityContext (org.springframework.boot.actuate.endpoint.SecurityContext)4 Principal (java.security.Principal)3 OperationInvoker (org.springframework.boot.actuate.endpoint.invoke.OperationInvoker)3 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 Mono (reactor.core.publisher.Mono)1