diff options
| author | Kostya <mail@sartin.in> | 2026-10-11 12:20:47 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-11 12:21:10 +0300 |
| commit | cf13d4608acfb2d81ec0056717e561bb40f991a2 (patch) | |
| tree | d12b0eaed413e8e17200a6bcb3a6fc6370960044 /server/src/test/java | |
| parent | 1cb67cec8951553fdb5969d947e49f2288f3847f (diff) | |
| download | guess-market-cf13d4608acfb2d81ec0056717e561bb40f991a2.tar.gz guess-market-cf13d4608acfb2d81ec0056717e561bb40f991a2.tar.xz guess-market-cf13d4608acfb2d81ec0056717e561bb40f991a2.zip | |
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.
Diffstat (limited to 'server/src/test/java')
| -rw-r--r-- | server/src/test/java/market/guess/server/AccountTest.java | 112 |
1 files changed, 112 insertions, 0 deletions
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 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import static market.guess.server.EventsTest.loggedIn; | ||
| 4 | import static market.guess.server.EventsTest.path; | ||
| 5 | import static market.guess.server.EventsTest.upload; | ||
| 6 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 7 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| 8 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 9 | |||
| 10 | import com.google.gson.JsonObject; | ||
| 11 | import com.google.gson.JsonParser; | ||
| 12 | import java.math.BigDecimal; | ||
| 13 | import java.net.http.HttpClient; | ||
| 14 | import java.nio.file.Path; | ||
| 15 | import org.junit.jupiter.api.AfterAll; | ||
| 16 | import org.junit.jupiter.api.BeforeAll; | ||
| 17 | import org.junit.jupiter.api.Test; | ||
| 18 | import org.junit.jupiter.api.io.TempDir; | ||
| 19 | |||
| 20 | class AccountTest { | ||
| 21 | @TempDir static Path base; | ||
| 22 | static TestServer server; | ||
| 23 | |||
| 24 | @BeforeAll | ||
| 25 | static void start() throws Exception { | ||
| 26 | server = new TestServer(base); | ||
| 27 | } | ||
| 28 | |||
| 29 | @AfterAll | ||
| 30 | static void stop() throws Exception { | ||
| 31 | server.close(); | ||
| 32 | } | ||
| 33 | |||
| 34 | private static JsonObject account(HttpClient client) throws Exception { | ||
| 35 | var res = server.get(client, "/user/account"); | ||
| 36 | assertEquals(200, res.statusCode(), res.body()); | ||
| 37 | return JsonParser.parseString(res.body()).getAsJsonObject(); | ||
| 38 | } | ||
| 39 | |||
| 40 | private static BigDecimal balance(HttpClient client) throws Exception { | ||
| 41 | return new BigDecimal(account(client).get("balance").getAsString()); | ||
| 42 | } | ||
| 43 | |||
| 44 | private static JsonObject userRow(HttpClient client, String name) throws Exception { | ||
| 45 | for (var row : JsonParser.parseString(server.get(client, "/users").body()).getAsJsonArray()) { | ||
| 46 | if (row.getAsJsonObject().get("username").getAsString().equals(name)) { | ||
| 47 | return row.getAsJsonObject(); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | throw new AssertionError(name + " not listed"); | ||
| 51 | } | ||
| 52 | |||
| 53 | @Test | ||
| 54 | void twoUsersTradeAnEventEndToEnd() throws Exception { | ||
| 55 | var maya = loggedIn(server, "Maya"); | ||
| 56 | var tom = loggedIn(server, "Tom"); | ||
| 57 | var event = "Mujtaba is Dead"; | ||
| 58 | var yes = event + ":0"; | ||
| 59 | |||
| 60 | var deposit = server.post(maya, "/user/deposit", "amount", "1000"); | ||
| 61 | assertEquals(200, deposit.statusCode(), deposit.body()); | ||
| 62 | assertEquals(new BigDecimal("1000.00"), balance(maya)); | ||
| 63 | assertTrue(account(maya).toString().contains("DEPOSIT")); | ||
| 64 | |||
| 65 | assertEquals(200, upload(server, maya, "small.xml").statusCode()); | ||
| 66 | assertTrue(userRow(tom, "Maya").get("marketMaker").getAsBoolean()); | ||
| 67 | assertFalse(userRow(tom, "Tom").get("marketMaker").getAsBoolean()); | ||
| 68 | |||
| 69 | assertEquals(200, server.post(maya, path(event) + "/open").statusCode()); | ||
| 70 | var afterSubsidy = balance(maya); | ||
| 71 | assertTrue(afterSubsidy.compareTo(new BigDecimal("1000")) < 0, "LMSR subsidy was not paid"); | ||
| 72 | |||
| 73 | server.post(tom, "/user/deposit", "amount", "100"); | ||
| 74 | var order = | ||
| 75 | server.post( | ||
| 76 | tom, "/orders", "event", event, "option", yes, "side", "BUY", "price", "1", "quantity", "10"); | ||
| 77 | assertEquals(200, order.statusCode(), order.body()); | ||
| 78 | var afterBuy = balance(tom); | ||
| 79 | assertTrue(afterBuy.compareTo(new BigDecimal("100")) < 0); | ||
| 80 | assertEquals(afterSubsidy, balance(maya)); | ||
| 81 | |||
| 82 | assertEquals(afterBuy.toPlainString(), userRow(maya, "Tom").get("balance").getAsString()); | ||
| 83 | |||
| 84 | var resolve = server.post(maya, path(event) + "/resolve", "option", yes); | ||
| 85 | assertEquals(200, resolve.statusCode(), resolve.body()); | ||
| 86 | assertEquals(afterBuy.add(new BigDecimal("10")), balance(tom)); | ||
| 87 | } | ||
| 88 | |||
| 89 | @Test | ||
| 90 | void depositValidationAndLogin() throws Exception { | ||
| 91 | var ana = loggedIn(server, "Ana"); | ||
| 92 | var bad = server.post(ana, "/user/deposit", "amount", "-5"); | ||
| 93 | assertEquals(400, bad.statusCode()); | ||
| 94 | assertTrue(bad.body().contains("INVALID_AMOUNT"), bad.body()); | ||
| 95 | assertEquals(400, server.post(ana, "/user/deposit").statusCode()); | ||
| 96 | assertEquals(BigDecimal.ZERO.setScale(2), balance(ana)); | ||
| 97 | |||
| 98 | var anon = TestServer.client(); | ||
| 99 | assertEquals(401, server.post(anon, "/user/deposit", "amount", "5").statusCode()); | ||
| 100 | assertEquals(401, server.get(anon, "/user/account").statusCode()); | ||
| 101 | assertEquals(401, server.get(anon, "/users").statusCode()); | ||
| 102 | } | ||
| 103 | |||
| 104 | @Test | ||
| 105 | void directoryShowsWhoIsOnline() throws Exception { | ||
| 106 | var watcher = loggedIn(server, "Wes"); | ||
| 107 | var leaver = loggedIn(server, "Lee"); | ||
| 108 | assertTrue(userRow(watcher, "Lee").get("online").getAsBoolean()); | ||
| 109 | server.post(leaver, "/logout"); | ||
| 110 | assertFalse(userRow(watcher, "Lee").get("online").getAsBoolean()); | ||
| 111 | } | ||
| 112 | } | ||