implemented session service tests for FileStore, prepared tests for SqliteSessionService
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,4 +1,99 @@
|
|||||||
/* © SRSoftware 2024 */
|
/* © SRSoftware 2024 */
|
||||||
package de.srsoftware.oidc.api;
|
package de.srsoftware.oidc.api;
|
||||||
|
|
||||||
public class SessionServiceTest {}
|
import static de.srsoftware.utils.Strings.uuid;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import de.srsoftware.oidc.api.data.User;
|
||||||
|
import de.srsoftware.utils.PasswordHasher;
|
||||||
|
import de.srsoftware.utils.UuidHasher;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public abstract class SessionServiceTest {
|
||||||
|
private PasswordHasher<String> hasher = null;
|
||||||
|
|
||||||
|
private static final String EMAIL = "arno@nym.de";
|
||||||
|
private static final String PASSWORD = "grunzwanzling";
|
||||||
|
private static final String REALNAME = "Arno Nym";
|
||||||
|
private static final String USERNAME = "arno";
|
||||||
|
|
||||||
|
protected PasswordHasher<String> hasher() {
|
||||||
|
if (hasher == null) try {
|
||||||
|
hasher = new UuidHasher();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasher;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract SessionService sessionService();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAndLoad() {
|
||||||
|
var uuid = uuid();
|
||||||
|
var pass = hasher().hash(PASSWORD, uuid);
|
||||||
|
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
||||||
|
|
||||||
|
Instant now = Instant.now();
|
||||||
|
var session = sessionService().createSession(user);
|
||||||
|
var expiration = session.expiration();
|
||||||
|
assertTrue(expiration.isAfter(now.plus(5, ChronoUnit.MINUTES).minusSeconds(1)));
|
||||||
|
assertTrue(expiration.isBefore(now.plus(5, ChronoUnit.MINUTES).plusSeconds(1)));
|
||||||
|
|
||||||
|
var loaded = sessionService().retrieve(session.id());
|
||||||
|
assertTrue(loaded.isPresent());
|
||||||
|
assertEquals(session, loaded.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAndExtend() {
|
||||||
|
var uuid = uuid();
|
||||||
|
var pass = hasher().hash(PASSWORD, uuid);
|
||||||
|
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
||||||
|
|
||||||
|
var session = sessionService().createSession(user);
|
||||||
|
|
||||||
|
Instant now = Instant.now();
|
||||||
|
sessionService().extend(session, user.sessionDuration(Duration.ofMinutes(10)));
|
||||||
|
var loaded = sessionService().retrieve(session.id());
|
||||||
|
assertTrue(loaded.isPresent());
|
||||||
|
assertEquals(session.id(), loaded.get().id());
|
||||||
|
var expiration = loaded.get().expiration();
|
||||||
|
assertTrue(expiration.isAfter(now.plus(10, ChronoUnit.MINUTES).minusSeconds(1)));
|
||||||
|
assertTrue(expiration.isBefore(now.plus(10, ChronoUnit.MINUTES).plusSeconds(1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void textCreateAndDrop() {
|
||||||
|
var uuid = uuid();
|
||||||
|
var pass = hasher().hash(PASSWORD, uuid);
|
||||||
|
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
||||||
|
|
||||||
|
var session = sessionService().createSession(user);
|
||||||
|
assertTrue(sessionService().retrieve(session.id()).isPresent());
|
||||||
|
|
||||||
|
sessionService().dropSession(session.id());
|
||||||
|
var loaded = sessionService().retrieve(session.id());
|
||||||
|
assertTrue(sessionService().retrieve(session.id()).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpiration() throws InterruptedException {
|
||||||
|
var uuid = uuid();
|
||||||
|
var pass = hasher().hash(PASSWORD, uuid);
|
||||||
|
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofSeconds(2));
|
||||||
|
|
||||||
|
var session = sessionService().createSession(user);
|
||||||
|
assertTrue(sessionService().retrieve(session.id()).isPresent());
|
||||||
|
|
||||||
|
Thread.sleep(2500);
|
||||||
|
|
||||||
|
assertTrue(sessionService().retrieve(session.id()).isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/* © SRSoftware 2024 */
|
||||||
|
package de.srsoftware.oidc.datastore.file;
|
||||||
|
|
||||||
|
|
||||||
|
import de.srsoftware.oidc.api.SessionService;
|
||||||
|
import de.srsoftware.oidc.api.SessionServiceTest;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.UUID;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
|
||||||
|
public class FileStoreSessionServiceTest extends SessionServiceTest {
|
||||||
|
private File storage = new File("/tmp/" + UUID.randomUUID());
|
||||||
|
private SessionService sessionService = null;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() throws IOException {
|
||||||
|
if (storage.exists()) storage.delete();
|
||||||
|
sessionService = new FileStore(storage, hasher());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SessionService sessionService() {
|
||||||
|
return sessionService;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
/* © SRSoftware 2024 */
|
|
||||||
package de.srsoftware.oidc.datastore.file;
|
|
||||||
|
|
||||||
import static de.srsoftware.utils.Strings.uuid;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import de.srsoftware.oidc.api.SessionService;
|
|
||||||
import de.srsoftware.oidc.api.data.User;
|
|
||||||
import de.srsoftware.utils.PasswordHasher;
|
|
||||||
import de.srsoftware.utils.UuidHasher;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
import java.util.UUID;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class SessionServiceTest {
|
|
||||||
private PasswordHasher<String> hasher = null;
|
|
||||||
private File storage = new File("/tmp/" + UUID.randomUUID());
|
|
||||||
private SessionService sessionService;
|
|
||||||
|
|
||||||
private static final String EMAIL = "arno@nym.de";
|
|
||||||
private static final String PASSWORD = "grunzwanzling";
|
|
||||||
private static final String REALNAME = "Arno Nym";
|
|
||||||
private static final String USERNAME = "arno";
|
|
||||||
|
|
||||||
protected PasswordHasher<String> hasher() {
|
|
||||||
if (hasher == null) try {
|
|
||||||
hasher = new UuidHasher();
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasher;
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setup() throws IOException {
|
|
||||||
if (storage.exists()) storage.delete();
|
|
||||||
sessionService = new FileStore(storage, hasher());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateAndLoad() {
|
|
||||||
var uuid = uuid();
|
|
||||||
var pass = hasher().hash(PASSWORD, uuid);
|
|
||||||
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
|
||||||
|
|
||||||
Instant now = Instant.now();
|
|
||||||
var session = sessionService.createSession(user);
|
|
||||||
var expiration = session.expiration();
|
|
||||||
assertTrue(expiration.isAfter(now.plus(5, ChronoUnit.MINUTES).minusSeconds(1)));
|
|
||||||
assertTrue(expiration.isBefore(now.plus(5, ChronoUnit.MINUTES).plusSeconds(1)));
|
|
||||||
|
|
||||||
var loaded = sessionService.retrieve(session.id());
|
|
||||||
assertTrue(loaded.isPresent());
|
|
||||||
assertEquals(session, loaded.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateAndExtend() {
|
|
||||||
var uuid = uuid();
|
|
||||||
var pass = hasher().hash(PASSWORD, uuid);
|
|
||||||
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
|
||||||
|
|
||||||
var session = sessionService.createSession(user);
|
|
||||||
|
|
||||||
Instant now = Instant.now();
|
|
||||||
sessionService.extend(session, user.sessionDuration(Duration.ofMinutes(10)));
|
|
||||||
var loaded = sessionService.retrieve(session.id());
|
|
||||||
assertTrue(loaded.isPresent());
|
|
||||||
assertEquals(session.id(), loaded.get().id());
|
|
||||||
var expiration = loaded.get().expiration();
|
|
||||||
assertTrue(expiration.isAfter(now.plus(10, ChronoUnit.MINUTES).minusSeconds(1)));
|
|
||||||
assertTrue(expiration.isBefore(now.plus(10, ChronoUnit.MINUTES).plusSeconds(1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void textCreateAndDrop() {
|
|
||||||
var uuid = uuid();
|
|
||||||
var pass = hasher().hash(PASSWORD, uuid);
|
|
||||||
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
|
|
||||||
|
|
||||||
var session = sessionService.createSession(user);
|
|
||||||
assertTrue(sessionService.retrieve(session.id()).isPresent());
|
|
||||||
|
|
||||||
sessionService.dropSession(session.id());
|
|
||||||
var loaded = sessionService.retrieve(session.id());
|
|
||||||
assertTrue(sessionService.retrieve(session.id()).isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExpiration() throws InterruptedException {
|
|
||||||
var uuid = uuid();
|
|
||||||
var pass = hasher().hash(PASSWORD, uuid);
|
|
||||||
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofSeconds(2));
|
|
||||||
|
|
||||||
var session = sessionService.createSession(user);
|
|
||||||
assertTrue(sessionService.retrieve(session.id()).isPresent());
|
|
||||||
|
|
||||||
Thread.sleep(2500);
|
|
||||||
|
|
||||||
assertTrue(sessionService.retrieve(session.id()).isEmpty());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/* © SRSoftware 2024 */
|
||||||
|
package de.srsoftware.oidc.datastore.sqlite;
|
||||||
|
|
||||||
|
|
||||||
|
import de.srsoftware.oidc.api.SessionService;
|
||||||
|
import de.srsoftware.oidc.api.SessionServiceTest;
|
||||||
|
import java.io.File;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.UUID;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
|
||||||
|
public class SqliteSessionServiceTest extends SessionServiceTest {
|
||||||
|
private File storage = new File("/tmp/" + UUID.randomUUID());
|
||||||
|
private SessionService sessionService = null;
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown() {
|
||||||
|
if (storage.exists()) storage.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() throws SQLException {
|
||||||
|
tearDown();
|
||||||
|
sessionService = new SqliteSessionService(new ConnectionProvider().get(storage));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SessionService sessionService() {
|
||||||
|
return sessionService;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user