com.c4_soft.springaddons.security.oauth2.test.webflux.MockAuthenticationWebTestClientConfigurer Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2019 Jérôme Wacongne
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
package com.c4_soft.springaddons.security.oauth2.test.webflux;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collection;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.c4_soft.springaddons.security.oauth2.test.Defaults;
public class MockAuthenticationWebTestClientConfigurer implements AuthenticationConfigurer {
private final T authMock;
private MockAuthenticationWebTestClientConfigurer(T authMock) {
this.authMock = authMock;
}
@Override
public T build() {
return authMock;
}
public MockAuthenticationWebTestClientConfigurer authorities(String... authorities) {
return authorities(Stream.of(authorities));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public MockAuthenticationWebTestClientConfigurer authorities(Stream authorities) {
when(authMock.getAuthorities()).thenReturn((Collection) authorities.map(SimpleGrantedAuthority::new).collect(Collectors.toSet()));
return this;
}
public MockAuthenticationWebTestClientConfigurer name(String name) {
when(authMock.getName()).thenReturn(name);
return this;
}
public MockAuthenticationWebTestClientConfigurer credentials(Object credentials) {
when(authMock.getCredentials()).thenReturn(credentials);
return this;
}
public MockAuthenticationWebTestClientConfigurer details(Object details) {
when(authMock.getDetails()).thenReturn(details);
return this;
}
public MockAuthenticationWebTestClientConfigurer principal(Object principal) {
when(authMock.getPrincipal()).thenReturn(principal);
return this;
}
public MockAuthenticationWebTestClientConfigurer setAuthenticated(boolean authenticated) {
when(authMock.isAuthenticated()).thenReturn(authenticated);
return this;
}
public static MockAuthenticationWebTestClientConfigurer mockAuthentication() {
return mockAuthentication(Authentication.class);
}
public static MockAuthenticationWebTestClientConfigurer mockAuthentication(Class authType) {
return mockAuthentication(authType, auth -> {
});
}
public static <
T extends Authentication> MockAuthenticationWebTestClientConfigurer mockAuthentication(Class authType, Consumer authMockConfigurer) {
final T authMock = authMock(authType);
authMockConfigurer.accept(authMock);
return new MockAuthenticationWebTestClientConfigurer<>(authMock);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
static T authMock(Class authType) {
final T auth = mock(authType);
when(auth.getAuthorities()).thenReturn((Collection) Defaults.GRANTED_AUTHORITIES);
when(auth.getName()).thenReturn(Defaults.AUTH_NAME);
when(auth.isAuthenticated()).thenReturn(true);
return auth;
}
}