Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E Tests: Provide Auth Header when Uploading Model JAR #1493

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()){
Expand All @@ -69,5 +97,4 @@ public int uploadJarFile(String jarPath) throws IOException {
return bodyJson.getInt("id");
}
}

}
18 changes: 17 additions & 1 deletion sequencing-server/test/testUtils/MissionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ export async function uploadMissionModel(graphqlClient: GraphQLClient): Promise<
const file = await fileFrom(latestBuild.path);
formData.set('file', file, 'banananation-latest.jar');

// Get an authorization token
const authHeader = `Bearer ${await login()}`;

// Upload File
const uploadRes = await fetch(`${process.env['MERLIN_GATEWAY_URL']}/file`, {
method: 'POST',
body: formData,
headers: { 'x-auth-sso-token': process.env['SSO_TOKEN'] as string },
headers: { 'Authorization': authHeader },
});
if (!uploadRes.ok) {
throw new Error(`Failed to upload mission model: ${uploadRes.statusText}`);
Expand Down Expand Up @@ -62,6 +66,18 @@ export async function uploadMissionModel(graphqlClient: GraphQLClient): Promise<
return (res.insert_mission_model_one as { id: number } as { id: number }).id;
}

async function login() {
const response = await fetch(`${process.env['MERLIN_GATEWAY_URL']}/auth/login`, {
method: 'POST',
body: `{"username": "AerieE2ETests", "password": "password"}`,
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) {
throw new Error(`Failed to login: ${response.statusText}`);
}
return (await response.json() as {token: string}).token;
}

export async function removeMissionModel(graphqlClient: GraphQLClient, missionModelId: number): Promise<void> {
/*
* Remove a mission model
Expand Down
Loading