Skip to content
Snippets Groups Projects
Commit 368f266f authored by Lukas Malte Monnerjahn's avatar Lukas Malte Monnerjahn
Browse files

OZG-5176 restructure CurrentUserHelperTest

parent 7e9146b6
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,6 @@
package de.ozgcloud.admin.common.user;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
......@@ -40,9 +39,120 @@ import org.springframework.security.core.userdetails.User;
import java.util.Collection;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;
class CurrentUserHelperTest {
@DisplayName("Has role")
@Nested
class TestHasRole {
@Mock
private final Authentication mockAuthentication = Mockito.mock(Authentication.class);
@Mock
private final User mockPrincipal = Mockito.mock(User.class);
@Test
void shouldNotHaveRoleIfNull() {
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(null);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(hasRole).isFalse();
}
}
@Test
void shouldNotHaveRoleIfPrincipalIsNull() {
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(null);
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(hasRole).isFalse();
}
}
@Test
void shouldNotHaveRole() {
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(mockPrincipal);
List<GrantedAuthority> authorities = List.of();
Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities);
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
){
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString()))
.thenReturn(false);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(hasRole).isFalse();
}
}
@Test
void shouldHaveRole() {
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(mockPrincipal);
List<GrantedAuthority> authorities = List.of();
Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities);
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
){
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString()))
.thenReturn(true);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(hasRole).isTrue();
}
}
}
@DisplayName("Contains role")
@Nested
class TestContainsRole {
@Test
void shouldNotContainRoleIfNull() {
boolean containsRole = CurrentUserHelper.containsRole(null, UserRole.ADMIN_USER);
assertThat(containsRole).isFalse();
}
@Test
void shouldNotContainRole() {
List<GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER")
);
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER);
assertThat(containsRole).isFalse();
}
@Test
void shouldContainRole() {
Collection<? extends GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER)
);
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER);
assertThat(containsRole).isTrue();
}
}
public class CurrentUserHelperTest {
@DisplayName("Prepare role for check")
@Nested
class TestPrepareRoleForCheck {
......@@ -66,69 +176,43 @@ public class CurrentUserHelperTest {
}
@Test
void shouldReturnPassingRoleIfNonNull() {
void shouldReturnNullIfPassingNull() {
var role = CurrentUserHelper.prepareRoleForCheck(null);
assertThat(role).isNull();
}
}
@DisplayName("Has role")
@DisplayName("Get authentication")
@Nested
class TestContainsRole {
class TestGetAuthentication {
@Mock
Authentication mockAuthentication = Mockito.mock(Authentication.class);
@Mock
SecurityContext mockSecurityContext = Mockito.mock(SecurityContext.class);
@BeforeEach
void beforeEach() {
Mockito.when(mockSecurityContext.getAuthentication()).thenReturn(mockAuthentication);
}
private final SecurityContext mockSecurityContext = Mockito.mock(SecurityContext.class);
@Test
void shouldNotHaveRoleIfNull() {
Mockito.when(mockAuthentication.getAuthorities()).thenReturn(null);
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(null);
void shouldThrowIfNull() {
Mockito.when(mockSecurityContext.getAuthentication()).thenReturn(null);
try (MockedStatic<SecurityContextHolder> contextHolder = Mockito.mockStatic(SecurityContextHolder.class)) {
contextHolder.when(SecurityContextHolder::getContext).thenReturn(mockSecurityContext);
boolean containsRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(containsRole).isFalse();
assertThatIllegalStateException()
.isThrownBy(CurrentUserHelper::getAuthentication)
.withMessage("No authenticated User found");
}
}
@Test
void shouldNotHaveRole() {
List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER"));
User principal = new User("user", "password", authorities);
Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities);
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(principal);
try (MockedStatic<SecurityContextHolder> contextHolder = Mockito.mockStatic(SecurityContextHolder.class)) {
contextHolder.when(SecurityContextHolder::getContext).thenReturn(mockSecurityContext);
boolean containsRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
assertThat(containsRole).isFalse();
}
}
@Test
void shouldHaveRole() {
Collection<? extends GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER));
User principal = new User("user", "password", authorities);
Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities);
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(principal);
void shouldPassAuthentication() {
Authentication mockAuthentication = Mockito.mock(Authentication.class);
Mockito.when(mockSecurityContext.getAuthentication()).thenReturn(mockAuthentication);
try (MockedStatic<SecurityContextHolder> contextHolder = Mockito.mockStatic(SecurityContextHolder.class)) {
contextHolder.when(SecurityContextHolder::getContext).thenReturn(mockSecurityContext);
boolean containsRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
Authentication authentication = CurrentUserHelper.getAuthentication();
assertThat(containsRole).isTrue();
assertThat(authentication).isSameAs(mockAuthentication);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment