88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
package games.dmg.accountmanager;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import java.io.IOException;
|
|
import java.net.InetSocketAddress;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.Duration;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
class AccountManagerClientTest {
|
|
@Test
|
|
void deniesPlayersWhenTheApiCannotBeReached() {
|
|
PluginConfig config = new PluginConfig(
|
|
"http://127.0.0.1:1",
|
|
"velocity-test",
|
|
"test-token",
|
|
Duration.ofMillis(100),
|
|
"Register through Discord."
|
|
);
|
|
|
|
AccessDecision decision = new AccountManagerClient(config).check(
|
|
UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5"),
|
|
"Notch",
|
|
"203.0.113.10"
|
|
);
|
|
|
|
assertFalse(decision.allowed());
|
|
assertEquals("Register through Discord.", decision.message());
|
|
}
|
|
|
|
@Test
|
|
void reportsConfirmedConnectionsToTheAuthenticatedEndpoint() throws IOException {
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
AtomicReference<String> body = new AtomicReference<>();
|
|
AtomicReference<String> authorization = new AtomicReference<>();
|
|
server.createContext("/api/velocity/connection", exchange -> {
|
|
body.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
|
|
authorization.set(exchange.getRequestHeaders().getFirst("Authorization"));
|
|
exchange.sendResponseHeaders(204, -1);
|
|
exchange.close();
|
|
});
|
|
server.start();
|
|
try {
|
|
PluginConfig config = new PluginConfig(
|
|
"http://127.0.0.1:" + server.getAddress().getPort(),
|
|
"velocity-test",
|
|
"test-token",
|
|
Duration.ofSeconds(2),
|
|
"Register through Discord."
|
|
);
|
|
|
|
assertTrue(new AccountManagerClient(config).reportConnected(
|
|
UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5"),
|
|
"Notch"
|
|
));
|
|
assertEquals("Bearer test-token", authorization.get());
|
|
assertTrue(body.get().contains("\"minecraftUuid\":\"069a79f444e94726a5befca90e38aaf5\""));
|
|
assertTrue(body.get().contains("\"username\":\"Notch\""));
|
|
} finally {
|
|
server.stop(0);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void connectionReportingIsBestEffortWhenTheApiCannotBeReached() {
|
|
PluginConfig config = new PluginConfig(
|
|
"http://127.0.0.1:1",
|
|
"velocity-test",
|
|
"test-token",
|
|
Duration.ofMillis(100),
|
|
"Register through Discord."
|
|
);
|
|
|
|
boolean recorded = new AccountManagerClient(config).reportConnected(
|
|
UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5"),
|
|
"Notch"
|
|
);
|
|
|
|
assertFalse(recorded);
|
|
}
|
|
}
|