Add MongoOperationsSessionRepository
Fixes gh-17
This commit is contained in:
committed by
Rob Winch
parent
7b28b214ff
commit
34cebc3df6
@@ -0,0 +1,28 @@
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextImpl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
public class AuthenticationParserTests {
|
||||
|
||||
@Test
|
||||
public void shouldExtractName() {
|
||||
//given
|
||||
String principalName = "john_the_springer";
|
||||
SecurityContextImpl context = new SecurityContextImpl();
|
||||
context.setAuthentication(new UsernamePasswordAuthenticationToken(principalName, null));
|
||||
|
||||
//when
|
||||
String extractedName = AuthenticationParser.extractName(context);
|
||||
|
||||
//then
|
||||
assertThat(extractedName).isEqualTo(principalName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextImpl;
|
||||
import org.springframework.session.ExpiringSession;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
public class JdkMongoSessionConverterTests {
|
||||
|
||||
JdkMongoSessionConverter sut = new JdkMongoSessionConverter();
|
||||
|
||||
@Test
|
||||
public void verifyRoundTripSerialization() throws Exception {
|
||||
//given
|
||||
MongoExpiringSession toSerialize = new MongoExpiringSession();
|
||||
toSerialize.setAttribute("username", "john_the_springer");
|
||||
|
||||
//when
|
||||
DBObject dbObject = convertToDBObject(toSerialize);
|
||||
ExpiringSession deserialized = convertToSession(dbObject);
|
||||
|
||||
//then
|
||||
assertThat(deserialized).isEqualToComparingFieldByField(toSerialize);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExtractPrincipalNameFromAttributes() throws Exception {
|
||||
//given
|
||||
MongoExpiringSession toSerialize = new MongoExpiringSession();
|
||||
String principalName = "john_the_springer";
|
||||
toSerialize.setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, principalName);
|
||||
|
||||
//when
|
||||
DBObject dbObject = convertToDBObject(toSerialize);
|
||||
|
||||
//then
|
||||
assertThat(dbObject.get("principal")).isEqualTo(principalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExtractPrincipalNameFromAuthentication() throws Exception {
|
||||
//given
|
||||
MongoExpiringSession toSerialize = new MongoExpiringSession();
|
||||
String principalName = "john_the_springer";
|
||||
SecurityContextImpl context = new SecurityContextImpl();
|
||||
context.setAuthentication(new UsernamePasswordAuthenticationToken(principalName, null));
|
||||
toSerialize.setAttribute("SPRING_SECURITY_CONTEXT", context);
|
||||
|
||||
//when
|
||||
DBObject dbObject = convertToDBObject(toSerialize);
|
||||
|
||||
//then
|
||||
assertThat(dbObject.get("principal")).isEqualTo(principalName);
|
||||
}
|
||||
|
||||
MongoExpiringSession convertToSession(DBObject session) {
|
||||
return (MongoExpiringSession) sut.convert(session, TypeDescriptor.valueOf(DBObject.class), TypeDescriptor.valueOf(MongoExpiringSession.class));
|
||||
}
|
||||
|
||||
DBObject convertToDBObject(MongoExpiringSession session) {
|
||||
return (DBObject) sut.convert(session, TypeDescriptor.valueOf(MongoExpiringSession.class), TypeDescriptor.valueOf(DBObject.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 org.springframework.session.data.mongo;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.session.ExpiringSession;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MongoOperationsSessionRepositoryTests {
|
||||
|
||||
@Mock
|
||||
MongoOperations mongoOperations;
|
||||
@Mock
|
||||
MongoSessionConverter converter;
|
||||
|
||||
MongoOperationsSessionRepository sut;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
sut = new MongoOperationsSessionRepository(mongoOperations);
|
||||
sut.setMongoSessionConverter(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateSession() throws Exception {
|
||||
//when
|
||||
ExpiringSession session = sut.createSession();
|
||||
|
||||
//then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveSession() throws Exception {
|
||||
//given
|
||||
MongoExpiringSession session = new MongoExpiringSession();
|
||||
BasicDBObject dbSession = new BasicDBObject();
|
||||
DBCollection collection = mock(DBCollection.class);
|
||||
|
||||
when(converter.convert(session, TypeDescriptor.valueOf(MongoExpiringSession.class), TypeDescriptor.valueOf(DBObject.class))).thenReturn(dbSession);
|
||||
when(mongoOperations.getCollection(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).thenReturn(collection);
|
||||
//when
|
||||
sut.save(session);
|
||||
|
||||
//then
|
||||
verify(collection).save(dbSession);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetSession() throws Exception {
|
||||
//given
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
BasicDBObject dbSession = new BasicDBObject();
|
||||
when(mongoOperations.findById(sessionId, DBObject.class, MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).thenReturn(dbSession);
|
||||
MongoExpiringSession session = new MongoExpiringSession();
|
||||
when(converter.convert(dbSession, TypeDescriptor.valueOf(DBObject.class), TypeDescriptor.valueOf(MongoExpiringSession.class))).thenReturn(session);
|
||||
|
||||
//when
|
||||
ExpiringSession retrievedSession = sut.getSession(sessionId);
|
||||
|
||||
//then
|
||||
assertThat(retrievedSession).isEqualTo(session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleExpiredSession() throws Exception {
|
||||
//given
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
BasicDBObject dbSession = new BasicDBObject();
|
||||
when(mongoOperations.findById(sessionId, DBObject.class, MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).thenReturn(dbSession);
|
||||
MongoExpiringSession session = mock(MongoExpiringSession.class);
|
||||
when(session.isExpired()).thenReturn(true);
|
||||
when(session.getId()).thenReturn(sessionId);
|
||||
when(converter.convert(dbSession, TypeDescriptor.valueOf(DBObject.class), TypeDescriptor.valueOf(MongoExpiringSession.class))).thenReturn(session);
|
||||
|
||||
//when
|
||||
sut.getSession(sessionId);
|
||||
|
||||
//then
|
||||
verify(mongoOperations).remove(any(DBObject.class), eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteSession() throws Exception {
|
||||
//given
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
|
||||
//when
|
||||
sut.delete(sessionId);
|
||||
|
||||
//then
|
||||
verify(mongoOperations).remove(any(DBObject.class), eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetSessionsMapByPrincipal() throws Exception {
|
||||
//given
|
||||
String principalNameIndexName = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
|
||||
|
||||
DBObject dbSession = new BasicDBObject();
|
||||
when(converter.getQueryForIndex(anyString(), Matchers.anyObject())).thenReturn(mock(Query.class));
|
||||
when(mongoOperations.find(any(Query.class), eq(DBObject.class), eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)))
|
||||
.thenReturn(Collections.singletonList(dbSession));
|
||||
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
|
||||
MongoExpiringSession session = new MongoExpiringSession(sessionId, 1800);
|
||||
when(converter.convert(dbSession, TypeDescriptor.valueOf(DBObject.class), TypeDescriptor.valueOf(MongoExpiringSession.class))).thenReturn(session);
|
||||
//when
|
||||
Map<String, MongoExpiringSession> sessionsMap = sut.findByIndexNameAndIndexValue(principalNameIndexName, "john");
|
||||
|
||||
//then
|
||||
assertThat(sessionsMap).containsOnlyKeys(sessionId);
|
||||
assertThat(sessionsMap).containsValues(session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnEmptyMapForNotSupportedIndex() throws Exception {
|
||||
//given
|
||||
String index = "some_not_supported_index_name";
|
||||
|
||||
//when
|
||||
Map<String, MongoExpiringSession> sessionsMap = sut.findByIndexNameAndIndexValue(index, "some_value");
|
||||
|
||||
//then
|
||||
assertThat(sessionsMap).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user