feat(policy): velocity plugin sending a lovisual:policy payload
This commit is contained in:
parent
af2b417289
commit
711fcafc3a
9 changed files with 543 additions and 0 deletions
|
|
@ -0,0 +1,105 @@
|
|||
package dev.loki.lovisual.policy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PolicyConfigTest {
|
||||
|
||||
@Test
|
||||
void defaultsAreEnabledLockWithExampleDeny() {
|
||||
PolicyConfig cfg = PolicyConfig.defaults();
|
||||
assertTrue(cfg.enabled());
|
||||
assertEquals(PolicyMode.LOCK, cfg.mode());
|
||||
assertFalse(cfg.reason().isBlank());
|
||||
assertFalse(cfg.deny().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromMapParsesAllFields() {
|
||||
PolicyConfig cfg = PolicyConfig.fromMap(Map.of(
|
||||
"enabled", true,
|
||||
"mode", "suggest",
|
||||
"reason", "custom rules",
|
||||
"deny", List.of("esp", "freecam")
|
||||
));
|
||||
assertTrue(cfg.enabled());
|
||||
assertEquals(PolicyMode.SUGGEST, cfg.mode());
|
||||
assertEquals("custom rules", cfg.reason());
|
||||
assertEquals(List.of("esp", "freecam"), cfg.deny());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromMapMissingModeDefaultsToLock() {
|
||||
PolicyConfig cfg = PolicyConfig.fromMap(Map.of("deny", List.of("esp")));
|
||||
assertEquals(PolicyMode.LOCK, cfg.mode());
|
||||
assertEquals(List.of("esp"), cfg.deny());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromMapUnknownModeThrows() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> PolicyConfig.fromMap(Map.of("mode", "banana")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromMapDisabledWithEmptyDeny() {
|
||||
PolicyConfig cfg = PolicyConfig.fromMap(Map.of(
|
||||
"enabled", false,
|
||||
"mode", "lock",
|
||||
"deny", List.of()
|
||||
));
|
||||
assertFalse(cfg.enabled());
|
||||
assertTrue(cfg.deny().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadWritesDefaultConfigWhenMissing(@TempDir Path dir) throws Exception {
|
||||
Path file = dir.resolve("config.yml");
|
||||
PolicyConfig cfg = PolicyConfig.load(file);
|
||||
assertTrue(Files.exists(file));
|
||||
assertEquals(PolicyConfig.defaults(), cfg);
|
||||
assertEquals(cfg, PolicyConfig.load(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadParsesYamlFile(@TempDir Path dir) throws Exception {
|
||||
Path file = dir.resolve("config.yml");
|
||||
Files.writeString(file, """
|
||||
enabled: false
|
||||
mode: suggest
|
||||
reason: "no cheats"
|
||||
deny:
|
||||
- esp
|
||||
- hitboxes
|
||||
""");
|
||||
PolicyConfig cfg = PolicyConfig.load(file);
|
||||
assertFalse(cfg.enabled());
|
||||
assertEquals(PolicyMode.SUGGEST, cfg.mode());
|
||||
assertEquals("no cheats", cfg.reason());
|
||||
assertEquals(List.of("esp", "hitboxes"), cfg.deny());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadThrowsOnUnknownMode(@TempDir Path dir) throws Exception {
|
||||
Path file = dir.resolve("config.yml");
|
||||
Files.writeString(file, "mode: banana\n");
|
||||
assertThrows(IllegalArgumentException.class, () -> PolicyConfig.load(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadOfEmptyFileFallsBackToDefaults(@TempDir Path dir) throws Exception {
|
||||
Path file = dir.resolve("config.yml");
|
||||
Files.writeString(file, "");
|
||||
assertEquals(PolicyConfig.defaults(), PolicyConfig.load(file));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package dev.loki.lovisual.policy;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PolicyMessageTest {
|
||||
|
||||
@Test
|
||||
void encodePolicyWritesAllFields() {
|
||||
PolicyConfig cfg = new PolicyConfig(true, PolicyMode.SUGGEST, "server rules",
|
||||
List.of("esp", "freecam"));
|
||||
String json = PolicyMessage.encodePolicy(cfg, "lobby-1");
|
||||
JsonObject parsed = JsonParser.parseString(json).getAsJsonObject();
|
||||
assertEquals("policy", parsed.get("type").getAsString());
|
||||
assertEquals("suggest", parsed.get("mode").getAsString());
|
||||
assertEquals("server rules", parsed.get("reason").getAsString());
|
||||
assertEquals("lobby-1", parsed.get("server").getAsString());
|
||||
assertEquals(2, parsed.getAsJsonArray("deny").size());
|
||||
assertEquals("esp", parsed.getAsJsonArray("deny").get(0).getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void encodePolicyEscapesReasonAndDefaultsServerToEmpty() {
|
||||
PolicyConfig cfg = new PolicyConfig(true, PolicyMode.LOCK, "say \"hi\"\nline2", List.of());
|
||||
JsonObject parsed = JsonParser.parseString(PolicyMessage.encodePolicy(cfg, null)).getAsJsonObject();
|
||||
assertEquals("say \"hi\"\nline2", parsed.get("reason").getAsString());
|
||||
assertEquals("", parsed.get("server").getAsString());
|
||||
assertEquals(0, parsed.getAsJsonArray("deny").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void isRequestAcceptsOnlyTypedRequests() {
|
||||
assertTrue(PolicyMessage.isRequest("{\"type\":\"request\"}"));
|
||||
assertTrue(PolicyMessage.isRequest(" { \"type\" : \"request\" } "));
|
||||
assertFalse(PolicyMessage.isRequest("{\"type\":\"policy\"}"));
|
||||
assertFalse(PolicyMessage.isRequest("{\"type\":42}"));
|
||||
assertFalse(PolicyMessage.isRequest("garbage"));
|
||||
assertFalse(PolicyMessage.isRequest(""));
|
||||
assertFalse(PolicyMessage.isRequest(null));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue