diff options
| author | Kostya <mail@sartin.in> | 2026-10-09 18:44:33 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-09 18:45:01 +0300 |
| commit | 33d313e17b841db3736c465447913df0cc73d5df (patch) | |
| tree | 33ecf73f3b71897c4f8413c6e61a025837d88a83 /server/src/test/java | |
| parent | 9c1a10712a724b448de9e066ff76f97059c86b40 (diff) | |
| download | guess-market-33d313e17b841db3736c465447913df0cc73d5df.tar.gz guess-market-33d313e17b841db3736c465447913df0cc73d5df.tar.xz guess-market-33d313e17b841db3736c465447913df0cc73d5df.zip | |
server: add username login and logout endpoints
The spec asks for a login by unique name only, with no passwords or
sign-up, and an error that lets the user try again if the name exists.
Add POST /api/login (form field "username") and POST /api/logout.
Sessions maps each name, ignoring case, to the one HTTP session
holding it. A second session asking for a held name gets 409
NAME_TAKEN, and a blank name gets 400. When a session ends, by logout
or by Tomcat timing it out after a client quits without logging out,
an HttpSessionListener frees the name. The account itself (ledger,
Market Maker roles) stays, and logging in with that name again resumes
it rather than locking the user out for the life of the server. New
accounts start with a zero balance and are funded through deposits.
Json gives every endpoint the same response shape, with errors as
{"error": CODE, "message": text} built from a failed Result. Tomcat
setup for the tests moves into a shared TestServer helper, whose
clients each keep their own cookie jar and so act as separate users.
Diffstat (limited to 'server/src/test/java')
3 files changed, 159 insertions, 37 deletions
diff --git a/server/src/test/java/market/guess/server/LoginTest.java b/server/src/test/java/market/guess/server/LoginTest.java new file mode 100644 index 0000000..fc556ae --- /dev/null +++ b/server/src/test/java/market/guess/server/LoginTest.java | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 5 | |||
| 6 | import java.nio.file.Path; | ||
| 7 | import org.junit.jupiter.api.AfterAll; | ||
| 8 | import org.junit.jupiter.api.BeforeAll; | ||
| 9 | import org.junit.jupiter.api.Test; | ||
| 10 | import org.junit.jupiter.api.io.TempDir; | ||
| 11 | |||
| 12 | class LoginTest { | ||
| 13 | @TempDir static Path base; | ||
| 14 | private static TestServer server; | ||
| 15 | |||
| 16 | @BeforeAll | ||
| 17 | static void start() throws Exception { | ||
| 18 | server = new TestServer(base); | ||
| 19 | } | ||
| 20 | |||
| 21 | @AfterAll | ||
| 22 | static void stop() throws Exception { | ||
| 23 | server.close(); | ||
| 24 | } | ||
| 25 | |||
| 26 | @Test | ||
| 27 | void nameIsHeldByOneSessionUntilLogout() throws Exception { | ||
| 28 | var alice = TestServer.client(); | ||
| 29 | var res = server.post(alice, "/login", "username", " Ann "); | ||
| 30 | assertEquals(200, res.statusCode(), res.body()); | ||
| 31 | assertTrue(res.body().contains("\"username\":\"Ann\""), res.body()); | ||
| 32 | |||
| 33 | var impostor = TestServer.client(); | ||
| 34 | var taken = server.post(impostor, "/login", "username", "ann"); | ||
| 35 | assertEquals(409, taken.statusCode()); | ||
| 36 | assertTrue(taken.body().contains("NAME_TAKEN"), taken.body()); | ||
| 37 | |||
| 38 | assertEquals(200, server.post(alice, "/login", "username", "Ann").statusCode()); | ||
| 39 | |||
| 40 | assertEquals(200, server.post(alice, "/logout").statusCode()); | ||
| 41 | var back = server.post(impostor, "/login", "username", "ann"); | ||
| 42 | assertEquals(200, back.statusCode(), back.body()); | ||
| 43 | assertTrue(back.body().contains("\"username\":\"Ann\""), back.body()); | ||
| 44 | } | ||
| 45 | |||
| 46 | @Test | ||
| 47 | void switchingNameFreesThePreviousOne() throws Exception { | ||
| 48 | var client = TestServer.client(); | ||
| 49 | assertEquals(200, server.post(client, "/login", "username", "Ben").statusCode()); | ||
| 50 | assertEquals(200, server.post(client, "/login", "username", "Bob").statusCode()); | ||
| 51 | assertEquals(200, server.post(TestServer.client(), "/login", "username", "Ben").statusCode()); | ||
| 52 | } | ||
| 53 | |||
| 54 | @Test | ||
| 55 | void rejectsBlankNames() throws Exception { | ||
| 56 | var res = server.post(TestServer.client(), "/login", "username", " "); | ||
| 57 | assertEquals(400, res.statusCode()); | ||
| 58 | assertTrue(res.body().contains("INVALID_USERNAME"), res.body()); | ||
| 59 | assertEquals(400, server.post(TestServer.client(), "/login").statusCode()); | ||
| 60 | } | ||
| 61 | |||
| 62 | @Test | ||
| 63 | void loginRegistersAnAccount() throws Exception { | ||
| 64 | server.post(TestServer.client(), "/login", "username", "Cleo"); | ||
| 65 | var health = server.get(TestServer.client(), "/health"); | ||
| 66 | assertTrue(health.body().matches(".*\"users\":[1-9].*"), health.body()); | ||
| 67 | } | ||
| 68 | |||
| 69 | @Test | ||
| 70 | void logoutWithoutSessionIsHarmless() throws Exception { | ||
| 71 | assertEquals(200, server.post(TestServer.client(), "/logout").statusCode()); | ||
| 72 | } | ||
| 73 | } | ||
diff --git a/server/src/test/java/market/guess/server/ServerBootTest.java b/server/src/test/java/market/guess/server/ServerBootTest.java index 6fdcdc6..f8b21cf 100644 --- a/server/src/test/java/market/guess/server/ServerBootTest.java +++ b/server/src/test/java/market/guess/server/ServerBootTest.java | |||
| @@ -5,16 +5,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse; | |||
| 5 | import static org.junit.jupiter.api.Assertions.assertNull; | 5 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | import static org.junit.jupiter.api.Assertions.assertTrue; | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | 7 | ||
| 8 | import java.net.URI; | ||
| 9 | import java.net.http.HttpClient; | ||
| 10 | import java.net.http.HttpRequest; | 8 | import java.net.http.HttpRequest; |
| 11 | import java.net.http.HttpResponse; | 9 | import java.net.http.HttpResponse; |
| 12 | import java.nio.file.Files; | ||
| 13 | import java.nio.file.Path; | 10 | import java.nio.file.Path; |
| 14 | import org.apache.catalina.startup.Tomcat; | ||
| 15 | import org.apache.catalina.webresources.DirResourceSet; | ||
| 16 | import org.apache.catalina.webresources.StandardRoot; | ||
| 17 | import org.apache.tomcat.util.scan.StandardJarScanner; | ||
| 18 | import org.junit.jupiter.api.AfterAll; | 11 | import org.junit.jupiter.api.AfterAll; |
| 19 | import org.junit.jupiter.api.BeforeAll; | 12 | import org.junit.jupiter.api.BeforeAll; |
| 20 | import org.junit.jupiter.api.Test; | 13 | import org.junit.jupiter.api.Test; |
| @@ -22,49 +15,27 @@ import org.junit.jupiter.api.io.TempDir; | |||
| 22 | 15 | ||
| 23 | class ServerBootTest { | 16 | class ServerBootTest { |
| 24 | private static final String ORIGIN = "http://localhost:5173"; | 17 | private static final String ORIGIN = "http://localhost:5173"; |
| 25 | private static final HttpClient HTTP = HttpClient.newHttpClient(); | ||
| 26 | 18 | ||
| 27 | @TempDir static Path base; | 19 | @TempDir static Path base; |
| 28 | private static Tomcat tomcat; | 20 | private static TestServer server; |
| 29 | private static String api; | ||
| 30 | 21 | ||
| 31 | @BeforeAll | 22 | @BeforeAll |
| 32 | static void start() throws Exception { | 23 | static void start() throws Exception { |
| 33 | tomcat = new Tomcat(); | 24 | server = new TestServer(base); |
| 34 | tomcat.setBaseDir(base.toString()); | ||
| 35 | tomcat.setPort(0); | ||
| 36 | tomcat.getConnector(); | ||
| 37 | |||
| 38 | var docBase = Files.createDirectories(base.resolve("webapp")); | ||
| 39 | var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); | ||
| 40 | ctx.setParentClassLoader(ServerBootTest.class.getClassLoader()); | ||
| 41 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); | ||
| 42 | var resources = new StandardRoot(ctx); | ||
| 43 | resources.addPreResources( | ||
| 44 | new DirResourceSet( | ||
| 45 | resources, | ||
| 46 | "/WEB-INF/classes", | ||
| 47 | Path.of("build/classes/server").toAbsolutePath().toString(), | ||
| 48 | "/")); | ||
| 49 | ctx.setResources(resources); | ||
| 50 | |||
| 51 | tomcat.start(); | ||
| 52 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; | ||
| 53 | } | 25 | } |
| 54 | 26 | ||
| 55 | @AfterAll | 27 | @AfterAll |
| 56 | static void stop() throws Exception { | 28 | static void stop() throws Exception { |
| 57 | tomcat.stop(); | 29 | server.close(); |
| 58 | tomcat.destroy(); | ||
| 59 | } | 30 | } |
| 60 | 31 | ||
| 61 | private static HttpResponse<String> send(HttpRequest.Builder req) throws Exception { | 32 | private static HttpResponse<String> send(HttpRequest.Builder req) throws Exception { |
| 62 | return HTTP.send(req.build(), HttpResponse.BodyHandlers.ofString()); | 33 | return TestServer.client().send(req.build(), HttpResponse.BodyHandlers.ofString()); |
| 63 | } | 34 | } |
| 64 | 35 | ||
| 65 | @Test | 36 | @Test |
| 66 | void healthReportsEmptyMarket() throws Exception { | 37 | void healthReportsEmptyMarket() throws Exception { |
| 67 | var res = send(HttpRequest.newBuilder(URI.create(api + "/health"))); | 38 | var res = send(server.request("/health")); |
| 68 | assertEquals(200, res.statusCode()); | 39 | assertEquals(200, res.statusCode()); |
| 69 | assertTrue(res.headers().firstValue("Content-Type").orElse("").startsWith("application/json")); | 40 | assertTrue(res.headers().firstValue("Content-Type").orElse("").startsWith("application/json")); |
| 70 | assertTrue(res.body().contains("\"status\":\"ok\""), res.body()); | 41 | assertTrue(res.body().contains("\"status\":\"ok\""), res.body()); |
| @@ -75,7 +46,8 @@ class ServerBootTest { | |||
| 75 | void preflightAllowsCredentialedCrossOriginCalls() throws Exception { | 46 | void preflightAllowsCredentialedCrossOriginCalls() throws Exception { |
| 76 | var res = | 47 | var res = |
| 77 | send( | 48 | send( |
| 78 | HttpRequest.newBuilder(URI.create(api + "/health")) | 49 | server |
| 50 | .request("/health") | ||
| 79 | .method("OPTIONS", HttpRequest.BodyPublishers.noBody()) | 51 | .method("OPTIONS", HttpRequest.BodyPublishers.noBody()) |
| 80 | .header("Origin", ORIGIN) | 52 | .header("Origin", ORIGIN) |
| 81 | .header("Access-Control-Request-Method", "POST")); | 53 | .header("Access-Control-Request-Method", "POST")); |
| @@ -87,10 +59,10 @@ class ServerBootTest { | |||
| 87 | 59 | ||
| 88 | @Test | 60 | @Test |
| 89 | void corsHeadersOnlyForCrossOriginRequests() throws Exception { | 61 | void corsHeadersOnlyForCrossOriginRequests() throws Exception { |
| 90 | var cross = send(HttpRequest.newBuilder(URI.create(api + "/health")).header("Origin", ORIGIN)); | 62 | var cross = send(server.request("/health").header("Origin", ORIGIN)); |
| 91 | assertEquals(ORIGIN, cross.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); | 63 | assertEquals(ORIGIN, cross.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); |
| 92 | 64 | ||
| 93 | var same = send(HttpRequest.newBuilder(URI.create(api + "/health"))); | 65 | var same = send(server.request("/health")); |
| 94 | assertNull(same.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); | 66 | assertNull(same.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); |
| 95 | assertFalse(same.body().isEmpty()); | 67 | assertFalse(same.body().isEmpty()); |
| 96 | } | 68 | } |
diff --git a/server/src/test/java/market/guess/server/TestServer.java b/server/src/test/java/market/guess/server/TestServer.java new file mode 100644 index 0000000..9b77819 --- /dev/null +++ b/server/src/test/java/market/guess/server/TestServer.java | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import java.net.CookieManager; | ||
| 4 | import java.net.URI; | ||
| 5 | import java.net.URLEncoder; | ||
| 6 | import java.net.http.HttpClient; | ||
| 7 | import java.net.http.HttpRequest; | ||
| 8 | import java.net.http.HttpResponse; | ||
| 9 | import java.nio.charset.StandardCharsets; | ||
| 10 | import java.nio.file.Files; | ||
| 11 | import java.nio.file.Path; | ||
| 12 | import org.apache.catalina.startup.Tomcat; | ||
| 13 | import org.apache.catalina.webresources.DirResourceSet; | ||
| 14 | import org.apache.catalina.webresources.StandardRoot; | ||
| 15 | import org.apache.tomcat.util.scan.StandardJarScanner; | ||
| 16 | |||
| 17 | final class TestServer implements AutoCloseable { | ||
| 18 | private final Tomcat tomcat; | ||
| 19 | final String api; | ||
| 20 | |||
| 21 | TestServer(Path base) throws Exception { | ||
| 22 | tomcat = new Tomcat(); | ||
| 23 | tomcat.setBaseDir(base.toString()); | ||
| 24 | tomcat.setPort(0); | ||
| 25 | tomcat.getConnector(); | ||
| 26 | |||
| 27 | var docBase = Files.createDirectories(base.resolve("webapp")); | ||
| 28 | var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); | ||
| 29 | ctx.setParentClassLoader(TestServer.class.getClassLoader()); | ||
| 30 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); | ||
| 31 | var resources = new StandardRoot(ctx); | ||
| 32 | resources.addPreResources( | ||
| 33 | new DirResourceSet( | ||
| 34 | resources, | ||
| 35 | "/WEB-INF/classes", | ||
| 36 | Path.of("build/classes/server").toAbsolutePath().toString(), | ||
| 37 | "/")); | ||
| 38 | ctx.setResources(resources); | ||
| 39 | |||
| 40 | tomcat.start(); | ||
| 41 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; | ||
| 42 | } | ||
| 43 | |||
| 44 | static HttpClient client() { | ||
| 45 | return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); | ||
| 46 | } | ||
| 47 | |||
| 48 | HttpRequest.Builder request(String path) { | ||
| 49 | return HttpRequest.newBuilder(URI.create(api + path)); | ||
| 50 | } | ||
| 51 | |||
| 52 | HttpResponse<String> get(HttpClient client, String path) throws Exception { | ||
| 53 | return client.send(request(path).build(), HttpResponse.BodyHandlers.ofString()); | ||
| 54 | } | ||
| 55 | |||
| 56 | HttpResponse<String> post(HttpClient client, String path, String... fields) throws Exception { | ||
| 57 | var form = new StringBuilder(); | ||
| 58 | for (var i = 0; i < fields.length; i += 2) { | ||
| 59 | if (!form.isEmpty()) form.append('&'); | ||
| 60 | form.append(fields[i]) | ||
| 61 | .append('=') | ||
| 62 | .append(URLEncoder.encode(fields[i + 1], StandardCharsets.UTF_8)); | ||
| 63 | } | ||
| 64 | var req = | ||
| 65 | request(path) | ||
| 66 | .header("Content-Type", "application/x-www-form-urlencoded") | ||
| 67 | .POST(HttpRequest.BodyPublishers.ofString(form.toString())) | ||
| 68 | .build(); | ||
| 69 | return client.send(req, HttpResponse.BodyHandlers.ofString()); | ||
| 70 | } | ||
| 71 | |||
| 72 | @Override | ||
| 73 | public void close() throws Exception { | ||
| 74 | tomcat.stop(); | ||
| 75 | tomcat.destroy(); | ||
| 76 | } | ||
| 77 | } | ||