working on tests for UserService

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-09-10 15:15:11 +02:00
parent 8865b19fae
commit caa9f07d79
6 changed files with 273 additions and 6 deletions

View File

@@ -50,6 +50,11 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
Files.writeString(storageFile, "{}");
}
json = new JSONObject(Files.readString(storageFile));
json.put(AUTHORIZATIONS, new JSONObject());
json.put(CLIENTS, new JSONObject());
json.put(MAILCONFIG, new JSONObject());
json.put(SESSIONS, new JSONObject());
json.put(USERS, new JSONObject());
auth = null; // lazy init!
}
@@ -137,11 +142,8 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
@Override
public FileStore init(User defaultUser) {
if (!json.has(AUTHORIZATIONS)) json.put(AUTHORIZATIONS, new JSONObject());
if (!json.has(CLIENTS)) json.put(CLIENTS, new JSONObject());
if (!json.has(MAILCONFIG)) json.put(MAILCONFIG, new JSONObject());
if (!json.has(SESSIONS)) json.put(SESSIONS, new JSONObject());
if (!json.has(USERS)) save(defaultUser);
var users = json.getJSONObject(USERS);
if (users.length() < 1) save(defaultUser);
return this;
}

View File

@@ -28,7 +28,7 @@ public class UuidHasher implements PasswordHasher<String> {
@Override
public String salt(String hashedPassword) {
return hashedPassword.split("@")[1];
return hashedPassword.split("@", 2)[1];
}
public static String hex(byte[] bytes) {

View File

@@ -0,0 +1,39 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.datastore.file;
import de.srsoftware.oidc.api.PasswordHasher;
import de.srsoftware.oidc.api.UserService;
import de.srsoftware.oidc.api.UserServiceTest;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
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 UserService userService;
@Override
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();
userService = new FileStore(storage, hasher);
}
@Override
protected UserService userService() {
return userService;
}
}