mirror of
https://github.com/coder/coder.git
synced 2026-06-05 14:08:20 +00:00
962608cde0
Closes coder/internal#168 Gets rid of the "global state" authentication, and adds a `login` helper which should be called at the beginning of each test. This means that not every test needs to authenticated as admin, and we can even have tests that encompass multiple permission levels. We also now create more than just the single admin user during setup, so that we can have a set of users to pick from as appropriate.
88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import http from "node:http";
|
|
import type { BrowserContext, Page } from "@playwright/test";
|
|
import { coderPort, gitAuth } from "./constants";
|
|
|
|
export const beforeCoderTest = (page: Page) => {
|
|
page.on("console", (msg) => console.info(`[onConsole] ${msg.text()}`));
|
|
|
|
page.on("request", (request) => {
|
|
if (!isApiCall(request.url())) {
|
|
return;
|
|
}
|
|
|
|
console.info(
|
|
`[onRequest] method=${request.method()} url=${request.url()} postData=${
|
|
request.postData() ? request.postData() : ""
|
|
}`,
|
|
);
|
|
});
|
|
page.on("response", async (response) => {
|
|
if (!isApiCall(response.url())) {
|
|
return;
|
|
}
|
|
|
|
const shouldLogResponse =
|
|
!response.url().endsWith("/api/v2/deployment/config") &&
|
|
!response.url().endsWith("/api/v2/debug/health?force=false");
|
|
|
|
let responseText = "";
|
|
try {
|
|
if (shouldLogResponse) {
|
|
const buffer = await response.body();
|
|
responseText = buffer.toString("utf-8");
|
|
responseText = responseText.replace(/\n$/g, "");
|
|
} else {
|
|
responseText = "skipped...";
|
|
}
|
|
} catch (error) {
|
|
responseText = "not_available";
|
|
}
|
|
|
|
console.info(
|
|
`[onResponse] url=${response.url()} status=${response.status()} body=${responseText}`,
|
|
);
|
|
});
|
|
};
|
|
|
|
export const resetExternalAuthKey = async (context: BrowserContext) => {
|
|
// Find the session token so we can destroy the external auth link between tests, to ensure valid authentication happens each time.
|
|
const cookies = await context.cookies();
|
|
const sessionCookie = cookies.find((c) => c.name === "coder_session_token");
|
|
const options = {
|
|
method: "DELETE",
|
|
hostname: "127.0.0.1",
|
|
port: coderPort,
|
|
path: `/api/v2/external-auth/${gitAuth.webProvider}?coder_session_token=${sessionCookie?.value}`,
|
|
};
|
|
|
|
const req = http.request(options, (res) => {
|
|
let data = "";
|
|
res.on("data", (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on("end", () => {
|
|
// Both 200 (key deleted successfully) and 500 (key was not found) are valid responses.
|
|
if (res.statusCode !== 200 && res.statusCode !== 500) {
|
|
console.error("failed to delete external auth link", data);
|
|
throw new Error(
|
|
`failed to delete external auth link: HTTP response ${res.statusCode}`,
|
|
);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on("error", (err) => {
|
|
throw err.message;
|
|
});
|
|
|
|
req.end();
|
|
};
|
|
|
|
const isApiCall = (urlString: string): boolean => {
|
|
const url = new URL(urlString);
|
|
const apiPath = "/api/v2";
|
|
|
|
return url.pathname.startsWith(apiPath);
|
|
};
|