From cf13d4608acfb2d81ec0056717e561bb40f991a2 Mon Sep 17 00:00:00 2001 From: Kostya Date: Sun, 11 Oct 2026 12:20:47 +0300 Subject: server: add account, deposit and user directory endpoints Give each logged-in user their own account view and a view of everyone else, as the spec asks: - GET /api/user/account returns the caller's balance, blocked flag and every ledger row (trades, fees, payouts, deposits). - POST /api/user/deposit (form field "amount") tops the account up through AccountContext.deposit, so its validation and DEPOSIT ledger entry are the same as in the engine. Bad amounts get 400. - GET /api/users lists every account with its name, balance and whether it makes any market, the three facts the spec requires about other users, plus whether it is logged in right now, which the login registry already knows. Accounts outlive their sessions, so the list includes users who are currently logged out. A test drives two clients through a whole market: one deposits, uploads and opens an event, the other deposits and buys, the first resolves it, and the winner's balance rises by exactly one per winning share. --- .../test/java/market/guess/server/AccountTest.java | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 server/src/test/java/market/guess/server/AccountTest.java (limited to 'server/src/test/java') diff --git a/server/src/test/java/market/guess/server/AccountTest.java b/server/src/test/java/market/guess/server/AccountTest.java new file mode 100644 index 0000000..0e094b8 --- /dev/null +++ b/server/src/test/java/market/guess/server/AccountTest.java @@ -0,0 +1,112 @@ +package market.guess.server; + +import static market.guess.server.EventsTest.loggedIn; +import static market.guess.server.EventsTest.path; +import static market.guess.server.EventsTest.upload; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.math.BigDecimal; +import java.net.http.HttpClient; +import java.nio.file.Path; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class AccountTest { + @TempDir static Path base; + static TestServer server; + + @BeforeAll + static void start() throws Exception { + server = new TestServer(base); + } + + @AfterAll + static void stop() throws Exception { + server.close(); + } + + private static JsonObject account(HttpClient client) throws Exception { + var res = server.get(client, "/user/account"); + assertEquals(200, res.statusCode(), res.body()); + return JsonParser.parseString(res.body()).getAsJsonObject(); + } + + private static BigDecimal balance(HttpClient client) throws Exception { + return new BigDecimal(account(client).get("balance").getAsString()); + } + + private static JsonObject userRow(HttpClient client, String name) throws Exception { + for (var row : JsonParser.parseString(server.get(client, "/users").body()).getAsJsonArray()) { + if (row.getAsJsonObject().get("username").getAsString().equals(name)) { + return row.getAsJsonObject(); + } + } + throw new AssertionError(name + " not listed"); + } + + @Test + void twoUsersTradeAnEventEndToEnd() throws Exception { + var maya = loggedIn(server, "Maya"); + var tom = loggedIn(server, "Tom"); + var event = "Mujtaba is Dead"; + var yes = event + ":0"; + + var deposit = server.post(maya, "/user/deposit", "amount", "1000"); + assertEquals(200, deposit.statusCode(), deposit.body()); + assertEquals(new BigDecimal("1000.00"), balance(maya)); + assertTrue(account(maya).toString().contains("DEPOSIT")); + + assertEquals(200, upload(server, maya, "small.xml").statusCode()); + assertTrue(userRow(tom, "Maya").get("marketMaker").getAsBoolean()); + assertFalse(userRow(tom, "Tom").get("marketMaker").getAsBoolean()); + + assertEquals(200, server.post(maya, path(event) + "/open").statusCode()); + var afterSubsidy = balance(maya); + assertTrue(afterSubsidy.compareTo(new BigDecimal("1000")) < 0, "LMSR subsidy was not paid"); + + server.post(tom, "/user/deposit", "amount", "100"); + var order = + server.post( + tom, "/orders", "event", event, "option", yes, "side", "BUY", "price", "1", "quantity", "10"); + assertEquals(200, order.statusCode(), order.body()); + var afterBuy = balance(tom); + assertTrue(afterBuy.compareTo(new BigDecimal("100")) < 0); + assertEquals(afterSubsidy, balance(maya)); + + assertEquals(afterBuy.toPlainString(), userRow(maya, "Tom").get("balance").getAsString()); + + var resolve = server.post(maya, path(event) + "/resolve", "option", yes); + assertEquals(200, resolve.statusCode(), resolve.body()); + assertEquals(afterBuy.add(new BigDecimal("10")), balance(tom)); + } + + @Test + void depositValidationAndLogin() throws Exception { + var ana = loggedIn(server, "Ana"); + var bad = server.post(ana, "/user/deposit", "amount", "-5"); + assertEquals(400, bad.statusCode()); + assertTrue(bad.body().contains("INVALID_AMOUNT"), bad.body()); + assertEquals(400, server.post(ana, "/user/deposit").statusCode()); + assertEquals(BigDecimal.ZERO.setScale(2), balance(ana)); + + var anon = TestServer.client(); + assertEquals(401, server.post(anon, "/user/deposit", "amount", "5").statusCode()); + assertEquals(401, server.get(anon, "/user/account").statusCode()); + assertEquals(401, server.get(anon, "/users").statusCode()); + } + + @Test + void directoryShowsWhoIsOnline() throws Exception { + var watcher = loggedIn(server, "Wes"); + var leaver = loggedIn(server, "Lee"); + assertTrue(userRow(watcher, "Lee").get("online").getAsBoolean()); + server.post(leaver, "/logout"); + assertFalse(userRow(watcher, "Lee").get("online").getAsBoolean()); + } +} -- cgit v1.2.3