mirror of
https://github.com/Graylog2/graylog2-server.git
synced 2026-03-13 09:32:21 +08:00
adjust for existing unit tests
This commit is contained in:
@@ -38,7 +38,7 @@ public class SessionConverter {
|
||||
SessionUtils.AUTH_CONTEXT_SESSION_KEY
|
||||
);
|
||||
|
||||
public static SessionDTO simpleSessionToSessionDTO(SimpleSession simpleSession) {
|
||||
public static SessionDTO.Builder simpleSessionToSessionDTOBuilder(SimpleSession simpleSession) {
|
||||
|
||||
final var unknownKeys = unknownSessionKeys(simpleSession);
|
||||
if (!unknownKeys.isEmpty()) {
|
||||
@@ -60,8 +60,7 @@ public class SessionConverter {
|
||||
.authenticationRealm(principalInfo.map(PrincipalInfo::realm).orElse(null))
|
||||
.authenticated((Boolean) simpleSession.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY))
|
||||
.userName((String) simpleSession.getAttribute(SessionUtils.USERNAME_SESSION_KEY))
|
||||
.authContext((SessionAuthContext) simpleSession.getAttribute(SessionUtils.AUTH_CONTEXT_SESSION_KEY))
|
||||
.build();
|
||||
.authContext((SessionAuthContext) simpleSession.getAttribute(SessionUtils.AUTH_CONTEXT_SESSION_KEY));
|
||||
}
|
||||
|
||||
private static Set<Object> unknownSessionKeys(SimpleSession simpleSession) {
|
||||
|
||||
@@ -72,7 +72,7 @@ public class SessionDAO extends CachingSessionDAO {
|
||||
|
||||
assignSessionId(session, sessionId);
|
||||
|
||||
final var primaryKey = sessionService.create(SessionDTO.fromSimpleSession(session));
|
||||
final var primaryKey = sessionService.create(SessionDTO.builderFromSimpleSession(session).build());
|
||||
LOG.debug("Created session {}", primaryKey);
|
||||
|
||||
return sessionId;
|
||||
@@ -93,7 +93,7 @@ public class SessionDAO extends CachingSessionDAO {
|
||||
|
||||
private void doUpdate(SimpleSession session) {
|
||||
LOG.debug("Updating session");
|
||||
final var sessionDTO = SessionDTO.fromSimpleSession(session);
|
||||
final var sessionDTO = SessionDTO.builderFromSimpleSession(session).build();
|
||||
sessionService.updateBySessionId(sessionDTO);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.util.Optional;
|
||||
public abstract class SessionDTO implements BuildableMongoEntity<SessionDTO, SessionDTO.Builder> {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public abstract String id();
|
||||
|
||||
@@ -130,8 +131,8 @@ public abstract class SessionDTO implements BuildableMongoEntity<SessionDTO, Ses
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionDTO fromSimpleSession(SimpleSession simpleSession) {
|
||||
return SessionConverter.simpleSessionToSessionDTO(simpleSession);
|
||||
public static Builder builderFromSimpleSession(SimpleSession simpleSession) {
|
||||
return SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession);
|
||||
}
|
||||
|
||||
public SimpleSession toSimpleSession() {
|
||||
|
||||
@@ -89,7 +89,7 @@ class SessionConverterTest {
|
||||
simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY,
|
||||
new SimplePrincipalCollection("user-id", "realm"));
|
||||
|
||||
final var sessionDTO = SessionConverter.simpleSessionToSessionDTO(simpleSession);
|
||||
final var sessionDTO = SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build();
|
||||
|
||||
assertThat(sessionDTO.sessionId()).isEqualTo("session-id");
|
||||
assertThat(sessionDTO.host()).contains("localhost");
|
||||
@@ -111,7 +111,7 @@ class SessionConverterTest {
|
||||
simpleSession.setHost("localhost");
|
||||
simpleSession.setAttribute("unknown-key", "some-value");
|
||||
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession))
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("unknown attribute keys");
|
||||
}
|
||||
@@ -122,25 +122,25 @@ class SessionConverterTest {
|
||||
simpleSession.setId("session-id");
|
||||
simpleSession.setHost("localhost");
|
||||
|
||||
assertThat(SessionConverter.simpleSessionToSessionDTO(simpleSession).userId()).isEmpty();
|
||||
assertThat(SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build().userId()).isEmpty();
|
||||
|
||||
simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, new SimplePrincipalCollection());
|
||||
assertThat(SessionConverter.simpleSessionToSessionDTO(simpleSession).userId()).isEmpty();
|
||||
assertThat(SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build().userId()).isEmpty();
|
||||
|
||||
simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, "not-a-collection");
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession))
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("Unexpected type");
|
||||
|
||||
simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY,
|
||||
new SimplePrincipalCollection(List.of("a", "b", "c"), "realm"));
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession))
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("Expected a single principal");
|
||||
|
||||
simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY,
|
||||
new SimplePrincipalCollection(1, "realm"));
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession))
|
||||
assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("Unexpected type");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user