diff --git a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java index 2683764cef..7481c22936 100644 --- a/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java +++ b/e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GatewayRequests.java @@ -16,12 +16,38 @@ public class GatewayRequests implements AutoCloseable { private final APIRequestContext request; + private static String token; - public GatewayRequests(Playwright playwright) { + public GatewayRequests(Playwright playwright) throws IOException { request = playwright.request().newContext( new APIRequest.NewContextOptions() .setBaseURL(BaseURL.GATEWAY.url)); + login(); } + + private void login() throws IOException { + if(token != null) return; + final var response = request.post("/auth/login", RequestOptions.create() + .setHeader("Content-Type", "application/json") + .setData(Json.createObjectBuilder() + .add("username", "AerieE2eTests") + .add("password", "password") + .build() + .toString())); + // Process Response + if(!response.ok()){ + throw new IOException(response.statusText()); + } + try(final var reader = Json.createReader(new StringReader(response.text()))){ + final JsonObject bodyJson = reader.readObject(); + if(!bodyJson.getBoolean("success")){ + System.err.println("Login failed"); + throw new RuntimeException(bodyJson.toString()); + } + token = bodyJson.getString("token"); + } + } + @Override public void close() { request.dispose(); @@ -54,7 +80,9 @@ public int uploadJarFile(String jarPath) throws IOException { "application/java-archive", buffer); - final var response = request.post("/file", RequestOptions.create().setMultipart(FormData.create().set("file", payload))); + final var response = request.post("/file", RequestOptions.create() + .setHeader("Authorization", "Bearer "+token) + .setMultipart(FormData.create().set("file", payload))); // Process Response if(!response.ok()){ @@ -69,5 +97,4 @@ public int uploadJarFile(String jarPath) throws IOException { return bodyJson.getInt("id"); } } - }