Skip to content

Commit

Permalink
refactor: rename static method to minimise code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
marikaris committed Feb 18, 2025
1 parent f162b00 commit 0efa460
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.molgenis.armadillo.audit;

import static org.molgenis.armadillo.info.UserInformationRetriever.getUser;
import static org.springframework.security.core.context.SecurityContextHolder.getContext;

import java.security.Principal;
import java.time.Clock;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import org.molgenis.armadillo.info.UserInformationRetriever;
import org.slf4j.MDC;
import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -121,7 +121,7 @@ public void audit(
Map<String, Object> sessionData = new HashMap<>(data);
sessionData.put("sessionId", sessionId);
sessionData.put("roles", roles);
var user = UserInformationRetriever.getUserIdentifierFromPrincipal(principal);
var user = getUser(principal);
applicationEventPublisher.publishEvent(
new AuditApplicationEvent(clock.instant(), user, type, sessionData));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.molgenis.armadillo.audit;

import static org.molgenis.armadillo.info.UserInformationRetriever.getUser;

import jakarta.validation.constraints.NotNull;
import java.util.Map;
import org.molgenis.armadillo.info.UserInformationRetriever;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
Expand All @@ -28,8 +29,7 @@ public void onApplicationEvent(@NotNull AbstractAuthenticationEvent event) {
private void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
publish(
new AuditEvent(
UserInformationRetriever.getUserIdentifierFromPrincipal(
event.getAuthentication().getPrincipal()),
getUser(event.getAuthentication().getPrincipal()),
AUTHENTICATION_SUCCESS,
Map.of(
"details",
Expand All @@ -41,8 +41,7 @@ private void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
private void onLogoutSuccessEvent(LogoutSuccessEvent event) {
publish(
new AuditEvent(
UserInformationRetriever.getUserIdentifierFromPrincipal(
event.getAuthentication().getPrincipal()),
getUser(event.getAuthentication().getPrincipal()),
LOGOUT_SUCCESS,
Map.of(
"details",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class UserInformationRetriever {
static final String EMAIL = "email";
static final String ANONYMOUS = "ANONYMOUS";

public static String getUserIdentifierFromPrincipal(Object principal) {
public static String getUser(Object principal) {
if (principal == null) {
return ANONYMOUS;
} else if (principal instanceof OAuth2AuthenticationToken token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.io.FilenameUtils.removeExtension;
import static org.molgenis.armadillo.info.UserInformationRetriever.getUserIdentifierFromPrincipal;
import static org.molgenis.armadillo.info.UserInformationRetriever.getUser;
import static org.molgenis.armadillo.storage.StorageService.getHumanReadableByteCount;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
Expand Down Expand Up @@ -251,7 +251,7 @@ private static String getOldUserBucketName(Principal principal) {
}

static String getUserBucketName(Principal principal) {
String userIdentifier = getUserIdentifierFromPrincipal(principal).replace("@", "__at__");
String userIdentifier = getUser(principal).replace("@", "__at__");
return USER_PREFIX + userIdentifier;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.molgenis.armadillo.info;

import static org.mockito.Mockito.*;
import static org.molgenis.armadillo.info.UserInformationRetriever.getUserIdentifierFromPrincipal;
import static org.molgenis.armadillo.info.UserInformationRetriever.getUser;

import java.security.Principal;
import org.junit.jupiter.api.Assertions;
Expand All @@ -18,61 +18,60 @@
class UserInformationRetrieverTest {
@Test
void testGetAnonymousUser() {
Assertions.assertEquals(
UserInformationRetriever.ANONYMOUS, getUserIdentifierFromPrincipal(null));
Assertions.assertEquals(UserInformationRetriever.ANONYMOUS, getUser(null));
}

@Test
void testGetOidcUser() {
var principal = mock(OAuth2AuthenticationToken.class, RETURNS_DEEP_STUBS);
when(principal.getPrincipal().getAttribute("email")).thenReturn("henk@molgenis.nl");
Assertions.assertEquals("henk@molgenis.nl", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("henk@molgenis.nl", getUser(principal));
}

@Test
void testGetBasicAuthUser() {
var principal = mock(Principal.class);
when(principal.getName()).thenReturn("admin");

Assertions.assertEquals("admin", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("admin", getUser(principal));
}

@Test
void testGetObject() {
var principal = mock(Object.class);
when(principal.toString()).thenReturn("object");

Assertions.assertEquals("object", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("object", getUser(principal));
}

@Test
void testJwtToken() {
var principal = mock(JwtAuthenticationToken.class, RETURNS_DEEP_STUBS);
when(principal.getTokenAttributes().get("email")).thenReturn("tommy@molgenis.nl");

Assertions.assertEquals("tommy@molgenis.nl", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("tommy@molgenis.nl", getUser(principal));
}

@Test
void testJwt() {
var principal = mock(Jwt.class, RETURNS_DEEP_STUBS);
when(principal.getClaims().get("email")).thenReturn("tommy@molgenis.nl");
Assertions.assertEquals("tommy@molgenis.nl", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("tommy@molgenis.nl", getUser(principal));
}

@Test
void testAuthenticationOfUser() {
var principal = mock(DefaultOAuth2User.class, RETURNS_DEEP_STUBS);
when(principal.getAttributes().get("email")).thenReturn("bofke@molgenis.nl");

Assertions.assertEquals("bofke@molgenis.nl", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("bofke@molgenis.nl", getUser(principal));
}

@Test
void testLoginBasicAuthUser() {
var principal = mock(User.class);
when(principal.getUsername()).thenReturn("admin");

Assertions.assertEquals("admin", getUserIdentifierFromPrincipal(principal));
Assertions.assertEquals("admin", getUser(principal));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.molgenis.armadillo.info.UserInformationRetriever.getUser;
import static org.molgenis.armadillo.storage.ArmadilloStorageService.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
Expand Down Expand Up @@ -540,9 +541,7 @@ void testSaveWorkspaceReturnsErrorWhenTooBig() {
when(workspaceMock.getSize()).thenReturn(123456789123456789L);
try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
assertThrows(
StorageException.class, () -> armadilloStorage.saveWorkspace(is, principal, "test"));
}
Expand All @@ -554,9 +553,7 @@ void testSaveWorkspaceReturnsErrorWhenBiggerThan2Gbs() {
.thenThrow(new StorageException(ArmadilloWorkspace.WORKSPACE_TOO_BIG_ERROR));
try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
try {
armadilloStorage.saveWorkspace(is, principal, "test");
} catch (StorageException e) {
Expand Down Expand Up @@ -922,9 +919,7 @@ void testMoveWorkspacesIfInOldBucket_WhenOldBucketDoesNotExist() {

try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
armadilloStorage.moveWorkspacesIfInOldBucket(principal);

verify(storageService, never()).listObjects(any());
Expand All @@ -941,9 +936,7 @@ void testMoveWorkspacesIfInOldBucket_WhenNewBucketExists() throws FileNotFoundEx

try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
armadilloStorage.moveWorkspacesIfInOldBucket(principal);

verify(storageService, never()).listObjects(any());
Expand All @@ -960,9 +953,7 @@ void testMoveWorkspacesIfInOldBucket_WhenOldBucketExistsButNoWorkspacesToMove()

try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
armadilloStorage.moveWorkspacesIfInOldBucket(principal);

verify(storageService, never()).moveWorkspace(any(), any(), any(), any());
Expand All @@ -981,9 +972,7 @@ void testMoveWorkspacesIfInOldBucket_WhenStorageServiceFails() {

try (MockedStatic<UserInformationRetriever> infoRetriever =
Mockito.mockStatic(UserInformationRetriever.class)) {
infoRetriever
.when(() -> UserInformationRetriever.getUserIdentifierFromPrincipal(principal))
.thenReturn(USER_EMAIL);
infoRetriever.when(() -> getUser(principal)).thenReturn(USER_EMAIL);
assertThrows(
RuntimeException.class, () -> armadilloStorage.moveWorkspacesIfInOldBucket(principal));
}
Expand Down

0 comments on commit 0efa460

Please sign in to comment.