bugfixes, added ClientServiceTest
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -35,7 +35,6 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
|
||||
private final Path storageFile;
|
||||
private final JSONObject json;
|
||||
private final PasswordHasher<String> passwordHasher;
|
||||
private Map<String, Client> clients = new HashMap<>();
|
||||
private Map<String, AccessToken> accessTokens = new HashMap<>();
|
||||
private Map<String, Authorization> authCodes = new HashMap<>();
|
||||
private Authenticator auth;
|
||||
@@ -268,13 +267,10 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
|
||||
|
||||
@Override
|
||||
public Optional<Client> getClient(String clientId) {
|
||||
var client = clients.get(clientId);
|
||||
if (client != null) return Optional.of(client);
|
||||
if (!json.has(CLIENTS)) return empty();
|
||||
var clientsJson = json.getJSONObject(CLIENTS);
|
||||
if (clientsJson.has(clientId)) {
|
||||
client = toClient(clientId, clientsJson.getJSONObject(clientId));
|
||||
clients.put(clientId, client);
|
||||
var client = toClient(clientId, clientsJson.getJSONObject(clientId));
|
||||
return Optional.of(client);
|
||||
}
|
||||
return empty();
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/* © 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.ClientService;
|
||||
import de.srsoftware.oidc.api.data.Client;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ClientServiceTest {
|
||||
private static ClientService clientService;
|
||||
private static final String NAME = "client-1";
|
||||
private static final String URI = "uri-1";
|
||||
private static final String URI2 = "uri-2";
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
var storage = new File("/tmp/" + UUID.randomUUID());
|
||||
if (storage.exists()) storage.delete();
|
||||
clientService = new FileStore(storage, null);
|
||||
}
|
||||
|
||||
protected ClientService clientService() {
|
||||
return clientService;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAndList() {
|
||||
var cs = clientService();
|
||||
assertTrue(cs.listClients().isEmpty());
|
||||
var clientId = uuid();
|
||||
var clientSecret = uuid();
|
||||
var client = new Client(clientId, NAME, clientSecret, Set.of(URI));
|
||||
var list = cs.save(client).listClients();
|
||||
assertEquals(1, list.size());
|
||||
assertTrue(list.contains(client));
|
||||
cs.remove(client);
|
||||
assertTrue(cs.listClients().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
var cs = clientService();
|
||||
var clientId = uuid();
|
||||
var clientSecret = uuid();
|
||||
var client = new Client(clientId, NAME, clientSecret, Set.of(URI));
|
||||
var optClient = cs.save(client).getClient(clientId);
|
||||
assertTrue(optClient.isPresent());
|
||||
assertEquals(client, optClient.get());
|
||||
optClient = cs.getClient(uuid());
|
||||
assertTrue(optClient.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverride() {
|
||||
var cs = clientService();
|
||||
var clientId = uuid();
|
||||
var clientSecret = uuid();
|
||||
var clientSecret2 = uuid();
|
||||
var client1 = new Client(clientId, NAME, clientSecret, Set.of(URI));
|
||||
var client2 = new Client(clientId, "test", clientSecret2, Set.of(URI2));
|
||||
|
||||
var optClient = cs.save(client1).getClient(clientId);
|
||||
assertTrue(optClient.isPresent());
|
||||
assertEquals(client1, optClient.get());
|
||||
|
||||
optClient = cs.save(client2).getClient(clientId);
|
||||
assertTrue(optClient.isPresent());
|
||||
assertEquals(client2, optClient.get());
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
public class FileStoreAuthServiceTest extends AuthServiceTest {
|
||||
private File storage = new File("/tmp/" + UUID.randomUUID());
|
||||
private AuthorizationService authorizationService;
|
||||
|
||||
@Override
|
||||
@@ -21,6 +20,7 @@ public class FileStoreAuthServiceTest extends AuthServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
var storage = new File("/tmp/" + UUID.randomUUID());
|
||||
if (storage.exists()) storage.delete();
|
||||
authorizationService = new FileStore(storage, null);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,11 @@ 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 {
|
||||
var storage = new File("/tmp/" + UUID.randomUUID());
|
||||
if (storage.exists()) storage.delete();
|
||||
sessionService = new FileStore(storage, hasher());
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
public class FileStoreUserServiceTest extends UserServiceTest {
|
||||
private PasswordHasher<String> hasher = null;
|
||||
private File storage = new File("/tmp/" + UUID.randomUUID());
|
||||
private PasswordHasher<String> hasher = null;
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
@@ -29,6 +28,7 @@ public class FileStoreUserServiceTest extends UserServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
var storage = new File("/tmp/" + UUID.randomUUID());
|
||||
if (storage.exists()) storage.delete();
|
||||
userService = new FileStore(storage, hasher());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user